// Addtext
function addtext(veld,text)
{
    document.form.elements[veld].value += " "+text+" ";
    document.form.elements[veld].focus();
}

function removeImage2(vars)
{
	$.get("/scripts/resetImage.php",

			function(data)
			{
  				$("#img").hide("slow");

			}
	);
}

function removeImage(vars)
{
	$.get("/scripts/resetImage.php",

			function(data)
			{
  				$("#img").hide("slow");
			}
	);
}

function ChangePage(pageVars)
{
	new Ajax.Updater($('MyPane'), pageVars);

	return false;
}

function deleteReply(replyID, parentID)
{
	$.get("/scripts/deleteReply.php", { replyID: + replyID, parent: + parentID },

			function()
			{
				$("li.reply_" + replyID).hide("slow");
			}
	);

}

function update()
{
	$("#notice").hide();
	$.ajax({
		type: 'GET',
		url: 'nowPlaying.php',
		timeout: 6000,
    	success: function(data)
    	{
      		$("#notice").hide();
   			$("#nowPlaying").html(data);
    		window.setTimeout(update, 10000);
    	},
   		error: function (XMLHttpRequest, textStatus, errorThrown)
   		{
   		 	$("#notice").show();
   			$("#notice").html('Server timeout..');
   			window.setTimeout(update, 21000);
   		}
    });
}

function makeScrollable(wrapper, scrollable){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);

	scrollable.hide();
	var loading = $('<div class="loading"><img src="/templates/radiolelystad/gfx/site/spinner.gif"></div>').appendTo(wrapper);

	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('li');
		var completed = 0;
			clearInterval(interval);
			setTimeout(function(){

				loading.hide();
				wrapper.css({overflow: 'hidden'});

				scrollable.slideDown('slow', function(){
					enable();
				});
			}, 500);
	}, 10);

	function enable(){
		var inactiveMargin = 69;
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;

		var tooltip = $('<div class="sc_menu_tooltip"></div>')
			.css('opacity', 0)
			.appendTo(wrapper);

		// Save menu titles
		scrollable.find('a').each(function(){
			$(this).data('tooltipText', this.title);
		});

		// Remove default tooltip
		scrollable.find('a').removeAttr('title');
		// Remove default tooltip in IE
		scrollable.find('img').removeAttr('alt');

		var lastTarget;
		//When user move mouse over menu
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;

			var wrapperOffset = wrapper.offset();

			var tooltipLeft = e.pageX - wrapperOffset.left;
			// Do not let tooltip to move out of menu.
			// Because overflow is set to hidden, we will not be able too see it
			tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());

			var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;
			// Move tooltip under the mouse when we are in the higher part of the menu
			if (e.pageY - wrapperOffset.top < wrapperHeight/2){
				tooltipTop += 80;
			}
			tooltip.css({top: tooltipTop, left: tooltipLeft});

			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
			if (top < 0){
				top = 0;
			}
			wrapper.scrollTop(top);
		});

		// Setting interval helps solving perfomance problems in IE
		var interval = setInterval(function(){
			if (!lastTarget) return;

			var currentText = tooltip.text();

			if (lastTarget.nodeName == 'IMG'){
				// We've attached data to a link, not image
				var newText = $(lastTarget).parent().data('tooltipText');

				// Show tooltip with the new text
				if (currentText != newText) {
					tooltip
						.stop(true)
						.css('opacity', 0)
						.text(newText)
						.animate({opacity: 1}, 1000);
				}
			}
		}, 200);

		// Hide tooltip when leaving menu
		wrapper.mouseleave(function(){
			lastTarget = false;
			tooltip.stop(true).css('opacity', 0).text('');
		});

	}
}

$(function(){
	makeScrollable("div#StraksWrapper", "div#StraksContainer");
});

function fadeImages()
{
	$('#artistGallery').innerfade({
		speed: 'slow',
 		timeout: 8000,
  		type: 'sequence',
   		containerheight: '80px'
  	});
}

function validate()
{
	$(document).ready(function(){

  	var default_values = {};

 	//global vars
	var form = $("#newsAlert");
	var email = $("#email");
	var emailInfo = $("#emailInfo");

  	email.each(function(i)
  	{
    	var index = $(this).attr('name');
    	var value = $(this).val();
    	default_values[index] = value;
  	});

 	email.focus(function()
 	{
    	if($(this).val() === default_values[$(this).attr('name')])
    	{
      		$(this).val('');
    	};
  	})

	//On blur
	email.blur(validateEmail);

	//On Submitting
	form.submit(function(){
		if(validateEmail())
		{
			form.slideUp("normal");

			$("#mailResult").append('<div class="loading">Bezig met laden...</div>');

			return false
		}
		else
		{
			return false;
		}
	});

	//validation functions
	function validateEmail(){
		//testing regular expression
		var a = $("#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email

		if(filter.test(a)){
			email.removeClass("error");
			emailInfo.text("");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("error");
			emailInfo.text("Geef een geldig email adres op");
			emailInfo.addClass("error");
			return false;
		}
	}
	});
}

function drawRater(trackID, average)
{

	$(function ()
	{
		function starRater(res)
		{
			$('#rater').empty().rater(
			'/scripts/trackVote.php', trackID, {
			maxvalue : 6,
			style    : 'basic',
			curvalue : res||0,
			callback : starRater
			});
  		}

   		starRater(average);

 	});

}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(pagina){
	
	
	
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.5"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
	
	$("#contentArea").html('Bezig met laden...');
	
	$.ajax({
		type: 'POST',
		url: 'contact.php',
    		
		success: function(data)
    		{
   		
			$("#contentArea").html(data);
 
    		}
 
    	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

}


function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

