/**
 * Select/deselect any matching checkboxes, radio buttons or option elements.
 */
$.fn.selected = function(select) {
	if (select == undefined) select = true;
	return this.each(function() {
		var t = this.type;
		if (t == 'checkbox' || t == 'radio')
			this.checked = select;
		else if (this.tagName.toLowerCase() == 'option') {
			var $sel = $(this).parent('select');
			if (select && $sel[0] && $sel[0].type == 'select-one') {
				// deselect all other options
				$sel.find('option').selected(false);
			}
			this.selected = select;
		}
	}); 
};

function initTheInitProcess() {
	s7ViewerInit();
	/*if ( $.browser.msie) {
		if (!$('#izView').is('div')) {		
			//alert($('#izView').is('div'));
			initTheInitProcess();
		} else {
			//alert($('#izView').is('div'));
		}
	}*/
};

/*
 * Makes WaitingLayer Visible
 * Author: Marvin Kerkhoff
 */

function showWaitingLayer() {
	$('#webServiceWaitingLayer').delay(2000).fadeIn(400);
};

function hideWaitingLayer() {
	/*alert("Queue:" + jQuery.queue($("#webServiceWaitingLayer")));*/
	$('#webServiceWaitingLayer').clearQueue();
	$('#webServiceWaitingLayer').stop();
	$('#webServiceWaitingLayer').fadeOut(400);
};

/*
 * Makes Dropdown visible if Javascript = on
 * Author: Marvin Kerkhoff
 */

function updateAJAXComponent() {
	
	
	  $(".showWaitingLayer").click(function() {
		  showWaitingLayer();
		  return true;
	  });
	  
	  $("#webServiceWaitingLayer button").click(function() {
		 hideWaitingLayer(); 
	  });
	
	  hideWaitingLayer();

	  $('div.dropdown').each(function() {
	    $(this).removeClass("hide");
	  });
	
	  $('div.filter').each(function() {
	    $(this).removeClass("hide");
	  });
	
	  $("div.dropdown").click(function() {
	
			//Following events are applied to the options itself (moving options up and down)
			$(this).find(".items").slideDown('medium'); //Drop down the options on click
	
			$(this).parent().hover(function() {
				$(this).parent().find("div.dropdown .items").slideUp('medium'); //When the mouse hovers out of the options, move it back up
			});
	
	    return false;
	    
	  });
	
	  $("div.dropdown ul a").click(function() {
	    $(this).parents(".items").prev(".button").find("span").html( $(this).html() );
	    $(this).parents(".items").slideUp('medium');
	    $(this).parents(".items").next('input').val($(this).html());
	
	    if ($(this).attr("href") != "#") {
	      if ($(this).attr("href").charAt(0) == "#") {
	    	  $(this).parents(".items").next('input').val($(this).attr("href").substr(1,$(this).attr("href").length));
	    	  $(this).parents(".items").next('input').trigger('change');
	      } else {
	    	  window.location.href = $(this).attr("href");
	      }
	    }
	    return false;
	  });
}


function simple_tooltip(target_items, name){
    $(target_items).each(function(i){
      //$("body").append("<div class='tooltipContent "+name+"' id='"+name+i+"'>"+ $(this).next(".tooltipContent").html() +"</div>");
      var my_tooltip = $("#"+name+i);

      $(this).mouseover(function(){
            my_tooltip.css({opacity:1, display:"none"}).fadeIn(400);
            if($(this).attr("rel") != "") {
              src = $(this).find("img:first-child").attr("src");
              $(this).find("img:first-child").attr("src", $(this).attr("rel"));
              $(this).attr("rel",src);
            }
            $(this).find(".pattern").css("display","inline");
            $(this).find(".pattern")
      }).mousemove(function(kmouse){
          var border_top = $(window).scrollTop();
          var border_right = $(window).width();
          var left_pos;
          var top_pos;
          var offset = 15;
          if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
            left_pos = kmouse.pageX+offset;
            } else{
            left_pos = border_right-my_tooltip.width()-offset;
            }

          if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
            top_pos = border_top +offset;
            } else{
            top_pos = kmouse.pageY+offset;
            }

          my_tooltip.css({left:left_pos, top:top_pos});
      }).mouseout(function(){
          my_tooltip.css({left:"-9999px"});
          if($(this).attr("rel") != "") {
            rel = $(this).find("img:first-child").attr("src");
            $(this).find("img:first-child").attr("src", $(this).attr("rel"));
            $(this).attr("rel",rel);
          }
          $(this).next(".tooltipContent").fadeOut(100);
          $(this).find(".pattern").css("display","none");
      });

    });
 }




