$(document).ready(function(){
		
	
	  var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
		url: $("#commentform").attr("action") + "&ajaxenabled=1"

		}; 
	  var downloadoptions = { 
        beforeSubmit:  showRequestDownload,  // pre-submit callback 
        success:       showResponseDownload  // post-submit callback 


		}; 		
	  var videooptions = { 
        beforeSubmit:  showRequestVideo,  // pre-submit callback 
        success:       showResponseVideo  // post-submit callback 


		}; 

	  var reportoptions = { 
        beforeSubmit:  showRequestReport,  // pre-submit callback 
        success:       showResponseReport  // post-submit callback 


		};
		$("#report-form").ajaxForm(reportoptions);
		function showRequestReport(formData, jqForm, options) { 
		
			$("#report-form-status").html('<img src="/images/loading_small.gif" alt="" />');
			$("#report-form-submit").attr("disabled", "disabled");
			
			return true;
			
		} 
		function showResponseReport(responseText, statusText)  { 
		
				$("#report-form-submit").attr("disabled", "");
				$("#report-form-status").html(responseText);

		} 



		
		$("#videoform").ajaxForm(videooptions);
		function showRequestVideo(formData, jqForm, options) { 
		
			$("#video-form-status").html('<img src="/images/loading_small.gif" alt="" />');
			$("#video-form-submit").attr("disabled", "disabled");
			
			return true;
			
		} 
		function showResponseVideo(responseText, statusText)  { 
		
			if(responseText.toString() == "OK") {
			
				$("#video-form-status").html("Your video mirror has been submitted and is awaiting approval.");
				$("#video-form-status").fadeOut(10000, function(){
					$(".add-download-form").slideUp("slow");
				});
				
			}
			else
			{
				$("#video-form-submit").attr("disabled", "");
				$("#video-form-status").html(responseText);
			}
			
		
		} 
		
		
		
		$("#downloadform").ajaxForm(downloadoptions);
		function showRequestDownload(formData, jqForm, options) { 
			
			$("#download-form-status").html('<img src="/images/loading_small.gif" alt="" />');
			$("#download-form-submit").attr("disabled", "disabled");
			
			return true;
			
		} 
		// post-submit callback 
		function showResponseDownload(responseText, statusText)  { 
		
			if(responseText.toString() == "OK") {
			
				$("#download-form-status").html("Your download link has been submitted and is awaiting approval.");
				$("#download-form-status").fadeOut(10000, function(){
					$(".add-download-form").slideUp("slow");
				});
				
			}
			else
			{
				$("#download-form-submit").attr("disabled", "");
				$("#download-form-status").html(responseText);
			}
			
		
		} 
	

	
		
		// bind form using 'ajaxForm' 
		$('#commentform').ajaxForm(options); 
		function showRequest(formData, jqForm, options) { 
			formData['ajaxenabled'] = "1";
			
			$("#commentstatus").html('<img src="http://www.animeultima.tv/images/loading_small.gif" alt="loading..." />');
			$("#comment-field").attr("disabled", "disabled");
			$("#submit-field").attr("disabled", "disabled");
			
			return true;
			
		} 
		 
		// post-submit callback 
		function showResponse(responseText, statusText)  { 
		
			$("#commentstatus").html(responseText);
			//load comments again
			var epnum = $("#epid").attr("value");
			var chid = $("#chid").attr("value");
			var videoid = $("#videoid").attr("value");
			$("#comment-container").load("/ajax.php?method=fetchepisodecomment&episode_num="+epnum+"&channelid="+chid+"&videoid="+videoid, function(){
				$("#comment-container .last").animate({opacity: 0.1}, 1000).animate({opacity: 1}, 500).animate({opacity: 0.1}, 500).animate({opacity: 1}, 500).animate({opacity: 0.1}, 500).animate({opacity: 1}, 500).animate({opacity: 0.1}, 500).animate({opacity: 1}, 500);
			
			});
			$("#comment-field").attr("disabled", "");
			$("#submit-field").attr("disabled", "");

		
		} 
	


	
		$('.star').click(function(){
			var theform = $(this).parent("span").parent("form");
			
			$.post($(theform).attr("action"), $(theform).serialize(), function(resp){
				if(resp != '') {
					alert(resp);
					$(".ratestatus :visible").text(resp);
				}
			});
			
		});

	
	$(".close-ad").click(function(){
	
		$(".adunit").fadeOut("slow");
	
	});

	
	$(".video-meta .title").click(function(){
		$(".videoembed").slideUp("fast", function(){
		
			$(".chosen-mirror").removeClass("chosen-mirror");
		
		});
			$(this).parent("div").parent("div").parent("div").children(".videoembed").slideDown("fast", function(){
			
				$(this).parent("div").parent("div").parent("div").addClass("chosen-mirror");
			});
				
	});
	$(".download-button").click(function(){
	
		window.open($(this).attr("rel"));
	});
	
	$(".add-download").click(function(){
		$(".add-download-form").slideDown("slow");
	
	});
	$(".add-video").click(function(){
		$(".add-video-form").slideDown("slow");
	
	});
	$(".videolink").click(function(){
		
		$(".active-tab").removeClass("active-tab");
		$("li.active-tab").removeClass("active-tab");
		$(this).parent("li").addClass("active-tab");
		
		$("#loading-what").text(" ("+$(this).children("span").text()+")");

		$(this).addClass("active-tab");
		$(".activeembed").removeClass("activeembed");


		$("#embed-"+$(this).attr("rel")).addClass("activeembed");
		return false;
	});
	$("#hd").click(function(){
	
		$(".activeembed").removeClass("activeembed");
		$(".active-tab").removeClass("active-tab");
		$("#embed-"+$(this).attr("rel")).addClass("activeembed");
	});
	$(".report-button").click(function(){
		$("#report-form").slideDown("slow");
	
	});
	$(".hide-mirror").click(function(){
		var r = $(this).attr("rel");
		$.get($(this).attr("href"), function(){
			var act = $(".active-tab");
			$("#embed-"+r).remove();
			act.removeClass("active-tab");
			act.remove();
			$("#video-tabs span:first").addClass("active-tab");
			$("#embed-"+$("#video-tabs span:first").attr("rel")).addClass("activeembed");
		});
		return false;
	});
	
	$(".show-archive").toggle(function(){
		$("#archive-videos").slideDown("slow");
		$(this).html("Less Videos &laquo;");
	}, function(){
		$(this).html("More Videos &raquo;");
		$("#archive-videos").slideUp("slow");
	});
	
	$(".veohlink").click(function(){
	
		$("#embed-"+$(this).attr("rel")).html('<embed src="'+SITE_PATH+'veoh/'+$(this).attr("rel")+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="100%" height="100%" wmode="transparent"></embed>');
	
	});
	$(".mvlink").click(function(){
	
		$("#embed-"+$(this).attr("rel")).html('<embed src="'+SITE_PATH+'mv/'+$(this).attr("rel")+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="100%" height="100%" wmode="transparent"></embed>');

	});	
	
	$(".toggle-part").click(function(){
		
		$(".parts").css("display","none");
		$("#"+$(this).attr("rel")).css("display", "block");
		$("#part-buttons li a.active").removeClass("active");
		$(this).addClass("active");
	});
	$("#lightsoff").click(function(){
		$("body").prepend('<div id="fader" style="position: absolute; z-index: 1000; left: 0px; top: 0px; background: url(/images/overlay.png); width: '+document.body.clientWidth+'px; height: '+document.body.clientHeight+'px; display: none;"></div>');
		$("#embed_holder").css("z-index", "2000");
		$("#fader").fadeIn(500, function(){
			$("body").prepend('<div id="fader-message" style="position: absolute; z-index: 1100; bottom: 10px; left: 10px; color: #fff; font-size: 18px;">Click anywhere on the screen to turn the lights on</div>');
			$("#fader").bind("click", function(){
			
				$("#fader-message").fadeOut(1000, function(){
					$("#fader-message").remove();
					$("#fader").fadeOut(500, function(){
						$("#fader").remove();
						$("#embed_holder").css("z-index", "0");
					});
				});
			});
		});
	
	});
	
	$("#watchlist").click(function(){
		$("body").prepend('<div id="boxloader" style="z-index: 2000; background: #fff; border: 5px solid #aaa; padding: 10px; width: 500px; height: 400px; position: absolute;"><div id="boxloader-controls" style="margin: 20px 0px;"><img src="/images/ajax-loading.gif" alt="" /></div><div id="boxloader-content"></div></div>');
		showdeadcenterdiv(500, 400, "boxloader");
		$("#boxloader-content").load($(this).attr("href"), function(){
			$("#boxloader-controls").html('<a href="javascript:void(0);" id="boxloader-close">Close this window</a>');
			$("#boxloader-close").bind('click', function(){
			
				$("#boxloader").slideUp("fast", function(){
					$("#boxloader").remove();
				});

			
			});
			//bind form
			$("#formtracker").submit(function(){
				$("#tracker-status").html('<img src="/images/ajax-loading.gif" alt="" />');
				$.post($(this).attr("action"), $(this).serialize(), function(data){
				
					if(data.toString() == "OK")
					{
						$("#boxloader").slideUp("fast", function(){
							$("#boxloader").remove();
						});
					}
					else
					{
						$("#trackre-status").html(data);
					}
				});
				return false;
			});
			
		});
		return false;
		
	});
});

function showdeadcenterdiv(Xwidth,Yheight,divid) {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
scrolledX = self.pageXoffset;
scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topoffset + 'px';
r.left = leftoffset + 'px';
r.display = "block";
} 
function likeit(epnum,animeid) {
		$.post("/ajax.php?method=likethis&episode_num="+epnum+"&animeid="+animeid, {like: "1"}, function(data){
			/*if(data.toString().length > 0)
			{
				$("#isworking-"+vidid).text(data);
				$("#isworking-"+vidid).fadeOut(5000);
			}*/
			$("#like-link").text(data);
		});

}

function switchplayer(playerid) {
	$(".mirrorselected span.nowplaying").remove();
	$(".mirrorselected").removeClass("mirrorselected");
	$("#link-player-"+playerid).addClass("mirrorselected");
	$("#link-player-"+playerid+" a").append('<span class="nowplaying">now playing</span>');
	$(".divplayer:visible").fadeOut("slow",function(){
		$("#player-"+playerid).fadeIn("slow");
	});
	
	document.location.href = "#watch="+playerid;

}


function working(vidid) {
	
	$.post("/ajax.php?method=videoworking&mirrorid="+vidid, {working: "1"}, function(data){
		alert(data);
	});
}

function notworking(vidid) {

	$.post("/ajax.php?method=videoworking&mirrorid="+vidid, {notworking: "1"}, function(data){
		alert(data);
	});
}

function votelang(vidid, lang) {
	$.post("/ajax.php?method=votelang&mirrorid="+vidid, {language: lang}, function(data){
		alert(data);
	});

}