/* Onload */
$(document).ready (

	function ()
	{
		$(".nojs").hide();
		
		/* Standortauswahl Weltkarte */
		$("#world area").mouseover(
	      function () {
	      	$("#asia-active").hide();
	      	$("#africa-active").hide();
	      	$("#southamerica-active").hide();
	      	$("#europe-active").hide();
	      	$("#northamerica-active").hide();
	      	$("#australia-active").hide();
	      	$("#southeastasia-active").hide();
	      	$("#middleeast-active").hide();
	      	$("#middleamerica-active").hide();
	      	$("#" + $(this).attr("id") + "-active").show();
	      }
	  );
	
		$(".continent").mouseout(
	      function () {
	      	$("#" + $(this).attr("id")).hide();
	      }
	  );
	  
		/* Standort-Selectbox chooseregion */
		$("#chooseregion select").change(function () {
			if($("#chooseregion select option:selected").val() != "")
			{
				self.location.href=$("#chooseregion select option:selected").val();
			}
		});
		$("#chooselocation select").change(function () {
			if($("#chooselocation select option:selected").val() != "")
			{
				self.location.href=$("#chooselocation select option:selected").val();
			}
		});
		
		/* Worldwide */
			$("#worldwide").submit(function() {
				if($("#worldwide_location_header option:selected").val() == 'default')
				{
					return false;
				}
				return true;
			});
	  
	  /** Linke Spalte Kontinentauswahl absenden **/
		$("#worldwide_location").change (
			function () {
				if($("#worldwide_location option:selected").val() != 'default')
					$("#box_worldwide").submit();
			}
		);
		
		/** Linke Seite Kontinentauswahl Button verstecken **/
		$("#box_worldwide > fieldset > input.submit").css("display", "none");
		
		/** Header Kontinentauswahl absenden **/
		$("#worldwide_location_header").change (
			function () {
				if($("#worldwide_location_header option:selected").val() != 'default')
					$("#worldwide").submit();
			}
		);		
		
		
		
		
		
		
		
		
		/** Infobestellung **/
		$(".infoitem").click(function () 
		{
	  	if($(this).attr('checked')) {
	  		$.post(ajaxRequestInfowarenkorb, { action: "add", id: $(this).attr("id"), name: $(this).attr("name")},
			  	function(data) {
			  		$("#infowarenkorb").removeClass('ajaxloadingbigbox');
						$("#infowarenkorb").html(data);
			    	//alert("Data Loaded: " + data);
			    	$("#infowarenkorb a").click(function () {
					  	$(this).addClickEvent();
					  	return false;
					  });
			  });
	  	}
	  	else
	  	{
	  		$.post(ajaxRequestInfowarenkorb, { action: "remove", id: $(this).attr("id")},
			  	function(data) {
			  		$("#infowarenkorb").removeClass('ajaxloadingbigbox');
						$("#infowarenkorb").html(data);
			    	//alert("Data Loaded: " + data);
			    	$("#infowarenkorb a").click(function () {
			    		$(this).addClickEvent();
			    		return false;
					  });
			  });
	  	}
	  });

		
		
		
		
		jQuery.fn.extend({
			addClickEvent: function() {
				var parts = $(this).attr("id").split("_");

				if(document.getElementById(parts[1])) {
					$("#" + parts[1]).attr('checked', 0);
				}

				$("#infowarenkorb").html('');
				$("#infowarenkorb").addClass('ajaxloadingbigbox');
				$.post(ajaxRequestInfowarenkorb, { action: "remove", id: parts[1]},
			  	function(data) {
			  		$("#infowarenkorb").removeClass('ajaxloadingbigbox');
						$("#infowarenkorb").html(data);
			    	//alert("Data Loaded: " + data);
			    	$("#infowarenkorb a").click(function () {
			    		$(this).addClickEvent();
			    		return false;
					  });
			  });

			}
		});

		


		/** COOKIECHECK **/
		function setCookie(cookieName,cookieValue,nDays)
		{
		 var today = new Date();
		 var expire = new Date();
		 if (nDays==null || nDays==0) nDays=1;
		 expire.setTime(today.getTime() + 3600000*24*nDays);
		 document.cookie = cookieName+"="+escape(cookieValue)
		                 + ";expires="+expire.toGMTString();
		}
		
		function getCookie(cookieName)
		{
		 var theCookie=""+document.cookie;
		 var ind=theCookie.indexOf(cookieName);
		 if (ind==-1 || cookieName=="") return "";
		 var ind1=theCookie.indexOf(';',ind);
		 if (ind1==-1) ind1=theCookie.length;
		 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
		}
		
		function checkCookies()
		{
			setCookie("CookieTest", "OK");
			return !getCookie("CookieTest") ? false : true;
		}
		
		/* Keine Cookies aktiviert */
	  if(!checkCookies())
	  {
	  	$("#nocookies").show();
	  }		

	  /* Klick auf Link zur Loginbox */
	  $("#openloginbox").click(function () {
	  	$.prompt(createLoginBox(login_redirect, login_action, login_name, login_behavior, login_url),{show:'show', submit: mysubmitfunc, buttons: {_:false}});
	  });
	
	  try
		{
			if(document.getElementById('openloginbox'))
			{
				$.prompt(createLoginBox(login_redirect, login_action ,login_name, login_behavior, login_url),{show:'show', submit: mysubmitfunc, buttons: {_:false}});
			}
		}
		catch(E) {}		
	
	}
);

