jQuery.fn.rater = function(options)
{
	var settings = {
		maxvalue  : 4,   // max number of stars
		curvalue  : 0,   // number of selected stars
		pageID    : 0,    // ID of the Current Page
		toolTip   : ''   // Tool Tip fpr the Stars
	};
	
	if(options) { jQuery.extend(settings, options); };
	jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
	
	var container = jQuery(this);
	jQuery.extend(container, { averageRating: settings.curvalue});
	
	var raterwidth = settings.maxvalue * 33;
	var ratingparent = '<ul class="star-rating" style="width:'+raterwidth+'px">';
	
	container.append(ratingparent);
	
	// create items
	var starWidth, starIndex, listitems = '';
	for(var i = 0; i <= settings.maxvalue ; i++) {
		starWidth = 20 * (i + 1);
		starIndex = (settings.maxvalue - i) + 2;
		starValue = i + 1;
	    listitems+='<li class="star"><a pageid="' + settings.pageID + '" rate="' + starValue + '" href="" title="' + settings.toolTip + '" style="width:' + starWidth + '%;z-index:' + starIndex + '">'+ starValue +'</a></li>';
	}
	container.find('.star-rating').append(listitems);

	var stars = jQuery(container).find('.star-rating').children('.star');
	stars.click(function()
	{
		settings.curvalue = stars.index(this) + 1;
		if (settings.curvalue <= 2)
		{
		    var currentRating = $(this).children('a');
		    raterValue = currentRating.attr('rate');
		    raterPageID = currentRating.attr('pageid');
		    blankImg = "/Swisscom.CorporatePortal.Web.RES/Images/Layout/blank.gif";
		    try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { }
		    
		    $.post("/Swisscom.CorporatePortal.Web.RES/Pages/JQueryHandler.aspx", { Method: "LowRatePageMasc"}, function(response) {
                $('#LowRatePageInfo').html(response);
                $('#RatePage').hide();  
                $('#LowRatePage').show(1, function(ev){
                
                     blankImg = "/Swisscom.CorporatePortal.Web.RES/Images/Layout/blank.gif";
                     try { this.execCommand('BackgroundImageCache', false, true); } catch (e) { }
                     
                     $('#LowRatePage .standardbutton').prepend(
               		        $("<strong style='position:absolute; top:0; left:0; z-index:10; cursor:pointer;'/>")
              			        .css("height", $('.standardbutton').height())
						        .css("width", $('.standardbutton').width())
						        .css("background", "url(" + blankImg + ")")
						        .mouseover(function() {
							        $(this)
								        .next()
								        .addClass("standardbutton-hover-span")
									        .parent()
									        .addClass("standardbutton-hover");
						        })
						        .mouseout(function() {
							        $(this)
								        .next()
								        .removeClass("standardbutton-hover-span")
									        .parent()
									        .removeClass("standardbutton-hover");
						        })
					   );
                     
                     if (this.all &&
				        /MSIE (5\.5|6)/.test(navigator.userAgent) &&
				        this.styleSheets && document.styleSheets[0] &&
				        this.styleSheets[0].addRule){
				        this.styleSheets[0].addRule('#LowRatePage .standardbutton', 'behavior: url(/Swisscom.CorporatePortal.Web.RES/scripts/iepngfix.htc)');
				        this.styleSheets[0].addRule('#LowRatePage .standardbutton span', 'behavior: url(/Swisscom.CorporatePortal.Web.RES/scripts/iepngfix.htc)');
				        this.styleSheets[0].addRule('#LowRatePage .standardbutton-hover', 'behavior: url(/Swisscom.CorporatePortal.Web.RES/scripts/iepngfix.htc)');
				        this.styleSheets[0].addRule('#LowRatePage .standardbutton-hover span', 'behavior: url(/Swisscom.CorporatePortal.Web.RES/scripts/iepngfix.htc)');
				        this.styleSheets[0].addRule('#LowRatePage .cta img', 'behavior: url(/Swisscom.CorporatePortal.Web.RES/scripts/iepngfix.htc)');
			            }
                });
            });	
           
          
           $('#divSubmitRater').click(function(event)
            {
                event.preventDefault();
                $.post("/Swisscom.CorporatePortal.Web.RES/Pages/JQueryHandler.aspx", { Method: "RatePage", RatingValue: raterValue, RatingPageID: raterPageID, RatingPageUrl: location.href, RatingPageComment: $('textarea.textarea').val()}, function(response) {
                    $('#RatePageConfirm').html(response);
                    $('#RatePageConfirm').show();
                    $('#LowRatePage').hide();
                    $('#RatePage').hide();
                });
            });
		}
        else
        {		
		    var currentRating = $(this).children('a');
		    raterValue = currentRating.attr('rate');
		    raterPageID = currentRating.attr('pageid');
            $.post("/Swisscom.CorporatePortal.Web.RES/Pages/JQueryHandler.aspx", { Method: "RatePage", RatingValue: raterValue, RatingPageID: raterPageID, RatingPageUrl: location.href, RatingPageComment: "<NULL>"}, function(response) {
                $('#RatePageConfirm').html(response);
                $('#RatePageConfirm').show();
                $('#RatePage').hide();
            });	
        }
		return false;
	});
	return this;
}


