/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

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

function loadPopup (theElement, theUrl) {
	if ($(theElement).text() == '') {
		$(theElement).load(theUrl + '&type=1', function () {
			initForm(theElement);
		});
	}
	popupStatus = theElement;
}

function initForm (theElement) {
	$("#popupFormContact").ajaxForm({
		type: 'POST',
		target: theElement,
		success: function () {
			$(theElement).fadeIn();
			initForm(theElement);
		}
	});
	$("#resetContact").click(function() {
		disablePopup(popupStatus, true);
	});
}

//loading popup with jQuery magic!
function showPopup(theElement) {
	//loads popup only if it is disabled
	if (popupStatus == theElement) {
		$(theBackground).css({
			"opacity": "0.7"
		});
		$(theBackground).fadeIn(400, function () {
			$(theElement).fadeIn('slow');
		});
	}
}

//disabling popup with jQuery magic!
function disablePopup(theElement, clear) {
	//disables popup only if it is enabled
	if (popupStatus == theElement) {
		$(theElement).fadeOut('slow', function () {
			$(theBackground).fadeOut();
			if (clear) {
				$(theElement).text('');
			}
		});
		popupStatus = '';
	}
}

//centering popup
function centerPopup(theElement){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(theElement).height();
	var popupWidth = $(theElement).width();
		
	//centering
	$(theElement).css({
		"position": "absolute",
		"top": 100,
		"left": (windowWidth-popupWidth)/2
	});
	//only need force for IE6
	$(theBackground).css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {
	
	//LOADING POPUP
	//Click the button event!
	$(".popupButton").click(function() {
		theBackground = "#backgroundPopup";
		var theParts = $(this).attr("href").split('#');
		loadPopup('#' + theParts[1], theParts[0]);
		//centering with css
		centerPopup('#' + theParts[1]);
		//load popup
		showPopup('#' + theParts[1]);
		
		return false;
	});
	
	$(".popupButtonPayment").click(function() {
		popupStatus = ".popupPayment";
		theBackground = "#backgroundPopupPayment";
		centerPopup(popupStatus);
		showPopup(popupStatus);
	});
	
	$(".popupPaymentClose").click(function() {
		theBackground = "#backgroundPopupPayment";
		disablePopup(popupStatus, false);
	});
	
	//CLOSING POPUP
	//Click the x event!
	$(".popupContactClose").click(function() {
		theBackground = "#backgroundPopup";
		disablePopup(popupStatus, true);
	});
	//Click out event!
	$("#backgroundPopup").click(function() {
		theBackground = "#backgroundPopup";
		disablePopup(popupStatus, true);
	});
	$("#backgroundPopupPayment").click(function() {
		theBackground = "#backgroundPopupPayment";
		disablePopup(popupStatus, false);
	});
	$("#popupOkClose").load(function () {
		theBackground = "#backgroundPopup";
		window.setTimeout(disablePopup(popupStatus, true), 2000);
	});
	//Press Escape event!
//	$(document).keypress(function(e) {
//		if(e.keyCode==27 && popupStatus==1){
//			disablePopup();
//		}
//	});

});


// Funktionen zum Vertauschen der Eingabezeilen fuer Strasse und Hausnummer im
// Formular zur Benutzerregistrierung und Accountbearbeitung

function swapElements(e1, e2) {
  if (e1 && e2 && e1 != e2) {
     var p1 = e1.parentNode, p2 = e2.parentNode, e1n = e1.nextSibling;
     p1.removeChild(e1);
     p2.replaceChild(e1, e2);
     if (e1n) {
       p1.insertBefore(e2, e1n);
     } else {
       p1.appendChild(e2);
     }
  }
}

function swapRows (theText, e1, e2) {
	switch (theText) {
		case "USA":
		case "CAN":
		case "GBR":
		case "IRL":
		case "Ireland":
		case "United Kingdom":
		case "United States":
		case "Canada":
			swapElements(e1, e2);
			break;
	}
}

