﻿function createRatingSilverlight(parentName, idName, engineId, assetId, userId, initialMode, initialValue, lbl, initialAvgCount  ) {
	var scene = new Chopsticks.RatingControl();
	scene.set_EngineId (engineId);
	scene.set_AssetId(assetId);
	scene.set_UserId(userId);
	scene.set_InitialMode( initialMode);
	scene.set_InitialValue ( initialValue);
	scene._lbl = lbl;
	scene._avgCount = initialAvgCount;
	/*scene.set_imageSource(imageSource);
	scene.set_url(url);
	scene.set_isPremium(isPremium);
	scene.set_rating(rating);
	*/
	Silverlight.createObjectEx({
		source: "/belux/chopsticks/xaml/RatingControl.xaml",
		parentElement: document.getElementById(parentName),
		id: idName,
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0",
			isWindowless: "true",
			background: "#00000000"
		},
		events: {
			onLoad: Silverlight.createDelegate(scene, scene.handleLoad)
		}
	});
	
}


if (!window.Chopsticks)
	window.RatingControl = {};

Chopsticks.RatingControl = function() 
{
    this._mode = "CantRate";
    this._rating = 0;
    this._lbl = "";
    this._engineId = 0;
    this._assetId = 0;
    this._userId = 0;
    this._avgCount = 0;
}