$(document).ready(function() {
	
  $(".marken li").mouseenter(function(){
	  $(this).find(".marken_box").fadeIn(200);
  }).mouseleave(function(){
	  $(this).find(".marken_box").fadeOut(200);
  });
  
  if ( $.browser.msie && $.browser.version == "7.0") {
	  $("a button").click(function(){
		  window.location.href = $(this).parent("a").attr("href");
	  });
  }
  
  $(".marken_box").click(function(){
	  window.location.href = $(".marken_box").prev("a").attr("href");
  });
	
  $('.tabContent table td:nth-child(2n)').css({'background-color':'#dfdfdf'});
  $('.tabContent table table td:nth-child(2n)').css('background-color','transparent');
  $('.tabContent table tr.none_background td').css('background-color','transparent');

  // Homepage: Anpassung der H�he

  if ($(window).height() > 1024) {
    $(".homepage").css("height",$(window).height()-10);
  } else {
    $(".homepage").css("height",1024);
  }

  $(window).resize( function() {
    if ($(window).height() > 1024) {
      $(".homepage").css("height",$(window).height()-10);
    } else {
      $(".homepage").css("height",1024);
    }
    /* F�r Bottom Fixed
     * if ( $.browser.msie && $.browser.version == "7.0") {
        $(".homepage ~ .footer").css("left",$(window).width()/2-960/2+1);
    }*/

  });

  simple_tooltip("a.tooltip","tooltips");

  /* Fancybox */
  
  $(".ratingaverage button").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_rating',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 782,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'overlayColor'  : '#ffffff',
    'onStart'       : function() {
      $("#fb_rating .close").bind("click", function () {
        $.fancybox.close();
        return false;
      });
    }
  });
  
  $(".shippingInfo").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_shippingInformation',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 782,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'overlayColor'  : '#ffffff'
  });

  $(".sendmail").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_sendmail',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 782,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'overlayColor'  : '#ffffff',
    'onStart'       : function() {
      $("#fb_sendmail .close").bind("click", function () {
        $.fancybox.close();
        return false;
      });
    }
  });

  $(".notify").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_notify',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 282,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'overlayColor'  : '#ffffff',
    'onStart'       : function() {
      $("#fb_notify .close").bind("click", function () {
        $.fancybox.close();
        return false;
      });
    }
  });
  
  function setErrorFancybox(id) {
	  if (id == null) {
		  id = "#fb_error";
	  }
	  $(id).fancybox({
		    'titlePosition' : 'inside',
		    'transitionIn'  : 'none',
		    'transitionOut' : 'none',
		    'href'          : id,
		    'showCloseButton' : false,
		    'width'         : 742,
		    'height'         : 402,
		    'overlayOpacity': 0.7,
		    'centerOnScroll': true,
		    'autoDimensions': false,
		    'overlayColor'  : '#ffffff',
		    'onStart'       : function() {
		      $(id + " .close").bind("click", function () {
		        $.fancybox.close();
		        return false;
		      });
		    }
	  });
  };
  
  setErrorFancybox();

  $(".fbMeinKontoLink").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_login',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 930,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'autoDimensions': false,
    'autoScale'		: false,
    'overlayColor'  : '#ffffff',
    'onStart'       : function() {
      $("#fb_login .close").bind("click", function () {
        $.fancybox.close();
        return false;
      });
    }
  });
  
  $(".btnwarenkorb input").fancybox({
	    'titlePosition' : 'inside',
	    'href'          : '#fb_showItemInCart',
	    'transitionIn'  : 'none',
	    'transitionOut' : 'none',
	    'showCloseButton' : false,
	    'width'         : 782,
	    'overlayOpacity': 0.7,
	    'centerOnScroll': true,
	    'autoDimensions': false,
	    'overlayColor'  : '#ffffff',
	    'onStart'       : function() {
	      $("#fb_showItemInCart .close").bind("click", function () {
	        $.fancybox.close();
	        return false;
	      });
	    }
  });
  
  $("#fb_umfrage_click").fancybox({
	    'titlePosition' : 'inside',
	    'href'          : '#fb_umfrage',
	    'transitionIn'  : 'none',
	    'transitionOut' : 'none',
	    'showCloseButton' : false,
	    'width'         : 668,
	    'overlayOpacity': 0.7,
	    'centerOnScroll': true,
	    'autoDimensions': false,
	    'overlayColor'  : '#ffffff',
	    'onStart'       : function() {
	      $("#fb_umfrage .close").bind("click", function () {
	        $.fancybox.close();
	        return false;
	      });
	    }
});
  
  $("#fb_umfrage button").click(function() {	  
	  if (window.location.hostname.indexOf(".de")) {
		  window.location.href = "https://www.netigate.se/s.asp?s=50387X6608";
	  }
	  if (window.location.hostname.indexOf(".at")) {
		  window.location.href = "https://www.netigate.se/s.asp?s=50384X9767";
	  }
	  if (window.location.hostname.indexOf(".at")) {
		  window.location.href = "https://www.netigate.se/s.asp?s=50365X6313";
	  }
  });
  


  $(".lostpassword").fancybox({
    'titlePosition' : 'inside',
    'href'          : '#fb_password',
    'transitionIn'  : 'none',
    'transitionOut' : 'none',
    'showCloseButton' : false,
    'width'         : 482,
    'overlayOpacity': 0.7,
    'centerOnScroll': true,
    'autoDimensions': false,
    'overlayColor'  : '#ffffff',
    'onStart'       : function() {
      $("#fb_password .close").bind("click", function () {
        $.fancybox.close();
        return false;
      });
    }
  });
  
  $(".starrating").find("img").each(function() {    
    $(this).bind("mouseover", function() {
      $(this).prevAll("img").attr("src","img/content/fbratings_active.gif");
      $(this).attr("src","img/content/fbratings_active.gif");
      $(".starrating .rating").attr("value",$(this).attr("class"));
      $(this).nextAll("img").attr("src","img/content/fbratings_inactive.gif");
    });

    $(this).parent().bind("mouseout", function() {
      $("#fb_rating .stars").find("img").attr("src","img/content/fbratings_inactive.gif");
    });

    $(this).click(function() {
      $(this).parent().unbind("mouseout");
    });
  });

  $(".productdetails").toggle(function() {
    $(this).next(".productdetails").slideDown(500);
  }, function() {
    $(this).next(".productdetails").slideUp(500);
  });

  /*
   * Accordion Men�s
   */

  $(".checkout .accordion").click(function() {
    if (!$(this).find("input[type=radio]").attr("checked")) {
   	  $(".accordionItem").slideUp(500);
      $(".accordionItem").find("input").attr("disabled","disabled");
      $(this).next(".accordionItem").slideDown(500);
      $(this).next(".accordionItem").find("input").attr("disabled","");
      $(this).find("input[type=radio]:first-child").attr("checked","true");
      $(this).find("input[type=radio]:first-child").trigger('click');
    }
  });
  
  $(".checkout dd label:not(.accordionItem label)").click(function() {
	$(".accordionItem").slideUp(500);
	$(".accordionItem").find("input").attr("disabled","disabled");
  });
  
  $(".checkout input[type=radio]:not(.accordionItem input):not(.adrvalidation input)").mouseup(function() {
    $(".accordionItem").slideUp(500);
    $(".accordionItem").find("input").attr("disabled","disabled");
    $(this).parents(".accordion").next(".accordionItem").slideDown(500);
    $(this).parents(".accordion").next(".accordionItem").find("input").attr("disabled","");
  });

  $(".orders .accordion").toggle(function() {
    $(this).next(".accordionItem").slideDown(500);
    $(this).addClass("up");
  }, function() {
    $(this).next(".accordionItem").slideUp(500);
    $(this).removeClass("up");
  });

  $(".faq .accordion a:not(a.right.up):not(.answer a);").toggle(function() {
		$(this).next(".question").slideDown(500);
	    $(this).removeClass("down");
	    $(this).addClass("up");
  }, function() {
		$(this).next(".question").slideUp(500);
	    $(this).removeClass("up");
	    $(this).addClass("down");
  });
  
  $(".faq .question a.right.up:not(.answer a)").click(function() {
	  $(this).parent(".question").prev("a").triggerHandler("click");
	  scroll(0,0);
  });  

  $(".faq .question a:not(.answer a)").toggle(function() {
    $(this).next("hr").next(".answer").slideDown(500);
    if ($(this).next("hr").hasClass("last")) {
      $(this).next("hr").removeClass("hide");
    }
  }, function() {
    $(this).next("hr").next(".answer").slideUp(500, function() {
      if ($(this).prev("hr").hasClass("last")) {
        $(this).prev("hr").addClass("hide");
      }
    });    
  }); 
  
  $(".faq .answer a.right").click(function() {
	  if ($(this).parents(".answer").prevAll("a").hasClass("up")) {
		  $(this).parents(".answer").prevAll("a").triggerHandler("click");
		  scroll(0,0);
	  }
  });

  $(".sizes a").click(function() {
    $(this).parent().find("a").removeClass("selected")
    $(this).addClass("selected");
    $(this).parent().next("input[name='selectedSize']").attr("value", $(this).attr("href").replace("#",""));
    return false;
  });


  /* Init Register
   * Author: MK
   */
  $.each($(".tabsUL"), function() {
    var thisRegister = $(this);
    thisRegister.find("a").each(function(index) {
      var thisAnker = $(this);
      thisAnker.click(function() {
        hideRegisterContents(thisRegister);
        showRegisterContent(thisRegister, index , thisAnker.attr('rel'));
        return false; // To disable the href
      });
    });
  });

  /* Set all registers hidden
   * Author: MK
   */
  function hideRegisterContents(objRegister) {
    objRegister.find("li").removeClass("selected");
    registerContents = objRegister.nextAll(".tabContents");
    registerContents.find(".tabContent").addClass("hide");
    return true;
  }

  /* Show the clicked register
   * Author: MK
   */
  function showRegisterContent(objRegister, ContentIndex, ajaxMethod) {
    objRegister.find("li:eq(" + ContentIndex + ")").addClass("selected");
    registerContents = objRegister.nextAll(".tabContents");
    registerContent = registerContents.find(".tabContent:eq(" + ContentIndex + ")");
    if (registerContent.hasClass("hide")) {
      
      registerContent.removeClass("hide");
      if (ajaxMethod) return loadAjax(registerContent, ajaxMethod);
      return true;
    } else {
      return false;
    }
  }


  $("#language a:not(.languageSelection a)").click(function() {

		//Following events are applied to the options itself (moving options up and down)
		$(this).parent().find("span").slideDown('medium'); //Drop down the options on click

		$(this).parent().hover(function() {
			$(this).parent().find("#language span").slideUp('medium'); //When the mouse hovers out of the options, move it back up
		});

    return false;

  });
  
  $("div.filter").click(function() {
	  $("div.filter .items form").click(function() {	  
	   return true;
	  });
	  

    $(this).toggleClass('active'); //Drop down the options on click
		//Following events are applied to the options itself (moving options up and down)
		$(this).parent().find("div.filter .items").toggleClass('hide'); //Drop down the options on click
		$(this).parent().hover(function() {
        $("div.filter").removeClass('active');
        $(this).parent().find("div.filter .items").addClass('hide');    
		});

    return false;
  });

  $("div.filter .items a").click(function() {
   $(this).toggleClass('selected');
   return false;
  });

  $("div.filter .items button.reset").click(function() {
	  $("div.filter .items a").each(function() {
		    $(this).removeClass("selected");
	  });
  });
  
  
  /* Gratis Infos */
  $(".freeinfo input[name=noneRadio]").click(function() {
	  $("input[name=mailRadio]").attr("checked", "");
	  $("input[name=newsletterRadio]").attr("checked", "");
  });
  
  $(".freeinfo input[name=mailRadio]").click(function() {
	  $("input[name=noneRadio]").attr("checked", "");
  });
  
  $(".freeinfo input[name=newsletterRadio]").click(function() {
	  $("input[name=noneRadio]").attr("checked", "");
  });
  
  updateAJAXComponent();
  
  if (location.host.indexOf("localhost") == 0 || location.host.indexOf("previewshop") == 0) {
	  $("body").append('<script type="text/javascript" src="/webshop/js/hashgrid.js"></script>');
  }
  
  /**
   * Sitemap Scripts
   */
 

 
  
});

