/* default vars */
var defaultValue = "";
var defaultValues = ["Vul hier uw vraag in...","Vul hier uw zoekterm in...","Vul hier een plaatsnaam of postcode in..."];
var question = false;
var zipcode = false;
var zipchars = false;
var urlPath = document.location.hash;

$(document).ready(function() {
		
		$('#footer li:last').css('border','none');
    // Load home as the default content
    if (urlPath.length == 0) {
			loadPage("home.aspx");
      $("#navigation li:first").children().addClass("active");
      $('#search li:eq(1)').addClass('subactive');
    }
    
    $('#question').focus(function(){
			$(this).removeClass('error').val('');
    });
    
    // Load pages
    ajaxNav();
    // Validate field
    validateZipcode();
    // Search
    $("#search a.btn").click(function() {
			searchQuestion();
    });
    // Highlight active search-tab
    searchTab();
    // Highlight active navigation-tab
    //highlightActiveTab();
    
    // Show tooltips
		//toolTip();
	
    // Open tooltips in new window
    $(".tooltip li a").click(function() {
        var target = $(this).attr("href");
        window.open(target);
        return false;
    });
    
    // Resize text
    $("#footer li.resize a").click(function() {
        resizeText($(this));
        return false;
    });

    $("#footer ul li:not(.resize) a").click(function() {
        var page = $(this).attr("href");
        loadPage(page);
        $("#footer ul li:not(.resize)").children().each(function() {
            $(this).removeClass("active");
        });
        
        if (page == "home.aspx") {
            $("#navigation li:first").find("a").addClass("active");
        }

        $(this).addClass("active");
        return false;
    });

    // Open links in a new window
    rescale();
    openInNewWindow();
    $(document).pngFix();
});

/*------------- Functions --------------*/
function searchTab() {
	if ($("body").hasClass("search")) {
		$("fieldset .field label:first").html("Zoek op plaatsnaam of postcode:");
		$('#question').val('').focus();
	} else {
		$("body").removeClass("search");
		
		if($("#search li.active a.question").length>0){
			$("fieldset .field label:first").html("Stel uw vraag aan Aladin, de vragendienst van de bibliotheek");
			$('#question').val('').focus();
		}else{
			$("fieldset .field label:first").html("Vind boeken, achtergronden, actualiteit, muziek en films");
			$('#question').val('').focus();
		}
	}
	
	$("#search ul li a").click(function(){
		$("input#question").removeClass("error");
		$("#search li").each(function(){
			$(this).removeClass("active");
			$(this).removeClass("subactive");
		});
		$(this).parent().addClass("active");
		$(this).parent().next().addClass("subactive");
		
		if($(this).attr("class") == "question"){
			$("#search a.btn").html("Stel vraag!");
		}else{
			$("#search a.btn").html("Zoek");
		}
		
		// Highlight the correct form
		if($("#search li.active a.library").length>0){
				$("body").addClass("search");
				$("fieldset .field label:first").html("Zoek op plaatsnaam of postcode:");
				
				$('#question').val('').focus();
		}else if($("#search li.active a.collection").length>0){
				$("body").removeClass("search");
				$("fieldset .field label:first").html("Vind boeken, achtergronden, actualiteit, muziek en films");

				$('#question').val('').focus();
		}else if($("#search li.active a.question").length>0){
				$("body").removeClass("search");
				$("fieldset .field label:first").html("Stel uw vraag aan Aladin, de vragendienst van de bibliotheek");

				$('#question').val('').focus();
		}
		return false;
	});
}

function GaZoeken(e) {
	  if (document.all) {
        e = event;
    }
    if (e.keyCode == 13) {
			e.returnValue = false;
      e.cancel = true;
			e.cancelBubble = true;
			searchQuestion();
			return false;
    }
}

function searchQuestion(){
  var url = $("#search ul li.active a").attr("href");
  var rel = $("#search ul li.active a").attr("rel");

  var questionValue = $("input#question").val();
  questionValue = questionValue.replace(/ /g, "%20");
  var zipcodeNrs = $("input#numbers").val();
  var zipcodeChars = $("input#chars").val();

  // Zoeken naar bibliotheek
  if (($("input#question").val() == "") && url != "ZoekResultaten.aspx") {
      // zit in de array dus niet submitten
      $("input#question").addClass("error");
      $("input#question").val("U moet minimaal 1 woord invoeren om te zoeken.");
      return false;
  } else if (url != "ZoekResultaten.aspx") {
			targetUrl = url + questionValue;
      // Open the correct page
			if (rel == 'external') {
					window.open(targetUrl);
			} else {
					loadPage(targetUrl);
			}
  } else if (url == "ZoekResultaten.aspx") {
			// Remove active navigation
      $("#navigation ul li").children().removeClass("active");
      if (!$("input#question").val() == ""){
					$("input#question,input#numbers,input#chars").removeClass("error");
					targetUrl = url + "?zoekbibliotheek=" + questionValue;
          // Open the correct page
					if (rel == 'external') {
							window.open(targetUrl);
					} else {
							loadPage(targetUrl);
					}
      } else {
				// search is in defaults -> error!
				$("input#question,input#numbers,input#chars").addClass("error");
				$("input#question").val("U moet minimaal 1 woord invoeren om te zoeken.");
      }
  }
			//searchTab();
}

