$(document).ready(function() { 	
	//$("#bg-container img").hide();
	//$("#bg-container").readydiv(function() { ResizeHandler(); }); 
	
	//DisplayNavigation();
		
	if($('.fade').length > 0){
		$('.fade').innerfade({
			speed: 'slow',
			timeout: 6000,
			type: 'random_start',
			containerheight: '127px'
		});
	}

	$(".login h4").click( function() {
		if($(".login").hasClass("open")) {
			$("h4", $(".login")).css("background-image", "url(/pics/arrow_down.gif)");
			$(".slide",$(".login")).animate(
				{
					height: 'toggle'
				}, 500, function() {}
			);
			$(".login").removeClass("open");
		} else {
			$("h4", $(".login")).css("background-image", "url(/pics/arrow_up.gif)");
			$(".slide",$(".login")).animate(
				{
					height: 'show'
				}, 500, function() {}
			);
			$(".login").addClass("open");
		}
	});

	$(".winkelwagen h4").click( function() {
		if($(".winkelwagen").hasClass("open")) {
			$("h4", $(".winkelwagen")).css("background-image", "url(/pics/arrow_down.gif)");
			$(".slide",$(".winkelwagen")).animate(
				{
					height: 'toggle'
				}, 500, function() {}
			);
			$(".winkelwagen").removeClass("open");
		} else {
			$("h4", $(".winkelwagen")).css("background-image", "url(/pics/arrow_up.gif)");
			$(".slide",$(".winkelwagen")).animate(
				{
					height: 'show'
				}, 500, function() {}
			);
			$(".winkelwagen").addClass("open");
		}
	});

	
	/*$(".slidecontent h4").click( function() {
		if($(".slidecontent").hasClass("open")) {
			$("h4", $(".slidecontent")).css("background-image", "url(pics/arrow_down.png)");
			$(".slide",$(".slidecontent")).animate(
				{
					height: 'toggle'
				}, 500, function() {}
			);
			$(".slidecontent").removeClass("open");
		} else {
			$("h4", $(".slidecontent")).css("background-image", "url(pics/arrow_up.png)");
			$(".slide",$(".slidecontent")).animate(
				{
					height: 'show'
				}, 500, function() {}
			);
			$(".slidecontent").addClass("open");
		}
	});*/
	
	/*
	$("#subnavigation ul li:not(.current) ul").animate(
		{
			height: 'hide'
		}, 500, function() {}
	);
	*/
	
	/*
	$("#subnavigation ul li span.label").click( function() {
		if($("ul", $(this).parent()).length > 0) {
			if($(this).parent().hasClass("open")) {
				$(this).parent().css("background", "url(http://vormlicht.ivengi.com/pics/arrow_down.gif) top right no-repeat");
				$("ul",$(this).parent()).animate(
					{
						height: 'toggle'
					}, 500, function() {}
				);
				$(this).parent().removeClass("open");
			} else {
				$(this).parent().css("background", "url(http://vormlicht.ivengi.com/pics/arrow_up.gif) top right no-repeat");
				$("ul",$(this).parent()).animate(
					{
						height: 'show'
					}, 500, function() {}
				);
				$(this).parent().addClass("open");
			}
		}
	});
	*/
		
	$('#slider').cycle({ 
		fx: 		'scrollLeft',
		timeout: 	6000,
		speed: 		2000		
	});
	
	$("a.lightbox").fancybox({
		'titlePosition'  : 'over',
		onComplete: function(selectedArray, selectedIndex, selectedOpts) {
        		if($("#igallery").length == 1) {
        			iGalleryPager(selectedIndex);
        		}
			}
	});
	

	$("#products .product").hover(
	  function () {
		$(".image", $(this)).css("border-color","#94ae26");
	  }, 
	  function () {
		$(".image", $(this)).css("border-color","#fff");
	  }
	);
	
	$("#content .block").hover(
	  function () {
		$(".image", $(this)).css("border-color","#94ae26");
	  }, 
	  function () {
		$(".image", $(this)).css("border-color","#fff");
	  }
	);

});

//$(window).resize(function(){
  //ResizeHandler();
//});

function DisplayNavigation()
{
	var NavigationElement 	   = $("a.slide");
	var SubNavigationContainer = $("ul.sub");
		
	NavigationElement.each(function(){
		NavigationElement.hover(function() {
			$(this).next("ul.sub").animate({
				height: $(this).next("ul")[0].scrollHeight + "px"
			}, {queue: false });	
		}, function() {
			$(this).next("ul.sub").animate({
				height: "0px"
			}, {queue: false });	
		});

		SubNavigationContainer.hover(function() {
			$(this).stop();
			$(this).animate({
				height: $(this)[0].scrollHeight + "px"
			}, {queue: false });	
		}, function() {
			$(this).animate({
				height: "0px"
			}, {queue: false });	
		});
	});			
}

var bFadeIn = true;

