// JavaScript Document ääööö

$.ajaxSetup ({
    cache: false
});

var videoWidth = 0;
var videoIndex = 1;
var videoCount = 0;
var videoSlide = 0;

$(document).ready(function(){
	//jQuery	
	
	$("a[title='extrainfo']").parent().next().html("<p class='extrainfo'>"+$("a[title='extrainfo']").parent().next().html()+"</p>");
	$("a[title='extrainfo']").parent().next().hide();
	
	$("#menu ul li a").each(function() {
		$(this).html($(this).text().replace("UNDO", "<strong>UNDO</strong>"));
	});
	
	
	//blockquotes
	$("blockquote").wrap("<div class='shadowbox'>");
	
	// Video slider
	$("#video-slider li").each(function() {
		videoWidth += $(this).outerWidth();
		videoCount++;
	});
	

	
	
	
	$("#video-slider ul").css('width', videoWidth + 37);
	

	
	//extrainfo
	$("a[title='extrainfo']").click(
      function () {
		$(this).parent().next().css("z-index", "9");  
        $(this).parent().next().show();
		return false;
      }
    );
	
	$("p.extrainfo").click(
      function () {
        $(this).parent().hide();
      }
    );
	
	//$("blockquote ol li strong").wrap("<div class='bubble'>");
	$("blockquote ol li").each(function() {						
		$(this).replaceWith("<div class='bubble-holder'>"+$(this).html()+"</div>");
	});
	
	
	$("blockquote .bubble-holder").each(function() {
		var teksti = $(this).text();
		$(this).html($(this).html().replace(teksti, ""));
		
		$(this).append("<div class='bubble'><div class='bubble-btm'><table cellpadding='0' cellspacing='0'><tr><td valign='middle'>"+teksti);
	});
	
	var i = 1;
	$("blockquote .bubble-holder").each(function() {
		if( i % 8 == 0 ) {
			$(this).css("margin-right", "0");
		}
		i++;
	});
	
	/*
	$("blockquote .bubble-holder img").each(function() {
		var teksti = $(this).parent().text();
		$(this).parent().html($(this).parent().html().replace(teksti, ""));
		
		$(this).after("<div class='bubble'>"+teksti);
	});
	
	*/
	
	$("blockquote ol").append("<div class='clear'>");
	
	$(".bubble").hide();
	
	$("blockquote img").mouseover(function() {
		$(this).next().show();
	}).mouseout(function(){
      	$(this).next().hide();
    });
	
	//firstpage table bg
	$("table.table-bg").wrap('<div class="table-bg" />');
	
	
	
 });

/*
function toggleShare ( linkElement )
{
	if ( $( linkElement ).prev().is( ':hidden' ) )
		$( linkElement ).prev().fadeIn();
	else
		$( linkElement ).prev().fadeOut();
		
	
	return false;
}
*/

function toggleComments() {
	if ( $("#comments").is( ':hidden' ) )
		$("#comments").fadeIn();
	else
		$("#comments").fadeOut();
		
	
	return false;
}

function toggleCommentForm() {
	if ( $("#respond").is( ':hidden' ) )
		$("#respond").fadeIn();
	else
		$("#respond").fadeOut();
		
		
	return false;
}

function toggleVideoinfo (element)
{
	if ( $( element ).parent().next().is( ':hidden' ) ) {
		$( element ).html($( element ).html().replace(/open\.jpg/g, "close.jpg"));		
		$( element ).parent().next().slideDown();
	} else {
		$( element ).html($( element ).html().replace(/close\.jpg/g, "open.jpg"));
		$( element ).parent().next().slideUp();
	}
	
	return false;
}


