
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//0 means disabled; 1 means enabled;
var jpopupStatus = 0;

//loading popup
/*
function jpopupLoad(jpopupTitle)
{
	var jpopupTitle;

	if(jpopupStatus==0)
	{
		$j(".jpopup-header-title").html(jpopupTitle);
		$j(".jpopup-background").fadeIn(200);
		$j(".jpopup").fadeIn(200);
		jpopupStatus = 1;
	}
}
*/
function jpopupLoad(jpopupTitle, noIeBackground, altClass)
{
	var jpopupTitle;
	var noIeBackground;
	var altClass;

	if(jpopupStatus==0)
	{
		$j(".jpopup-header-title").html(jpopupTitle);
		if(noIeBackground == 1)
		{
			jQuery.each(jQuery.browser, function(i) {
				if(!$j.browser.msie){
					$j(".jpopup-background").fadeIn(200);
				}
			});
		}
		else
		{
			$j(".jpopup-background").fadeIn(200);
		}
		$j(".jpopup").fadeIn(200);

		if(altClass != '')
		{
			$j(".jpopup").addClass(altClass);
		}

		jpopupStatus = 1;
	}
}

//disabling popup
function jpopupDisable()
{
	if(jpopupStatus==1)
	{
		$j(".jpopup-background").fadeOut(200);
		$j(".jpopup").fadeOut(200);
		jpopupStatus = 0;
	}
}

//centering popup
function jpopupCenter()
{
	//request data for centering
	var windowWidth = $j(window).width();
	var windowHeight = $j(window).height();
	var popupHeight = $j(".jpopup").height();
	var popupWidth = $j(".jpopup").width();
	var scrollTop = $j(window).scrollTop();

	//centering
	$j(".jpopup").css
	(
		{
			"position": "absolute",
			"top": (scrollTop+50),
			"left": windowWidth/2-popupWidth/2
		}
	);
	//only need force for IE
	$j.each
	(
		$j.browser, function(i,val)
		{
			if( i == 'msie' && val == true && $j.browser.version.substr(0,1) <= 8)
			{
				$j(".jpopup-background").css
				(
					{
						"height": windowHeight + scrollTop,
						"display": "none"
					}
				);
			}
		}
	);
}

	/*** selectVariations ***/
	/**
 * simulate a dropdown select box
 */
function selectVariations()
{
	$j('.product-view-variations-list li').click
			(
			function()
			{
				$j("[input[name='products_variations_id']").val($j(this).attr("class"));
				$j('.product-view-variations .newListSelected').html($j(this).html());
				$j(this).parent().hide();
				return false;
			}
	);
}

function jpopupAddProduct(pageId, productsId, bol_productVariation)
{
	var pageId;
	var productsId;
	var bol_productVariation;
	var productsVariationsId;

	if(bol_productVariation)
	{
		//wrong: productsVariationsId = $j("input[@name='products_variations_id']:checked").val();
		//wrong: productsVariationsId = $j("input[name='products_variations_id']").val();
		productsVariationsId = $j("input[name='products_variations_id']:checked").val();
		if(productsVariationsId == 0)
		{
			$j(".product_variations_msg").show();
			$j(".product-view-variations-list").css({'border' : '2px solid #FE0000'});
			productsVariationsId = 0;
			return false;
		}
	}

	jpopupCenter();
	jpopupLoad('', 1);
	jpopupLoading();

	if(productsId > 0)
	{
		var productsIdGet = "&tx_nfcshop_pi1[products_id]="+productsId;
		var productsVariationsIdGet = productsVariationsId > 0 ? "&tx_nfcshop_pi1[products_variations_id]="+productsVariationsId : "";
		$j.ajax
		(
			{
				type: "POST",
				//timeout: 7000,
				data: {},
				url: "index.php?id="+pageId+"&type=576&tx_nfcshop_pi1[action]=AJAX_ADD_CART"+productsIdGet+productsVariationsIdGet,
				success: function(result)
				{
					$j(".jpopup-content").html(result);
					selectVariations();
				},
				error:function (xhr, ajaxOptions, thrownError)
				{
					console.log(xhr.status);
					console.log(ajaxOptions);
					console.log(thrownError);
				}
			}
		);
	}
	return false;
}



function jpopupLoading()
{
	$j(".jpopup-content").html('<div style="text-align: center"><img src="fileadmin/_global/graphics/icons/loading.gif" /></div>');
}




/*** TOOLTIP ***/
//0 means disabled; 1 means enabled;
var jtooltipStatus = 0;

function jtooltipLoad(jtooltipTitle)
{
	var jtooltipTitle;

	if(jtooltipStatus==0)
	{
		$j("#jtooltip-content").html(jtooltipTitle);
		$j("#jtooltip").show();
		jtooltipStatus = 1;
	}
}


function jtooltipPosition(relativeX, relativeY)
{
	$j("#jtooltip").css
	(
		{
			"position": "absolute",
			"top": (relativeY),
			"left": relativeX
		}
	);
}

//disabling tooltip
function jtooltipDisable()
{
	if(jtooltipStatus==1)
	{
		$j("#jtooltip").hide();
		jtooltipStatus = 0;
	}
}




var openLinkStatus = 0;

function openLink(domPath, addParams)
{
	var addParams = addParams ? addParams : '';
	var url = $j(domPath).attr("href") + addParams;

	if(openLinkStatus == 0)
	{
		openLinkStatus = 1;
		//console.log(url);
		window.location = url;
	}
}

function openLinkUrl(linkUrl)
{
	if(openLinkStatus == 0)
	{
		openLinkStatus = 1;
		//console.log(linkUrl);
		window.location = linkUrl;
	}
}

