$(document).ready(function() {
	$.video_player = function(video_id, auto_play, is_hp){
	   var html  = '';
	   params = "?enablejsapi=1&version=3&rel=0&playerapiid=ytplayer&ap=%2526fmt%3D22&showinfo=0&autoplay=" + auto_play
	   if(Boolean(is_hp)){
	       playerHeight = 258;
		   playerWidth = 350;
		   params += "&controls=0";
	   }
	   else{
	       playerHeight = 370;
		   playerWidth = 650;
	   }
	   
		html += '<object height="'+ playerHeight + '" width="' + playerHeight + '">';
		html += '<param name="movie" value="http://www.youtube.com/v/' + video_id + params + '"> </param>';
		html += '<param name="wmode" value="transparent"> </param>';
		html += '<param name="allowfullscreen" value="true"> </param>'; 
		html += '<embed src="http://www.youtube.com/v/'+video_id + params+'"';
   		html += ' allowfullscreen="true" '; 
		html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+playerHeight+'" width="'+playerWidth+'"></embed>';
		html += '</object>';

		return html;
	}
	
	
	$.fn.create_player = function(is_parent)
	{
		$("div.video").removeClass("video_selected");
		
		if(is_parent){
			$(this).addClass("video_selected");
			video_id = $(this).attr("rel");
		}
		else{
			$(this).parent("div").parent("div").addClass("video_selected");
			video_id = $(this).parent("div").parent("div").attr("rel");
		}		
		html = $.video_player(video_id, 0, false);
		$("div#video_player").html(html);
	};
	
	$("select[name=video_category]").change(function(){
		location.href= $("select[name=video_category] option:selected").val() + "#videos";
	});
	
	$("select[name=video_category]").keyup(function(){
		location.href= $("select[name=video_category] option:selected").val() + "#videos";
	});
	
	$("div.video div a.vid_link:first").create_player(false);

	$("div.video a.vid_link").mouseover(function(){
		$("div.video").parent("div").parent("div").removeClass("video_on");
		$(this).parent("div").parent("div").addClass("video_on");
	});
	
	$("div.video img").mouseover(function(){
		$("div.video").parent("div").parent("div").removeClass("video_on");
		$(this).parent("div").parent("div").addClass("video_on");
	});
	
	$("div.video").mouseover(function(){
		$("div.video").removeClass("video_on");
		$(this).addClass("video_on");
	});
	
	$("div.video a.vid_link").click(function() {
		$(this).create_player(false);
		return false;
	});
	
	$("div.video").click(function() {
		$(this).create_player(true);
		return false;
	});
	
	$("div.video img").click(function() {
		$(this).create_player(false);
	});
	/*
	if($("div#video_list div.video").length == 1){
		$("div#video_list").hide();
	}*/
	
	if($("select[name=video_category] option").size() <= 2){
		$("div#video_cat_form").hide();
	}	
});