//Videoslider
function videoSlider( increment ) {
	
	var container = 600;
	
	
	if(increment > 0) {
		if(videoCount > videoIndex) {
			if( container < videoWidth+videoSlide ) {
				videoSlide -= $("#video-slider a[tabindex='" + videoIndex + "']").parent().outerWidth();
				videoIndex += increment;
				$("#video-title-container ul").animate({left: videoSlide}, 500);
			}
		}
	} else {
		if(videoIndex > 1) {
				videoIndex += increment;
				videoSlide += $("#video-slider a[tabindex='" + videoIndex + "']").parent().outerWidth();
				$("#video-title-container ul").animate({left: videoSlide}, 500);
		}
	}
	
	return false;
}


function showVideo(element) {
	var videoID = 225;
	if(element == "default") {
		var querypath = location.search;
		if(!querypath) {
			videoID = $("#video-title-container ul li a").first().attr("title");
		} else if (/[?&]v=([0-9]+)/.test(querypath)) {
			code = querypath.match(/[?&]v=([0-9]+)/)[1];
			videoID = code;
		}
	} else {
		var videoID = $(element).attr("title");
	}
	
	
	//ajaxia tähän
	$.post("?p="+videoID, null, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$("#video-container").html( data );
			
			//ajetaan cufoni
			Cufon.refresh();
			
			//hide comments and commenting
			$("#respond").hide();
			$("#comments").hide();
			//blockquotes
			$("blockquote").wrap("<div class='shadowbox'>");
			
			//Videoinfo table
			/*
			$("#video-info-content table td strong").each(function() {
				$(this).html($(this).html().replace(/^0\s?€/g, "<span>"+$(this).text()+"</span>"));													   
			});
			*/
		}
	});
	
	
	return false;
}

function likeThis(videoID) {

	$.post("like.php", { videoID: videoID }, function( data2, textStatus2 )
	{
		if ( textStatus2 == 'success' )
		{	
			$("#video-info-btm p").html(data2);
		}
	});

	return false;
}

function submitForm(form) {

	
	if($("#comment").val() != '' && $("#author").val() != '') {
		form.submit();
	} else {
		alert("Nimi eikä viestikenttä saa olla tyhjä.");
	}
}

function toggleFriendForm() {
	
	if ( $("#friendForm-top").is( ':hidden' ) ) {
		pageTracker._trackPageview ('/kilpailulomake/avaus');
		$("#friendForm-top").slideDown();
		$("#friendLink").css("background-position", "bottom");
	} else {
		$("#friendForm-top").slideUp("normal", function() {
			$("#friendLink").css("background-position", "top");
		});
	}
		
}

function submitFriendForm(form) {

	$("#formMessage").html("");
	
	if($("#name").val() != '' && $("#email").val() != '' && $("#friendname").val() != '' && $("#friendemail").val() != '' && $("#item").val() != '') {
		
		var email1 = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test($("#email").val());
		var email2 = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test($("#friendemail").val());																													   
		if(email1 && email2) {
			pageTracker._trackPageview ('/kilpailulomake/lahetys');
			$.post("/like.php", { name: $("#name").val(),
								  email: $("#email").val(),
								  friend: $("#friendname").val(),
								  friendemail: $("#friendemail").val(),
								  suggestion: $("#item").val()}, function( data3, textStatus3 )
			{
				if ( textStatus3 == 'success' )
				{	
					$("#friendname").val('');
					$("#friendemail").val('');
					$("#item").val('');
					$("#formMessage").html(data3);
					
				} else {
					$("#formMessage").html("<p>Viestin lähetys epäonnistui!</p>");
				}
			});
			
			
		} else {
			$("#formMessage").html("Tarkista sähköpostiosoitteiden muoto.");
		}
		
		
	} else {
		$("#formMessage").html("Täytäthän kaikki kentät.");
	}
}



function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("video");
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
  
}

function onytplayerStateChange(newState) {
	
	videoUrl = $("#video-title h5").text();
	
	if(newState == -1)
   		pageTracker._trackPageview ('video/' + videoUrl + '/avaus');
	
	if(newState == 0)
   		pageTracker._trackPageview ('video/' + videoUrl + '/loppu');
		
}

