


Vudu.widget.Rating = function( holder , imgSoruce, directorUrl  , criticsRating , communityRating , contentId , key , userId , ofbizLogin ,  rating , compaqMode  )	{
	
	this._holder = YAHOO.util.Dom.get(holder);
	this._imgSoruce = imgSoruce;
	this._directorUrl = directorUrl;
	this._ofbizLogin =  ofbizLogin;
	
	this.contentId = contentId;
	this.key = key;
	this.userId = userId;
	
	this._communityRating = communityRating? communityRating * 2 : 0;
	this._criticsRating = criticsRating? criticsRating * 2 : 0;
	this._origRating = rating? rating * 2 : 0;
	
	this._starImages = new Array(20);
	this._communityImages = new Array(20);
	
	this._compaqMode = (compaqMode && compaqMode != "false")? true : false; 
	
	this.loading = false;
	
	this.render = function()	{
	
		var Dom = YAHOO.util.Dom;
		
		// *************** //
		// CRITIICS RAITNG //
		// *************** //
		
		var criticsRatingContainer = document.createElement('div');
		Dom.setStyle( criticsRatingContainer , 'height', '25px');
		Dom.setStyle( criticsRatingContainer , 'width', '200px');
		Dom.setStyle( criticsRatingContainer , 'position', 'relative');
		Dom.setStyle( criticsRatingContainer , 'float', 'left');
		
		var criticsRatingTitle = document.createElement('div');
		Dom.setStyle( criticsRatingTitle , 'position', 'relative'); 
		Dom.setStyle( criticsRatingTitle , 'float', 'left');
		Dom.setStyle( criticsRatingTitle , 'height', '25px');
		Dom.setStyle( criticsRatingTitle , 'width', '90px');
		Dom.setStyle( criticsRatingTitle , 'white-space', 'nowrap');
		Dom.setStyle( criticsRatingTitle , 'font-family' , 'arial,helvetica,clean,sans-serif');
		Dom.setStyle( criticsRatingTitle , 'color' , '#CCCCCC');
		Dom.setStyle( criticsRatingTitle , 'font-size' , '10pt');
		criticsRatingTitle.innerHTML = 'Critics\' Rating:';
		criticsRatingContainer.appendChild(criticsRatingTitle);
		
		var criticsRatingDiv = document.createElement('div');
		Dom.setStyle( criticsRatingDiv , 'float', 'right');
		Dom.setStyle( criticsRatingDiv , 'width', '110px');
		Dom.setStyle( criticsRatingDiv , 'height', '16px');
		Dom.setStyle( criticsRatingDiv , 'position', 'relative');
		
		// if the community rating is set - show
		if(this._criticsRating && this._criticsRating > 0 )	{
		
			for(var i=1; i <= 10; i++)	{
			
				var halfStar = document.createElement('img');
				halfStar.width = '9';
				halfStar.height = '16';
				halfStar.src = 	this._imgSoruce+'/'+
								(i % 2 == 0? 'right' : 'left' ) + '_half_'+
								( i <= this._criticsRating ? 'yellow' : 'empty') + '_star.png'; 
								+ '_half_empty_star.png';
								
				criticsRatingDiv.appendChild(halfStar);
			}
			
			
			criticsRatingContainer.appendChild(criticsRatingDiv);
			this._holder.appendChild(criticsRatingContainer);
		}
		
		
		// **************** //
		// COMMUNITY RAITNG //
		// **************** //
		
		var communityRatingContainer = document.createElement('div');
		Dom.setStyle( communityRatingContainer , 'height', '25px');
		Dom.setStyle( communityRatingContainer , 'width', '200px');
		Dom.setStyle( communityRatingContainer , 'position', 'relative');
		Dom.setStyle( communityRatingContainer , 'float', 'left');
		
		var communityRatingTitle = document.createElement('div');
		Dom.setStyle( communityRatingTitle , 'position', 'relative'); 
		Dom.setStyle( communityRatingTitle , 'float', 'left');
		Dom.setStyle( communityRatingTitle , 'height', '25px');
		Dom.setStyle( communityRatingTitle , 'width', '90px');
		Dom.setStyle( communityRatingTitle , 'white-space', 'nowrap');
		Dom.setStyle( communityRatingTitle , 'font-family' , 'arial,helvetica,clean,sans-serif');
		Dom.setStyle( communityRatingTitle , 'color' , '#CCCCCC');
		Dom.setStyle( communityRatingTitle , 'font-size' , '10pt');
		communityRatingTitle.innerHTML = 'Community:';
		communityRatingContainer.appendChild(communityRatingTitle);
		
		this.communityRatingDiv = document.createElement('div');
		Dom.setStyle( this.communityRatingDiv , 'float', 'right');
		Dom.setStyle( this.communityRatingDiv , 'width', '110px');
		Dom.setStyle( this.communityRatingDiv , 'height', '16px');
		Dom.setStyle( this.communityRatingDiv , 'position', 'relative');
		
		
		for(var i=1; i <= 10; i++)	{
		
			var halfStar = document.createElement('img');
			halfStar.width = '9';
			halfStar.height = '16';
			halfStar.src = 	this._imgSoruce+'/'+
						(i % 2 == 0? 'right' : 'left' ) + '_half_'+
						( i <= this._communityRating ? 'blue' : 'empty') + '_star.png'; 
						+ '_half_empty_star.png';
			
			
			this.communityRatingDiv.appendChild(halfStar);
			this._communityImages[i-1] = halfStar;
		}
		
		// if the community rating is set - show 
		if(this._communityRating && this._communityRating > 0 )	{
			// show the community ratings star
		}
		// else show - be the first
		else	{
			Dom.setStyle(this.communityRatingDiv , 'display' , 'none');
			
			this.communityRatingAltDiv = document.createElement('div');
			Dom.setStyle( this.communityRatingAltDiv , 'float', 'right');
			Dom.setStyle( this.communityRatingAltDiv , 'width', '110px');
			Dom.setStyle( this.communityRatingAltDiv , 'height', '16px');
			Dom.setStyle( this.communityRatingAltDiv , 'position', 'relative');
			Dom.setStyle( this.communityRatingAltDiv , 'font-family' , 'arial,helvetica,clean,sans-serif');
			Dom.setStyle( this.communityRatingAltDiv , 'color' , '#CCCCCC');
			Dom.setStyle( this.communityRatingAltDiv , 'font-size' , '10pt');
			this.communityRatingAltDiv.innerHTML = 'Be the First!';
			communityRatingContainer.appendChild(this.communityRatingAltDiv);
		}
		
		communityRatingContainer.appendChild(this.communityRatingDiv);
		this._holder.appendChild(communityRatingContainer);
		
		// ************ //
		// USERS RAITNG //
		// ************ //
		
		var userRatingsContainer = document.createElement('div');
		Dom.setStyle( userRatingsContainer , 'height', '26px');
		Dom.setStyle( userRatingsContainer , 'width', '200px');
		Dom.setStyle( userRatingsContainer , 'position', 'relative');
		Dom.setStyle( userRatingsContainer , 'float', 'left');
		
		var userRatingsTitle = document.createElement('div');
		Dom.setStyle( userRatingsTitle , 'position', 'relative'); 
		Dom.setStyle( userRatingsTitle , 'float', 'left');
		Dom.setStyle( userRatingsTitle , 'height', '26px');
		Dom.setStyle( userRatingsTitle , 'width', '90px');
		Dom.setStyle( userRatingsTitle , 'white-space', 'nowrap');
		Dom.setStyle( userRatingsTitle , 'font-family' , 'arial,helvetica,clean,sans-serif');
		Dom.setStyle( userRatingsTitle , 'color' , '#CCCCCC');
		Dom.setStyle( userRatingsTitle , 'font-size' , '10pt');
		userRatingsTitle.innerHTML = 'My Rating:';
		userRatingsContainer.appendChild(userRatingsTitle);
		
		
		var userRatingsDiv = document.createElement('div');
		Dom.setStyle( userRatingsDiv , 'position', 'relative');
		Dom.setStyle( userRatingsDiv , 'float', 'right');
		Dom.setStyle( userRatingsDiv , 'width', '110px');
		Dom.setStyle( userRatingsDiv , 'height', '16px');
		
		if(this.key && this.userId )	{
			 
			// add mouse over events
			YAHOO.util.Event.addListener  (
				 userRatingsDiv,
				'mouseout' ,
				function( e , widget) {
					widget.showMouseOutRating();
				},
				this ,
				true
			);
			
			for(var i=1; i <= 10; i++)	{
				
				var starImage = this.createStarImage(i);
				 userRatingsDiv.appendChild(starImage);
				this._starImages[i-1] = starImage;
			}
			
			this.savedFloatingText = document.createElement('div');
			Dom.setStyle(this.savedFloatingText , 'position' 	, 'absolute' );
			Dom.setStyle(this.savedFloatingText , 'top' 	, '20px'  );
			Dom.setStyle(this.savedFloatingText , 'left' , '0px'  );
			Dom.setStyle(this.savedFloatingText , 'width' 	, '100px'  );
			Dom.setStyle(this.savedFloatingText , 'height' 	, '20px'  );
			Dom.setStyle(this.savedFloatingText , 'font-family' , 'arial,helvetica,clean,sans-serif');
			Dom.setStyle(this.savedFloatingText , 'color' , '#CCCCCC');
			Dom.setStyle(this.savedFloatingText , 'font-size' , '9pt');
			this.savedFloatingText.innerHTML = '';
			userRatingsDiv.appendChild(this.savedFloatingText);
			// show the stars
			this.showMouseOutRating();
			// add to container
			userRatingsContainer.appendChild(userRatingsDiv);
			this._holder.appendChild(userRatingsContainer);
		}
		else if(!this._compaqMode)	{
		
			Dom.setStyle(userRatingsDiv , 'font-family' , 'arial,helvetica,clean,sans-serif');
			Dom.setStyle(userRatingsDiv , 'color' , '#CCCCCC');
			Dom.setStyle(userRatingsDiv , 'font-size' , '10pt');
			
			// add the login button div
			var loginButton = document.createElement('div');
			Dom.setStyle(loginButton , 'float' 	, 'right' );
			Dom.setStyle(loginButton , 'padding-top' 	, '6px' );
			Dom.setStyle(loginButton , 'cursor' , 'pointer' );
			Dom.setStyle(loginButton , 'color' , '#000000' );
			Dom.setStyle(loginButton , 'width' , '110px' );
			Dom.setStyle(loginButton , 'height' , '26px' );
			Dom.setStyle(loginButton , 'background-repeat' , 'no-repeat' );
			Dom.setStyle(loginButton , 'background-image' , 'url('+this._imgSoruce+'/vudu_button_110.gif)' );
			Dom.setStyle(loginButton , 'background-color' , 'transparent' );
		 	Dom.setStyle(loginButton , 'font-family' 	, 'arial,helvetica,clean,sans-serif' );
		 	Dom.setStyle(loginButton , 'font-size' 	, '8pt' );
		 	Dom.setStyle(loginButton , 'font-weight' 	, 'bold' );
		 	Dom.setStyle(loginButton , 'text-align' 	, 'center' );
		 	
		 	loginButton.innerHTML = 'Login to Rate';
		 	
		 	var backUrl = document.location.href;
		 	backUrl = (backUrl.indexOf('?') != -1 )? backUrl + '&active=1' : backUrl +  '?active=1';
		 	var loginRetunRef = this._ofbizLogin + '?url='+ Vudu.util.URLEncode(backUrl);
		 	YAHOO.util.Event.addListener  (
				loginButton ,
				'click' ,
				function( e , widget ) {
					document.location.href = loginRetunRef;
				},
				this ,
				true
			);
			// adding login div
			userRatingsDiv.appendChild(loginButton);
			// add to container
			userRatingsContainer.appendChild(userRatingsDiv);
			this._holder.appendChild(userRatingsContainer);
		}
	}
    
    this.onRatingCallback = {
        success: function( oResponse ) {
            
			var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var ratingWidget = oResponse.argument.ratingWidget;
            
			if(results.starRating) {
                var newRating = results.starRating[0];
                ratingWidget._origRating =  newRating * 2;
                ratingWidget.onMyRatingUpdate(newRating);
            }
            if(results.ratingsSummary )    {
                var newSummary = results.ratingsSummary[0].starRatingsAvg[0];
                ratingWidget._communityRating =  newSummary * 2;
                ratingWidget.onUsersRatingUpdate(newSummary);
            }
			
            ratingWidget.savedFloatingText.innerHTML = 'Saved Rating';
			
            var thisRef = ratingWidget;
			window.setTimeout(function() {
                thisRef.updateUsersRatings();
            }, 2000);
            ratingWidget.showMouseOutRating();
        },
        
        failure: function( oResponse ) {
			
			var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var ratingWidget = oResponse.argument.ratingWidget;
            
            ratingWidget.savedFloatingText.innerHTML = 'Failed Saveing Rating';
			
			var thisRef = ratingWidget;
            window.setTimeout(function() {
                thisRef.savedFloatingText.innerHTML = '';
                thisRef.loading = false;
            }, 2000);
            ratingWidget.showMouseOutRating();
        }
    };
	
    this.onSummaryCallback = {
        success: function( oResponse ) {
			
			var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var ratingWidget = oResponse.argument.ratingWidget;
			
            if(results.starRating) {
                var newRating = results.starRating[0];
                ratingWidget._origRating =  newRating * 2;
                ratingWidget.onMyRatingUpdate(newRating);
            }
            if(results.ratingsSummary )    {
                var newSummary = results.ratingsSummary[0].starRatingsAvg[0];
                ratingWidget._communityRating =  newSummary * 2;
                ratingWidget.onUsersRatingUpdate(newSummary);
            }
            ratingWidget.savedFloatingText.innerHTML = 'Updated Community';
			
			var thisRef = ratingWidget;
            // hide the text in 2 sec
            window.setTimeout(function() {
                thisRef.savedFloatingText.innerHTML = '';
                thisRef.loading = false;
            }, 2000);
            
            // if alt text is showing - replace
            if (ratingWidget.communityRatingAltDiv) {
				
                ratingWidget.communityRatingAltDiv.parentNode.removeChild(ratingWidget.communityRatingAltDiv);
                ratingWidget.communityRatingDiv.style.display = 'block';
            }
            
            for (var i = 1; i <= 10; i++) {
                
				var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
                var ratingWidget = oResponse.argument.ratingWidget;
            
                var newSrc = 
				    ratingWidget._imgSoruce + '/' +
                    (i % 2 == 0 ? 'right' : 'left') +
                    '_half_' +
                    (i <= ratingWidget._communityRating ? 'blue' : 'empty') +
                    '_star.png';
                ratingWidget._communityImages[i - 1].src = newSrc;
            }
        },
        
        failure: function( oResponse ) {
			
			var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            var ratingWidget = oResponse.argument.ratingWidget;
            
            ratingWidget.savedFloatingText.innerHTML = 'Failed Updating Community';
			
			var thisRef = ratingWidget;
            window.setTimeout(function() {
                thisRef.savedFloatingText.innerHTML = '';
                thisRef.loading = false;
            }, 2000);
        }
    };
	
	this.createStarImage = function(index)	{
		
		var Dom = YAHOO.util.Dom;
		
		var halfStar = document.createElement('img');
		halfStar.onmousedown=function(){return false;}
		halfStar.width = '9';
		halfStar.height = '16';
		halfStar.src = 	this._imgSoruce+'/'+(index % 2 == 0? 'right' : 'left' ) 
						+ '_half_empty_star.png';
		
		var newRatingRef = index;
		// add mouse over events
		YAHOO.util.Event.addListener  (
			halfStar ,
			'mouseover' ,
			function( e , widget) {
				widget.showMouseOverRating( newRatingRef );
			},
			this ,
			true
		);
		// add mouse over events
		YAHOO.util.Event.addListener  (
			halfStar ,
			'mouseup' ,
			function( e , widget) {
				widget.rateMovie( newRatingRef );
			},
			this ,
			true
		);
		
		return halfStar;
	};

	this.rateMovie = function( newRating )  {
		
		//  validate & convert newRating
		if(!newRating || isNaN(newRating) || newRating <= 0 ||  newRating > 10 )	{
			return;
		}
		
		var req = "_type=ratingStore&format=application%2Fjson";
		// set userId
		req += "&userId="+this.userId;
		// set movie Id & type 
		req += "&reviewedId="+this.contentId;
		req += "&reviewedType=content";
		// set the rating
		newRating = newRating/2;
		req += "&starRating="+newRating;
		// set the auth token
		req += "&sessionKey="+this.key;
		
		this.loading = true;
		
		this.onRatingCallback.argument = { ratingWidget : this };
		// add custom content-type header
        YAHOO.util.Connect.setDefaultPostHeader(false);
        YAHOO.util.Connect.initHeader("Content-Type", "application/x-vudu-url-note");
//      this._ds.sendRequest( req , this.onRatingCallback);
        YAHOO.util.Connect.asyncRequest('POST', this._directorUrl , this.onRatingCallback, req );
	};
	
	this.updateUsersRatings = function()  {
		
		var req = "_type=ratingsSummarySearch&format=application%2Fjson";
		// set movie Id & type 
		req += "&reviewedId="+this.contentId;
		req += "&reviewedType=content";
		
		this.onSummaryCallback.argument = { ratingWidget : this };
		YAHOO.util.Connect.setDefaultPostHeader(false);
        YAHOO.util.Connect.initHeader("Content-Type", "application/x-vudu-url-note");
		//this._ds.sendRequest( req , this.onSummaryCallback);
        YAHOO.util.Connect.asyncRequest('POST', this._directorUrl , this.onSummaryCallback , req);
	};
	
	this.showMouseOverRating = function ( newRating )	{
		
		if(this.loading) return;
		
		for(var i=1; i <= 10; i++)	{
		
			var newSrc = this._imgSoruce + '/'+
				(i % 2 == 0? 'right' : 'left' ) + '_half_'+
				( i <= newRating ? 'green' : 'empty') + '_star.png';
				
			this._starImages[i-1].src = newSrc; 
				
		}
	};
	
	this.showMouseOutRating = function ()	{
		
		if(this.loading) return;
		
		for(var i=1; i <= 10; i++)	{
		
			var newSrc = this._imgSoruce + '/'+
				(i % 2 == 0? 'right' : 'left' ) + '_half_'+
				( i <= this._origRating ? 'green' : 'empty') + '_star.png';
				
			this._starImages[i-1].src = newSrc; 
		}
	}
	
	this.render();
}

Vudu.widget.Rating.prototype.onMyRatingUpdate = function (newRating)	{}
Vudu.widget.Rating.prototype.onUsersRatingUpdate = function (newRating)	{}
