// JavaScript Document
$.ajaxSetup ({
    cache: false
});

$(document).ready(function ()
{
	setTimeout("$('#balloon').fadeIn('slow', fadeBalloon())",5000);
        
        $('#student-link').click(function() {
             $(this).next().show();
        });
});

function fadeBalloon ()
{
	setTimeout("$('#balloon').fadeOut()",2000);
}

function imagesVideosPaging ()
{
	var pictures = $('#image_video_container .show_images_videos > div:visible').length;
	
	if ( pictures > 0 )
	{
		$( "#image_video_container .images_total" ).text( pictures );
		if ( pictures < 18 )
			$( "#image_video_container .images_showing" ).text( "1-" + pictures );
		else
			$( "#image_video_container .images_showing" ).text( "1-18" );
	}
	$( "#image_video_container .show_images_videos" ).css({top: "0px"});
	$( "#image_video_container .previous_images" ).hide();
	
	if ( pictures > 18 )
		$( "#image_video_container .next_images" ).show();
	else
		$( "#image_video_container .next_images" ).hide();
}

function filterMedia( removeHash )
{
	if ( removeHash )
		location.hash = '';
	
	if ( (!$( "#videot" ).is( ":checked" ) && !$( "#kuvat" ).is( ":checked" )) || ($( "#videot" ).is( ":checked" ) && $( "#kuvat" ).is( ":checked" )) )
	{
		$('#image_video_container .show_images_videos div.video').show();
		$('#image_video_container .show_images_videos div.picture').show();
	}
	else if ( !$( "#videot" ).is( ":checked" ) )
	{
		var currentItem = 0;
		var totalItems = $('#image_video_container .show_images_videos div.video').length;
		$('#image_video_container .show_images_videos div.video').hide();
	}
	else if ( !$( "#kuvat" ).is( ":checked" ) )
	{
		var currentItem = 0;
		var totalItems = $('#image_video_container .show_images_videos div.picture').length;
		$('#image_video_container .show_images_videos div.picture').hide();
	}
	
	imagesVideosPaging();
	
	var imageVideoHash = location.hash.substr(2);
	if( imageVideoHash )
	{
		showImageVideo( $( '#item' + imageVideoHash )[0] );
		gotoRightPage();
	}
	else
		showImageVideo( $('#image_video_container .show_images_videos > div:visible').filter( ":first" ).find( "a" )[0] );
}

