var PreviousFaq = null;
var PreviousUrl = null;
var bMoving = false;
var Counter = 0; 

$(document).ready(function() { 
	//$("#bg-container img").hide();

  Cufon.replace('.tekst h1', { fontFamily: 'Creampuf' });
    
  if($("ul.merken-list li").length > 0){
    $('ul.merken-list li:empty').remove();
    $('ul.merken-list li').each(function(){
      if($(this).html() == " "){
        $(this).remove();
      }
    });
    
    $('ul.merken-list li a').click(function(){
       setTimeout(function(){ 
         return MerkenClick($(this))
       , 500});
    });
  }  
   
   $('#subnavigation li a').click(function(){
     return MenuSwitch($(this));
   });
   
   $('.faq .titel-block').click(function(){
    ShowHideFaq($(this).parent());
   });
   
   $('#content a.loadPage').click(function(){
     return LoadPage($(this));
   });
   
   $('.block.nieuwsbrief').css('cursor','pointer');
   
   $('.block.nieuwsbrief').click(function(){
     return NewsletterShow();
   });
   
   $('.block.nieuwsbrief .close').click(function(){
     return NewsletterHide();
   });
   
   if($('#flash-home').length > 0){
     var CounterBlocks = 0;
     var BlockCounter = 0;
     $('.block.nieuws').each(function(){
       if(aShow[CounterBlocks] == 1) {
         $(this).remove();
       } else {
         BlockCounter = BlockCounter + 1;
       }
       CounterBlocks++;
     });
     
     newsSlider($('.block.nieuws'), (BlockCounter - 1));
   }
   
    $('#content .merken-ticker a').each(function(){
    var _this = $(this);
    var Rel = _this.attr('rel');
    var RelString = Rel.toString().substr(0,11);
      if(RelString == "imageviewer"){
        _this.attr('rel','')
      }
    });
    
    $('#content .merken-ticker a').click(function(){
      
    });
    
});

$(window).resize(function(){
  ResizeHandler();
});


function newsSlider(SlideElement, NumberOfItems){
  SlideElement.eq(Counter).css('z-index','12');
  if(Counter < NumberOfItems){
    setTimeout(function(){
      SlideElement.eq(Counter + 1).css('z-index','11');
      SlideElement.eq(Counter).animate({'top':'100px'}, 500, function(){
        $(this).css({'z-index':'8','top':'0px'});
        Counter++;
        newsSlider(SlideElement, NumberOfItems);
      }); 
    },5000);
  } else {
    Counter = NumberOfItems;
    setTimeout(function(){
      SlideElement.eq(0).css('z-index','11');
      SlideElement.eq(Counter).animate({'top':'100px'}, 500, function(){
        $(this).css({'z-index':'8','top':'0px'});
        Counter = 0;
        newsSlider(SlideElement, NumberOfItems);
      }); 
    },5000);
  } 
}

function MerkenClick(LinkElement){       
    var ImageSource = LinkElement.attr('href');
    var TextToDisplay = LinkElement.attr('title');
    $('.imageLoad').fadeOut(500,function(){
      $(this).html('<img src="' + ImageSource + '" alt=" ' + TextToDisplay + ' " />');

      $(this).fadeIn(500);  
    });
    
    $('.artikel').fadeOut(500,function(){
      $(this).html( TextToDisplay);
      $(this).fadeIn(500);  
    });
    
    
    return false; 
}

function MenuSwitch(ClickedElement){
  var CurrentLiElement =  $('#subnavigation li.current ul.subsub');
  var Url = ClickedElement.attr('href');
  
  if(ClickedElement.hasClass('subsubLink')){
    window.location.href = Url;
  } else {
    if(CurrentLiElement.length > 0){
      CurrentLiElement.slideUp('slow', function(){
        window.location = Url;
      });
    } else {
      window.location.href = Url;  
    }
  }
  return false;
}

function CheckActiveMenu(){
  $('#subnavigation li').each(function(){
    if($(this).hasClass('current')){
      var HeightElement = $(this).find('ul.subsub');
      if(HeightElement.html() != null){
        var Height = $(HeightElement)[0].scrollHeight;
        $(this).find('ul.subsub').css('margin-bottom','5px')
        $(this).find('ul.subsub').animate({'height': Height + 'px'}); 
      }
    }
  });  
}

function ShowHideFaq(FaqElement){
  if(!bMoving){
    bMoving = true;
    var FaqContent = FaqElement.find('.content-block');
    if(FaqElement.hasClass('opened')){
      FaqContent.slideUp('slow',function(){
        $(this).parent().removeClass('opened');
        bMoving = false;
      });    
    } else {
      if(PreviousFaq != null){
        PreviousFaq.slideUp('slow',function(){
          $(this).parent().removeClass('opened');
          FaqContent.slideDown('slow', function(){
            $(this).parent().addClass('opened');  
          });
        }); 
      } else {
        FaqContent.slideDown('slow',function(){
          $(this).parent().addClass('opened');
        });  
      }
      bMoving = false;
    }
    
    PreviousFaq = FaqContent;
  }
}

