var bAllowRate = true;

$(document).ready( function() {

	// Play movie
	flowplayer("player", LINKROOT+"/extensions/flowplayer/flowplayer-3.1.1.swf", { 
	    clip: {   
	        url: sStreamUrl,
	        provider: 'lighttpd'
	    }, 
		plugins: { 
		    lighttpd: {  
		        url: LINKROOT+'/extensions/flowplayer/flowplayer.pseudostreaming-3.1.2.swf'  
		    },
		 
		    myContent: { 
		        url:  LINKROOT+'/extensions/flowplayer/flowplayer.content-3.1.0.swf', 
		        bottom: 50, 
		        width: '99%',
		        height: 310,
		        borderRadius: 0,
		        display: 'none',
		        backgroundColor: '#99BAF8',
		        stylesheet: LINKROOT+'/css/frontend/advertising.css',
		        html: '' 
		    } 		    
	    }
	});
	
	
	/*** INVideo Advertising init **/
	setTimeout( function() {
		displayAdv();
	},2000);
	
	
	/*** Accounting ***/
	setInterval(accounting,10000);
	
	/*** Events ***/
	// Add to favorites
	$('#movie_favorites').click(function() {
		addToFavorite(iMovie);
	});
	
	
	// Change bitrate
	$('#bitrate_100').click(function(){
		changeBitRate(100);
	})
	$('#bitrate_400').click(function(){
		changeBitRate(400);
	})
	$('#bitrate_1000').click(function(){
		if(sUsername == '') {
			sendToRegister('hq');
		} else {
			changeBitRate(1000);
		}
	})
	
	// Download
	$('#download_mp4_button').click( function () {
		if(sUsername == '') {
			sendToRegister('dl');
		} else {
			sendToDownload('mp4');
		}
	});
	
	$('#download_org_button').click( function () {
		if(sUsername == '') {
			sendToRegister('dl');
		} else {
			sendToDownload('org');
		}
	});	
	
	$('#bitrate_nohd').click(function() {
		alert('High quality video is not available for this movie');
	});
	
	// Rate
	$('div.ratable img').mouseover( function() {
		if(!bAllowRate) {
			return;
		}
		var iRating = $(this).attr('rating');
		x=1;
		while(x <= iRating) {
			lightStar($('#rate_'+x));
			x++;
		}
		
		if(x < 5) {
			while(x < 6) {
				unLightStar($('#rate_'+x));
				x++;
			}
		}
		
	});
	$('div.ratable img').mouseout( function() {
		if(!bAllowRate) {
			return;
		}		
		var iRating = $(this).attr('rating');
		x=1;
		while(x <= iRating) {
			unLightStar($('#rate_'+x));
			x++;
		}
		if(x < 5) {
			while(x < 6) {
				unLightStar($('#rate_'+x));
				x++;
			}
		}		
	});
	
	$('#movie_rating').mouseout( function() {
		if(!bAllowRate) {
			return;
		}		
		x=1;
		while(x<6) {
			restoreStar($('#rate_'+x));
			x++;
		}
	});
	
	$('div.ratable img').click( function () {
		if(!bAllowRate) {
			return;
		}		
		var aParts = this.id.split('_');
		var iRating = aParts[1];
		$('#rating_label').html('Thanks for rating!');
		$('#rating_container').removeClass('ratable');
		bAllowRate = false;
		
		$.post(LINKROOT+'/do/movie/rate/'+iMovie,{rating: iRating}, function(iNew) {
			i = 1;
			iNew = parseInt(iNew);
			while(i < 6) {
				if(i <= iNew) {
					lightStar( $('#rate_'+i));
				} else {
					unLightStar( $('#rate_'+i));
				}
				
				i++;
			}
		});
		
	});
	
});


function lightStar(that) {
	var sOver = $(that).attr('on');
	$(that).attr({src: sOver});
}