function gotoRightPage ()
{
	var currentImagePosition = Math.abs( $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' ).position().top );
	
	if ( currentImagePosition >= 267 )
	{
		var positionToGo = ( Math.floor( currentImagePosition / 267 ) - 1 ) * 267;
		$( "#image_video_container .show_images_videos" ).css({top: "-" + positionToGo + "px"});
		
		imagesVideosNext();
	}
}

function imagesVideosNext ()
{
	var newPosition = Math.abs( $( "#image_video_container .show_images_videos" ).position().top ) + 267;
	
	if ( newPosition <= $( "#image_video_container .show_images_videos" ).height() )
	{
		$( "#image_video_container .show_images_videos" ).css({top: "-" + newPosition + "px"});
		$( "#image_video_container .previous_images" ).show();
	}
	if ( ( Math.abs( $( "#image_video_container .show_images_videos" ).position().top ) + 267 ) > $( "#image_video_container .show_images_videos" ).height() )
		$( "#image_video_container .next_images" ).hide();
		
	imagesShowing();
	
	return false;
}

function imagesVideosPrevious ()
{
	if ( $( "#image_video_container .show_images_videos" ).position().top < 0 )
	{
		$( "#image_video_container .show_images_videos" ).css({top: $( "#image_video_container .show_images_videos" ).position().top + 267 + "px"});
		$( "#image_video_container .next_images" ).show();
	}
	if ( $( "#image_video_container .show_images_videos" ).position().top >= 0 )
		$( "#image_video_container .previous_images" ).hide();
		
	imagesShowing();
	
	return false;

}

function imageVideoNext()
{
	if ( parseInt( $( "#image_video_container .image_nro" ).text() ) < parseInt( $( "#image_video_container .images_total:first" ).text() ) )
	{
		var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' );
		var activeElementClassName = $( activeElement )[0].className.split( " ", 1 );
		var mediaElement = $( activeElement ).parents( '.' + activeElementClassName ).next().find( 'a' )[0];
		showImageVideo( mediaElement );
		
		if ( $( activeElement ).parents( '.' + activeElementClassName ).next().position().top >= ( Math.abs( $( "#image_video_container .show_images_videos" ).position().top ) + 267 ) )
			imagesVideosNext();
			
		location.hash = '#m' + mediaElement.id.substr( 4 );
	}
		
	return false;
}

function imageVideoPrevious()
{
	if ( parseInt( $( "#image_video_container .image_nro" ).text() ) > 1 )
	{
		var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' );
		var activeElementClassName = $( activeElement )[0].className.split( " ", 1 );
		var mediaElement = $( activeElement ).parents( '.' + activeElementClassName ).prev().find( 'a' )[0];
		showImageVideo( mediaElement );
		
		if ( $( activeElement ).parents( '.' + activeElementClassName ).prev().position().top < Math.abs( $( "#image_video_container .show_images_videos" ).position().top ) )
			imagesVideosPrevious();
			
		location.hash = '#m' + mediaElement.id.substr( 4 );
	}
		
	return false;
}

function imagesShowing()
{
	var pixelsFromTop = $( "#image_video_container .show_images_videos" ).position().top;
	var pictures = $('#image_video_container .show_images_videos > div:visible').length;
	
	if ( pixelsFromTop != 0 )
	{
		var currentImageVideoPage = Math.abs( pixelsFromTop ) / 267;
		var imageVideoBefore = Math.floor( currentImageVideoPage ) * 18;
		if ( ( pictures - imageVideoBefore ) < 18 )
			$( "#image_video_container .images_showing" ).text( imageVideoBefore + 1 + "-"  + pictures );
		else
		{
			var imagesInPage = imageVideoBefore + 18;
			$( "#image_video_container .images_showing" ).text( imageVideoBefore + 1 + "-"  + imagesInPage );
		}
	}
	else
	{
		if ( pictures < 18 )
			$( "#image_video_container .images_showing" ).text( "1-" + pictures );
		else
			$( "#image_video_container .images_showing" ).text( "1-18" );
	}
}

function showImageVideo ( mediaElement )
{
	if ( mediaElement.className == "video" )
	{
		swfobject.embedSWF( mediaElement.title, "image_big", "480", "360", "8", "/files/lakerol/flash/expressInstall.swf", null, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "image_big" });
	}
	else if ( mediaElement.className == "picture" )
		$( "#image_big" ).replaceWith( '<div id="image_big"><img src="' + mediaElement.title + '" alt="' + $( mediaElement ).find( "img" ).attr( "alt" ) + '" /></div>' );
		
	$( "#image_video_container h3" ).text( $( mediaElement ).find( "img" ).attr( "alt" ) );
	
	var testiE = 1;
	$('#image_video_container .show_images_videos > div:visible').each(function()
	{
		if ( $( this ).find( "a" ).attr( "id" ) == mediaElement.id )
			$( "#image_video_container .image_nro" ).text( testiE );
		testiE += 1;
	});
	$('#image_video_container .show_images_videos > div').each(function()
	{
		$( this ).find( "a" ).removeClass( "selected" );
	});
	
	$( "#image_big" ).css({visibility: "visible"});
	$( mediaElement ).addClass( "selected" );
	
	if ( parseInt( $( "#image_video_container .image_nro" ).text() ) >= parseInt( $( "#image_video_container .images_total:first" ).text() ) )
		$( ".next_image" ).hide();
	else
		$( ".next_image" ).show();
	if ( parseInt( $( "#image_video_container .image_nro" ).text() ) <= 1  )
		$( ".previous_image" ).hide();
	else
		$( ".previous_image" ).show();
		
	$(".errorMessage").text( "" );
	$(".errorMessage").hide();
		
	$.post("/hyvan-maun-ystavat/kilpailut/kuvan-videon-tiedot", { uId: mediaElement.id.substr( 4 ) }, function( data1, textStatus1 )
	{
		if ( textStatus1 == 'success' )
		{
			$( "#video_like a" ).removeClass();
			$( "#video_like a" ).addClass( mediaElement.id );
			$( "#video_dislike a" ).removeClass();
			$( "#video_dislike a" ).addClass( mediaElement.id );
			
			$( "#image_video_container .image_owner" ).text( $( data1 ).filter( ".nick" ).text() );
			$( "#video_like span" ).text( $( data1 ).filter( ".plus" ).text() );
			$( "#video_dislike span" ).text( $( data1 ).filter( ".miinus" ).text() );
		}
	});
	
	var imageVideoHash = location.hash;
	if( !imageVideoHash || !imageVideoHash.substr( 1 ) )
		location.hash = '#m' + mediaElement.id.substr( 4 );
}

