/* Suckerfich Dropdown für jQuery */
jQuery.fn.suckerfish = function() {  
	// if ($.browser.msie) {
		var parent = this.find("li");
    	this.find("li").hover(function(e){  
    		$(this).addClass('over');
		},
		function() {
        	$(this).removeClass('over');  
		});
	// }  
}

/* Aufklappende Erweiterte Suche für jQuery */
function sfSearch(){
	$("a#erweiterte_suche_onclick").click(function () { 
		$("div#erweiterte_suche").toggle();
    });
}

function remove_search() {
	document.getElementById("erweiterte_suche").style.display = "none";
}

/* Warenkorb Tooltip */
function sfAccountTooltip(){

	$("a#open_account_tooltip").click(function () { 
		return false;
    });
    
    $("a#open_account_tooltip").bind("mouseenter",function(){
      $('div#account_tooltip').show();
    });
    
    $("div#account_tooltip").bind("mouseleave",function(){
      $('div#account_tooltip').hide();
    });

}

/* Schließen Button der Subnavi für jQuery */
function sfCloseSubnavi(){
	$("img.close_subnavi").click(function () { 
		$("#nav li").removeClass('over');  
    });
}

/* Aktiviert dir Filter bei den Checkboxen im Navigations Dropdown/Tooltipmenü */
function sfAttributeSelectorFilter(){
	$("li.vorauswahl input").click(function () { 

		var parentElement = $(this).parent().parent().parent();
		var attribute = "&" + $(this).attr("name") + "=" + $(this).attr("value");
	
		if ($(this).attr("checked")) {;
			parentElement.find("a").each(function(){
		 		$(this).attr("href", $(this).attr("href") + attribute + "&fnc=executefilter");
 			});
		} else {
			parentElement.find("a").each(function(){
		 		$(this).attr("href", $(this).attr("href").replace(attribute  + "&fnc=executefilter", ''));
 			});
		}
		
	});

}

/* Hover-Vergrößerung der Detailbilder für jQuery */
function sfHoverImages(){
	$("a.sfHover").click(function () { 
      return false;
    });
	$("a.sfHover").hover(
      function () {
        $("img#product_img").attr("src", $(this).attr("href"));
      }, 
      function () {
        $("img#product_img").attr("src", $(this).attr("href"));
      }
    );
}

/* Marken ausklappen für jQuery */
function sfBrands(){
	$("a#marken_show_hide").click( function(){ 
		$("div#topmarken").slideToggle("medium");
		$(this).toggleClass("link_marken_open");
		$.getJSON("/index.php?cl=dropdown&sid=" + sid);
		return false;
	});
}


/* Warenkorb größen Hinweis */
function sfBasket(){
		
	if ($("select#inc_product_variants_drop")) {
		
		if ($("select#inc_product_variants_drop").val() == "") {
			$(".tobasket_form").submit(function () {
				return false;
			}); 
			
			    $("#link_warenkorb").hover(
      function () {
        sfListTooltipsBasket("link_warenkorb");
        $('#basket_tooltip').bgiframe();
      }, 
      function () {
        $('#basket_tooltip').remove();
      }
    );
	
			
			
			$("#link_warenkorb").css("cursor", "not-allowed");
		}
		
		$("select#inc_product_variants_drop").change( function() {
			if ($("select#inc_product_variants_drop").val() == "") {
				$(".tobasket_form").submit(function () {
					return false;
				});
				
							    $("#link_warenkorb").hover(
      function () {
        sfListTooltipsBasket("link_warenkorb");
        $('#basket_tooltip').bgiframe();
      }, 
      function () {
        $('#basket_tooltip').remove();
      }
    );
				$("#link_warenkorb").css("cursor", "not-allowed");

			}
		});
	
	}

}

function sfShippingCosts() {
	$("a#fancy_shipping_link").fancybox({
		'zoomSpeedIn':	0, 
		'zoomSpeedOut':	0, 
		'overlayShow':	true,
		'overlayOpacity': 0.85
	});
}


/* Tooltips der Listen, basiert auf JTIP von Cody Lindley (http://www.codylindley.com) */

sfListTooltipsClosed = true;

function sfListTooltips(){
	$("img.plus").click(function(){
		if (sfListTooltipsClosed) {
			sfListTooltipsClosed = false;
			$('#JT').remove();
			sfListTooltipsShop($(this).attr("id"));
			sfListTooltipsClosed = true;
			//console.log($(this).attr("id"));
		}
	});
}

function sfListTooltipsRemove(){
$("#JT").bind("mouseleave",function(){
      $('#JT').remove();
    });
}

function remove() {
	document.getElementById("JT").style.display = "none";
}

