﻿/***************************/
//@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 = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popup").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popup").fadeOut("slow");
		popupStatus = 0;
		
		if( $("#select_string").get(0).value > 0 ) { // If a string was selected
			$("#string").get(0).value = 1; // Set 'Bespannen?' to Ja
		} else {
			$("#string").get(0).value = 0; // Set 'Bespannen?' to Nee
		}
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	//centering
	$("#popup").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//Function is called when user selects an option in the string dropdown box
function openPopup(){
	if( $("#string").get(0).value == 1 ) { // If user selects yes
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	} else if( $("#string").get(0).value == 0 ) { // If user selects no, reset values
		$("#selected_weight").get(0).value = 0;
		$("#selected_string").get(0).value = 0;
		// Hide the div element that shows what weight and string were selected
		$("#show_string").hide();
	}
}

//function is called when user has selected a weight and string and hits the 'select' button
function closePopup(){
	var selectWeight = $("#select_weight").get(0).value;
	var selectString = $("#select_string").get(0).value;
	
	if( selectString > 0 ) { // If a string was selected
		$("#selected_weight").get(0).value = selectWeight;
		$("#selected_string").get(0).value = selectString;
		$("#text_weight").get(0).value = $("#select_weight option:selected").text();
		$("#text_string").get(0).value = $("#select_string option:selected").text();
		$("#show_string").show();
	} else {
		$("#selected_weight").get(0).value = 0;
		$("#selected_string").get(0).value = 0;
		$("#show_string").hide();
	}
	
	// Close popup
	disablePopup();
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
