﻿// Jquery //
var loadingMessage = "<div class=\"loading\"><img src=\"/img/laddar-bow.gif\" width=\"16\" height=\"16\" /> Laddar...</div>";
var whatActive = false;
var whereActive = false;

//EVENT LISTENERS
$(document).ready(function(){
	$("#ddWhat").click(function(){
		if (whatActive == false){closeWhere(); openWhat();} else{closeWhat();}
	});
	
	$("#ddWhere").click(function(){
		if (whereActive == false){closeWhat(); openWhere();} else{closeWhere();}
	});
	
	$("#sb .fc input").click(function(){
		if (whatActive == true){closeWhat();}
		if (whereActive == true){closeWhere();}
	});
	
	$("#bgl").click(function(){
		cAll();
	});
	
	$(document).keypress(function(e){
		if (e.keyCode==27){
			cAll();
		}
	});
});

function cAll(){
	closeWhat(); closeWhere(); cLogin();
}

function openDD(id, url){
	$(id).html(loadingMessage);	
	$(id).slideDown();
	$(id).load(url, function(){
		$(id).animate({height: "450px"}, 250);
	});
	$("#bgl").css("display", "block");
}

function closeDD(id){
	$(id).slideUp("fast", function(){
		$(id).css("height", "27px");
	});
	$("#bgl").css("display", "none");
}

function openWhat(){
	whatActive = true;
	var url = "/ajax.dl-what.asp";
	openDD("#dlWhat", url);
}

function openWhere(){
	whereActive = true;
	var url = "/ajax.dl-where.asp";
	openDD("#dlWhere", url);
}

function closeWhat(){
	whatActive = false;
	closeDD("#dlWhat");
}

function closeWhere(){
	whereActive = false;
	closeDD("#dlWhere");
}

function addToWhat(val){
	$("#what").val(val);
	$("#what").focus();
	createRef();
	closeWhat();
	return false;
}

function addToWhere(val){
	$("#where").val(val);
	$("#where").focus();
	createRef();
	closeWhere();
	return false;
}

var refExists = false;

function createRef(){
	if (refExists == false){
		refExists = true;
		$(".fs .submit").after("<input type=\"hidden\" name=\"r\" value=\"list\"");
	}
}

function oLogin(returnpath){
	var url = "/ajax.login.asp?returnpath=" + returnpath;
	$("#login").html("<p>Laddar...</p>");
	$("#login").load(url);
	$("#login").fadeIn();
	
	$("#login").ajaxStop(function(){
		$("#login_email").focus();
	});
	
	$("#bgl").css("display", "block");
	return false;
}
function cLogin(){
	$("#login").fadeOut("fast");
	$("#bgl").css("display", "none");
	return false;
}

function oAjaxBox(url){
	$("#bAjax").html("<p>Laddar...</p>");
	$("#bAjax").load(url);
	$("#bAjax").fadeIn();
	return false;
}
function cAjaxBox(){
	$("#bAjax").slideUp("fast");
	return false;
}
function oTOU(id) {
	var url = "/ajax.agreement.asp?id=36";
	$("#TOU").html(loadingMessage);
	$("#TOU").load(url);
	$("#TOU").slideDown("fast");
	return false;
}

function sToplist(listID, category) {
	var url = "/ajax.toplist.asp?list_id=" + listID + "&c=" + category;
	$("#topList ol").before("<span class=\"tab\">Laddar...</span>");
	$("#topList").load(url);
	
	$("#topList").ajaxStop(function(){
		$("#topList ol").css("display", "block");
	});
}

// 3RD PARTY //

function chkNewsletter(f) {
	var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
	if (!email_re.test(f.email.value)) {
		alert("E-postadressen innehöll fel, vänligen korrigera och försök igen.");
		f.email.focus();
		return false;
	}
	return true;
}

// Old TK //

function menuDefault(id) {
	if (document.getElementById) { 
		document.getElementById(id).src = '/img/' + id + '.png'; 
	}
}

function menuHover(id) {
	if (document.getElementById) { 
		document.getElementById(id).src = '/img/' + id + '-markerad.png'; 
	}
}

function openAdvancedSearch() {
	if (document.getElementById) {
		document.getElementById('searchSimple').style.display = 'none';
		document.getElementById('searchAdvanced').style.display = 'block';
	}
}

function closeAdvancedSearch() {
	if (document.getElementById) {
		document.getElementById('searchAdvanced').style.display = 'none';
		document.getElementById('searchSimple').style.display = 'block';
		document.getElementById('affiliation').options[0].selected = true;
		document.getElementById('capacity').value = '';
		document.getElementById('ecological').checked = '';
	}
}
