
/***************************/
//@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, 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"
					}
				);
			}
		}
	);
}


function jpopupAddProduct(pageId, productsId, productsOptionsId)
{
	var pageId;
	var productsId;
	var productsOptionsId;
	var attributesId;
	
	if(productsOptionsId)
	{
		attributesId = $j("input[@name='idOS["+productsOptionsId+"]']:checked").val();
		if(attributesId == undefined && productsOptionsId != 'true')
		{
			$j(".product_variations_msg").show();
			$j(".attributesList").css({'border' : '2px solid #FE0000'});
			attributesId = 0;
			return false;
		}
	}
	
	jpopupCenter();
	jpopupLoad('', 1); 
	jpopupLoading();
	
	if(productsId > 0)
	{
		var productsIdGet = "&tx_nfcshop_pi1[products_id]="+productsId;
		var productsVariationsIdGet = attributesId > 0 ? "&tx_nfcshop_pi1[attributes_id]="+attributesId : "";
		var productsOptionsIdGet = productsOptionsId ? "&tx_nfcshop_pi1[products_options_id]="+productsOptionsId : "";
		$j.ajax
		(
			{
				type: "POST",
				//timeout: 7000,
				data: {},
				url: "index.php?id="+pageId+"&type=576&tx_nfcshop_pi1[action]=AJAX_ADD_CART&no_cache=1"+productsIdGet+productsVariationsIdGet+productsOptionsIdGet,
				success: function(result) 
				{
					$j(".jpopup-content").html(result);
					//selectVariations();
				},
				error:function (xhr, ajaxOptions, thrownError)
				{
					console.log(xhr.status);
					console.log(ajaxOptions);					
					console.log(thrownError);
				} 
			}
		);
	/*
		$j.ajax
		(
			{
				type: "POST",
				timeout: 7000,
				data: {products_id: productsId, attributes_id: attributesId, products_options_id:productsOptionsId},
				url: "index.php?eID=tx_nfcshop_add_cart",
				success: function(result) 
				{
					$j(".jpopup-content").html(result);
				},
				error:function (xhr, ajaxOptions, thrownError)
				{
					console.log(xhr.status);
					console.log(thrownError);
				} 
			}
		);
	*/
	}
	
}



function jpopupLoading()
{
	$j(".jpopup-content").html('<div style="text-align: center"><img src="fileadmin/templates/graphics/fahrrad/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 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;
    }
};



$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/templates/graphics/fahrrad/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
	});

	/*** flyout navigation PRODUKTE ***/
	$j(".flyouttree li").hover(
		function () {
			$j(this).children("ul").show();
			var fl = $j(this).parents('.level_1').find('.item_1_childs');
			fl.removeClass();
			fl.addClass("flyout_level_1_highlight");
		},
		function(){
			$j(this).children("ul").hide();
			var fl = $j(this).parents('.level_1').find('.flyout_level_1_highlight');
			fl.removeClass();
			fl.addClass("item_1_childs");
		}
	);

	/*** 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");
			
			if(!$j(this).children("ul").hasClass("manu_level_2"))
			{
				$j(this).children("ul").show();
				setRootline($j(this), $j(this).css('z-index'));
			}			
		},
		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");

			$j(this).children("ul").hide();

			unsetRootline($j(this), $j(this).css('z-index'));
		}
	);

	$j('.click_level_1').click(function() {
		$j(this).parents('li.manu_level_1').find('ul.manu_level_2').show();
	});

	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 ***/

	$j('.hideSellingPoints').hide();

	$j('.moreSellingPoints').click(
		function()
		{
			$j('.hideSellingPoints').toggle();
			$j('.moreSellingPoints').toggle();
			return false;
		}
	);

	$j('span.hideSellingPoints').click(
		function()
		{
			$j('.hideSellingPoints').toggle();
			$j('.moreSellingPoints').toggle();
			return false;
		}
	);

});