/* General scripts for KickXL */

/* onloads */

$(function(){
	// inititialize more function embedded from blocks
	if (typeof(init) == 'function') init();
	if (typeof(initSlider) == 'function') initSlider();
	if (typeof(postcodeBg) == 'function') postcodeBg();
	
	loadExcludeWords();
	initBlurText();
	
	if ($("ul.column_1").text() != '') initMenu();
	if ($("#searchBox").html() != '') initSearchField();

	//$("#PostcodeCheck #postcode").focus();

	
	// check for tabs!
	initializeTabsOnPage();
	
	//load verkooppunten
	if ($('#verkooppuntenCity').val() !="") initVerkooppunten();
						
	//load stappen voor overstapservice
	initializeOverstapNav();

	initClickMeasure();
})



// Check for dynamic lists on the current page
function initSalesPoints()
{

	var field = $("#verkooppuntenCity");
	var ul = $("#salespoints");

	$(field).blur(function(){ul.hide(); });

	$("#verkooppuntenCity").keyup(function(){

		ul.html('');	
		if(field.val().length > 1)
		{
			$.getJSON('/backend/search-salespoints.php?&q='+escape(field.val()),
			  function(data) {
				if (data.length > 0) {
					for( var j in data)
					{
						ul.append('<li><a href=\"#\" class=\"ui-corner-all\">'+data[j]+'</a></li>');
					}
					$("li a", ul).mousedown(function(e){
						ul.slideUp();
						$("#verkooppuntenCity").val($(this).html());
						loadVerkooppunten(field.val());
				
					}).hover(function(){$(this).addClass('ui-state-hover')},function(){$(this).removeClass('ui-state-hover')});

					ul.show();
				}
			});
		}
			
	});
}


// verkooppunten 

function initVerkooppunten() {
	$("#verkooppuntenSubmit").click(loadVerkooppunten);
	initSalesPoints();
}

function loadVerkooppunten()
{
	$('#verkooppunten').load('/backend/verkooppunten.php?stad='+escape($('#verkooppuntenCity').val()), function(){Cufon.replace('h1, h2')});
}


// overstapservice

function initializeOverstapNav()
{
	$('.step_1').click(
		function()
		{
				$('.overstapStap').fadeOut("fast");
				$('.overstapStap_1').delay(180).fadeIn("slow");	
				$('#overstapSteps li').removeClass('selected');
				$('#overstapSteps .step_1').addClass('selected');
		}
	);

	$('.step_2').click(
		function()
		{
				$('.overstapStap').fadeOut("fast");
				$('.overstapStap_2').delay(180).fadeIn("slow");	
				$('#overstapSteps li').removeClass('selected');
				$('#overstapSteps .step_2').addClass('selected');				
		}
	);

	$('.step_3').click(
		function()
		{
				$('.overstapStap').fadeOut("fast");
				$('.overstapStap_3').delay(180).fadeIn("slow");				
				$('#overstapSteps li').removeClass('selected');
				$('#overstapSteps .step_3').addClass('selected');				
		}
	);

	$('.step_4').click(
		function()
		{
				$('.overstapStap').fadeOut("fast");
				$('.overstapStap_4').delay(180).fadeIn("slow");	
				$('#overstapSteps li').removeClass('selected');
				$('#overstapSteps .step_4').addClass('selected');				
		}
	);
}

function initMenu()
{
	$("DIV.Menu ul li ul.submenu").each(function(){
		$(this).parent().prev().prev().addClass('hasSub');
	});
	
	$("DIV.Menu ul ul.submenu li.tab").each(function(){
		$(this).css({width: $(this).parent().parent().prev().prev().width()+30});
	});
	$("DIV.Menu ul li ul.submenu").each(function(){$(this).parent().css({width: $(this).parent().prev().prev().width()})});
	$("DIV.Menu ul li").hover(function(){$('ul.submenu',this).stop().delay(100).fadeIn(400)},function(){$('ul.submenu',this).stop().hide().css({opacity:1})})
	$("DIV.Menu ul li ul li:first-child").next().addClass('first-child');
	$("DIV.Menu ul li ul li:last-child").addClass('last-child');
}


