canVote = 2,
global_timeouts = {
	"votefeedback": function (){
		jQuery("#votefeedback").fadeOut();
	},
	"quotator": {
		"quotate": function (){
			var q = global_timeouts.quotator;
			q.quotes.eq(q.current).hide();
			q.current = ++q.current % q.count;
			q.quotes.eq(q.current).show();
		},
		"quotes": jQuery("div.block.quotes blockquote").eq(0).show().end(),
		"current": 0
	}
},
resetVoter = function (song) {
	jQuery(".iRate").val(0);
	jQuery("input[name=iRate]").val(0);
	jQuery(".stars label").removeClass("selected");
	jQuery("#rateForm")[0].reset();
	jQuery("#votetrack strong").text(song);
	jQuery(".popupform input[name=sSong]").val(song);
	canVote = 1;
},
email = readCookie('myEmail');
global_timeouts.quotator.count = global_timeouts.quotator.quotes.length;
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

(function ($){

	// MANIFEST!
	var manifest = $('a[href=#manifest]');
	if (manifest.size() > 0){
		$('#manifest').hide();
		manifest
			.css({
				"fontSize": "1.2em",
				"textDecoration": "underline"
			})
			.fancybox({ 
				"frameWidth": 600,
				"frameHeight": 550,
				"padding": 10
			})
			.parent()
			.css("text-align", "center");
	}

	var vote = $("a#voteclick"),
		toggleDefaultValue = function (el){
			var defaultValue;
			if (email) {
				defaultValue = email;
				el.val(email);
			}
			else {
				defaultValue = el[0].value;
				el.focus(function (){
					if (this.value == defaultValue){
						this.value = "";
					}
				}).blur(function (){
					if (this.value == ""){
						this.value = defaultValue;
					}
				});
			}
		},
		changeRating = function (label, iRate){
			label.siblings().removeClass("selected").end().addClass("selected");
			$("input[name=iRate]").val(iRate);
		},
		sStars = '',
		support = $("a.support"),
		slideshow = $("#slideshow");
	/* rotate quotes: quotate! */
	setInterval("global_timeouts.quotator.quotate()", 10000);
	if (slideshow.get(0)) {
		slideshow.cycle({ 
			"fx": "fade", 
			"speed":  4000 
		});
	}
	
	if (support.get(0)) {
		support.fancybox({ 
			"frameWidth": 500,
			"frameHeight": 300,
			"callbackOnShow": function(){
				toggleDefaultValue($("input:text:visible[name=sEmail].email.required"));
				$("form").submit(function(){
					var $this = $(this);
					email = $this.find("input:text[name=sEmail]").val()
					createCookie('myEmail',email , 60);
				});
			}
		});
	}
	
	if (vote.get(0)){
		vote.fancybox({ 
			"frameWidth": 500,
			"frameHeight": 330,
			"callbackOnShow": function (){
				
				var stars = ["", "♫", "♫♫", "♫♫♫", "♫♫♫♫", "♫♫♫♫♫"];
				toggleDefaultValue($("input:text:visible[name=sEmail].email:not(.required)"));
				
				$("form").submit(function(){
					var $this = $(this);
					email = $this.find("input:text[name=sEmail]").val()
					createCookie('myEmail',email , 60);
					$.post('/vote.php', $this.serialize());
					twit = 'Just rated ' + $this.find("input:hidden[name=sSong]").val() + ' with ' + stars[parseInt($this.find("input:hidden[name=iRate]").val(), 10) / 20] + ' @Radiojazz (http://radiojazz.nl)';
					$("#rateForm").after('<p id="votefeedback"><a href="http://twitter.com/?status='+encodeURIComponent(twit)+'" target="_blank">Twit this</a>Thank you for rating this song!</p>');
					setTimeout("global_timeouts['votefeedback']()", 10000);
					canVote = 0; // Prevents double rating
					$.fn.fancybox.close();
					return false;
				});
			}
		});
	}
	
	/** Star rating **/
	$(".iRate option").nextAll().each(function(i){
		sStars += '<label for="' + this.id + '" class="' + this.id + '">' + this.innerHTML + "</label>";
	});
	$("#iRateText").after('<em class="stars"> ' + sStars + "</em>");
		
	$(".stars label").click(function (i){
		var label = $(this), iRate = label.attr("for").substring(5);
		if (iRate > 0 && canVote == 1){
			changeRating(label, iRate);
			vote.click();
		} 
		else if ( iRate > 0 && canVote == 2) {
			$("#rateForm").after('<p id="votefeedback">Press play first!</p>');
			setTimeout("global_timeouts['votefeedback']()", 2500);
		}
		else {
			$("#rateForm").after('<p id="votefeedback">You can only rate a song once!</p>');
			setTimeout("global_timeouts['votefeedback']()", 2500);
		}
	});
	$(".iRate").hide();

		var nl = $("body").hasClass("nl"), cf = $("form#comment");
		
		if (cf.get(0)) {
			$.extend($.validator.messages, {
				required: (nl?"Dit is een verplicht veld.":"This field is required."),
				email: (nl?"Vul hier een geldig email adres in.":"Please enter a valid email address.")
			});
			
			/* Validate the form */
			cf.validate({
				errorPlacement: function(er,el){
					el.before(er);
				}
			});
		}
		
	
	$("input.banner").click(function(ev){
		this.focus();
		this.select();
	});

	$("#calendar-form select").change(function(){
		$("#calendar-form").submit();
	});

}
(jQuery));
