$(function(){
	$("#searchForm #marksona").autocomplete({
		source : "queries/search_movie.php",
		minLenght : 2,
		select: function( event, ui ) {
		  window.location.href = ui.item.hyper;
		},
	}).data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a href='" + item.hyper +"'>" + item.name_est + " (" + item.year + ")<br><small>" + item.name_eng + "</small></a>" )
				.appendTo( ul );
		};
	$("#searchForm a.sendButton").click(function() {
		$("#searchForm").submit();
		return false;
	});;
	
	$('#example').tabs();
	
	$("table.list_small tr td:last-child, table.list_small tr th:last-child, table.list td:last-child, .quickbox ul:last-child, .quickbox ol:last-child, .quickbox table.links:last-child").addClass('lastchild');
	
	$("tr.seasonepisode").hide();
	$("form.addToDiary input.diaryType").click(function() {
		if($(this).val() == "movies") {
			$("form.addToDiary tr.seasonepisode").hide();
		} else {
			$("form.addToDiary tr.seasonepisode").show();
		}
	});

	$('#slides').slides({
		generatePagination: true,
		play: 5000,
		pause: 2500,
		hoverPause: true,
		generateNextPrev: false,
	});
	$('ul.morePictures').hide();
	$('.desc .block').css({opacity: 0.8});
	$('.videothumb img').fadeTo('fast', 0.7);
	
	$("a.collapse").click(function(){
		$("#slides .block").slideToggle();
		$("#slides a.collapse").toggleClass("show");
	});
	
	$("a.morePictures").toggle(function(){
		$("ul.morePictures").slideToggle();
		$(this).html("&laquo; v&auml;hem pilte");
	},
	function () {
		$("ul.morePictures").slideToggle();
		$(this).html("&raquo; rohkem pilte");
	});
	
	Cufon.replace('#slides h2, .loginpanel h1, .quickbox h1, .comingsoon h1, .quickbox h3, .loginpanel h3, .information h1, .information h2, .newsitem .date span, .bigbox h6, .bigbox h3, .bigbox h4, .list th, .list_small th, .profile h3');
	Cufon.replace('.menubar li a, table.newsbox td h4 a, .loginpanel h2 a', {hover: 'true'});
    $('#moviepreviewCarousel').jcarousel({
		visible: 4,
		scroll: 4,
    });
	
	$(".duo_left").before('<div style="height: 100%; width: 10px; position: absolute; top: 0; left: 710px; background: #535353;"></div>');
	
	$(".smallbox .pictures a, .information .poster a.pic, .quickbox .miniGallery a, #readRules").fancybox({
		'titleShow'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition' : 'over',
		'overlayOpacity' : 0.8,
		'overlayColor' : '#000'
	});
	
	$('#lookMore').fancybox({
		'width':	660
	});
	
	$(".videothumb a").click(function() {
		$.fancybox({
			'padding'       : 0,
			'autoScale'   	: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'overlayOpacity' : 0.8,
			'overlayColor' : '#000',
			'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'          : 'swf',    // <--add a comma here
			'swf'           : {'allowfullscreen':'true', 'wmode':'opaque'} // <-- flashvars here	});
		});
		return false;
	});
	
	$(".videothumb img").hover(function()
	{
		$(this).fadeTo('fast', 1);
	},
	function ()
	{
		$(this).fadeTo('fast', 0.7);
	});
	
	/* 
		HINDAMINE
		HINDAMINE
		HINDAMINE
	*/
	
	$(".rate").hide();
	$(".advanced").hide();
	$(".overallrating").hide().attr('disabled', true);;
	$(".advanced select").attr('disabled', true);
	
	$(".ratings .stars, .ratings .rate a.close").click(function() 
	{
		$(".rate").slideToggle("fast");
	})
	
	$(".rate a.more").toggle(function(){
		// When advanced rating is opened
		$(".advanced").slideToggle("slow");
		$(".overall").attr('disabled', true).hide();
		$(".advanced select").removeAttr('disabled');
		$(".overallrating").show().removeAttr('disabled');
		$(this).text("« üldine hindamine");

	},
	function()
	{
		// When advanced rating is closed
		$(this).text("» põhjalikum hindamine");
		$(".overall").removeAttr('disabled').show();
		$(".advanced select").attr('disabled', true);
		$(".overallrating").hide().attr('disabled', true);
		$(".advanced").slideToggle("slow");
	});
	
	$(".advanced_voting").change(function() {
		var vote1			= parseInt($(".vote1").val());
		var vote2			= parseInt($(".vote2").val());
		var vote3			= parseInt($(".vote3").val());
		var vote4			= parseInt($(".vote4").val());
		var middleRating	= (vote1 + vote2 + vote3 + vote4) / 4;
		$(".overallrating").val(middleRating.toFixed(2));
	});
	/* HINDAMISE LÕPP */
	
	$("a.add_thing").click(function() {
		if($(this).text() == "Lisa näitleja") {
			$('<tr><td><input name="actor[]" type="text" value=""> <input name="actor_role[]" type="text" /></td><td><a class="removeMe" href="#">Eemalda näitleja</a></td></tr>').appendTo($(this).closest("table"));
		}
		else if($(this).text() == "Lisa lavastaja") {
			$('<tr><td><input name="director[]" type="text" value=""></td><td><a class="removeMe" href="#">Eemalda lavastaja</a></td></tr>').appendTo($(this).closest("table"));
		}
		else if($(this).text() == "Lisa pilt") {
			$('<tr><td><input name="pictures[]" type="text" class="long" /> <input type="text" name="sources[]"></td><td><a class="removeMe" href="#">Eemalda pilt</a></td></tr>').appendTo($(this).closest("table"));
		}
		else if($(this).text() == "Lisa video") {
			$('<tr><td><input name="videos[]" type="text" class="long" /> <input type="text" name="video_name[]"></td><td><a class="removeMe" href="#">Eemalda video</a></td></tr>').appendTo($(this).closest("table"));
		}
		else if(($(this).text() == "Lisa link") && ($(this).closest("table").attr('class') == "news")) {
			$('<tr><td><input name="links[]" class="long" type="text" /> <input type="text" name="link_name[]"></td><td><a class="removeMe" href="#">Eemalda link</a></td></tr>').appendTo($(this).closest("table"));
		}
		else if(($(this).text() == "Lisa link") && ($(this).closest("table").attr('class') == "people")) {
			$('<tr><td><input name="links[]" class="long" type="text" /> <select name="link_types[]"><option>Ametlik kodulehekülg</option><option>Vikipeedia</option><option>Facebook</option><option>IMDB</option></select></td><td><a class="removeMe" href="#">Eemalda link</a></td></tr>').appendTo($(this).closest("table"));
		}
		else
		{
			$('<tr><td><input name="links[]" class="long" type="text" /> <select name="link_types[]"><option>Ametlik kodulehekülg</option><option>Vikipeedia</option><option>Treilerid</option><option>IMDB</option></select></td><td><a class="removeMe" href="#">Eemalda link</a></td></tr>').appendTo($(this).closest("table"));
		}
		return false;
	});
	
	$(".removeMe").live("click", function() {
		$(this).closest("tr").remove();
		return false;
	});
	
	$('.miniGallery li img:first').addClass("active");
	
	$('#changeDiary a.changeDiary').live("click", function() {
		var splitaction	= $(this).attr('href').split("-");
		$(this).closest("tr").load("loadFunctions.php", { id : splitaction[2] , tegevus : splitaction[1]});
		return false;
	});
});

function sortTable(type, page)
{
	if(type == "genre" && document.sorteering.genre.value != "Vali žanr") {
		window.location.href = "/" + page + "/" + "zanr/" + document.sorteering.genre.value + "/1/";
	}
	else if(type == "year" && document.sorteering.year.value != "Vali aasta") {
		window.location.href = "/" + page + "/" + "aasta/" + document.sorteering.year.options[document.sorteering.year.selectedIndex].text + "/1/";
	}
	else if(type == "other" && document.sorteering.sorteeri.value != "Vali järjekord") {
		if(document.sorteering.sorteeri.value == "nimi")
		{
			window.location.href = "/" + page + "/" + "nimi/a/1/";
		}
		else
		{
			window.location.href = "/" + page + "/" + document.sorteering.sorteeri.value + "/1/";
		}
	}
	else if(type == "order") {
		alert(window.location);
	}
}