function orderImageVideoByTime ( pagesName, linkElement, removeHash )
{
	if ( removeHash == undefined )
		removeHash = true;
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-" + pagesName + "/kilpailut-" + pagesName + "-aika", {}, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$( "#show_images_videos_container .show_images_videos" ).html( data );
			if ( linkElement )
			{
				$( linkElement ).find( 'img' ).replaceWith( '<img src="/files/lakerol/gfx/green_arrow_down.gif" alt="Aika" />' );
				$( linkElement ).next().find( 'img' ).replaceWith( '<img src="/files/lakerol/gfx/green_arrow.gif" alt="Suosio" />' );
			}
			filterMedia( removeHash );
		}
	});
	
	return false;
}

function orderImageVideoByPopularity ( pagesName, linkElement, removeHash )
{
	if ( removeHash == undefined )
		removeHash = true;
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-" + pagesName + "/kilpailut-" + pagesName + "-pisteet", {}, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$( "#show_images_videos_container .show_images_videos" ).html( data );
			if ( linkElement )
			{
				$( linkElement ).find( 'img' ).replaceWith( '<img src="/files/lakerol/gfx/green_arrow_down.gif" alt="Suosio" />' );
				$( linkElement ).prev().find( 'img' ).replaceWith( '<img src="/files/lakerol/gfx/green_arrow.gif" alt="Aika" />' );
			}
			filterMedia( removeHash );
		}
	});
	
	return false;
}

function orderImageVideoByPopularityPictureOfTheWeek ( pagesName, linkElement, removeHash )
{
	if ( removeHash == undefined )
		removeHash = true;
		
	var weekNumberValues = $( "#weekNumber" ).val();
	weekNumberValues = weekNumberValues.split( " " );
	var startDate = weekNumberValues[0];
	var endDate = weekNumberValues[1];
		
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-" + pagesName + "/kilpailut-" + pagesName + "-pisteet", { aloituspvm: startDate, lopetuspvm: endDate }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$( "#show_images_videos_container .show_images_videos" ).html( data );
			filterMedia( removeHash );
		}
	});
	
	return false;
}

function orderImageVideoByPopularityPictureOfTheMonth ( pagesName, linkElement, removeHash )
{
	if ( removeHash == undefined )
		removeHash = true;
		
	var monthNumberValues = $( "#monthNumber" ).val();
	monthNumberValues = monthNumberValues.split( " " );
	var startDate = monthNumberValues[0];
	var endDate = monthNumberValues[1];
		
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-" + pagesName + "/kilpailut-" + pagesName + "-pisteet", { aloituspvm: startDate, lopetuspvm: endDate }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$( "#show_images_videos_container .show_images_videos" ).html( data );
			filterMedia( removeHash );
		}
	});
	
	return false;
}