function sfListTooltipsBasket(linkId){
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId); //set y position
	$("body").append('<div id="basket_tooltip"></div>'); //right side
	var arrowOffset = getElementWidth(linkId) - 280;
	
	var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	// Wenn das Tooltip außerhalb des aktuellen Viewports erscheinen würde, zur andere Seite ausklappen, document.body.clientWidth = IE, window.innerWidth = Rest
	if (clickElementx + 266 > window.innerWidth || clickElementx + 266 > document.body.clientWidth) {
		clickElementx = clickElementx - 266 - arrowOffset;
	}
	$("#basket_tooltip").append('<h1 class="basket">Bitte wähle eine Größe aus!</h1>');
	$('#basket_tooltip').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#basket_tooltip').show();
}


function sfListTooltipsShop(linkId){
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) + 30; //set y position
	$("body").append('<div id="JT"></div>'); //right side
	var arrowOffset = getElementWidth(linkId) + 16;
	
	var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
	// Wenn das Tooltip außerhalb des aktuellen Viewports erscheinen würde, zur andere Seite ausklappen, document.body.clientWidth = IE, window.innerWidth = Rest
	if (clickElementx + 266 > window.innerWidth || clickElementx + 266 > document.body.clientWidth) {
		clickElementx = clickElementx - 266 - arrowOffset;
	}
	$("#JT").append('<div class="loading">');
	$("#JT").append('<h1 class="loading">Tooltip-Ansicht wird geladen</h1>');
	$("#JT").append('<img class="loading" src="/out/basic/img/loadingAnimation.gif" />');
	$("#JT").append('</div>');
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').load("/index.php?cl=detailstooltip&anid="+ linkId.replace("oxnid", "") + "&sid=" + sid);
	$('#JT').show();
	$('#JT').bgiframe();
}

// get width
function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

// Get an object left position from the upper left viewport corner
function getAbsoluteLeft(objectId) {
	o = document.getElementById(objectId); 
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

// Get an object top position from the upper left viewport corner
function getAbsoluteTop(objectId) {
	
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

// blocks other events
function blockEvents(evt) {
	if(evt.target){
		evt.preventDefault();
	}else{
		evt.returnValue = false;
	}
}

// blocks other events
function HighlightMessagebox() {
	$("div.msgbox").animate({ 
        opacity: 0.7
      }, 600 );
      $("div.msgbox").animate({ 
        opacity: 1.0
      }, 600 );
      	$("div.msgbox").animate({ 
        opacity: 0.7
      }, 600 );
      $("div.msgbox").animate({ 
        opacity: 1.0
      }, 600 );
}

// blocks other events
function FadeOutMessagebox() {
	$("div.msgbox").animate({ 
        height: "0px",
        opacity: 0.0
      }, 1500 );
}

/* Tooltips Ende */


function check_agb() {
	$('input.ord_agb').click(function(){
		var checkedValue = $(this).attr('checked') ? 'checked' : '';
		$('input.ord_agb:checkbox').attr('checked', checkedValue);
	});
}

/* Ruft die ganzen Funktionen nach dem Laden der Seite auf, fancy ist das Iframe auf der Detailseite */
$(document).ready(function(){
	// sfBrands();
	sfVendorOverview();
	sfSearch();
	sfListTooltips();
	HighlightMessagebox();
	sfCloseSubnavi();
	sfAttributeSelectorFilter();
	window.setTimeout("FadeOutMessagebox()", 5000);
	$("a.fancy").fancybox({
		'zoomSpeedIn':	200, 
		'zoomSpeedOut':	200, 
		'overlayShow':	true,
		'overlayOpacity': 0
	});
	$("#nav").suckerfish();  
	sfHoverImages();
	sfBasket();
	sfShippingCosts();
	sfAccountTooltip();
	check_agb();
	$('ul#nav ul').bgiframe();
	$('div#erweiterte_suche').bgiframe();
	$('div.marken_overview').bgiframe();
	
	window.setTimeout("cycle()", 2500);
	
});

function cycle() {
	$('#cycle_gallery div').cycle({ 
	    fx:			'cover', 
	    easing:		'easeOutCubic', 
	    speed:		1000, 
		pause: 1,
	    timeout:	5000 
	});
}

/* trusted shop */
function trustedShop() {
	window.open("","fensterauf","width=800, height=600, scrollbars=yes, resizable=yes, menubar=no, location=no");
}

/* SUCHABFRAGEN */
function check_search () {
 if(document.getElementById('searchparam').value=='suchbegriff')
 	{
		document.getElementById('searchparam').value='';
		return true;
	}
}

/* Aufklappende Marken für jQuery */
function sfVendorOverview(){
	$("a#marken_show_hide").click(function () { 
		$(this).toggleClass("link_marken_open");
		$("div.marken_overview").toggle();
		return false;
    });
    $(".marken_overview img.close_button").click(function () { 
    	$(this).toggleClass("link_marken_open");
		$("div.marken_overview").toggle();
		return false;
    });
}
