

$(document).ready(function() {


		$("div#benefit-popup img").click(function () {
			document.cookie="benefit_not_interested_3=is_used; domain=rockway.fi; path=/";
			$("div#benefit-popup").hide();
		});


		$("#user-benefit-page form#printForm input").click(function () {

					
			var printHtml = window.open('', 'Rockway etukuponki', 'height=630,width=830');
			printHtml.document.write('<html><head><title>Rockway etukuponki</title>');
			        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
			
			printHtml.document.write('<style type="text/css">#print {position: relative;background-color: #FFF;height: 600px;width: 800px;color: #000;padding: 40px;line-height: 18px;font-size: 12px;margin-bottom: 10px;	border: 2px dashed #000;}#print img {position: absolute;top: 50px;left: 600px;} #print div#box {	height: 100px; width: 600px; border: 1px solid #000; } .big { font-size: 21px; line-height: 26px; font-weight: bold; }</style>');			
			
			printHtml.document.write('</head><body><div id="print">');
			printHtml.document.write($("#user-benefit-page #print").html());
			printHtml.document.write('</div></body></html>');
			printHtml.document.close();
			printHtml.print();
			
			
		});



		$("div#user-profile-page li#get-news-feed").click(function() {
			$("div#user-profile-page div#news-feed").show();
			$("div#user-profile-page li#get-news-feed").addClass("feed-selected");
			
			$("div#user-profile-page div#friend-feed").hide();
			$("div#user-profile-page li#get-friend-feed").removeClass("feed-selected");
			$("div#user-profile-page div#user-feed").hide();
			$("div#user-profile-page li#get-user-feed").removeClass("feed-selected");
			$("div#user-profile-page div#userfriend-feed").hide();
			$("div#user-profile-page li#get-userfriend-feed").removeClass("feed-selected");
		});
		
		$("div#user-profile-page li#get-friend-feed").click(function() {
			$("div#user-profile-page div#friend-feed").show();
			$("div#user-profile-page li#get-friend-feed").addClass("feed-selected");
			
			$("div#user-profile-page div#news-feed").hide();
			$("div#user-profile-page li#get-news-feed").removeClass("feed-selected");
			$("div#user-profile-page div#user-feed").hide();
			$("div#user-profile-page li#get-user-feed").removeClass("feed-selected");
			$("div#user-profile-page div#userfriend-feed").hide();
			$("div#user-profile-page li#get-userfriend-feed").removeClass("feed-selected");
		});
		
		$("div#user-profile-page li#get-user-feed").click(function() {
			$("div#user-profile-page div#user-feed").show();
			$("div#user-profile-page li#get-user-feed").addClass("feed-selected");
			
			$("div#user-profile-page div#news-feed").hide();
			$("div#user-profile-page li#get-news-feed").removeClass("feed-selected");
			$("div#user-profile-page div#friend-feed").hide();
			$("div#user-profile-page li#get-friend-feed").removeClass("feed-selected");
			$("div#user-profile-page div#userfriend-feed").hide();
			$("div#user-profile-page li#get-userfriend-feed").removeClass("feed-selected");
		});
		
		$("div#user-profile-page li#get-userfriend-feed").click(function() {
			$("div#user-profile-page div#userfriend-feed").show();
			$("div#user-profile-page li#get-userfriend-feed").addClass("feed-selected");
			
			$("div#user-profile-page div#news-feed").hide();
			$("div#user-profile-page li#get-news-feed").removeClass("feed-selected");
			$("div#user-profile-page div#friend-feed").hide();
			$("div#user-profile-page li#get-friend-feed").removeClass("feed-selected");
			$("div#user-profile-page div#user-feed").hide();
			$("div#user-profile-page li#get-user-feed").removeClass("feed-selected");
		});


		$("#menu").accordion({
			autoHeight: false,
			active: 0
		});
		
		
		
		var listItem = $('.state-active');
		var index = $('#lesson-list-accordion h3').index(listItem);

		
		$("#lesson-list-accordion").accordion({
				fillSpace: true,
				active: index
		});
		
		$("#lesson-list-container").resizable({
				minHeight: 400,
				handles: 's',
				resize: function() {
					$("#lesson-list-accordion").accordion("resize");
				}
		});
		

			$("#backing_tracks_page .loadSound").click(function() {
				var fo = new SWFObject( "http://www.rockway.fi/soundEn.swf?0001", "fo", "352", "620", "8", "#292929" );				
				fo.addVariable("song", $(this).attr("id"));
				fo.addVariable("quality", $(this).attr("quality"));
				fo.write("track_player" );
			});
	
		
		$("#backing_tracks_page .getTime").click(function() {
			$("#order_info").show();
		});


			$("#player-backingtrack-container .loadSound").click(function() {
				var fob = new SWFObject( "http://www.rockway.fi/flash/soundEns_small.swf", "fob", "240", "105", "0", "#000000" );
				fob.addParam("wmode", "transparent");
				fob.addVariable("song", $(this).attr("id"));
				fob.addVariable("quality", $(this).attr("quality"));
				fob.write( "player-backingtrack" );
			});
	
		
		$("#player-backingtrack-container .getTime").click(function() {
			$("#order_info").show();
		});





	//Player set zoom default
		$("div#player-container").css('margin-top', '-108px');
	//

	$(".avatar-img").each(function() {
		if ($(this).width() > 120 || $(this).height() > 140) {
			$(this).width(120);
			$(this).height(140);
		}
	});

	$("a#read-more-link-open").show();
	$("div#read-more-box").hide();
	
	$("#tabs").tabs();
	
	$("span.private-comment-help").qtip({ style: { name: 'cream', tip: true } });
	
	$("span.help-tooltip a").each(function() {

		$(this).qtip({
			show: {
				solo: true
			},
			style: { 
				width: 500,
				name: 'light',
				tip: true
			}
		});
	});
	
	if ($("div.gray-container form#lesson-search input#query").val() == "") {
		$("div.gray-container form#lesson-search input#query").val("Kirjoita hakusana...");
		$("div.gray-container form#lesson-search input#query").css("color", "#777");
	}
	$("div.gray-container form#lesson-search input#query").click(function () {
		if ($("div.gray-container form#lesson-search input#query").val() == 'Kirjoita hakusana...') {
			$("div.gray-container form#lesson-search input#query").val("");
			$("div.gray-container form#lesson-search input#query").css("color", "#000");
		}
	});
	
	if ($("div#menu-site-search input.search-field").val() == "") {
		$("div#menu-site-search input.search-field").val("Hae rockway.fi sivustolta");
		$("div#menu-site-search input.search-field").css("color", "#777");
	}
	$("div#menu-site-search input.search-field").click(function () {
		if ($("div#menu-site-search input.search-field").val() == 'Hae rockway.fi sivustolta') {
			$("div#menu-site-search input.search-field").val("");
			$("div#menu-site-search input.search-field").css("color", "#000");
		}
	});
	
	if ($("div#frontpage-user-box input.username").val() == "") {
		$("div#frontpage-user-box input.username").val("Käyttäjänimi");
		$("div#frontpage-user-box input.username").css("color", "#777");
	}
	$("div#frontpage-user-box input.username").click(function () {
		if ($("div#frontpage-user-box input.username").val() == 'Käyttäjänimi') {
			$("div#frontpage-user-box input.username").val("");
			$("div#frontpage-user-box input.username").css("color", "#000");
		}
	});
	
	if ($("div#frontpage-user-box input.email").val() == "") {
		$("div#frontpage-user-box input.email").val("Sähköposti");
		$("div#frontpage-user-box input.email").css("color", "#777");
	}
	$("div#frontpage-user-box input.email").click(function () {
		if ($("div#frontpage-user-box input.email").val() == 'Sähköposti') {
			$("div#frontpage-user-box input.email").val("");
			$("div#frontpage-user-box input.email").css("color", "#000");
		}
	});
	
	if ($("div#frontpage-user-box input.password").val() == "") {
		$("div#frontpage-user-box input.password-text").val("Salasana");
		$("div#frontpage-user-box input.password-text").css("color", "#777");
		$("div#frontpage-user-box input.password").hide();
		$("div#frontpage-user-box input.password-text").show();
	}
	
	$("div#frontpage-user-box input.password-text").click(function () {
			$("div#frontpage-user-box input.password-text").hide();
			$("div#frontpage-user-box input.password").show();
			$("div#frontpage-user-box input.password").focus();
	});
	
	$("div#frontpage-user-box input.password-text").focus(function () {
			$("div#frontpage-user-box input.password-text").hide();
			$("div#frontpage-user-box input.password").show();
			$("div#frontpage-user-box input.password").focus();
	});
	
	
	if ($("div#menu-login-container input.username").val() == "") {
		$("div#menu-login-container input.username").val("Käyttäjänimi");
		$("div#menu-login-container input.username").css("color", "#777");
	}
	
	$("div#menu-login-container input.username").click(function () {
		if ($("div#menu-login-container input.username").val() == 'Käyttäjänimi') {
			$("div#menu-login-container input.username").val("");
			$("div#menu-login-container input.username").css("color", "#000");
		}
	});
	
	if ($("div#menu-login-container input.password").val() == "") {
		$("div#menu-login-container input.password-text").val("Salasana");
		$("div#menu-login-container input.password-text").css("color", "#777");
		$("div#menu-login-container input.password").hide();
		$("div#menu-login-container input.password-text").show();
	}
	$("div#menu-login-container input.password-text").click(function () {
			$("div#menu-login-container input.password-text").hide();
			$("div#menu-login-container input.password").show();
			$("div#menu-login-container input.password").focus();
	});
	
	$("div#menu-login-container input.password-text").focus(function () {
			$("div#menu-login-container input.password-text").hide();
			$("div#menu-login-container input.password").show();
			$("div#menu-login-container input.password").focus();
	});
	
	/*
	$("form#competition-form").submit(function() {

		var competition = $("input#competition")[0].value;
		
		$.ajax({
			type: "POST",
			url: "http://www.rockway.fi/competition.php",
			data: "submit_competition=&ajax=1&competition=" + competition,
			success: function(data) {
				$("div#competition-box").empty();
				$("div#competition-box").append(data);
			}
		});

		return false;
	});
	*/
	
	
	$("a#read-more-link-open").click(function() {
		$("div#read-more-box").show("slow");
		$("a#read-more-link-open").hide();
		$("a#read-more-link-close").show();
		
		return false;
	});
	
	$("a#read-more-link-close").click(function() {
		$("div#read-more-box").hide("slow");
		$("a#read-more-link-close").hide();
		$("a#read-more-link-open").show();
		
		return false;
	});
	
	/* Countdown timer */
	var date = new Date();
	date.setFullYear(2009);
	date.setMonth(11 - 1);
	date.setDate(29);
	date.setHours(0);
	date.setMinutes(0);
	date.setSeconds(0);
	$("div#avoimet-ovet-countdown").countdown({
		until: date,
		timezone: "+2",
		labels: ['Vuotta', 'Kuukautta', 'Viikkoa', 'Päivää', 'Tuntia', 'Minuuttia', 'Sekuntia'],
		labels1: ['Vuosi', 'Kuukausi', 'Viikko', 'Päivä', 'Tunti', 'Minuutti', 'Sekunti']
	});
	
	// Teachermenu
	
	var div = $('div.sc_menu'),
	ul = $('ul.sc_menu'),
	ulPadding = 15;
	
	//Get teachermenu width
	var divWidth = div.width();

	//Remove scrollbars	
	div.css({overflow: 'hidden'});
	
	//Find last image container
	var lastLi = ul.find('li:last-child');

	//When user move mouse over teachermenu
	div.mousemove(function(e){
		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});
	
	
	
	//Clip feeds box
	
	$("div#select_newest").css({background: 'url(../../../graphics/over.jpg)'});
	
	$("div#select_newest").click(function() {
		$("div#select_newest").css({background: 'url(../../../graphics/over.jpg)'});
		$("div#select_most_watched").css({background: 'none'});
		$("div#select_recently_watched").css({background: 'none'});
		
		$("div#clips_newest").css({display: 'block'});
		$("div#clips_most_watched").css({display: 'none'});
		$("div#clips_recently_watched").css({display: 'none'});
		
		return false;
	});
	
	$("div#select_most_watched").click(function() {
		$("div#select_newest").css({background: 'none'});
		$("div#select_most_watched").css({background: 'url(../../../graphics/over.jpg)'});
		$("div#select_recently_watched").css({background: 'none'});
		
		$("div#clips_newest").css({display: 'none'});
		$("div#clips_most_watched").css({display: 'block'});
		$("div#clips_recently_watched").css({display: 'none'});
		
		return false;
	});
	
	$("div#select_recently_watched").click(function() {
		$("div#select_newest").css({background: 'none'});
		$("div#select_most_watched").css({background: 'none'});
		$("div#select_recently_watched").css({background: 'url(../../../graphics/over.jpg)'});
		
		$("div#clips_newest").css({display: 'none'});
		$("div#clips_most_watched").css({display: 'none'});
		$("div#clips_recently_watched").css({display: 'block'});
		
		return false;
	});
	
	$("select#show-results").change(function() {
		$("form#show-results-form").submit();
	});
	
	$("form#lesson-search select#instrument").change(function() {
		var post_data = "ajax=1&";
		var data_size = $("select#instrument option:selected").length;
		var i = 0;
		$("select#instrument option:selected").each(function() {
			++i;
			post_data += "instruments[]=" + this.value;
			if (i < data_size) {
				post_data += "&";
			}
		});
		
		if (data_size > 0) {
			$.ajax({
				type: "post",
				url: "http://www.rockway.fi/oppitunnit/haku/",
				data: post_data,
				success: function (data) {
					$("select#teachers").empty();
					$("select#teachers").append(data);
				}
			});
		}
	});
	
	hideCommentNotifyFormButton();
	
	$("div#lesson-comments input#post-comment-button").live("click", function() {
		var clip = $("form#new-comment-form-bottom input#clip").val();
		
		$.ajax({
			type: "get",
			url: "http://www.rockway.fi/oppituntikommentit/uusi/" + clip + "/ajax/",
			success: function (data) {
				$("div#lesson-comments input#post-comment-button").hide();
				$("div#lesson-comments input#post-comment-button").after(data);
				$("form#new-comment-form-top input#title-top").focus();			
				$("span.private-comment-help").qtip({ style: { name: 'cream', tip: true } });
			}
		});
		
		return false;
	});
	
	$("form#new-comment-form-top input#submit-top-form").live("click", function() {
		var title = $("form#new-comment-form-top input#title-top").val();
		var body = $("form#new-comment-form-top textarea#body-top").val();
		var private_comment = $("form#new-comment-form-top input#private-top").val()
		var clip = $("form#new-comment-form-top input#clip-top").val();
		
		var post_data = "submit_comment=&title=" + title + "&body=" + body + "&clip=" + clip;
		
		if ($("form#new-comment-form-top input#private-top:checked").val() != null) {
			post_data += "&private=" + private_comment;
		}
		
		var error = false;
		
		if (title == "") {
			$("form#new-comment-form-top input#title-top + div.error").remove();
			$("form#new-comment-form-top input#title-top").after('<div class="error"><p>Otsikko puuttuu</p></div>');
			error = true;
		}
		
		if (body == "") {
			$("form#new-comment-form-top textarea#body-top + div.error").remove();
			$("form#new-comment-form-top textarea#body-top").after('<div class="error"><p>Kommentti puuttuu</p></div>');
			error = true;
		}
		
		if (!error) {
		
			$.ajax({
				type: "post",
				url: $("form#new-comment-form-top").attr("action") + "/ajax/",
				data: post_data,
				success: function (data) {
					$("div#lesson-comments").replaceWith(data);
					hideCommentNotifyFormButton();
					$.scrollTo("div#lesson-comments div.new-comment", 1000, {offset: {top: -20, left: 0}});
				}
			});
		}
		
		return false;
	});
	
	$("form#new-comment-form-top input#hide-top-form").live("click", function() {
		$("form#new-comment-form-top").remove();
		$("div#lesson-comments input#post-comment-button").show();
		
		return false;
	});
	
	$("form#new-comment-form-bottom input#submit-bottom-form").live("click", function() {
		var title = $("form#new-comment-form-bottom input#title-bottom").val();
		var body = $("form#new-comment-form-bottom textarea#body-bottom").val();
		var private_comment = $("form#new-comment-form-bottom input#private-bottom").val();
		var clip = $("form#new-comment-form-bottom input#clip").val();
		
		var post_data = "submit_comment=&title=" + title + "&body=" + body + "&clip=" + clip;
		
		if ($("form#new-comment-form-bottom input#private-bottom:checked").val() != null) {
			post_data += "&private=" + private_comment;
		}
		
		var error = false;
		
		if (title == "") {
			$("form#new-comment-form-bottom input#title-bottom + div.error").remove();
			$("form#new-comment-form-bottom input#title-bottom").after('<div class="error"><p>Otsikko puuttuu</p></div>');
			error = true;
		}
		
		if (body == "") {
			$("form#new-comment-form-bottom textarea#body-bottom + div.error").remove();
			$("form#new-comment-form-bottom textarea#body-bottom").after('<div class="error"><p>Kommentti puuttuu</p></div>');
			error = true;
		}
		
		if (!error) {
		
			$.ajax({
				type: "post",
				url: $("form#new-comment-form-bottom").attr("action") + "/ajax/",
				data: post_data,
				success: function (data) {
					$("div#lesson-comments").replaceWith(data);
					hideCommentNotifyFormButton();
					$.scrollTo("div#lesson-comments div.new-comment", 1000, {offset: {top: -20, left: 0}});
				}
			});
		}
		
		return false;
	});
	
	$("div.lesson-comment ul.comment-menu input.reply-button").live("click", function() {
		var re = /comment\-reply\-([0-9]+)/i;
		var str = $(this).attr("id");
		matches = str.match(re);
		
		var comment = matches[1];
		
		$.ajax({
			type: "get",
			url: "http://www.rockway.fi/oppituntikommentit/vastaa/" + comment + "/ajax/",
			success: function (data) {
				$("ul#comment-menu-" + comment).hide();
				$("div#lesson-comment-" + comment).after(data);
				$("form#reply-comment-form-" + comment + " input#title-reply-" + comment).focus();
				$("span.private-comment-help").qtip({ style: { name: 'cream', tip: true } });
				
				$("form#reply-comment-form-" + comment + " input#hide-reply-form-" + comment).live("click", function() {
					$("form#reply-comment-form-" + comment).remove();
					$("ul#comment-menu-" + comment).show();
					
					return false;
				});
				
				$("form#reply-comment-form-" + comment + " input#submit-reply-form-" + comment).live("click", function() {
					var title = $("form#reply-comment-form-" + comment + " input#title-reply-" + comment).val();
					var body = $("form#reply-comment-form-" + comment + " textarea#body-reply-" + comment).val();
					var private_comment = $("form#reply-comment-form-" + comment + " input#private-reply-" + comment).val();
					var clip = $("form#reply-comment-form-" + comment + " input#clip-reply-" + comment).val();
					var parent = $("form#reply-comment-form-" + comment + " input#parent-reply-" + comment).val();
					
					var post_data = "submit_comment=&title=" + title + "&body=" + body + "&clip=" + clip + "&parent_id=" + parent;
					
					if ($("form#reply-comment-form-" + comment + " input#private-reply-" + comment + ":checked").val() != null) {
						post_data += "&private=" + private_comment;
					}
					
					var error = false;
		
					if (title == "") {
						$("form#reply-comment-form-" + comment + " input#title-reply-" + comment + " + div.error").remove();
						$("form#reply-comment-form-" + comment + " input#title-reply-" + comment).after('<div class="error"><p>Otsikko puuttuu</p></div>');
						error = true;
					}
					
					if (body == "") {
						$("form#reply-comment-form-" + comment + " textarea#body-reply-" + comment + " + div.error").remove();
						$("form#reply-comment-form-" + comment + " textarea#body-reply-" + comment).after('<div class="error"><p>Kommentti puuttuu</p></div>');
						error = true;
					}
					
					if (!error) {
					
						$.ajax({
							type: "post",
							url: $("form#reply-comment-form-" + comment).attr("action") + "/ajax/",
							data: post_data,
							success: function (data) {
								$("div#lesson-comments").replaceWith(data);
								hideCommentNotifyFormButton();
								$.scrollTo("div#lesson-comments div.new-comment", 1000, {offset: {top: -20, left: 0}});
							}
						});
					}
					
					return false;
				});
			}
		});
		
		return false;
	});
	
	$("div.lesson-comment ul.comment-menu input.edit-button").live("click", function() {
		var re = /comment\-edit\-([0-9]+)/i;
		var str = $(this).attr("id");
		matches = str.match(re);
		
		var comment = matches[1];
		
		$.ajax({
			type: "get",
			url: "http://www.rockway.fi/oppituntikommentit/muokkaa/" + comment + "/ajax/",
			success: function (data) {
				$("ul#comment-menu-" + comment).hide();
				$("div#lesson-comment-" + comment).after(data);
				$("div#lesson-comment-" + comment).css("margin-bottom", 0);
				$("form#edit-comment-form-" + comment + " input#title-edit-" + comment).focus();
				$("span.private-comment-help").qtip({ style: { name: 'cream', tip: true } });
				
				$("form#edit-comment-form-" + comment + " input#hide-edit-form-" + comment).live("click", function() {
					$("div#lesson-comment-" + comment).css("margin-bottom", 10);
					$("form#edit-comment-form-" + comment).remove();
					$("ul#comment-menu-" + comment).show();
					
					return false;
				});
				
				$("form#edit-comment-form-" + comment + " input#submit-edit-form-" + comment).live("click", function() {
					var title = $("form#edit-comment-form-" + comment + " input#title-edit-" + comment).val();
					var body = $("form#edit-comment-form-" + comment + " textarea#body-edit-" + comment).val();
					var private_comment = $("form#edit-comment-form-" + comment + " input#private-edit-" + comment).val();
					var id = $("form#edit-comment-form-" + comment + " input#id-edit-" + comment).val();
					
					var post_data = "edit_comment=&title=" + title + "&body=" + body + "&id=" + id;
					
					if ($("form#edit-comment-form-" + comment + " input#private-edit-" + comment + ":checked").val() != null) {
						post_data += "&private=" + private_comment;
					}
					
					var error = false;
		
					if (title == "") {
						$("form#edit-comment-form-" + comment + " input#title-edit-" + comment + " + div.error").remove();
						$("form#edit-comment-form-" + comment + " input#title-edit-" + comment).after('<div class="error"><p>Otsikko puuttuu</p></div>');
						error = true;
					}
					
					if (body == "") {
						$("form#edit-comment-form-" + comment + " textarea#body-edit-" + comment + " + div.error").remove();
						$("form#edit-comment-form-" + comment + " textarea#body-edit-" + comment).after('<div class="error"><p>Kommentti puuttuu</p></div>');
						error = true;
					}
					
					if (!error) {
					
						$.ajax({
							type: "post",
							url: $("form#edit-comment-form-" + comment).attr("action") + "/ajax/",
							data: post_data,
							success: function (data) {
								$("div#lesson-comments").replaceWith(data);
								hideCommentNotifyFormButton();
							}
						});
					}
					
					return false;
				});
			}
		});
		
		return false;
	});
	
	$("div.lesson-comment ul.comment-menu input.delete-button").live("click", function() {
		var re = /comment\-delete\-([0-9]+)/i;
		var str = $(this).attr("id");
		matches = str.match(re);
		
		var comment = matches[1];
		
		$.ajax({
			type: "get",
			url: "http://www.rockway.fi/oppituntikommentit/poista/" + comment + "/ajax/",
			success: function (data) {
				$("ul#comment-menu-" + comment).hide();
				$("div#lesson-comment-" + comment).after(data);
				
				$("form#delete-comment-form-" + comment + " input#hide-delete-form-" + comment).live("click", function() {
					$("form#delete-comment-form-" + comment).remove();
					$("ul#comment-menu-" + comment).show();
					
					return false;
				});
				
				$("form#delete-comment-form-" + comment + " input#submit-delete-form-" + comment).live("click", function() {
					var id = $("form#delete-comment-form-" + comment + " input#id-delete-" + comment).val();
					
					var post_data = "delete_comment=&id=" + id;
					
					if (id == "") {
					
					}
					
					$.ajax({
						type: "post",
						url: $("form#delete-comment-form-" + comment).attr("action") + "/ajax/",
						data: post_data,
						success: function (data) {
							$("div#lesson-comments").replaceWith(data);
							hideCommentNotifyFormButton();
						}
					});
					
					return false;
				});
			}
		});
		
		return false;
	});
	
	$("form#comment-notify-form input#notify").live("click", function() {
		var notify = $("form#comment-notify-form input#notify").val();
		var clip = $("form#comment-notify-form input#clip-notify").val();
		
		var post_data = "submit_comment_notify=&clip=" + clip;
		
		if ($("form#comment-notify-form input#notify:checked").val() != null) {
			post_data += "&notify=" + notify;
		}
		
		$.ajax({
			type: "post",
			url: "http://www.rockway.fi/oppituntikommentit/ilmoita/ajax/",
			data: post_data,
			success: function (data) {
				$("form#comment-notify-form div.status").remove();
				$("form#comment-notify-form div.checkbox").after(data);
				$("form#comment-notify-form div.status").fadeOut(3000, function() {
					$(this).remove();
				});
			}
		});
	});
	
	$("ul.pagination a.comment-pagination").live("click", function() {
		var re = /(?:top|bottom)-comment\-page\-(?:next-|prev-)?([0-9]+)/i;
		var str = $(this).attr("id");
		matches = str.match(re);
		
		var page = matches[1];
		
		var clip = $("div#lesson-comments").attr('class');
        
        clip = clip.replace('lesson-', '');
		
		$("div#lesson-comments").empty();
		$("div#lesson-comments").append('<img src="http://www.rockway.fi/graphics/ajax-loader-white.gif" alt="" />');
		
		$.ajax({
			type: "get",
			url: "http://www.rockway.fi/oppituntikommentit/" + clip + "/sivu/" + page + "/ajax/",
			success: function (data) {
				$("div#lesson-comments").replaceWith(data);
				hideCommentNotifyFormButton();
			}
		});
		
		return false;
	});
	
	$("input#activate-giftcard-button").click(function() {
		alert(1, '../lahjakortti/');
		
		return false;
	});
	
	$("div#campaign-box").hide();
	$("div#campaign-link-container").empty();
	$("div#campaign-link-container").append('<a href="#" id="campaign-link">Onko sinulla kampanjakoodi?</a>');
		
	$("a#campaign-link").click(function() {
		$("div#campaign-box").show();
		
		return false;
	});
	
	$('input#search-username').autocomplete($('input#user-search-list-url').val(), {
		
	});
	
	if (($('div#registration-page input#username').val() != '') && ($('div#registration-page input#username').val() != null)) {
			check_registration_validity("username");
	}
	
	if (($('div#registration-page input#password').val() != '') && ($('div#registration-page input#password').val() != null)) {
			check_password_strength();
	}
	
	if (($('div#registration-page input#email').val() != '') && ($('div#registration-page input#email').val() != null)) {
			check_registration_validity("email");
	}
	
	var delayCheck = null;
	var delayTime = 500;
	var loadingImageHTML = "<img src=\"http://www.rockway.fi/graphics/loading.gif\" alt=\"loading\" />";
	
	$('div#registration-page input#username').keyup(function (event){
		if (event.keyCode != 9) {
			$('#check_username').html(loadingImageHTML);
			if(delayCheck != null) {
				clearTimeout(delayCheck);
			}
			delayCheck = setTimeout('check_registration_validity("username")', delayTime);
		}
	});

	$('div#registration-page input#email').keyup(function (event){
		if (event.keyCode != 9) {
			
			if(delayCheck != null) {
				clearTimeout(delayCheck);
			}
			delayCheck = setTimeout('check_registration_validity("email")', 2000);
		}
	});
	
	$('div#registration-page input#password').keyup(function (event){
		if (event.keyCode != 9) {
			$('#check_password').html(loadingImageHTML);
			if(delayCheck != null) {
				clearTimeout(delayCheck);
			}
			delayCheck = setTimeout('check_password_strength()', delayTime);
		}
	});
	/*
	$('div#registration-page input#password').change(function (){
	check_registration_validity("password");
	});*/


	$('div#registration-page input#email').change(function (){
		$('#check_email').html(loadingImageHTML);
		check_registration_validity("email");
	});

	$("div#registration-page input.input").each(function() {
		$(this).qtip({
			show: {
				solo: true
			},
			style: { 
				name: 'light',
				tip: true
			}
		});
	});

	$('div#registration-page input#birthdate').mask('99.99.9999');
	
	$('div#user-info-page input#birthdate').mask('99.99.9999');
	
});

