var commentbaractive = 0;
var searchbaractive = 0;
$(document).ready(function(){

Cufon.replace('h1, h2, h3, .sub_menu li a', { fontFamily: 'Bebas Neue', ignoreClass:'nocufon', hover:true });
$( "#tabs" ).tabs();
$(".typecomment").click(function(){

if (commentbaractive == "x")
{
	return false;
}

if (commentbaractive == 0)
	{
		$(".form").animate({height: "285px"},function(){
			$(".form ul").fadeIn();
		});
		commentbaractive = 1;
	}
else
	{
		$(".form ul").fadeOut(function(){
		$(".form").animate({height: "0px"});
		});
		commentbaractive = 0;
	}

});

//formoviebar

$("#dialog").click(function(){
	
	var diattr = $("#dialog").css("display");
	if (diattr != "none")
	{
	$("#dialog").dialog('close');
	}
});


$("#mainmenu li").hover(function(){
	var position = $(this).position();
	$(this).children("ul").css("left", position.left+10 + "px");
	$(this).children("ul").show();
},function(){
	$(this).children("ul").hide();
});

$("#mainmenu li ul li").hover(function(){
	var position = $(this).position();
	var m_left;
	var m_top;
	if ( $.browser.opera ) {
		m_left = position.left-position.left-position.left+127;
		m_top = -10;
	}
	else
	{
		m_left = 125;
		m_top = -10;
	}

	$(this).children("ul").css("left", position.left+m_left + "px");
	$(this).children("ul").css("top", position.top+m_top + "px");
	$(this).children("ul").show();
},function(){
	$(this).children("ul").hide();
});


$("#mainmenu li").css("cursor","pointer");

$("#mainmenu li:not(#searchformovie, #search_input)").click(function(){
	var href = $(this).children("a").attr("href");
	if (href != null)
	{
	location.href = href;
	}
	return false;
})


//torrenthref
$(".torrenthref").click(function(){

var thisobj = this;
var href = $(this).attr("href");
var title = $(this).attr("title");
var partantalyear = $(this).next().attr("title");
var year = partantalyear.substring(0,4);
var antal = partantalyear.substring(4,partantalyear.length);
var copycontentid = $(this).next().attr("id");
var is_showing = $("#" + copycontentid).css('display');
	

	if (is_showing != 'none')
	{
		$("#" + copycontentid).fadeOut(function(){
		var li_h = $(thisobj).parent().css("height");
		$(thisobj).parent().parent().css("backgroundColor", "#fff");
		$(thisobj).parent().next().css("height", li_h);
		$(thisobj).parent().next().next().css("height", li_h);
		$(thisobj).parent().next().next().next().css("height", li_h);
		$(thisobj).parent().next().next().next().next().css("height", li_h);
		$(thisobj).parent().next().next().next().next().next().css("height", li_h);
		$(thisobj).parent().next().next().next().next().next().next().css("height", li_h);
		
		});
		return false;
	}

	if (antal > 1)
	{		

		$(this).append("<img src='" + dns + "images/loading_big.gif' class='loadingimg' alt='Loading...' />");
					
		$.ajax({
			  type: "POST",
			  url: dns + 'gettorrent.php',
			  data: 'title=' + escape(title) + "&year=" + year,
			  success: function(data) {
				$("#" + copycontentid).html(data);
				$(".loadingimg").remove();
				$("#" + copycontentid).fadeIn();
				//tar reda på height
				var li_h = $(thisobj).parent().css("height");
				
				$(thisobj).parent().parent().css("backgroundColor", "#f8f8f8");
				$(thisobj).parent().next().css("height", li_h);
				$(thisobj).parent().next().next().css("height", li_h);
				$(thisobj).parent().next().next().next().css("height", li_h);
				$(thisobj).parent().next().next().next().next().css("height", li_h);
				$(thisobj).parent().next().next().next().next().next().css("height", li_h);
				$(thisobj).parent().next().next().next().next().next().next().css("height", li_h);

			 }
		});
	}
	else
	{
		location.href=href;
	}

return false;
});


$("#problem_submit").click(function(){
	
	var msg = $("#problem_textarea").val();
	var id = $("#problem_id").val();

	if (msg == "")
	{
		alert("Vad är det du vill rapportera?");
		return false;
	}
	else if (msg.length < 10)
	{
		alert("Vad snäll och skriv mer än " + msg.length + " tecken.");
		return false;
	}
	
	$("#tabs-3").html("<center><img src='" + dns + "images/loading.gif' alt='Laddar' /></center>");
	
	var wherearewe = $("#wherearewe").text();
	$.ajax({
			  type: "POST",
			  url: dns + 'save.php?what=reportproblem&cat=' + wherearewe,
			  data: 'msg=' + escape(msg) + '&torrentid=' + id,
			  success: function(data) {
				$("#tabs-3").html(data);
			 }
		});

return false;
});


$(".infoimg").click(function(){

	var torrentid = $(this).attr("id");
	if (torrentid.indexOf("torrent") == -1)
	{
		return false;
	}
	
	var torrentname = $(this).attr("alt");
	$("#dialog").dialog({width: 800, show: "fade", height: 500,  resizable: false});
	$('#dialog').dialog('option', 'title', torrentname);
	$("#dialog").html("<br/><br/><br/><center><img src='" + dns + "images/loading.gif' alt='Laddar...' /></center>");
	
	$.ajax({
		  type: "POST",
		  url: dns + 'getinfomovie.php',
	      data: 'torrentid=' + escape(torrentid),
		  success: function(data) {
			$("#dialog").html(data);
		 }
	});

return false;
});

	//pages
	$("#pages li").css("cursor","pointer");
	$("#pages li").click(function(){
		var href = $(this).children("a").attr("href"); 
		location.href = href;
	return false;
	});


	$("#comment_submit").click(function(){
		
		var torrentid = $("#comment_torrentid").attr("title");
		var name = $("#comment_name").val();
		var msg = $("#comment_msg").val();
		var wherearewe = $("#wherearewe").text();

		if (name.length < 3 && msg.length < 5)
		{
			alert("Användarnamnet bör vara längre än 3 tecken, samt meddelande längre än 5 tecken.");
			return false;
		}
		$("#comment_submit").fadeOut();

		$.ajax({
		  type: "POST",
		  url: dns + 'save.php?what=comment&torrentid=' + torrentid + '&cat=' + wherearewe,
	      data: 'name=' + escape(name) + '&msg=' + escape(msg),
		  success: function(data) {
			$(".form").animate({height: "40px"});
			$(".form").html("<p style='font-size: 12px; text-align: center; font-family: arial;'>" + data + "</p>");
			commentbaractive = "x";
		 }
		});

	});


	$("#picksearchcat").change(function(){
			if (this.value == "warez")
			{
				$(".formoviebar").fadeOut(function(){
					$("#search_bar").animate({height: "120px"});
					$(".searchbarcontent .titel").focus();
				});
			}
			else
			{
				$("#search_bar").animate({height: "190px"},function(){
				$(".formoviebar").fadeIn();
				$(".searchbarcontent .titel").focus();
				});
			
			}
		});	


	$("#searchformovie").click(function(){
		
		if (searchbaractive == 0)
		{	
			$("#search_bar").fadeIn();
			$("#search_bar").animate({height: "190px", top: "-17px"},function(){
			$("#search_bar").css("padding","5px");
					$.ajax({
					  type: "GET",
					  url: dns + 'searchbar.php',
					  success: function(data) {
						$("#search_bar").html(data);
						$(".searchbarcontent .titel").focus();
					 }
				});
			});
			searchbaractive = 1;
		}
		else
		{
			$("#search_bar").html("");
			$("#search_bar").animate({height: "0px", top: "0px"});
			$("#search_bar").fadeOut();
			$("#search_bar").css("padding","0px");
			searchbaractive = 0;
		}
	return false;
	});



    $('.boxgrid.caption').hover(function(){  
        $(".cover", this).stop().animate({top:'165px'},{queue:false,duration:160});  
    }, function() {  
        $(".cover", this).stop().animate({top:'190px'},{queue:false,duration:160});  
    }); 

	$('.boxgrid.captionfull').hover(function(){  
        $(".cover", this).stop().animate({top:'164px'},{queue:false,duration:160});  
    }, function() {  
        $(".cover", this).stop().animate({top:'190px'},{queue:false,duration:160});  
    }); 

	//SERIALCLICK
	var serialcontenttitle;
	$(".serialtitel").css("cursor","pointer");
	$(".serialtitel").toggle(function(){
		var thisobj = this;
		serialcontenttitle = $(this).parent().next().attr("title");
		var antal = serialcontenttitle.substring(0,1);
		
		if (antal < 2)
		{
		
		var href = $(this).children("a").attr("href");
		window.open(href, '_blank');
		return false;
		}

		$(this).parent().css("background","#eee");
		$(this).parent().next().fadeIn();
		$(this).parent().next().attr("title","");

		$.ajax({
		  type: "POST",
		  url: dns + 'gettvshow.php',
		  data: 'search=' + escape(serialcontenttitle),
		  success: function(data) {
			$(thisobj).parent().next().html(data);		  
		  }
		});

	return false;
	},function(){
		$(this).parent().css("background","none");
		$(this).parent().next().fadeOut();
		$(this).parent().next().attr("title",serialcontenttitle);
		return false;
	});

	$(".updatesele").click(function(){
		
		var id = $("#torrentpresentation .rightcontainer .stats li .holder").html();
		var thisobj = this;
		var wherearewe = $("#wherearewe").text();
		$(this).attr("src",dns + "images/loading.gif");
		 $.ajax({
		  type: "POST",
		  url: dns + 'updatesele.php?what=' + wherearewe,
	      data: 'torrentid=' + escape(id),
		  success: function(data) {
			$(thisobj).fadeOut();
			$("#seleinfo").html(data);
		 }
		});
	});

});


function search_submit()
{	
		var searchinput = $("#searchinput").val();
		var searchpick = $("#picksearchcat").value;

		alert(searchpick);
		return false;
		if ($("#maincontent").attr("id")!="maincontent")
		{
		location.href= dns + 'search/' + searchinput + "/";
		return false;
		}

		$.ajax({
		  type: "POST",
		  url: dns + 'getsearch.php',
	      data: 'search=' + escape(searchinput),
		  success: function(data) {
			$("#maincontent").html(data);
		 }
		});

return false;
}

function run_search()
{
search_submit();
return false;
}

function dialogbox(torrentid, title)
{

	$("#dialog").dialog({width: 800, show: "fade", height: 500,  resizable: false});
	$('#dialog').dialog('option', 'title', title);
	$("#dialog").html("<br/><br/><br/><center><img src='images/loading.gif' alt='Laddar...' /></center>");
	
	$.ajax({
		  type: "POST",
		  url: dns + 'getinfomovie.php',
	      data: 'torrentid=' + escape(torrentid),
		  success: function(data) {
			$("#dialog").html(data);
		 }
	});

return false;
}
