﻿// 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 oNewsletter() {
	$("#newsletterName").slideDown("fast");
}

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");
	});
}

// Old TK //

function showMap(county,name) {
	if (document.getElementById) { 
		document.getElementById('swedenMap').src = '/img/sverigekarta-'+county+'.png'; 
		document.getElementById('swedenMap').alt = name; 
	}
}

function hideMap() {
	if (document.getElementById) { 
		document.getElementById('swedenMap').src = '/img/sverigekarta.png'; 
		document.getElementById('swedenMap').alt = 'Sverige'; 
	}
}

function confirmClick(url,msg) {
	if(confirm(msg)) {
		document.location = url;
	}
	else {
	}
}

function toggleDisplay(val) {
	if (document.getElementById) {
		if (val==0) {
			document.getElementById('mainInformation').style.display = 'none';
			document.getElementById('deleteHide').value = 1;
		} 
		else {
			document.getElementById('mainInformation').style.display = 'block';
			document.getElementById('deleteHide').value = 0;
		}
	}
}

function closeBox(id) {
	if (document.getElementById) {
		document.getElementById(id).style.display = 'none';
	}
}

function openXnWindow(url) {
	window.open(url,'xn','width=800, height=700, scrollbars=yes, resizable=yes, status=yes, location=no, toolbar=no, menubar=no, screenX=0, screenY=200, left=0, top=200');
	return false;
}

function openFeedback(url, item_id) {
	window.open('/feedback.asp?item_id=' + item_id + '&url=' + url,'Feedback','width=430, height=500, scrollbars=yes, resizable=yes, status=yes, location=no, toolbar=no, menubar=no');
}