$.fn.findobject = function(find_element){
  	var find_elements = $(this).find(find_element);
  	var find_elements_array = jQuery.makeArray(find_elements);
  	return find_elements_array;
};
  
function setSitemapHeight(){
  	var catWrapperArray = new Array;
  	catWrapperArray = $('.genSitemap').findobject('.catWrapper');
  	var catWrapperHeight = 0;
  	$.each(catWrapperArray, function(index, element){
  		if ($(element).hasClass('clearWrapper')) {
  			catWrapperHeight = 0;
  		}
  		if (catWrapperHeight < $(element).height()) {
  			catWrapperHeight = $(element).height();
  		}
  		$(element).height(catWrapperHeight);
  	});
}

/**
 * Feedback AddressPannel
 */
function setErrorFeedback(field,container, component){
	$(container).find(component).addClass('error');
	$(container).find(component).removeClass('success');
}

function setSuccessFeedback(field,container, component){
	$(field).removeClass("error");
	$(container).find(component).addClass('success');
	$(container).find(component).removeClass('error');
}

function checkAddressFeedback() {
	var feedbackPanelAddress = new Array();
	feedbackPanelAddress = $('.feedbackAddress').findobject('.feedbackPanel');
	if (feedbackPanelAddress.length == 0) {
		$('.feedbackAddress div').addClass('success');
		$('.feedbackAddress').parent('.addressContainer').find('input').removeClass("error");
	} else {
		$('.feedbackAddress div').addClass('error');
		$('.feedbackAddress').parent('.addressContainer').find('input').addClass("error");
	}
}

function checkZipCityFeedback() {
	var feedbackPanelAddress = new Array();
	feedbackPanelAddress = $('.feedbackZipCity').findobject('.feedbackPanel');
	if (feedbackPanelAddress.length == 0) {
		$('.feedbackZipCity div').addClass('success');
		$('.feedbackZipCity').parent('.zipCityContainer').find('input').removeClass("error");
	} else {
		$('.feedbackZipCity div').addClass('error');
		$('.feedbackZipCity').parent('.zipCityContainer').find('input').addClass("error");
	}
}