function unLightStar(that) {
	var sOver = $(that).attr('off');
	$(that).attr({src: sOver});
}
function restoreStar(that) {
	var sRestore = $(that).attr('original');
	$(that).attr({src: sRestore});
}
function changeBitRate(iRate) {
	$.get(LINKROOT+'/do/movie/change_bitrate/'+iRate, function(){
		location.reload(true);	
	});
	
}

function sendToRegister(sType) {

	switch(sType) {
		
	
	case 'dl':
		document.location.href = LINKROOT+'/view/user/register/?dl=true';
		break;
		
	case 'hq':
		document.location.href = LINKROOT+'/view/user/register/?hq=true';
		break;
		
	default:
		document.location.href = LINKROOT+'/view/user/register';
		break;
	}

}

function addToFavorite(iMovie) {
	if(sUsername == '') {
		sendToRegister('You need to be logged in to add movies to your favorites.');
	} else {
		
	}
}

function sendToDownload(sType) {
	document.location.href = LINKROOT+'/do/movie/download/'+iMovie+'/?type='+sType;
}

function displayAdv() {
	// Get hook
	var oContentLayer = $f("player").getPlugin("myContent");
	
	var oDate = new Date; // Generic JS date object
	var iTime = oDate.getTime(); // Returns milliseconds since the epoch
	var iTime = parseInt(iTime / 1000);	
	
	// Set content & display
	$.get(LINKROOT+'/view/movie/advertising/'+iMovie+'?now='+iTime, function(sContent) {
		// Get height
		iPos = sContent.indexOf('<!--  HEIGHT =');
		sHeight = sContent.substring(iPos);
		aParts = sHeight.split('"');
		iHeight = parseInt(aParts[1].replace('"',''));
		
		// Get width
		iPos = sContent.indexOf('<!--  WIDTH =');
		sWidth = sContent.substring(iPos);
		aParts = sWidth.split('"');
		iWidth = parseInt(aParts[1].replace('"',''));
		
		iHeight = iHeight + 40;
		iWidth = iWidth + 25;
		
		// Logged in users do not see big images
		if(sUsername != '') {
			iHeight = 25;
			iWidth = '99%';
		}
		
		oContentLayer.setHtml(sContent);
		
		// Give a couple of secs to load content
		setTimeout( function() {
			oContentLayer.fadeIn();
			oContentLayer.show();
			
			if(sUsername != '') {
				fadeAdv(iHeight,iWidth);
			} else {
				popAdv(iHeight,iWidth);
			}

			// Show for 10 secs and hide
			setTimeout( hideAdv,10000);
			
		},5000);
	});
	
	
	
	
	
	
}

function popAdv(iHeight,iWidth) {
	var oContentLayer = $f("player").getPlugin("myContent");
	oContentLayer.animate({bottom:30, height:iHeight, width: iWidth});
}

function fadeAdv(iHeight,iWidth) {
	var oContentLayer = $f("player").getPlugin("myContent");
	oContentLayer.css({bottom:30, height:iHeight, width: iWidth});
	oContentLayer.fadeIn(3000);
}

function unpopAdv() {
	var oContentLayer = $f("player").getPlugin("myContent");
	oContentLayer.animate({bottom:30, height:'20'});
	
}

function hideAdv() {
	// Get hook
	var oContentLayer = $f("player").getPlugin("myContent");
	
	
	if(sUsername == '') {
		oContentLayer.fadeOut(3000);
		
		// Wait 300 secs and display again
		setTimeout( displayAdv,300000);		
		
	} else {
	
		// Move to bottom and take full width
		oContentLayer.animate({bottom:30, height:25, width: '99%'});
		
		// Wait 15 secs before final hide
		setTimeout( function() {
			oContentLayer.fadeOut(3000);
	
			// Wait 120 secs and display again
			setTimeout( displayAdv,60000);
			
		},15000);
	}

}

function accounting() {
	$.get(LINKROOT+'/do/movie/accounting',function(iResponse) {
		if(iResponse == '0') {
			document.location.replace(LINKROOT+'/view/user/register?overusage=true');
		}
	});
}