/*
function ResizeHandler() { 
	var ContentContainer				= $("#site");
	var BackContainer					= $("#bg-container");
	var BackContainerImage				= $("#bg-container img");
	var ImageWidth						= $("#bg-container img").width();
	var ImageHeight						= $("#bg-container img").height();

	var ContentContainerHeight  = parseInt(ContentContainer.outerHeight()); 
	var ContentContainerWidth   = parseInt(ContentContainer.outerWidth());
	
	BackContainer.css("overflow", "hidden");
	BackContainerImage.css("position", "absolute");
	
	var SiteHeight 						= parseInt($("#site").innerHeight());
	var ContainerHeight					= SiteHeight;

	BackContainer.height(ContainerHeight); // set height of the container so no scrollbar is measured
	
	var WindowHeight 						= parseInt($(window).height());
	var WindowWidth 						= parseInt($(window).width());
	
	BackContainer.width(WindowWidth);
	BackContainer.css("width", "100%");

	if(WindowWidth < ContentContainerWidth) {
		BackContainerImage.css('width', ContentContainerWidth + "px");
	} else {
		BackContainerImage.css('width', WindowWidth + "px");
	}
	BackContainer.css("width", BackContainerImage.width() + "px");
	
	var NewImageHeight = GetDimensionChange(ImageWidth, BackContainerImage.width(), ImageHeight);
	if(NewImageHeight > ContainerHeight) {
		BackContainerImage.css("top", (Math.floor(NewImageHeight - ContainerHeight) / 2) * -1 + "px");
		BackContainerImage.height(NewImageHeight);
		BackContainerImage.css("left", "0px");
	} else {
		BackContainerImage.css("top", "0px");
		BackContainerImage.height(ContainerHeight);
		var NewImageWidth = GetDimensionChange(ImageHeight, ContainerHeight, ImageWidth);
		BackContainerImage.width(NewImageWidth);
	  if(WindowWidth < ContentContainerWidth) {
	  	BackContainerImage.css("left", (Math.floor(NewImageWidth - ContentContainerWidth) / 2) * -1 + "px");
	  } else {
	  	BackContainerImage.css("left", (Math.floor(NewImageWidth - WindowWidth) / 2) * -1 + "px");
	  }
	}

	BackContainer.height(parseInt($("#site").innerHeight())); // height changes during process, so don't use SiteHeight.

	if(bFadeIn) {
		BackContainerImage.animate({opacity: 1}, "slow");	
		bFadeIn = false;
	} else {
		BackContainerImage.show();
	}
}

function GetDimensionChange(SrcDimension, NewDimension, DimensionValue)
{
	return DimensionValue * (NewDimension/SrcDimension);
}
*/

function CheckImageSizes(){
  
  var CookieValue     = getCookie('ImageSizes');
  var aCookieValues   = null;
  var bUseCookieValue = false;
  
  if(CookieValue != 0){
    aCookieValues     = CookieValue.split('-');
    bUseCookieValue   = true;
  }

  var $BackgroundDiv = $('#bg-container');
  var $BackgroundImage = $BackgroundDiv.find('img');
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var BrowserHeight = parseInt($(window).height());
  var BrowserWidth = parseInt($(window).width());
  
  var BgImageHeight = parseInt($BackgroundImage.height());  
  var BgImageWidth = parseInt($BackgroundImage.width());

  //console.log("BgImageHeight: " + BgImageHeight + " BrowserHeight: " + BrowserHeight + " BgImageWidth: " + BgImageWidth + " BrowserWidth" + BrowserWidth );
  
  if(!isiPad) {
    if(BrowserHeight > BgImageHeight) {  
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : '100%', width : 'auto' });
      }
    }
    
    if(BrowserHeight == BgImageHeight && BgImageWidth > BrowserWidth) {
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : '100%', width : 'auto' });
      }
      return false;  
    }
    
    if(BrowserWidth == BgImageWidth && BgImageHeight > BrowserHeight) {
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : 'auto', width : '100%' });
      }
      return false;  
    }
    
    if(BgImageWidth < BrowserWidth) {
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : 'auto', width : '100%' });
      }
    } else {
      if(BrowserWidth > BrowserHeight) {
        if(BrowserHeight > BgImageHeight) {  
          if(bUseCookieValue && bFirst){
            $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
          } else {
            $BackgroundImage.css({ height : '100%', width : 'auto' });
          }
        } else { 
          if(bUseCookieValue && bFirst){
            $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
          } else {
            $BackgroundImage.css({ height : '100%', width : 'auto' });
          }
        }
      }
    }  
    if(bFirst) {
      if(bUseCookieValue){
        bFirst = false;
        $("#bg-container").css('visibility','visible');
      } else {
        $BackgroundDiv.css({ 'display':'none', 'visibility':'visible'});
        $BackgroundDiv.fadeIn('slow');
        bFirst = false;
      }
    }
  
  } else {
      
    if(bFirst){
      var SiteHeight = parseInt($("#site").height());
      var SiteWidth = parseInt($("#site").width());
      
      if(BgImageWidth < SiteWidth) {
        $BackgroundImage.css({ height : 'auto', width : '100%' }) 
      }
      
      if(SiteHeight > BgImageHeight) {
        $BackgroundImage.css({ height : '100%', width : 'auto' });
      }
      bFirst = false;
    } else {
            
      if(BgImageWidth < BrowserWidth) {
        $BackgroundImage.css({ height : 'auto', width : '100%' }) 
      }
      
      
      if(BrowserHeight > BgImageHeight) {
        $BackgroundImage.css({ height : BrowserHeight, width : 'auto' });
      }
    }
    
    $("#bg-container").css('visibility','visible');
  }
  
  var ImageWidth  = $BackgroundImage.css('width');
  var ImageHeight = $BackgroundImage.css('height');

  setCookie('ImageSizes',ImageWidth + "-" + ImageHeight,365);
  
}