function validateZipcode(){
	$("input#numbers").blur(function(){
	    if ($("input#numbers").val() != "") {
		    if($("input#numbers").val().match(/\d{4}/) == null){
			    $(this).addClass("error");
			    zipChecked = false;
		    }else{
			    $(this).removeClass("error");
			    zipChecked = true;
		    }
     	}
	});
}

function checkField(field, pcVal){
	$(field).focus(function(){
	 	if($(this).val() == defaultValue || $(this).val() == pcVal){
			$(this).val("");
		}
	});
	$(field).blur(function(){
		if($(this).val() == "" && $(this).attr("id") == "question" ){
			$(this).val(defaultValue);
		} else if ($(this).val() == "" && ($(this).attr("id") == "numbers" || $(this).attr("id") == "chars")) {
			$(this).val(pcVal);
		}
	});
}

function resizeText(index){
	var anchorClass = $(index).attr("class");
	$("body").attr("class",""); // Remove any class
	$("body").addClass(anchorClass); // Add the class of the link to the body
	// Remove .active on the links
	$("#footer li.resize a").each(function(){
		$(this).removeClass("active");
	});
	$(index).addClass("active");
}

function ajaxNav(){
	$("#navigation a, #footer ul li:not(.resize) a, a.intern").click(function(){
		// Check which tab is active
		$("#navigation,#footer").find(".active").removeClass("active");
		$(this).addClass("active");
		
		if($(this).hasClass('intern')){
			$('#navigation a:last').addClass('active');
		}
		// Check the link url and add to the path
		var page = $(this).attr("href");
		var url = "#" + page;
		var pageId = location.hash;
		window.location = url;

		// If page already opened don't animate
		var target = pageId.substr(1);
		if(target == page){
			return false;
		}
		// Animate & load page
		else
		{
			var pageName = $(this).text();
			pageName = pageName.replace("&amp;","&");
			var pageTitle = "Bibliotheek.nl - " + pageName;
			document.title = pageTitle;
			loadPage(page);
			return false;
		}
	});
}

function highlightActiveTab(){
	// Get the location
	var pageId = location.hash;
	pageId = pageId.substr(1);
	// Highlight the tab if it's present in the url
	$("#navigation a, #footer a").each(function() {
		var page = $(this).attr('href');
		// Check if the hash is equal to one of the tabs
		if(pageId == page){
			$("#navigation a").removeClass("active");
			$(this).addClass('active');
			
			// Change title
			var pageName = $(this).text();
			pageName = pageName.replace("&amp;","&");
			var pageTitle = "Bibliotheek.nl - " + pageName;
			document.title = pageTitle;
			loadPage(page);
		}		
	});
}

function toolTip(){
	// Add tooltips to links
	$("#content a.tooltip, #content ul.tooltip a").each(function(){
		if ($(this).attr("title") != undefined) {
			var title = $(this).attr("title");
		 	$(this).prepend('<span class="tooltip">' + title + '</span>');
			$(this).removeAttr("title");
		}
	});
	// Show tooltip when hovered
	$("#content a").hover(function(){
		$(this).children('span').css('display','block');
	},function(){
			$(this).children('span').css('display','none');		
	});
}

function loadPage(page) {
	$("#content").load(page, function(){
		$("#content a:not(.email,.tooltip,.intern)").each(function(){
			$(this).attr("rel","blank");
		});
		openInNewWindow();
		//toolTip();
		$('#related').css('zoom','1');
		ajaxNav();
	});
}

function openInNewWindow() {
	$("a[rel='blank']").each(function() {
		$(this).attr('title','Link opent in een nieuw venster');
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	});
}

function rescale() {
	var width = $(window).width();
	if(width < 1100){
			$('#search, #navigation').css('left','0');
			$('#content').css('margin-left','0');
		}else if(width > 1100){
			$('#search').css('left','142px');
		}
		
	$(window).bind('resize', function() {
		var width = $(window).width();
		if(width < 1100){
			$('#search, #navigation').css('left','0');
			$('#content').css('margin-left','0');
		}else if(width > 1100){
			$('#search').css('left','142px');
			$('#navigation').css('left','144px');
			$('#content').css('margin-left','144px');
		}
	});
}