function voteCompetition ( voteElement, voteOverall )
{
	if ( voteElement.className != "" )
	{
		$.post("/hyvan-maun-ystavat/kilpailut/kuvan-videon-aanestys", { uId: voteElement.className.substr( 4 ), tId: voteElement.title }, function( data1, textStatus1 )
		{
			if ( textStatus1 == 'success' )
			{
				var errorMessage = $(data1).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
				{
					if ( voteOverall )
					{
						if ( voteElement.title == "p" )
							$( voteElement ).parent().find( "span" ).text( parseInt( $( voteElement ).parent().find( "span" ).text() ) + 1 );
						else if ( voteElement.title == "m" )
							$( voteElement ).parent().find( "span" ).text( parseInt( $( voteElement ).parent().find( "span" ).text() ) - 1 );
					}
					else
						$( voteElement ).children( "span" ).text( parseInt( $( voteElement ).children( "span" ).text() ) + 1 );
					$(".errorMessage").text( "Kiitos" );
					if ( $(".errorMessage:hidden") )
						$(".errorMessage").fadeIn("def");
				}
			}
		});
	}

	return false;
}

function voteHymyProduct ( voteElement, voteOverall )
{
	if ( voteElement.className != "" )
	{
		$.post("/hyvan-maun-ystavat/palkinnot/palkinnon-aanestys", { uId: voteElement.className.substr( 4 ), tId: voteElement.title }, function( data1, textStatus1 )
		{
			if ( textStatus1 == 'success' )
			{
				var errorMessage = $(data1).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
				{
					if ( voteOverall )
					{
						if ( voteElement.title == "p" )
							$( voteElement ).parent().find( "span" ).text( parseInt( $( voteElement ).parent().find( "span" ).text() ) + 1 );
						else if ( voteElement.title == "m" )
							$( voteElement ).parent().find( "span" ).text( parseInt( $( voteElement ).parent().find( "span" ).text() ) - 1 );
					}
					else
						$( voteElement ).children( "span" ).text( parseInt( $( voteElement ).children( "span" ).text() ) + 1 );
					$(".errorMessage").text( "Kiitos" );
					if ( $(".errorMessage:hidden") )
						$(".errorMessage").fadeIn("def");
				}
			}
		});
	}

	return false;
}

function showPrizeInfo ( pidId, selectBoxNro )
{
	if ( pidId )
	{
		var taso2var = $( '#alasveto1' ).val();
		if ( !taso2var || !selectBoxNro )
			taso2var = '';
		var taso4var = $( '#alasveto2' ).val();
		if ( !taso4var || !selectBoxNro || selectBoxNro == 1 )
			taso4var = '';
			
		$.post("/hyvan-maun-ystavat/palkinnot/palkinnon-tiedot", { tuoteId: pidId, taso2: taso2var, taso4: taso4var }, function( data, textStatus )
		{
			if ( textStatus == 'success' )
			{
				$( '#prizeInfoContainer' ).html( data );
				
				if ( $( '#prizeInfoContainer:hidden' ) )
					$( '#prizeInfoContainer' ).slideDown( 'slow' );
			}
		});
	}
}

function orderCurrentPrize ( pidId )
{
	if ( pidId )
	{
		var taso2var = $( '#alasveto1' ).val();
		if ( !taso2var )
			taso2var = '';
		var taso4var = $( '#alasveto2' ).val();
		if ( !taso4var )
			taso4var = '';
	
		$.post("/hyvan-maun-ystavat/palkinnot/palkinnon-lunastus", { tuoteId: pidId, taso2: taso2var, taso4: taso4var }, function( data, textStatus )
		{
			if ( textStatus == 'success' )
			{
				if ( $(data).filter("#message").text() )
				{
					var errorMessage = $(data).filter("#message").text();
				
					$( '#prizeOrderError' ).fadeOut( 'slow', function ()
					{
						$( '#prizeOrderError' ).text( errorMessage );
						$( '#prizeOrderError' ).fadeIn( 'slow' );
					});
				}
				else
					window.location.assign("http://www.lakerol.fi/hyvan-maun-ystavat/palkinnot/palkinnot-tilaa?tuoteId=" + pidId + "&taso2=" + taso2var + "&taso4=" + taso4var );
			}
		});
	}

	return false;
}