//registration validation



function check_registration_validity(id) {
	delayCheck = null;
	
	//$('#check_'+id).html(loadingImageHTML);
	$.post("http://www.rockway.fi/partials/ajax/registration_check_partial.php", {
    field: id,
		input: $('#'+id).val()
  }, function(response){
		if (response == 1) {
			$('div#registration-page div#'+id+'-container').removeClass("rounded-container-error");
		  $('div#registration-page div#'+id+'-container').addClass("rounded-container-2");
			$('#check_'+id).html('<img src="http://www.rockway.fi/graphics/checked.png" alt="ok" />');
		} else {
			$('div#registration-page div#'+id+'-container').removeClass("rounded-container-2");
		  $('div#registration-page div#'+id+'-container').addClass("rounded-container-error");
    	$('#check_'+id).html(unescape(response));
		}
  });
} // check_validity



// /registration validation




var shortPass = 'Salasana on liian lyhyt.';
var longPass = 'Salasana on liian pitkä.';
var obviousPass = 'Salasana on liian ilmiselvä.';
var weakPass = '<img src="http://www.rockway.fi/graphics/checked.png" alt="ok" /><span style="color: #e8e61c;">Salasana on heikko.</span>';
var goodPass = '<img src="http://www.rockway.fi/graphics/checked.png" alt="ok" /><span style="color: #b1e81c;">Salasana on hyvä.</span>';
var strongPass = '<img src="http://www.rockway.fi/graphics/checked.png" alt="ok" /><span style="color: #39e81c;">Salasana on vahva.</span>';
var strength = null;

