// JavaScript Document

var currentSelectedVoteItem = '';
var currentSelectedVoteTitle = '';

$.ajaxSetup ({
    cache: false
});

$(document).ready(function ()
{
	/* Kuka Håkan Lightbox */
	$('#hakan_kuvat a.lightbox').lightBox({
		fixedNavigation:true,
		imageLoading: '/files/lakerol/images/lightbox-ico-loading.gif',
		imageBtnClose: '/files/lakerol/images/lightbox-btn-close.gif',
		imageBtnPrev: '/files/lakerol/images/lightbox-btn-prev.gif',
		imageBtnNext: '/files/lakerol/images/lightbox-btn-next.gif',
		imageBlank: '/files/lakerol/images/lightbox-blank.gif'
	});

	$('html').css( 'min-height', $(window).height() + 'px' );
	$('body').css( 'min-height', $(window).height() + 'px' );
	$('#raster_top').css( 'min-height', $(window).height() + 'px' );
	$('#raster_bottom').css( 'min-height', $(window).height() + 'px' );
	
	var productsLenght = 0;
	$('#product_scroll .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#product_scroll .product_container').width( productsLenght );
	if ( ($('#product_scroll .product_container').outerWidth( true ) && $('#product_scroll .product_container').width()) <= $('#product_scroll').width() )
		$("#product_slider").hide();
	else
	{
		$("#product_slider .product_slider").slider({
			slide: handleSliderSlide
	  	});
	}

	var productsLenght = 0;
	$('#product_scroll_2 .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#product_scroll_2 .product_container').width( productsLenght );
	if ( ($('#product_scroll_2 .product_container').outerWidth( true ) && $('#product_scroll_2 .product_container').width()) <= $('#product_scroll_2').width() )
		$("#product_slider_2").hide();
	else
	{
		$("#product_slider_2 .product_slider").slider({
			slide: handleSliderSlide2
	  	});
	}

	var productsLenght = 0;
	$('#product_scroll_3 .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#product_scroll_3 .product_container').width( productsLenght );
	if ( ($('#product_scroll_3 .product_container').outerWidth( true ) && $('#product_scroll_3 .product_container').width()) <= $('#product_scroll_3').width() )
		$("#product_slider_3").hide();
	else
	{
		$("#product_slider_3 .product_slider").slider({
			slide: handleSliderSlide3
	  	});
	}
	
	var productsLenght = 0;
	$('#product_carousel .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#product_carousel .product_container').width( productsLenght );
	if ( ($('#product_carousel .product_container').outerWidth( true ) && $('#product_carousel .product_container').width()) <= $('#product_carousel').width() )
		$("#product_carousel_slider").hide();
	else
	{
		$("#product_carousel_slider .product_carousel_slider").slider({
			slide: handleSliderSlide4
	  	});
	}

	var productsLenght = 0;
	$('#prices_scroll .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#prices_scroll .product_container').width( productsLenght );
	if ( ($('#prices_scroll .product_container').outerWidth( true ) && $('#prices_scroll .product_container').width()) <= $('#prices_scroll').width() )
		$("#prices_slider").hide();
	else
	{
		$("#prices_slider .product_slider").slider({
			slide: handleSliderSlide6
	  	});
	}

	var productsLenght = 0;
	$('#recent_scroll .product_item').each(function()
	{
		productsLenght += $(this).outerWidth( true );
	});	
	$('#recent_scroll .product_container').width( productsLenght );
	if ( ($('#recent_scroll .product_container').outerWidth( true ) && $('#recent_scroll .product_container').width()) <= $('#recent_scroll').width() )
		$("#recent_slider").hide();
	else
	{
		$("#recent_slider .product_slider").slider({
			slide: handleSliderSlide7
	  	});
	}

	$("#accordion").accordion({autoHeight: false, collapsible: true, active: 1000});

	$("input.browse_file").filestyle({ 
		image: "/files/lakerol/gfx/browse_btn.gif",
		imageheight : 29,
		imagewidth : 88,
		width : 210
	});
/*
	$( "#insert_data_image" ).submit( function() {
		var fileExtension = $( ".browse_file.file" ).val();
		
		if ( fileExtension != undefined && fileExtension != null && fileExtension != "" )
		{
			fileExtension = fileExtension.slice( -5 );
			if ( fileExtension.search( /.jpg/i ) != -1 || fileExtension.search( /.jpeg/i ) != -1 )
				return true;
		}
		
		if ( $( "#insert_data_image .form_error" ).text() != 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja.' )
		{
			$( "#insert_data_image .form_error" ).text( 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja.' );
			$( "#insert_data_image .form_error" ).fadeIn("def");
		}
		return false;
	});
*/
	$( "#insert_data_image" ).submit( function() {
											   
	   var n_checker = true;
	   var i_checker = true;
	   var cb_checker = true;

		if ( $( "#iCb:checked" ).is(':checked') ) cb_checker = true;
		else cb_checker = false;

											   
		var fileExtension = $( ".browse_file.file" ).val();
		
		if ( fileExtension != undefined && fileExtension != null && fileExtension != "")
		{
			fileExtension = fileExtension.slice( -5 );
			if ( fileExtension.search( /.jpg/i ) != -1 || fileExtension.search( /.jpeg/i ) != -1 )
				i_checker = true;
			else
				i_checker = false;
		}
		else
			i_checker = false;
		
		if ( $( "#q5_i" ).val() != "" )
			n_checker = true;
		else
			n_checker = false;
		
		if ( !i_checker && !n_checker )
		{
			if ( $( "#insert_data_image .form_error" ).text() != 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja. Anna myös nimi kuvalle.' )
			{
				$( "#insert_data_image .form_error" ).text( 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja. Anna myös nimi kuvalle.' );
				$( "#insert_data_image .form_error" ).fadeIn("def");
			}
		}
		else if ( !i_checker )
		{
			if ( $( "#insert_data_image .form_error" ).text() != 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja.' )
			{
				$( "#insert_data_image .form_error" ).text( 'Tarkistathan kuvasi tiedostotyypin. Vain jpg-kuvat ovat sallittuja.' );
				$( "#insert_data_image .form_error" ).fadeIn("def");
			}
		}
		else if ( !n_checker )
		{
			if ( $( "#insert_data_image .form_error" ).text() != 'Anna nimi kuvallesi.' )
			{
				$( "#insert_data_image .form_error" ).text( 'Anna nimi kuvallesi.' );
				$( "#insert_data_image .form_error" ).fadeIn("def");
			}
		}
		else if ( !cb_checker )
		{
			if ( $( "#insert_data_image .form_error" ).text() != 'Varmista, että sinulla on oikeus jakaa tämä kuva ja kuva ei riko sääntöjä.' )
			{
				$( "#insert_data_image .form_error" ).text( 'Varmista, että sinulla on oikeus jakaa tämä kuva ja kuva ei riko sääntöjä.' );
				$( "#insert_data_image .form_error" ).fadeIn("def");
			}
		}
		
		if (n_checker && i_checker && cb_checker)
			return true;
		else 
			return false;
	});

	/*
	$( "#insert_data_video" ).submit( function() {
		var youtubeCode = parseYoutubeVideo( $( "#youtubeAddress" ).val() );
		
		if ( youtubeCode != -1 )
		{
			$( "#youtubeAddress" ).val( youtubeCode );
			return true;
		}
		else
			return false;
	});
*/
	$( "#insert_data_video" ).submit( function() {
		var youtubeCode = parseYoutubeVideo( $( "#youtubeAddress" ).val() );
		
		var v_checker = true;
		var cbv_checker = true;
                var vn_checker = true;

                if ( $( "#q5" ).val() != "" ) vn_checker = true;
		else vn_checker = false;
		
                if ( $( "#vCb:checked" ).is(':checked') ) cbv_checker = true;
		else cbv_checker = false;

		if ( youtubeCode != -1 && cbv_checker && vn_checker )
		{
			$( "#youtubeAddress" ).val( youtubeCode );
			v_checker = true;
		}
		else
			v_checker = false;
			
		if (v_checker && cbv_checker && vn_checker)
                {
                        $( "#inserted_Video_error" ).hide();
			return true;
                }
		else
                {
                     $( "#inserted_Video_error" ).show();
		     return false;
                 }
			
	});


        //addAski();
});


function handleSliderSlide(e, ui)
{
	var maxScroll = $("#product_scroll").attr("scrollWidth") - $("#product_scroll").width();
	$("#product_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide2(e, ui)
{
	var maxScroll = $("#product_scroll_2").attr("scrollWidth") - $("#product_scroll_2").width();
	$("#product_scroll_2").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide3(e, ui)
{
	var maxScroll = $("#product_scroll_3").attr("scrollWidth") - $("#product_scroll_3").width();
	$("#product_scroll_3").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide4(e, ui)
{
	var maxScroll = $("#product_carousel").attr("scrollWidth") - $("#product_carousel").width();
	$("#product_carousel").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide5(e, ui)
{
	var maxScroll = $("#history_scroll").attr("scrollWidth") - $("#history_scroll").width();
	$("#history_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide6(e, ui)
{
	var maxScroll = $("#prices_scroll").attr("scrollWidth") - $("#prices_scroll").width();
	$("#prices_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function handleSliderSlide7(e, ui)
{
	var maxScroll = $("#recent_scroll").attr("scrollWidth") - $("#recent_scroll").width();
	$("#recent_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function showProductDetails( productNumber, productName, detailedData )
{
	if ( detailedData == 'tiedot' )
		detailedData = '&tiedot=1';
	else
		detailedData = '';

	detailedData = detailedData + '&ie8=' + Math.round(100000*Math.random());

	if( productName.search(/Classic/) != -1 )
	{
		$("#ClassicInfoContainer").slideUp("slow", function ()
		{
			$("#ClassicInfoContainer").load( "/tuotteet_ajax?id=" + productNumber + detailedData, {}, function( responseText, textStatus, XMLHttpRequest )
			{
				if ( textStatus == 'success' )
				{
					elementSlide ( "#DarkInfoContainer", "piiloon" );
					elementSlide ( "#FreshDropsInfoContainer", "piiloon" );
					elementSlide ( "#ClassicInfoContainer", "esiin" );
				}
				else
				{
					$("#ClassicInfoContainer").html('<div class="product_info"><div class="header_left"></div><div class="header_center"></div><div class="header_right"></div><div class="product_content"><p>Tuotetietojen hakeminen epäonnistui.</p></div><div class="footer_left"></div><div class="footer_center"></div><div class="footer_right"></div><div class="clear"></div></div>');
				}
			});
		});

	}
	else if ( productName.search(/Dark/) != -1 )
	{
		$("#DarkInfoContainer").slideUp("slow", function ()
		{
			$("#DarkInfoContainer").load( "/tuotteet_ajax?id=" + productNumber + detailedData, {}, function( responseText, textStatus, XMLHttpRequest )
			{
				if ( textStatus == 'success' )
				{
					elementSlide ( "#FreshDropsInfoContainer", "piiloon" );
					elementSlide ( "#ClassicInfoContainer", "piiloon" );
					elementSlide ( "#DarkInfoContainer", "esiin" );
				}
				else
				{
					$("#DarkInfoContainer").html('<div class="product_info"><div class="header_left"></div><div class="header_center"></div><div class="header_right"></div><div class="product_content"><p>Tuotetietojen hakeminen epäonnistui.</p></div><div class="footer_left"></div><div class="footer_center"></div><div class="footer_right"></div><div class="clear"></div></div>');
				}
			});
		});
	}
	else if ( productName.search(/Fresh Drops/) != -1 )
	{
		$("#FreshDropsInfoContainer").slideUp("slow", function ()
		{
			$("#FreshDropsInfoContainer").load( "/tuotteet_ajax?id=" + productNumber + detailedData, {}, function( responseText, textStatus, XMLHttpRequest )
			{
				if ( textStatus == 'success' )
				{
					elementSlide ( "#DarkInfoContainer", "piiloon" );
					elementSlide ( "#ClassicInfoContainer", "piiloon" );
					elementSlide ( "#FreshDropsInfoContainer", "esiin" );
				}
				else
				{
					$("#FreshDropsInfoContainer").html('<div class="product_info"><div class="header_left"></div><div class="header_center"></div><div class="header_right"></div><div class="product_content"><p>Tuotetietojen hakeminen epäonnistui.</p></div><div class="footer_left"></div><div class="footer_center"></div><div class="footer_right"></div><div class="clear"></div></div>');
				}
			});
		});
	}

	return false;
}

function parseXml()
{
	var feed = new google.feeds.Feed( "http://gdata.youtube.com/feeds/base/users/Lakerol100/uploads?alt=rss&v=2&orderby=published&max-results=50" );
	feed.setResultFormat( google.feeds.Feed.XML_FORMAT );
	feed.setNumEntries( 100 );
	feed.load( function( result )
	{
  		var container = document.getElementById( "feed" );
  		if ( !result.error )
		{
			var firstTime = true;
			var videoHash = location.hash;

			if ( videoHash != "" && videoHash != undefined && videoHash != null )
			{
				videoHash = videoHash.substr(7);
				swfobject.embedSWF( "http://www.youtube.com/v/" + videoHash + "?enablejsapi=1&playerapiid=ytplayer", "video", "480", "360", "8", "/files/lakerol/flash/expressInstall.swf", null, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "video" });
				$( "#video_like" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoHash + '" title="p"><img src="/files/lakerol/gfx/like_btn.gif" alt="Tykkään" /> <span id="' + videoHash + '_p"></span></a>');
				$( "#video_dislike" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoHash + '" title="m"><img src="/files/lakerol/gfx/dislike_btn.gif" alt="Tykkään" /> <span id="' + videoHash + '_m"></span></a>');
				$.post("/historia/videon-aanestystulokset", { uId: videoHash }, function( data2, textStatus2 )
				{
					if ( textStatus2 == 'success' )
					{
						var plusMessage = $(data2).filter("#video_plus").text();
						var miinusMessage = $(data2).filter("#video_miinus").text();
						$("#" + videoHash + "_p").text( plusMessage );
						$("#" + videoHash + "_m").text( miinusMessage );
					}
				});
				firstTime = false;
			}

			var items = result.xmlDocument.getElementsByTagName( "item" );
			for ( var i = 0; i < items.length; i++ ) 
			{
				var videoCode = '';
				var linkElement = items[i].getElementsByTagName( "link" )[0];
				var link = linkElement.firstChild.nodeValue;

				var titleElement = items[i].getElementsByTagName( "title" )[0];
				var title = titleElement.firstChild.nodeValue;

				var linkArray = link.split("?");
				var linkArray = linkArray[1].split("&");
				for ( var j = 0; j < linkArray.length; j++ )
				{
					if ( linkArray[j].search(/v=/) != -1 )
					{
						videoCode = linkArray[j].substr(2);
					}
				}

				$("#history_scroll .product_container").append( '<div class="product_item"><a href="#video_' + videoCode + '" id="' + videoCode + '" title="' + title + '" onclick="showVideo(this);"><img src="http://i.ytimg.com/vi/' + videoCode + '/default.jpg" alt="' + title + '" width="120" /></a></div>' );

				if ( firstTime )
				{
					var currentVideoCode = videoCode;
					$("#video_title").text( title );
					swfobject.embedSWF( "http://www.youtube.com/v/" + videoCode + "?enablejsapi=1&playerapiid=ytplayer", "video", "480", "360", "8", "/files/lakerol/flash/expressInstall.swf", null, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "video" });
					$( "#video_like" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoCode + '" title="p"><img src="/files/lakerol/gfx/like_btn.gif" alt="Tykkään" /> <span id="' + videoCode + '_p"></span></a>');
					$( "#video_dislike" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoCode + '" title="m"><img src="/files/lakerol/gfx/dislike_btn.gif" alt="Tykkään" /> <span id="' + videoCode + '_m"></span></a>');
					$.post("/historia/videon-aanestystulokset", { uId: currentVideoCode }, function( data3, textStatus3 )
					{
						if ( textStatus3 == 'success' )
						{
							var plusMessage2 = $(data3).filter("#video_plus").text();
							var miinusMessage2 = $(data3).filter("#video_miinus").text();
							$("#" + currentVideoCode + "_p").text( plusMessage2 );
							$("#" + currentVideoCode + "_m").text( miinusMessage2 );
						}
					});
					firstTime = false;
				}
			}
		}

		var productsLenght = 0;
		$('#history_scroll .product_item').each(function()
		{
			productsLenght += $(this).outerWidth( true );
		});
		if ( productsLenght == 0 )
			productsLenght = $('#videoBar-bar').outerWidth( true );
		$('#history_scroll .product_container').width( productsLenght );
		if ( ($('#history_scroll .product_container').outerWidth( true ) && $('#history_scroll .product_container').width()) <= $('#history_scroll').width() )
			$("#history_slider").hide();
		else
		{
			$("#history_slider .product_slider").slider({
				slide: handleSliderSlide5
			});
		}
	});
}

function showVideo( videoElement )
{
	$("#errorMessage").text( "" );
	$("#errorMessage").hide();

	$("#video_title").text( videoElement.title );
	swfobject.embedSWF( "http://www.youtube.com/v/" + videoElement.id + "?enablejsapi=1&playerapiid=ytplayer", "video", "480", "360", "8", "/files/lakerol/flash/expressInstall.swf", null, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "video" });
	$( "#video_like" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoElement.id + '" title="p"><img src="/files/lakerol/gfx/like_btn.gif" alt="Tykkään" /> <span id="' + videoElement.id + '_p"></span></a>');
	$( "#video_dislike" ).html('<a href="#" onclick="return voteVideo( this );" class="' + videoElement.id + '" title="m"><img src="/files/lakerol/gfx/dislike_btn.gif" alt="Tykkään" /> <span id="' + videoElement.id + '_m"></span></a>');
	$.post("/historia/videon-aanestystulokset", { uId: videoElement.id }, function( data3, textStatus3 )
	{
		if ( textStatus3 == 'success' )
		{
			var plusMessage = $(data3).filter("#video_plus").text();
			var miinusMessage = $(data3).filter("#video_miinus").text();
			$("#" + videoElement.id + "_p").text( plusMessage );
			$("#" + videoElement.id + "_m").text( miinusMessage );
		}
	});
}

function voteNews ( u, t )
{
	$.post("/uutiset/uutisen-tarkistus", { uId2: u }, function( data1, textStatus1 )
	{
		if ( textStatus1 == 'success' )
		{
			$.post("/uutiset/uutisen-aanestys", { uId: u, tId: t }, function( data2, textStatus2 )
			{
				if ( textStatus2 == 'success' )
				{
					var errorMessage = $(data2).filter("#voted").text();
					
					if ( errorMessage != '' )
					{
						if ( $("#ul_" + u + " .errorMessage").text() == "" )
						{
							$("#ul_" + u + " .errorMessage").text( "Olet jo äänestänyt" );
							$("#ul_" + u + " .errorMessage").fadeIn("def");
						}
						else if ( $("#ul_" + u + " .errorMessage").text() != "Olet jo äänestänyt" )
						{
							$("#ul_" + u + " .errorMessage").fadeOut("def", function callback() {
								$("#ul_" + u + " .errorMessage").text( "Olet jo äänestänyt" );
								$("#ul_" + u + " .errorMessage").fadeIn("def");
							});
						}

						/*$("#ul_" + u + " .errorMessage").text( "Olet jo äänestänyt" );
						if ( $("#ul_" + u + " .errorMessage:hidden") )
							$("#ul_" + u + " .errorMessage").fadeIn("def");*/
					}
					else
					{
						$("#" + u + "_" + t).text( parseInt( $("#" + u + "_" + t).text() ) + 1 );
						$("#ul_" + u + " .errorMessage").text( "Kiitos" );
						if ( $("#ul_" + u + " .errorMessage:hidden") )
							$("#ul_" + u + " .errorMessage").fadeIn("def");
					}
				}
			});
		}
	});

	return false;
}

function selectProductToVote ( voteItem )
{
	if ( currentSelectedVoteItem != "" )
		$( "#" + currentSelectedVoteItem ).attr( "class", "vote_product" );
	currentSelectedVoteItem = voteItem.id;
	currentSelectedVoteTitle = voteItem.title;
	$( "#" + currentSelectedVoteItem ).attr( "class", "vote_product selected" );
	return false;
}

function voteSelectedProduct()
{
	var voteValue = "p";
	if ( currentSelectedVoteItem != "" )
	{
		$.post("/tuotteet/tuotteen-tarkistus", { uId2: currentSelectedVoteItem, tId2: currentSelectedVoteTitle }, function( data1, textStatus1 )
		{
			if ( textStatus1 == 'success' )
			{
				$.post("/tuotteet/tuotteen-aanestys", { uId: currentSelectedVoteItem, tId: voteValue }, function( data2, textStatus2 )
				{
					if ( textStatus2 == 'success' )
					{
						$( "#vote_products" ).load( "/tuotteet/tuotteet-aanestystulokset", {}, function( responseText, textStatus, XMLHttpRequest )
						{
							if ( textStatus != 'success' )
							{
								$("#productVoteError").text( "Äänestystulosten hakeminen epäonnistui." );
								if ( $("#productVoteError:hidden") )
									$("#productVoteError").fadeIn("def");
							}
							else
								var t4=setTimeout("$('.iepng').supersleight({apply_positioning: false})",500);
						});
						
					}
				});
			}
		});
	}
	else
	{
		$("#productVoteError").text( "Valitse ensin tuote, jota haluat äänestää, klikkaamalla tuotetta." );
		if ( $("#productVoteError:hidden") )
			$("#productVoteError").fadeIn("def");
	}
	
	return false;
}

function voteVideo ( voteVideoElement )
{
	$.post("/historia/videon-tarkistus", { uId2: voteVideoElement.className, tId2: voteVideoElement.title }, function( data1, textStatus1 )
	{
		if ( textStatus1 == 'success' )
		{
			$.post("/historia/videon-aanestys", { uId: voteVideoElement.className, tId: voteVideoElement.title }, function( data2, textStatus2 )
			{
				if ( textStatus2 == 'success' )
				{
					var errorMessage = $(data2).filter("#voted").text();
					
					if ( errorMessage != '' )
					{
						if ( $("#errorMessage").text() == "" )
						{
							$("#errorMessage").text( "Olet jo äänestänyt" );
							$("#errorMessage").fadeIn("def");
						}
						else if ( $("#errorMessage").text() != "Olet jo äänestänyt" )
						{
							$("#errorMessage").fadeOut("def", function callback() {
								$("#errorMessage").text( "Olet jo äänestänyt" );
								$("#errorMessage").fadeIn("def");
							});
						}

					}
					else
					{
						$("#" + voteVideoElement.className + "_" + voteVideoElement.title).text( parseInt( $("#" + voteVideoElement.className + "_" + voteVideoElement.title).text() ) + 1 );
						$("#errorMessage").text( "Kiitos" );
						if ( $("#errorMessage:hidden") )
							$("#errorMessage").fadeIn("def");
					}
				}
			});
		}
	});

	return false;
}

function createLink (link_url, link_title, link_type, media_type, media_pid)
{
	if ( media_type && media_type == 'prize' )
		link_url = link_url;
	else
		link_url = window.location.href;
	var u = encodeURIComponent(link_url);
	var t = encodeURIComponent(link_title);

	
	if ( link_type == "digg" )
		targetUrl = 'http://digg.com/submit?phase=2&url=' + u + '&title=' + t;
	
	else if ( link_type == "fb" )
		targetUrl = 'http://www.facebook.com/share.php?u=' + u + '&amp;t=' + t;
		
	else if ( link_type == "delicious" )
		targetUrl = 'http://delicious.com/post?v=5&amp;url=' + u + '&amp;title=' + t;

	else if ( link_type == "myspace" )
		targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + t + '&c=<p>' + t + '</p>' + '&u=' + u + '&l=3';

	else if ( link_type == "twitter" )
		targetUrl = 'http://twitter.com?status=' + t + ' - ' + u;

	else if ( link_type == "stumbleupon" )
		targetUrl = 'http://www.stumbleupon.com/submit?url=' + u + '&amp;title=' + t;

	if( media_type )
	{
		if( !media_pid )
		{
			media_pid = $(".show_images_videos a.selected").attr("id");
			media_pid = media_pid.replace("item", "");
		}
		
		$.post("/hyvan-maun-ystavat/kilpailut/share-pisteet", { media_type: media_type, media_pid: media_pid });
	}
	
	window.open(targetUrl);
}

function showShareLinks( alternative )
{
	if ( alternative && $( "#" + alternative + ":hidden" ) )
		$( "#" + alternative ).slideDown('slow');
	else if ( $( "#footer-icons:hidden" ) )
		$("#footer-icons").slideDown('slow');

	return false;
}

function fixIePng ()
{
	setTimeout("$('.iepng').supersleight({apply_positioning: false})",500);
}

var tempElementName = '';
var tempTimeOut = null;

function elementSlide ( elementName, effectType )
{
	if ( tempTimeOut )
		clearTimeout( tempTimeOut );
	tempElementName = elementName;

	if ( effectType == "piiloon" )
	{
		$(elementName).slideUp("slow");
	}

	if ( effectType == "esiin" )
	{
		$(elementName).show();
		$(elementName).hide();
		tempTimeOut = setTimeout("$(tempElementName).slideDown('slow', fixIePng)", 150);
	}
}

function parseYoutubeVideo ( youtubeAddress )
{
	if ( youtubeAddress != undefined && youtubeAddress != null && youtubeAddress != "" )
	{
		var placeOfTheCode = youtubeAddress.search( /v=/i );
		
		if ( placeOfTheCode != -1 )
		{
			youtubeAddress = youtubeAddress.substr( placeOfTheCode+2 );
			youtubeAddress = youtubeAddress.split( "&" );
			
			return youtubeAddress[0];
		}
	}
	return -1;
}

function insertYoutubeVideo ()
{
	var youtubeCodeTemp = parseYoutubeVideo( $( "#youtubeAddress" ).val() );
	
	if ( youtubeCodeTemp != -1 )
	{
		swfobject.embedSWF( "http://www.youtube.com/v/" + youtubeCodeTemp + "?enablejsapi=1&playerapiid=ytplayer", "inserted_Video", "480", "360", "8", "/files/lakerol/flash/expressInstall.swf", null, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "inserted_Video" });
		
		$( "#insert_data_video .submit_container" ).html( '<input type="submit" value="" class="send_inserted_data" style="margin-left: 12px;" />' );
                $( "#inserted_Video_title" ).show();
	}
	else
	{
		$( "#inserted_Video" ).replaceWith( '<span id="inserted_Video">Videon lisääminen epäonnistui. Tarkista antamasi osoite.</span>' );
                $( "#inserted_Video_title" ).show();
	}
	
	return false;
}



function clearFormValue ( formfield )
{
	if ( formfield.defaultValue == formfield.value )
		formfield.value = "";
}

function resetFormValue ( formfield )
{
	if ( formfield.value == "" )
		formfield.value = formfield.defaultValue;
}

var tempU = '';
var tempCommentElement = null;

function showComments ( u )
{
	$.post("/uutiset/uutisen-kommentit", { uId2: u }, function( data1, textStatus1 )
	{
		if ( textStatus1 == 'success' )
		{
			$( "#news_" + u ).html( data1 );
			$( "#comment_image_" + u ).html( '<img src="/files/lakerol/gfx/green_arrow_down.gif" alt="Green arrow" />' );
			
			tempU = u;
			$( "#news_" + u ).show();
			$( "#news_" + u ).hide();
			setTimeout( '$( "#news_" + tempU ).slideDown( "slow" )', 150 );
			
			$( ".insert_comment" ).unbind();
			$( ".insert_comment" ).submit( function()
			{
				var commentText = $( this ).find( "textarea" ).val();
				
				if ( commentText )
				{
					var commentElement = this;
					$.post("/uutiset/kommentin-lisays", { uId: u, tId: commentText }, function( data1, textStatus1 )
					{
						if ( textStatus1 == 'success' )
						{
							$( commentElement ).find( ".form_error" ).fadeOut("def");
							$( commentElement ).find( "textarea" ).val( "" );
							
							var insertedComment = $(data1).filter(".news_block").html();
							if ( insertedComment )
							{
								$( "#news_" + u + " h5" ).before( '<div class="news_block" style="display: none; width: 490px;">' + insertedComment + '</div>' );
								$( "#news_" + u + " h5" ).prev().show();
								$( "#news_" + u + " h5" ).prev().hide();
								setTimeout( '$( "#news_" + tempU + " h5" ).prev().slideDown( "slow" )', 150 );
							}
						}
					});
				}
				else
				{
					if ( $( this ).find( ".form_error" ).text() != 'Kirjoitathan kommenttisi ennen sen lisäämistä.' )
					{
						$( this ).find( ".form_error" ).text( 'Kirjoitathan kommenttisi ennen sen lisäämistä.' );
						$( this ).find( ".form_error" ).fadeIn("def");
					}
				}
				
				return false;
			});
			
			$( ".insert_leaf_comment" ).unbind();
			$( ".insert_leaf_comment" ).submit( function()
			{
				var commentText = $( this ).find( "textarea" ).val();
				
				if ( commentText )
				{
					var commentsId = this.id.substr( 4 );
					var commentElement = this;
					
					$.post("/uutiset/leaf-kommentin-lisays", { uId: commentsId, tId: commentText }, function( data2, textStatus2 )
					{
						if ( textStatus2 == 'success' )
						{
							$( commentElement ).find( ".form_error" ).fadeOut("def");
							$( commentElement ).find( "textarea" ).val( "" );
							$( commentElement ).parent().children( "p" ).remove();
							
							var insertedAnswer = $(data2).filter(".answer").html();
							if ( insertedAnswer )
							{
								$( commentElement ).parent().children( "h6" ).after( '<p style="display: none; width: 470px;">' + insertedAnswer + '</p>' );
								tempCommentElement = commentElement;
								$( commentElement ).parent().children( "p" ).show();
								$( commentElement ).parent().children( "p" ).hide();
								setTimeout( '$( tempCommentElement ).parent().children( "p" ).slideDown( "slow" )', 150 );
							}
						}
					});
				}
				else
				{
					if ( $( this ).find( ".form_error" ).text() != 'Kirjoitathan vastauksen ennen sen lisäämistä.' )
					{
						$( this ).find( ".form_error" ).text( 'Kirjoitathan vastauksen ennen sen lisäämistä.' );
						$( this ).find( ".form_error" ).fadeIn("def");
					}
				}
				
				return false;
			});
		}
	});

	return false;
}

function slideShare ( linkElement )
{
	if ( $( linkElement ).next().is( ':hidden' ) )
		$( linkElement ).next().slideDown( "slow" );
	else
		$( linkElement ).next().slideUp( "slow" );
	
	return false;
}


/*function addAski ()
{
	$(".aski_info").hide();

	$( "#insert_aski" ).submit( function()
	{ 
		var askiCode = $( this ).find( "#koodiValue" ).val();

		$.post("/hyvan-maun-ystavat/kilpailut/askikoodi-tarkistus", { uId: askiCode, tId: "1" }, function( dataPoster, textStatusPost )
		{
			if ( textStatusPost == 'success' )
			{
				var askiData = $(dataPoster).filter(".askiInfo").html();
				var pisteetData = $(dataPoster).filter(".pisteetInfo").html();
				$(".aski_info").hide();
				$(".aski_info").html( askiData );
				if(pisteetData)
					$("span#points").html( pisteetData );
				$(".aski_info").fadeIn("def");
			}
		});

		return false;
	});
}*/