function SetColors(LightColor, DarkColor) {
  if((LightColor.length > 0) && (DarkColor.length > 0)) {
    var OriginalColor = "";
    if($('#navigation li a').eq(0).hasClass('current')){
      OriginalColor = $('#navigation li a').eq(1).css('background-color');
    } else {
      OriginalColor = $('#navigation li a').css('background-color');
    }
    
      OriginalLinkColor = "#92A3AB"; 
/*    $('#navigation li a').each(function(){
      if(!$(this).hasClass('current')){
        OriginalColor = $(this).css('background-color');
      }
    }); */
    
    var WhiteColor     = "#ffffff";
    var BlackColor     = "#000000";
    
    var NavigationLinkElement         = $("#navigation li a");
    var NavigationLinkElementCurrent  = $("#navigation li.current a");
    var SubnavLinkElement             = $("#content #subnavigation li a");
    var SubnavLinkElementCurrent      = $("#content #subnavigation li.current a");
    var SubSubLinkElement             = $("#content #subnavigation li ul.subsub li a");
    var SubSubLinkElementCurrent      = $("#content #subnavigation li ul.subsub li.current a");
    var FaqTitelElement               = $('.faq .titel-block');
    var FaqTitelElementCurrent        = $('.faq.opened .titel-block');
    
    SubnavLinkElement.css("background-color", LightColor);
    SubnavLinkElementCurrent.css("background-color", DarkColor);
    SubSubLinkElement.css("background-color", WhiteColor);
    SubSubLinkElementCurrent.css("color", DarkColor);
    
    NavigationLinkElement.css("background-color", OriginalColor);
    NavigationLinkElementCurrent.css("background-color", LightColor);
    $('div.color').css("background-color", LightColor);
    FaqTitelElement.css("background-color", LightColor);
    FaqTitelElementCurrent.css("background-color", LightColor);
    $(".jScrollPaneDrag").css("background-color", LightColor);   
    
    $("#content .column-left .tekst a").hover( 
      function() { $(this).css("color", DarkColor);   $(this).css("border-color", DarkColor);  }, 
      function() { $(this).css("color", BlackColor);  $(this).css("border-color", BlackColor);}
    );
    
    $("#content .column-left .merken a").hover( 
      function() { $(this).css("color", DarkColor);   $(this).css("border-color", DarkColor);  }, 
      function() { $(this).css("color", "#707173");  $(this).css("border-color", BlackColor);}
    );    
    
    NavigationLinkElement.hover( 
      function() { $(this).css("background-color", LightColor);    }, 
      function() { $(this).css("background-color", OriginalColor); }
    );
    
    NavigationLinkElementCurrent.hover( 
      function() { $(this).css("background-color", LightColor); }, 
      function() { $(this).css("background-color", LightColor);  }
    );
    
    SubnavLinkElement.hover(
     
      function() { $(this).css("color", WhiteColor); $(this).css("background-color", DarkColor);   }, 
      function() { $(this).css("color", WhiteColor); $(this).css("background-color", LightColor);  }
    );
    
    FaqTitelElement.hover( 
      function() { $(this).css("background-color", DarkColor);   }, 
      function() { $(this).css("background-color", LightColor);  }
    );
    
    SubnavLinkElementCurrent.hover( 
      function() { $(this).css("color", WhiteColor); $(this).css("background-color", DarkColor);   }, 
      function() { $(this).css("color", WhiteColor); $(this).css("background-color", DarkColor);  }
    );
        
    SubSubLinkElement.hover( 
      function() { $(this).css("color", DarkColor);     $(this).css("background-color", WhiteColor);  }, 
      function() { $(this).css("color", OriginalLinkColor); $(this).css("background-color", WhiteColor);  }
    );
    
    SubSubLinkElementCurrent.hover( 
      function() { $(this).css("color", LightColor);     $(this).css("background-color", WhiteColor);  }, 
      function() { $(this).css("color", DarkColor); $(this).css("background-color", WhiteColor);  }
    );
    
    $("h1").css("color", DarkColor);
    $("h2").css("color", DarkColor);
    $("h3").css("color", DarkColor);
    $("h4").css("color", DarkColor);
    $("h5").css("color", DarkColor);
    $("h6").css("color", DarkColor);     
    
    ColorFaderChange(LightColor, DarkColor);
    //Cufon.replace('#content #subnavigation li ul.subsub li a', { fontFamily: 'CachetBook', hover: true  });

  }
}

var bLoadedEvent = false;