Chopsticks.RatingControl.prototype =
{
    set_EngineId: function(val) {
        this._engineId = val;
    },    
    set_AssetId: function(val) {
        this._assetId = val;
    },    
    set_UserId: function(val) {
        this._userId = val;
    },        
    set_InitialMode: function(val) {
       this._mode = val;
    },        
    set_InitialValue: function(val) {
        this._rating = val;
    },        

	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.rootElement = rootElement;
		
		rootElement.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStarsMouseLeave));
		rootElement.findName("Star1").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleStar1MouseEnter));
		rootElement.findName("Star1").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStar1MouseLeave));
		rootElement.findName("Star1").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStar1MouseLeftButtonDown));		
		rootElement.findName("Star2").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleStar2MouseEnter));
		rootElement.findName("Star2").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStar2MouseLeave));
		rootElement.findName("Star2").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStar2MouseLeftButtonDown));
		rootElement.findName("Star3").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleStar3MouseEnter));
		rootElement.findName("Star3").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStar3MouseLeave));
		rootElement.findName("Star3").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStar3MouseLeftButtonDown));
		rootElement.findName("Star4").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleStar4MouseEnter));
		rootElement.findName("Star4").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStar4MouseLeave));
		rootElement.findName("Star4").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStar4MouseLeftButtonDown));
		rootElement.findName("Star5").addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleStar5MouseEnter));
		rootElement.findName("Star5").addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleStar5MouseLeave));
		rootElement.findName("Star5").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStar5MouseLeftButtonDown));

        this.switchMode( this._mode );
        this.assignValue( this._rating);
	},
	switchMode: function( mode) {
	    _mode = mode;
	    var lbl = $get(this._lbl);
	    switch(_mode) {
	        case "CanRate":
	            this.rootElement.findName("Star1").Cursor = "Hand";
	            this.rootElement.findName("Star2").Cursor = "Hand";
	            this.rootElement.findName("Star3").Cursor = "Hand";
	            this.rootElement.findName("Star4").Cursor = "Hand";
	            this.rootElement.findName("Star5").Cursor = "Hand";	  
	            
               
               if (lbl !=null) {
                lbl.innerHTML = "Rate this video:";
               }
           
	                  
	            break;
	        case "CantRate":
	            this.rootElement.findName("Star1").Cursor = "Default";
	            this.rootElement.findName("Star2").Cursor = "Default";
	            this.rootElement.findName("Star3").Cursor = "Default";
	            this.rootElement.findName("Star4").Cursor = "Default";
	            this.rootElement.findName("Star5").Cursor = "Default";	        
	            this.control.content.findName("Star1_MouseEnter").Stop();
	            this.control.content.findName("Star2_MouseEnter").Stop();
	            this.control.content.findName("Star3_MouseEnter").Stop();
	            this.control.content.findName("Star4_MouseEnter").Stop();
	            this.control.content.findName("Star5_MouseEnter").Stop();
	            
               
               if (lbl !=null) {
                lbl.innerHTML = String.format( "Average rating: ({0} ratings)", this._avgCount);
               }
           
	            break;
	    }
	},
	assignValue: function (value) {
        this.assignStar( "EmptyStar1Path", "HalfStar1Path", "FullStar1Path", value, 0);
        this.assignStar( "EmptyStar1Path", "HalfStar2Path", "FullStar2Path", value, 1);
        this.assignStar( "EmptyStar1Path", "HalfStar3Path", "FullStar3Path", value, 2);
        this.assignStar( "EmptyStar1Path", "HalfStar4Path", "FullStar4Path", value, 3);
        this.assignStar( "EmptyStar1Path", "HalfStar5Path", "FullStar5Path", value, 4);	    	
	},
	assignStar: function(imgEmpty, imgHalf, imgFull, val, offset) {
	    //alert(val);
	    //alert(rootElement.findName(imgFull));
	    
        if (val - offset >= 0.5) {
            if (val - offset >= 1.0) {
            
                this.rootElement.findName(imgFull).Visibility = "Visible";
                this.rootElement.findName(imgHalf).Visibility = "Collapsed";
                this.rootElement.findName(imgEmpty).Visibility = "Collapsed";

            } else {
                this.rootElement.findName(imgHalf).Visibility = "Visible";
                this.rootElement.findName(imgFull).Visibility = "Collapsed";
                this.rootElement.findName(imgEmpty).Visibility = "Collapsed";
            }
        } else {
            this.rootElement.findName(imgEmpty).Visibility = "Visible";
            this.rootElement.findName(imgHalf).Visibility = "Collapsed";
            this.rootElement.findName(imgFull).Visibility = "Collapsed";           
        }
	},	
    onCompleted: function(executor, eventArgs)  {
        if (executor.get_responseAvailable()) {        
                      
           var response=executor.get_responseData();
           
           var arr = response.split(";");
           var avg = parseFloat(arr[0].replace(",","."));
           var count = parseInt(arr[1]);
                                                
           this._rating = avg;
           this.assignValue(avg);      
           this._avgCount = count;
           this._mode = "CantRate";
           this.switchMode(this._mode);                                               
                      
        } else {
            if (executor.get_timedOut()) {
                //alert("timeout");
            } else {
                if (executor.get_aborted()) {
                    //alert("aborted");
                }

            }
        }
   },
   rateAsset: function(engineId,assetId,userId,ratingvalueId) {
        if (this._mode == "CanRate") {
            wRequest =  new Sys.Net.WebRequest();
            var url = String.format("/belux/chopsticks/rateasset.aspx?engineid={0}&assetid={1}&userid={2}&ratingvalue={3}",
                engineId, assetId, userId, ratingvalueId    
            );    
                
            wRequest.set_url(url);
            wRequest.add_completed(Function.createDelegate(this, this.onCompleted));
            wRequest.invoke();
        }
   },	
    handleStarsMouseLeave: function(sender, eventArgs) 
    {
        this.assignValue( this._rating);    
    },
	handleStar1MouseEnter: function(sender, eventArgs) 
	{
	    if (this._mode == "CanRate") {
		    this.control.content.findName("Star1_MouseEnter").Begin();
		    this.assignValue( 1 );
		}
	},
	handleStar1MouseLeave: function(sender, eventArgs) 
	{
		this.control.content.findName("Star1_MouseEnter").Stop();		
	},	
	handleStar1MouseLeftButtonDown: function(sender, eventArgs) 
	{
        this.rateAsset(this._engineId, this._assetId, this._userId,1);	    
	},	
	handleStar2MouseEnter: function(sender, eventArgs) 
	{
	    if (this._mode == "CanRate") {
		    this.control.content.findName("Star2_MouseEnter").Begin();
		    this.assignValue( 2 );
		}
	},
	handleStar2MouseLeave: function(sender, eventArgs) 
	{
		this.control.content.findName("Star2_MouseEnter").Stop();
	},	
	handleStar2MouseLeftButtonDown: function(sender, eventArgs) 
	{
        this.rateAsset(this._engineId, this._assetId, this._userId,2);	    
	},	
	handleStar3MouseEnter: function(sender, eventArgs) 
	{
	    if (this._mode == "CanRate") {
		    this.control.content.findName("Star3_MouseEnter").Begin();
		    this.assignValue( 3 );
		}

	},
	handleStar3MouseLeave: function(sender, eventArgs) 
	{
		this.control.content.findName("Star3_MouseEnter").Stop();
	},	
	handleStar3MouseLeftButtonDown: function(sender, eventArgs) 
	{
        this.rateAsset(this._engineId, this._assetId, this._userId,3);	    
	},	
	
	handleStar4MouseEnter: function(sender, eventArgs) 
	{
	    if (this._mode == "CanRate") {
		    this.control.content.findName("Star4_MouseEnter").Begin();
		    this.assignValue( 4 );
		}

	},
	handleStar4MouseLeave: function(sender, eventArgs) 
	{
		this.control.content.findName("Star4_MouseEnter").Stop();
	},	
	handleStar4MouseLeftButtonDown: function(sender, eventArgs) 
	{
        this.rateAsset(this._engineId, this._assetId, this._userId,4);	    
	},		
	handleStar5MouseEnter: function(sender, eventArgs) 
	{
	    if (this._mode == "CanRate") {
		    this.control.content.findName("Star5_MouseEnter").Begin();
		    this.assignValue( 5 );
		}
	},
	handleStar5MouseLeave: function(sender, eventArgs) 
	{
		this.control.content.findName("Star5_MouseEnter").Stop();
	},					
	handleStar5MouseLeftButtonDown: function(sender, eventArgs) 
	{
        this.rateAsset(this._engineId, this._assetId, this._userId,5);	    
	}		
}