/* Loginbox ( jsLogin.js)*/
function createLoginBox(redirect, action, title, behavior, requesturl)
{
	var txt = '';
	txt+= '<form action="' + action +'" method="post" id="loginform" class="loginbox">';
	txt+= '	<h2>' + title + '</h2>';
	txt+= '	<fieldset>';
	txt+= '		<input type="hidden" name="login_url" value="' + requesturl + '" />';
	txt+= '		<input type="hidden" name="redirect" value="' + redirect + '" />';
	if(behavior == 'email')
	{
		txt+= '		<p>' + langLoginEnterEmail + '</p>';
		txt+= '		<input type="hidden" name="login_behavior" id="login_behavior" value="' + behavior + '" />';
		txt+= '		<label for="login_email" id="login_email_label" class="standardlabel">' + langLoginEmail + ':</label>';
		txt+= '		<input type="text" name="login_email" id="login_email" class="text" />';
	}
	else
	{
		if(behavior == 'emailname')
		{
			txt+= '		<p>' + langLoginEnterEmailAndName + '</p>';
			txt+= '		<input type="hidden" name="login_behavior" id="login_behavior" value="' + behavior + '" />';
			txt+= '		<label for="login_name" id="login_name_label" class="standardlabel">' + langLoginName + ':</label>';
			txt+= '		<input type="text" value="" name="login_emailname" id="login_name" class="text" />';
			txt+= '		<div class="clear"></div>';
			txt+= '		<label for="login_email" id="login_email_label" class="standardlabel">' + langLoginEmail + ':</label>';
			txt+= '		<input type="text" name="login_email" id="login_email" class="text" />';
		}
		else
		{
			txt+= '		<p>' + langLoginExtranet + '</p>';
			txt+= '		<input type="hidden" name="login_behavior" id="login_behavior" value="' + behavior + '" />';
			txt+= '		<label for="login_username" id="login_username_label" class="standardlabel">' + langLoginUser + ':</label>';
			txt+= '		<input type="text" value="" name="login_username" id="login_username" class="text" />';
			txt+= '		<div class="clear"></div>';
			txt+= '		<label for="login_password" id="login_password_label" class="standardlabel">' + langLoginPassword + ':</label>';
			txt+= '		<input type="password" name="login_password" id="login_password" class="text" />';
		}
		txt+= '		<div class="clear"></div>';
		txt+= '	</fieldset>';
		txt+= '</form>';
	}
	return txt;
}

function mysubmitfunc(v,m)
{
	returnValue = true;

	/* Unterscheidung der Validierung nach Loginmethode */
	if($("#login_behavior").val() == "email")
	{
		if($("#login_email").val() == "")
		{
			$("#login_email_label").css("color", "#f00");
			returnValue = false;
		}
		else
		{
			$("#login_email_label").css("color", "#666");
		}
	}
	else
	{
		if($("#login_username").val() == "")
		{
			$("#login_username_label").css("color", "#f00");
			returnValue = false;
		}
		else
		{
			$("#login_username_label").css("color", "#666");
		}

		if($("#login_password").val() == "")
		{
			$("#login_password_label").css("color", "#f00");
			returnValue = false;
		}
		else
		{
			$("#login_username_label").css("color", "#666");
		}
	}

	/* Das Formular wird versendet, wenn die Pflichtfelder gesetzt sind */
	if(returnValue)
	{
		$("#loginform").submit();
	}

	return returnValue;
}		