/**
 * JS functions
 */

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var iMousePosX = 0;
var iMousePosY = 0;
function getMouseXY(e)
{
	if (IE)
	{
	    iMousePosX = event.clientX + document.body.scrollLeft;
	    iMousePosY = event.clientY + document.body.scrollTop;
	}
	else
	{
	    // grab the x-y pos.s if browser is NS
	    iMousePosX = e.pageX;
	    iMousePosY = e.pageY;
	}
}

function roundNumber(zahl,n_stelle){
   n_stelle = (n_stelle == "" || n_stelle == 0 ? 1 : Math.pow(10,n_stelle));   
   zahl = Math.round(zahl * n_stelle) / n_stelle;   
   return zahl;
}

$(document).ready(function(){
	
	$(".nxsFilterArtName").click(function (){		
		$sValue = $(this).val();
		if ($sValue != "")
		{			
			$("#nxsDeactiveFilter").hide();
			$("#nxsFilterAttributes").hide();
			nxsLoadAttributes($sValue);	
		}
		else
		{
			$("#nxsDeactiveFilter").show();
			$("#nxsFilterAttributes").hide();			
		}
		
	});
	
	/* TOOL-TIPP */
	$('.tooltip').each(function(){
		$(this).hover(function(){
			$('#tooltip_div').css('top', iMousePosY + 32);
			$('#tooltip_div').css('left', iMousePosX + 35);
			$('#tooltip_div').html($(this).attr('nxstext'));
			$('#tooltip_div').show();
		}, function(){
			$('#tooltip_div').hide();
		});
		$(this).mousemove(function(){
			$('#tooltip_div').css('top', iMousePosY + 32);
			$('#tooltip_div').css('left', iMousePosX + 35);
		});
	});
	
	
	$("#nxsDetRegLink1").click(function (){
		$("#nxsDetRegLink1").removeClass('register_off');
		$("#nxsDetRegLink1").addClass('register_on');
		
		$("#nxsDetRegLink2").removeClass('register_on');
		$("#nxsDetRegLink2").addClass('register_off');
		
		$("#nxsDetRegLink3").removeClass('register_on');
		$("#nxsDetRegLink3").addClass('register_off');
		
		$("#nxsDetRegLink4").removeClass('register_on');
		$("#nxsDetRegLink4").addClass('register_off');
		
		$("#prdouktbeschreibung").show();
		$("#spezifikationen").hide();
		$("#zubehoer").hide();
		$("#alternative_produkte").hide();			
	});
	
	$("#nxsDetRegLink2").click(function (){
		$("#nxsDetRegLink1").removeClass('register_on');
		$("#nxsDetRegLink1").addClass('register_off');
		
		$("#nxsDetRegLink2").removeClass('register_off');
		$("#nxsDetRegLink2").addClass('register_on');
		
		$("#nxsDetRegLink3").removeClass('register_on');
		$("#nxsDetRegLink3").addClass('register_off');
		
		$("#nxsDetRegLink4").removeClass('register_on');
		$("#nxsDetRegLink4").addClass('register_off');
		
		$("#prdouktbeschreibung").hide();
		$("#spezifikationen").show();
		$("#zubehoer").hide();
		$("#alternative_produkte").hide();				
	});
	
	$("#nxsDetRegLink3").click(function (){
		$("#nxsDetRegLink1").removeClass('register_on');
		$("#nxsDetRegLink1").addClass('register_off');
		
		$("#nxsDetRegLink2").removeClass('register_on');
		$("#nxsDetRegLink2").addClass('register_off');
		
		$("#nxsDetRegLink3").removeClass('register_off');
		$("#nxsDetRegLink3").addClass('register_on');
		
		$("#nxsDetRegLink4").removeClass('register_on');
		$("#nxsDetRegLink4").addClass('register_off');
		
		$("#prdouktbeschreibung").hide();
		$("#spezifikationen").hide();
		$("#zubehoer").show();	
		$("#alternative_produkte").hide();				
	});	
	
	$("#nxsDetRegLink4").click(function (){
		$("#nxsDetRegLink1").removeClass('register_on');
		$("#nxsDetRegLink1").addClass('register_off');
		
		$("#nxsDetRegLink2").removeClass('register_on');
		$("#nxsDetRegLink2").addClass('register_off');
		
		$("#nxsDetRegLink3").removeClass('register_on');
		$("#nxsDetRegLink3").addClass('register_off');
		
		$("#nxsDetRegLink4").removeClass('register_off');
		$("#nxsDetRegLink4").addClass('register_on');
		
		$("#prdouktbeschreibung").hide();
		$("#spezifikationen").hide();
		$("#zubehoer").hide();	
		$("#alternative_produkte").show();				
	});	
	
	$("#nxsDetProdAmount").keyup(function (){
		$iPrice = $(this).attr('nxsPrice');
		$iAmount = $(this).val();
		if ($iAmount != "") 
		{
			if ($iAmount > 0 && $iPrice > 0) 
			{
				$iNewPrice = $iPrice * $iAmount;
				
				aTmp = parseFloat($iNewPrice);
				aTmp = roundNumber(aTmp, 2);
				$iNewPrice = aTmp.toString();
				$iNewPrice = $iNewPrice.replace(".", ",");
				
				$iLength = $iNewPrice.length;
				$iIndexOf = $iNewPrice.indexOf(",");
				if (!$iIndexOf || $iIndexOf < 1) 
					$iIndexOf = $iLength;
				$iDiff = $iLength - $iIndexOf;
				if ($iDiff == 2) 
					$iNewPrice = $iNewPrice + "0";
				else if ($iDiff == 1) 
					$iNewPrice = $iNewPrice + "00";
				else if ($iDiff < 1) 
					$iNewPrice = $iNewPrice + ",00";	
				
				$iIndexOf = $iNewPrice.indexOf(",");	
				$iDiff = $iLength - $iIndexOf;
				if ($iDiff > 3)
				{
					$iNewPrice = $iNewPrice.substr(0, $iIndexOf+3);
				}
				
				$('#nxs_gesamtpreis').html($iNewPrice);
			}
		}
	});
	
	$("#nxsDetProdAmount").change(function(){
		$iPrice = $(this).attr('nxsPrice');
		$iAmount = $(this).val();
		if ($iAmount != "") {
			if ($iAmount > 0 && $iPrice > 0) {
				$iNewPrice = $iPrice * $iAmount;
				
				aTmp = parseFloat($iNewPrice);
				aTmp = roundNumber(aTmp, 2);
				$iNewPrice = aTmp.toString();
				$iNewPrice = $iNewPrice.replace(".", ",");
				
				$iLength = $iNewPrice.length;
				$iIndexOf = $iNewPrice.indexOf(",");
				if (!$iIndexOf || $iIndexOf < 1) 
					$iIndexOf = $iLength;
				$iDiff = $iLength - $iIndexOf;
				if ($iDiff == 2) 
					$iNewPrice = $iNewPrice + "0";
				else if ($iDiff == 1) 
					$iNewPrice = $iNewPrice + "00";
				else if ($iDiff < 1) 
					$iNewPrice = $iNewPrice + ",00";	
				
				$iIndexOf = $iNewPrice.indexOf(",");	
				$iDiff = $iLength - $iIndexOf;
				if ($iDiff > 3)
				{
					$iNewPrice = $iNewPrice.substr(0, $iIndexOf+3);
				}
				
				$('#nxs_gesamtpreis').html($iNewPrice);
				
			}
		}
	});
	
	$("#test_toBasket").click(function(){
		$iAmount = $("#nxsDetProdAmount").val();
		$iAmount = parseInt($iAmount);
		if (isNaN($iAmount)) {
			alert("Bitte geben Sie eine gültige Anzahl ein");
			return false;
		}
		if ($iAmount == "" || $iAmount < 1) {
			alert("Bitte geben Sie die Anzahl ein");
			return false;
		}
		$("#nxsBasketForm").submit();
	});
	
	$(".nxs_agb").css({"cursor":"pointer", "text-decoration":"underline"}).click(function(){ 
		
		window.open($nxsAGBLink, '_blank');
		
	});
	
	$iNxsArticlePages = 0;
	$iNxsActivePage = 'article_page_0';
	
	$(".articlepage").each(function (){
		$(this).hide();
		$iNxsArticlePages++;
	});	
	$("#article_page_0").show();
	
	var $sLastPageId = "";
	if ($iNxsArticlePages > 1) {
		$("#pageNavigation").append('<b>Seiten: </b> <a class="artPageLink" page="article_page_0"><<</a> | ');
		$("#pageNavigation2").append('<b>Seiten: </b> <a class="artPageLink" page="article_page_0"><<</a> | ');
		$iPageNr = 1;
		$(".articlepage").each(function (){
			$iPageId = $(this).attr('id');
			if ($iPageNr == 1)
				$("#pageNavigation").append('<a class="artPageLink nav'+$iPageId+'" page="'+$iPageId+'" style="font-weight: bold;">'+$iPageNr+'</a> | ');
			else
				$("#pageNavigation").append('<a class="artPageLink nav'+$iPageId+'" page="'+$iPageId+'">'+$iPageNr+'</a> | ');
			if ($iPageNr == 1)
				$("#pageNavigation2").append('<a class="artPageLink nav'+$iPageId+'" page="'+$iPageId+'" style="font-weight: bold;">'+$iPageNr+'</a> | ');
			else
				$("#pageNavigation2").append('<a class="artPageLink nav'+$iPageId+'" page="'+$iPageId+'">'+$iPageNr+'</a> | ');
			$sLastPageId = $iPageId;
			$iPageNr++;
		});
		$("#pageNavigation").append('<a class="artPageLink" page="'+$sLastPageId+'">>></a>');
		$("#pageNavigation2").append('<a class="artPageLink" page="'+$sLastPageId+'">>></a>');
	}
	
	$(".artPageLink").css('cursor', 'pointer');
	$(".artPageLink").click(function (){		
		$("#"+$iNxsActivePage).hide();
		$('.nav'+$iNxsActivePage).css('font-weight', 'normal');
		$iNxsActivePage = $(this).attr('page');
		$("#"+$iNxsActivePage).show();
		$('.nav'+$iNxsActivePage).css('font-weight', 'bold');	
	});
	
	
	var $sGermanyOxId = 'a7c40f631fc920687.20179984';
	window.checkCountryOnUserpage = function() {
		if ($("#deliveryCountryMessage").html() != null) {
			if ($("#useShipAddr").val() == "hide") {				
				if ($("#oxuser__oxcountryid").val() != "a7c40f631fc920687.20179984" && $("#oxuser__oxcountryid").val() != "") {
					alert($("#deliveryCountryMessage").html());
				}				
			}
			else {
				if ($("#oxaddress__oxcountryid").val() != "a7c40f631fc920687.20179984" && $("#oxaddress__oxcountryid").val() != "") {
					alert($("#deliveryCountryMessage").html());
				}
			}
		}
	}
	
	$("#shipAddr").click(function (){
		setTimeout("checkCountryOnUserpage()", 200);
	});
	$("#oxuser__oxcountryid").change(function (){
		checkCountryOnUserpage();
	});

	$("#oxaddress__oxcountryid").change(function (){
		checkCountryOnUserpage();
	});
	
	checkCountryOnUserpage();
	
	
	var $sHideObject = "";
	var $blRealyHide = false;
	
	window.hideSubBox = function() {
		if ($blRealyHide)
			$sHideObject.hide();
	}
	
	$("#sel_branche").change(function () {
		$sVal = $(this).val();
		if ($sVal == "Sonstige") {
			$("#branche_other").show();
		} else {
			$("#branche_other").hide();
			$("#branche_other").val("");
		}
	});
	
});




