
$(document).ready(function(){
	//setupCorners();
	setupRating();
	initializePlayer($('#videoplayer').attr('class'), 'low');
	setupBrowser();
	
});

function setupRating() {

	$('.rating a').click(function () {
      $.post("/_includes/_ajax/videojsrating.php", { videoref: $('#videoplayer').attr('class'), rating: $(this).attr('class') },
  			function(data){
				if (data.success == 'true') {
					alert('Thank you, your rating has been added.');
				} else {
					alert('Sorry, you can only rate this trailer once.');
				}
  			}
		, "json");
		$('.rating div').attr('class', $(this).attr('class'));
		setRating($(this).attr('class'));
	  return false;
    });
	
	$('.rating a').hover(function () {				   
		setRating($(this).attr('class'));
    },function () {
		setRating($('.rating div').attr('class'));
    });

}

function setRating(rating) {
		$('.rating a').each(function () {
			if ($(this).attr('class')>rating) {
				$(this).children('img').attr('src', '/_images/star-off.gif');	
			} else {
				$(this).children('img').attr('src', '/_images/star-on.gif');
			}
		});	
}

function initializePlayer(videoRef, quality) {
	
	if ($('#videoplayer')) {
		$.post("/_includes/_ajax/videojsdata.php", { videoref: videoRef },
  			function(data){
				//alert('rtmp://'+data.wowzaIp+'/'+data.wowzaPath+'/'+'hd/'+data.prefix+'-'+data.hash+'.mp4');
				var so = new SWFObject('/_flash/player.swf','mpl','663','402','9');
				so.addParam('allowscriptaccess','always');
				so.addParam('allowfullscreen','true');
				so.addParam('wmode','opaque');	
  				so.addVariable('image', data.root+'_data/previews/'+data.hash+'.jpg');
 				so.addVariable('streamer', 'rtmp://'+data.wowzaIp+'/'+data.wowzaPath);
				so.addVariable('skin', data.root+'_flash/modieus.swf');
				so.addVariable('controlbar', 'bottom');
				so.addVariable('bufferlength', '3');
				so.addVariable('autostart', 'false');
				//so.addVariable('token', 'mytestpassword');
				so.addVariable("plugins", '/_flash/hd');
				so.addVariable("hd.state", true);
				so.addVariable("hd.autoswitch", true);
				so.addVariable("hd.bandwidth", 2100);
				so.addVariable('hd.file', 'hd/'+data.prefix+'-'+data.hash+'.mp4');
				so.addVariable('file', 'normal/'+data.prefix+'-'+data.hash+'.mp4');
				so.write('videoplayer');
  			}
		, "json");
	}	
}

function setupCorners() {
	$('h1, #logo').corner("10px bottom");
	$('#information, #categories, #browser').corner("10px");
	$('#browser ul li a').corner("4px");
}

function setupBrowser() {
		$('#browser ul li a').corner("4px");
		$('#browserfrm').submit(function () {
			postBrowser();
			return false;
		});
		$('#sf').click(function () {
			if ($(this).attr('value') == 'Search term here...') {
				$(this).attr('value', '');
			}
		});
		$('#sf').blur(function () {
			if ($(this).attr('value') == '') {
				$(this).attr('value', 'Search term here...');
			}
		});
}
function postBrowser() {
	$.post("/_includes/_ajax/videojsbrowser.php", $("#browserfrm").serialize(),
	function(data){
		$('#browser').html(data);
		setupBrowser();
	});
}