function LoadPage(LinkElement){  
  var url = LinkElement.attr('href');
  var ScrollerComponent = null;
  var HyperLink = null;
  if(PreviousUrl != url){
    $('.tekst.static').fadeOut(500, function(){
      $(this).css('display','block'); 
      $(this).html('<img src="/pics/load.gif" style="margin-left: 359px; margin-top: 192px;" />'); 
    });
    setTimeout(function(){        
      bLoadedEvent = false;         
      $('.tekst.static').load(url + ' .tekst.static', function () { 
      	if(!bLoadedEvent) { 
      		bLoadedEvent = true; 
      		if($("ul.merken-list li").length > 0){
            GoMerkenList($(this)); 
          }
      	}
        
        if($('#merk-link').length > 0){

          if($('#merk-link').attr("href") == ''){
            $('.merk-link').remove();
          }
          Cufon.replace('a#merk-link', { fontFamily: 'CachetBook' });
        }

				if($('#productText a').length > 0) {
					HyperLink = $('#productText a').attr('href');
					if(HyperLink.length == 0) {
					  var Content = $('#productText a').html();
					   $('#productText a').remove();
					   $('#productText').html(Content);
					} 
				}

        Cufon.replace('.column-left h1', { fontFamily: 'Creampuf' });
 
      }); 
    },300);
  } else {
    PreviousUrl = url;
    return false;  
  }
  PreviousUrl = url;
  return false;
}

function GoMerkenList(obj)
{
  obj.css('display','none');
  $('#productImg img').css('visibility', 'hidden');
  
  Cufon.replace('.tekst h1', { fontFamily: 'Creampuf' });   

  obj.fadeIn(200, function() {
      var top = ( $('#productImg').height() - $('#productImg img').height() ) / 2; 
      $('#productImg img').css('margin-top', top); 
      $('#productImg img').css('visibility', 'visible');
      $('#productImg img').css('display','none');
      $('#productImg img').fadeIn(500);  

    });
  ChangeColors(); 

  if($("ul.merken-list li").length > 0){
    $('ul.merken-list li:empty').remove();
    $('ul.merken-list li').each(function(){
      if(obj.html() == " "){
        obj.remove();
      }
    });
    
    if($("ul.merken-list li").length > 0){
      ScrollerComponent = $('.merken-ticker').JScroller({LeftButton: ".simply-scroll-forward", RightButton: ".simply-scroll-back"});
      
      var text = $('ul.merken-list a:first').attr('title'); 
      $('.artikel').html(text);
      $('ul.merken-list li a').click(function(){
        return MerkenClick($(this));
      });
    }
  }        
}

function SetNavigationColor(Color){
    if(Color != ""){
      $('#navigation').css("background-color", Color);
      $('#navigation li a').each(function(){
        if(!$(this).hasClass('current')){
          $(this).css("background-color", Color);
        }
      });
    }
}

function NewsletterShow(){
  if(!bMoving){
    bMoving = true;
    var DivElement = $('.block.nieuwsbrief');
    DivElement.css({'position':'relative','z-index':'100'});
    DivElement.animate({'width':'370px'},function(){
      $('.block.nieuwsbrief').css('cursor','default');
      $('.close').css('cursor','pointer');
      $('.close').fadeIn(500);
      $('.inschrijven').fadeIn(500,function(){
        bMoving = false;
      });
    });
  }
}

function NewsletterHide(){
  if(!bMoving){
    bMoving = true;
    var DivElement = $('.block.nieuwsbrief');
    DivElement.css({'position':'relative','z-index':'100'});
    $('.close').fadeOut(500);
    $('.inschrijven').fadeOut(500,function(){
      $('.block.nieuwsbrief').css('cursor','pointer');
      $('.close').css('cursor','default');
      
      DivElement.animate({'width':'160px'}, function(){
        bMoving = false;
      });
      $('.inschrijven').css('display','none');
      $('.close').css('display','none');
    });
  }
}

function ClearTextbox(elementName, defaultValue)
{
    if (elementName.value == defaultValue){
        elementName.value = "";
        elementName.style.color = "#000000";
    }
}
function CheckTextbox(elementName, defaultValue)
{
    if (elementName.value == ""){
        elementName.value = defaultValue;
        elementName.style.color = "#CCCCCC";
    }
}

var bFadeIn = true;

function ResizeHandler() { 
	var ContentContainer				= $("#site-shadow");
	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-shadow").innerHeight());
	var SpacerHeight 						= parseInt($("#spacer").innerHeight());

	var ContainerHeight					= SiteHeight + SpacerHeight;
	
	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");
	  }
	}

	if(bFadeIn) {
		BackContainerImage.animate({opacity: 1}, "slow");
		bFadeIn = false;
	}
}

function GetDimensionChange(SrcDimension, NewDimension, DimensionValue)
{
	return DimensionValue * (NewDimension/SrcDimension);
}