function changeProductSubImage ( linkElement )
{
	$('#thumbnailAreaContainer div a').each(function()
	{
		$( this ).removeClass( "activePrizeImage" );
	});
	
	$( '#thumbnailArea' ).html( $( linkElement ).find( "img" ).clone() );
	$( linkElement ).addClass( "activePrizeImage" );
	
	return false;
}

function expurgatePicture ()
{
	var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' )[0];
	var pictureToExpurgate = activeElement.id.substr( 4 );
	
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-sensurointi/kuvat-ja-videot-sensuroi-kuva", { kuvanId: pictureToExpurgate }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			if ( parseInt( $( "#image_video_container .image_nro" ).text() ) < parseInt( $( "#image_video_container .images_total:first" ).text() ) )
				imageVideoNext();
			else if ( parseInt( $( "#image_video_container .image_nro" ).text() ) > 1 )
				imageVideoPrevious();
				
			orderImageVideoByTime( 'sensurointi', false, false );
		}
	});
	
	return false;
}

function sendPictureNotification ()
{
	var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' )[0];
	var pictureToNotify = activeElement.id.substr( 4 );
	
	$.post("/hyvan-maun-ystavat/kilpailut/ilmoita-asiaton-kuva", { kuva: pictureToNotify }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			$( ".notificationNotify" ).text( ' - ilmoitus lähetettiin' );
		}
	});
	
	return false;
}

function makeToPictureOfTheWeek ()
{
	var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' )[0];
	var pictureOfTheWeek = activeElement.id.substr( 4 );
	
	var weekNumberValues = $( "#weekNumber" ).val();
	weekNumberValues = weekNumberValues.split( " " );
	var startDate = weekNumberValues[0] + ' 12:00:00';
	
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-viikon-kuva/kilpailut-viikon-kuva-tee", { kuvanId: pictureOfTheWeek, viikkopvm: startDate }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			var errorMessage = 'Kuvasta tehtiin viikon kuva!';
				
			$( '.errorMessage' ).fadeOut( 'slow', function ()
			{
				$( '.errorMessage' ).text( errorMessage );
				$( '.errorMessage' ).fadeIn( 'slow' );
			});
			
			orderImageVideoByPopularityPictureOfTheWeek( 'viikon-kuva', false, false );
		}
	});
	
	return false;
}

function makeToPictureOfTheMonth ()
{
	var activeElement = $('#image_video_container .show_images_videos > div:visible').find( 'a.selected' )[0];
	var pictureOfTheWeek = activeElement.id.substr( 4 );
	
	var monthNumberValues = $( "#monthNumber" ).val();
	monthNumberValues = monthNumberValues.split( " " );
	var startDate = monthNumberValues[0] + ' 12:00:00';
	
	$.post("/hyvan-maun-ystavat/kilpailut/kilpailut-kuukauden-kuva/kilpailut-kuukauden-kuva-tee", { kuvanId: pictureOfTheWeek, kuukausipvm: startDate }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			var errorMessage = 'Kuvasta tehtiin kuukauden kuva!';
				
			$( '.errorMessage' ).fadeOut( 'slow', function ()
			{
				$( '.errorMessage' ).text( errorMessage );
				$( '.errorMessage' ).fadeIn( 'slow' );
			});
			
			orderImageVideoByPopularityPictureOfTheWeek( 'kuukauden-kuva', false, false );
		}
	});
	
	return false;
}