function jSwitchLayerVariations(value)
{
	if ($j(value).is(':hidden') )
	{
		$j(value).show();
	}
	else
	{
		$j(value).hide();
	}
}

function jSwitchLayer(value)
{
	if ($j(value).is(':hidden') )
	{
		$j(value).show();
	}
	else
	{
		$j(value).hide();
	}
}


/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


function LayerOff(id, elements, extension)
{
	for(i=1; i<=elements; i++)
	{
		if(id != i)
		{
			if(document.getElementById('layer-'+i))
			{
				document.getElementById('layer-'+i).style.display = 'none';
				document.getElementById('tab-'+i).className = extension+'-tab';
			}
		}
		else
		{
			document.getElementById('layer-'+id).style.display = 'block';
			document.getElementById('tab-'+id).className = extension+'-tab-active';
		}
	}
}

function array_key_exists ( key, search )
{
	// Checks if the given key or index exists in the array
	// version: 909.322
	// discuss at: http://phpjs.org/functions/array_key_exists
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Felix Geisendoerfer (http://www.debuggable.com/felix)
	if (!search || (search.constructor !== Array && search.constructor !== Object))
	{
			return false;
	}
	return key in search;
}


$j(document).ready(function()
{
	$j(".jquery-button").click
	(
		function()
		{
			jpopupCenter();
			return false;
		}
	);

	$j(".jpopup-close").click
	(
		function()
		{
			jpopupDisable();
			return false;
		}
	);

	$j(".jpopup-background").click
	(
		function()
		{
			jpopupDisable();
			return false;
		}
	);

	//Press Escape event!
	$j(document).keypress
	(
		function(e)
		{
			if(e.keyCode==27 && jpopupStatus==1)
			{
				jpopupDisable();
			}
		}
	);


	/*** TOOLTIP ***/
	$j(".jtooltip").mousemove
	(
		function(e)
		{
			var relativeX = e.pageX + 25;
			var relativeY = e.pageY + 5;

			var tooltipText = this.title;
			$j(this).removeAttr("title");

			jtooltipPosition(relativeX, relativeY);
			jtooltipLoad(tooltipText);

		}
	)

	$j(".jtooltip").mouseout
	(
		function()
		{
			this.title = $j("#jtooltip-content").html();
			jtooltipDisable();
		}
	)

	/*** LOGIN ***/	
	$j('.slide-out-div').css('display','');
	$j('.slide-out-div').tabSlideOut({
		tabHandle: '.handle',
		pathToTabImage: '/fileadmin/_global/graphics/v2/header/slideout_login.png', //path to the image for the tab *required*
		imageWidth: '33px',
		imageHeight: '62px',
		tabLocation: 'right',
		speed: 0,
		action: 'hover', // click oder hover
		topPos: '-62px',
		fixedPosition: false,
		onLoadSlideOut: false
	});

	/*** ****/
	selectVariations();

	/*** flyout navigation PRODUKTE begin***/
	$j(".flyouttree li").hover(
		function () {
			$j(this).children("ul").show();
			var fl = $j(this).parents('.level_2').find('.item_2_childs');
			fl.removeClass();
			fl.addClass("flyout_level_2_highlight");
		},
		function(){
			$j(this).children("ul").hide();
			var fl = $j(this).parents('.level_2').find('.flyout_level_2_highlight');
			fl.removeClass();
			fl.addClass("item_2_childs");
		}
	);
	/*** flyout navigation PRODUKTE end***/

	/*** flyout navigation mainnavigation begin ***/
	$j(".mainnavi li").hover(
		function (e) {
			/*** switch to flyout the navigation to the left site ***/
			var posEvent = e.pageX;
			if(posEvent > 950) $j(this).children("ul").addClass("flyout_left");
			if((posEvent < 950) && $j(this).children("ul").hasClass("flyout_left")) $j(this).children("ul").removeClass("flyout_left");
			/***/
			$j(this).children("div.flyout_level_1").addClass("hover");
			$j(this).children("div.flyout_level_1").children("div.a_level_1").children("a").addClass("hover_color_font");
			setRootline($j(this), $j(this).css('z-index'));

			return true;
		},
		function(){
			/*** remove style if flyout opens to the left site ***/
			if($j(this).children("ul").hasClass("flyout_left")) $j(this).children("ul").removeClass("flyout_left");
			/***/
			$j(this).children("div.flyout_level_1").removeClass("hover");
			$j(this).children("div.flyout_level_1").children("div.a_level_1").children("a").removeClass("hover_color_font");
			unsetRootline($j(this), $j(this).css('z-index'));

			return true;
		}
	);

	$j('.click_level_1').click(function() {
		$j("li.manu_level_1").children("ul").hide();
		alert('Handler for .click() called.');
	});

	function setRootline(e, zIndex) {
    var fl;

		if(zIndex >= 1502){
			fl = e.parents('.level_'+(zIndex-1)).find('.'+(zIndex-1));
			fl.removeClass();
			fl.addClass("flyout_highlight");

			setRootline(zIndex-1);
		}

		if(zIndex == 15){
			fl = e.parents('.level_1').find('.showArrow');
			fl.removeClass();
			fl.addClass("flyout_highlight");
		}

		return true;
	}

	function unsetRootline(e, zIndex) {
    var fl;

		if(zIndex >= 1502){
			fl = e.parents('.level_'+(zIndex-1)).find('.flyout_highlight');
			fl.removeClass();
			fl.addClass("showArrow "+(zIndex-1));

			unsetRootline(zIndex-1);
		}

		if(zIndex == 15){
			fl = e.parents('.level_1').find('.flyout_highlight');
			fl.removeClass();
			fl.addClass("showArrow");
		}

		return true;
	}
	/*** flyout navigation mainnavigation end ***/

});

