/* Global stuf*/
var Tabcontent;
var defaultTabContent;
var activeTab;
var CommentsTab;
function toggleTabs(newtab)
{
	 activeTab.removeClass('item_on');
	 activeTab.addClass('item_off');
	 newtab.removeClass('item_off');
	 newtab.addClass('item_on');
	 activeTab=newtab;
}

function hookVideoTabs()
{

  var RelVidsTab=$('#relvids_tab');	
  activeTab=RelVidsTab;
  var MoreRelTab=$('#morerelvids_tab');
  CommentsTab=$('#videocomments_tab');
  RelVidsTab.click(function(e) {
	toggleTabs(RelVidsTab);
	Tabcontent.html(defaultTabContent);
	e.preventDefault();
  });
  
  
  
  
  MoreRelTab.click(function(e) {
	 var element = $(this);
	 toggleTabs(MoreRelTab);
	 if (!element.data('cache')) {
		/* If no cache is present, show the gif preloader and run an AJAX request: */
		Tabcontent.html('<img src="/images/waitimg.gif" width="16" height="16" class="preloader" />');

		$.post('http://www.xxxymovies.com/dynamics/?r=crelated',{action:"crelated", vid:video['id'], pid:video['pid']},function(msg)
									{
											ShowRelatedVideos(msg);
											element.data('cache',Tabcontent.html());
									});
	     
	 }
	 else Tabcontent.html(element.data('cache'));
	 e.preventDefault();
	 
  });
  
  
  
  
  CommentsTab.click(function(e) {
	 //var element = $(this);
	 //alert("Clicked on  the comments");
	 toggleTabs(CommentsTab);
	 ShowComments(video['id'],1);
	 e.preventDefault();
  });
  
}


function ShowComments(vid,page) {
	var cachename="cache"+page;
	var element = CommentsTab;
	if (!element.data(cachename)) {
		Tabcontent.html('<img src="/images/waitimg.gif" width="16" height="16" class="preloader" />');
		$.post('http://www.xxxymovies.com/dynamics/?r=videoComments',{action:"videoComments", vid:video['id'], pid:page},function(msg)
									{
											ShowVideoComments(msg,page);
											element.data(cachename,Tabcontent.html());
									});		
	} else Tabcontent.html(element.data(cachename));	
}

function AddVideoComment(response,page){
	if (response['code']!="200") {
		$(".commentBoxWrapper").after("<b style=\"padding:20px;display:block;\">"+response['message']+"</b><div class=\"spacer5\"></div>");
		return;
	}	
	//200 code so process now
	$(".commentBoxWrapper").after("<div class=\"spacer5\"></div>");
	$.each(response.results, function(idx,el) {
		ShowCommentItem(idx,el,0);
	});
	$(".commentBoxWrapper").after("<div class=\"spacer5\"></div>");	
}
function ShowVideoComments(response,page){
	if (response['code']!="200") {
		Tabcontent.html(comPre+"<b style=\"padding:20px;display:block;\">"+response['message']+"</b>");
		return;
	}	
	//200 code so process now
	Tabcontent.html(comPre+"<div class=\"spacer5\"></div>");
	$.each(response.results, function(idx,el) {
		ShowCommentItem(idx,el,1);
	});
	Tabcontent.append("<div class=\"spacer5\"></div>");
}

function ShowCommentItem(x,item,m) {
	if (m==1) {
		Tabcontent.append("<div class=\"commentItem\"><div class=\"commentinfo commtxtsmall\">Posted by: <span>"+item['nick']+"</span><div class=\"spacer5\"></div>"+item['date']+"</div><div class=\"commentdata commtxtreg\">"+item['comment']+"</div><div class=\"commentreport\" id=\"repComment"+item['comment_id']+"\"><a href=\"javascript:void(0);\" onClick=\"reportComment("+item['comment_id']+");\">Report comment</a></div><div class=\"spacer\"></div></div>");
		Tabcontent.append("<div class=\"spacer5\"></div>");
	} else {
		
		$(".commentBoxWrapper").after("<div class=\"commentItem\"><div class=\"commentinfo commtxtsmall\">Posted by: <span>"+item['nick']+"</span><div class=\"spacer5\"></div>"+item['date']+"</div><div class=\"commentdata commtxtreg\">"+item['comment']+"</div><div class=\"commentreport\" id=\"repComment"+item['comment_id']+"\"><a href=\"javascript:void(0);\" onClick=\"reportComment("+item['comment_id']+");\">Report comment</a></div><div class=\"spacer\"></div></div>");
			
	}
} 

function ShowRelatedVideos(response) {
	if (response['code']!="200") {
		Tabcontent.html("<b>"+response['message']+"</b>");
		return;
	}
	//200 code so process now
	Tabcontent.html("<div class=\"spacer5\"></div>");
	$.each(response.results, function(idx,el) {
		ShowMovieItem(idx,el);
	});
	Tabcontent.append("<div class=\"spacer5\"></div>");
	
}


function ShowMovieItem(x,item) {
	
	Tabcontent.append("<div class=\"relvideo_box\"><span class=\"arrow\"></span><a href=\"http://www.xxxymovies.com"+item['video_url']+"\" class=\"vid_title\" title=\""+item['video_title']+"\">"+item['video_title']+"</a><a href=\"http://www.xxxymovies.com"+item['video_url']+"\" title=\""+item['video_title']+"\"><img src=\"http://media.xxxymovies.com"+item['pic_path']+"a"+item['mainpic']+"2_"+item['picture']+"\" id=\"vid"+x+"\" onmouseover=\"StartSlide('2','vid"+x+"','http://media.xxxymovies.com"+item['pic_path']+"','"+item['picture']+"',"+item['thumbs_no']+")\" onmouseout=\"StopSlide('vid"+x+"','"+item['mainpic']+"')\" class=\"vthumb\"/></a><div class=\"detail_bar\"><div class=\"tleft\" style=\"padding:2px;\">"+item['duration']+"</div><div class=\"tright\" style=\"padding:2px;\">"+item['date']+"</div></div><div class=\"spacer\"></div><div class=\"detail_bar redfire\"><div class=\"tleft\" style=\"padding:2px;\">"+item['views']+" views</div><div class=\"tright\">"+VideoRating(item['votesGood'],item['votesBad'])+"</div></div><div class=\"bvbar2\" id=\"bvid"+x+"\"></div></div>");	
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function VideoRating(vg,vb) {
	var g=parseInt(vg);
	var b=parseInt(vb);
	var x=(g*100)/(g+b);
	return "Rated "+Math.round(x*Math.pow(10,2))/Math.pow(10,2)+"%";
}

function ShowRateMessage(i) {
	$('#rateMsgBox').html(rateMessage[i-1]);
}


function txtzoneFocus(){
	if ( $('textarea#txtcommentarea').val()=="type here your comment") $('textarea#txtcommentarea').val("");
}
function txtzoneBlur() {
	if ( $('textarea#txtcommentarea').val()=="") $('#txtcommentarea').val("type here your comment");
}

$(document).ready(function() {
  
  Tabcontent=$('#reltabContent');
  defaultTabContent=Tabcontent.html();	
  //hook them
  hookVideoTabs(); 		
});