function check_password_strength() {
	delayCheck = null;
	//$('#check_password').html(loadingImageHTML);
	strength = passwordStrength($('#password').val(),$('#username').val());
	
	$.post("http://www.rockway.fi/partials/ajax/registration_check_partial.php", {
    field: 'password',
		input: $('#password').val()
  }, function(response){
		if (response == 1) {
			if ((strength == weakPass) || (strength == goodPass) || (strength == strongPass)) {
				$('div#registration-page div#password-container').removeClass("rounded-container-error");
			  $('div#registration-page div#password-container').addClass("rounded-container-2");
				$('#check_password').html(unescape(strength));
			} else {
				$('div#registration-page div#password-container').removeClass("rounded-container-2");
			  $('div#registration-page div#password-container').addClass("rounded-container-error");
				$('#check_password').html(unescape(strength));
			}
		} else {
			$('#check_password').html(unescape(obviousPass));
		}
  });
	
}

function passwordStrength(password,username)
{
    score = 0 
    
    //password < 5
    if (password.length < 5 )
		{ 
			return shortPass;
		}
    
		if (password.length > 32 ) 
		{ 
			return longPass; 
		}

    //password == username
    if (password.toLowerCase()==username.toLowerCase()) {
	 		return obviousPass;
		}

    //password length
    score += password.length * 4;
    score += ( checkRepetition(1,password).length - password.length ) * 1;
    score += ( checkRepetition(2,password).length - password.length ) * 1;
    score += ( checkRepetition(3,password).length - password.length ) * 1;
    score += ( checkRepetition(4,password).length - password.length ) * 1;

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))
		{
			score += 5;
    }
    //password has 2 symbols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) { score += 5; }
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) { score += 10; }
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) { score += 15; }
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) { score += 15; }
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) { score += 15; }
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) ) { score -= 10; }
    
    //verifing 0 < score < 100
    if ( score < 0 ) { score = 0; }
    if ( score > 100 ) { score = 100; }
    
    if (score < 34 ) { return weakPass; }
    if (score < 68 ) { return goodPass; }
    return strongPass
}


function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}




function hideCommentNotifyFormButton() {
	$("form#comment-notify-form div + div").hide();
	$("form#comment-notify-form div").addClass("last");
}



//Player function

function player_zoom_toggle(direction) {
	if (direction == '1') {
			$("div#player-container").css('margin-top', '0px');
	}
	if (direction == '0') {
			$("div#player-container").css('margin-top', '-108px');
	}
}


function backingtrack_height(amount) {
		pixels = 105 + (amount*53);
		$("#fob").height(pixels);
}