function initSearchField()
{
	$('input[name="search"]').focus(function() { 
		$(this).stop().animate({width:120});  
     }).blur(function() {
		 $("#suggestionList").slideUp();   
		$(this).stop().animate({width:80});  
     }).keyup(function(event){ 
	 event.which == 13?submitSearch():loadKeywords($(this).val())});
}

function initBlurText()
{
	$(".blurText").each(function(){
		$(this).attr({blurtext : $(this).val()}).bind('blur', function(){
		if ($(this).val() == '') $(this).val($(this).attr('blurtext')); 
		}).bind('focus',function() {
			($(this).attr('blurtext'));
			 if ($(this).val() == $(this).attr('blurtext')) $(this).val('');  
		});
	});
}


function trim(s) {
	jQuery.trim(s);
}


function clearTabs(arrayVar)
{
	
	var arrayA = $(arrayVar).children().children();
	for(var i=0; i< arrayA.length; i++)
	{
		$(arrayA[i]).removeClass('active');
	}
}

function initializeTabsOnPage()
{
	var tabsOnPage = $('div.Tabs ul li a');	
	
	
	for(var i =0; i < tabsOnPage.length; i++)
	{
		$(tabsOnPage[i]).attr('href', 'javascript: void(0);');
		
		$(tabsOnPage[i]).click(function(){
			var holder = $(this).parent().parent().parent().parent().children().children();
			var child = holder.children();
			
			
			var X = $(this).parent().index();
			$(holder).animate(
			{
				left: "-"+X*child.width()+"px"
			});			
			
			clearTabs($(this).parent().parent()); // clear tabs in current tabber
			$(this).attr('class', 'active');							  
		});		
	}
	
}



function loadKeywords(keyword)
{
	if (keyword == undefined || keyword =="")
	{
		$("#suggestionList").hide();
	}else {
		$.getJSON('/backend/search-keywords.php',"q="+escape(keyword),function(data) {
			if (data.length > 0) {
				var titles = [];
				for (i in data) titles[i] = data[i].title;
				updateAutoComplete(titles) 
			}else {
				$("#suggestionList").slideUp();
			}
		});
	}
}



function submitSearch()
{
	$.clickmeasure.update($("input[name='search']").val(),function(){
		document.location='/helpdesk/veel-gestelde-vragen#'+escape($("input[name='search']").val());
	});
}

function updateAutoComplete(data)
{
	$("#suggestionList").html('<li class="suggestions">suggesties&nbsp;</li>');
	for (i in data) {
		$("#suggestionList").append("<li class=\"ui-menu-item\"><a href=\"#\" class=\"ui-corner-all\">"+ data[i] +"</a></li>").slideDown();
	}
	$("#suggestionList li a").click(function(e){
		var keyword = $(this).text();
		e.preventDefault();
		$("#suggestionList").slideUp();
		$("input[name='keywords'],input[name='search']").val(keyword)
		if (typeof(searchFaq) == 'function') {
			// faq page
			searchFaq(keyword);
			searchPages(keyword);
		}else {
			//little searchbox
			document.location='/helpdesk/veel-gestelde-vragen#'+escape(keyword);
		}

	}).hover(function(){$(this).addClass('ui-state-hover')},function(){$(this).removeClass('ui-state-hover')});
}



function loadExcludeWords()
{
	$.get('/configs/excludewords.txt','',function(data){
		$.excludeWords = {
			words : data.split("\n"),
			exclude : function excludeTheWords(keywords)
			{
				var w = keywords.split(" ")
				for (i in $.excludeWords.words) {
					for (ii=w.length;ii>=0;ii--) {
						if ($.excludeWords.words[i] == w[ii]) w.splice(ii,1);
					}
				}
				//console.log(w.join(" "));

				return w.join(" ");
			},
		 	complete : function(){}
		 }
	});
}


function initClickMeasure()
{
	$.clickmeasure = {
		update : function (keyword,complete)
		{
			if (typeof(complete) != 'function') complete = function(){};
			$.getJSON('/backend/clicks-likes.php',"l=nl&q="+escape(keyword),complete);
		}
	}
}

