// Photoslider

var imageSlider = {
  duration: 1, // Duration for the transitions
  delay: 5, // Delay for the callback
  images: null, // Variable to store the images
  current: 1, // The current displayed image

  // Init function to start it all
  init: function(){

    // Get all the images and stores them in a variable
    imageSlider.images = $$('#header img');

    // Check the amount of images. Greater then 1 start the callback
    if(imageSlider.images.length > 1){

      // Hide all the images
      $$('#header img').each(function(el){
        el.hide();
      });

      // Set the last image to display
      imageSlider.images[imageSlider.images.length - imageSlider.current].show();

      // Start the periodical callback with the delay variable
      new PeriodicalExecuter(imageSlider.nextImage, imageSlider.delay);
    }
  },

  // Function for the next image
  nextImage: function(){

    // Hide the current image and set current image to the next one
    imageSlider.images[imageSlider.images.length - imageSlider.current++].fade({ duration: imageSlider.duration});

    // If the next image doesn't exist, set it to the first
    if(imageSlider.current > imageSlider.images.length){
      imageSlider.current = 1;
    }

    // Show the current image
    imageSlider.images[imageSlider.images.length - imageSlider.current].appear({ duration: imageSlider.duration});
  }
}

//Strict new window links
var strictNewWindows = {
  init: function(){
    $$('a[rel="external"]').each(function(link){
      if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
        link.writeAttribute('target','_blank');
      }
    });
  }
}

//Main menu
var menu = {
  appearTime: 0.3,
  currentMenu: false,
  mouseOutHide: false,

  init: function(){
    menu.hideAll();
    $$('ul#menu li').each(function(item){
      item.down().observe('mouseover',function(){
        $("content").down().fade({duration: .2});
        menu.mouseOutHide = false;
        if(menu.currentMenu != item.down().next()){
          if(menu.currentMenu != false){
            menu.currentMenu.hide();
            menu.currentMenu = false;
          }
          item.down().next().appear({duration: menu.appearTime});
          menu.currentMenu = item.down().next();
        }
      });
      item.down().next().observe('mouseover',function(){
        menu.mouseOutHide = false;
      });
      item.down().observe('mouseout',function(){
        menu.mouseOutHide = true;
      });
      item.down().next().observe('mouseout',function(){
        menu.mouseOutHide = true;
      });
    });
  new PeriodicalExecuter(menu.checkStatus, 2);
  },
  hideAll: function(){
    $$('.submenu').each(function(allItems){
      menu.currentMenu = false;
      allItems.hide();
    });
  },
  checkStatus: function(){
    if(menu.mouseOutHide == true){
      menu.mouseOutHide = false;
      menu.hideAll();
      $("content").down().appear({duration: .3});
    }
  }
};

// Products image slider
var productsSlider = {
  delay: 1, // Delay for the callback
  speed: 1, // Speed in which the products slide
  list: null, // Variable to store the image
  animate: true,
  amount: 1,
  clickedItem: null,
  gotoItem: false,

  // init to start it all
  init: function(){
    // Items array
    arrItems = $$('#products-list div');

//    arrItems.each(function(el,index){
//      el.id = 'slide'+index;
//      el.observe('click', function(){
//        productsSlider.delay = 0;
//        productsSlider.speed = 0.3;
////        productsSlider.gotoItem = 'slide'+index;
//
//        if(!productsSlider.animate){
//          productsSlider.animate = true;
//          productsSlider.start();
//        }
//      });
//    });


    if($$('#products-list div').length > 5){
      productsSlider.start();
    }
  },

  start: function(){

//    // If clicked item equals to an item in the array..
//    if(productsSlider.gotoItem != false){
//      arrItems = $$('#products-list div');
//      if(arrItems[2].id == productsSlider.gotoItem){
//        productsSlider.animate = false
//        productsSlider.delay = 1;
//        productsSlider.speed = 1.4;
//      }
//    }

    if(productsSlider.animate){
      productsSlider.list = $$('#products-list div'); // Get the div elements with the container div ID
      productsSlider.move.delay(productsSlider.delay);
    }
  },

  move: function(){
    new Effect.Move('products-list', {x: 0 - (148 * productsSlider.amount), duration: productsSlider.speed  });

    productsSlider.reset.delay(productsSlider.speed + 0.1); // + 0.1 to go around bug
  },

  reset: function(){
    for(var i = 0; i < productsSlider.amount; i++){
      new Insertion.Bottom('products-list', productsSlider.list[i]);
    }

    $('products-list').setStyle({ left : '0px'});

    productsSlider.start();
  }




}

// Products page product en color code ophalen uit de id en class
var productCodes = {
  init: function(){
    $$('ul#product-colors li.color-box').each(function(item){

      item.observe('mouseover', function(){
        itemsDown = item.down().down();
        productCodes.productID = itemsDown.id;
        productCodes.productColor = itemsDown.className;
        $('codes').innerHTML =  'Color #: ' + itemsDown.className + '<br />Product #: ' + itemsDown.id;
      });
      item.observe('mouseout', function(){
        $('codes').innerHTML = '';
      });
    });
  }
}

//Header products page slider
var headerSlide = {
  init: function(){
    $$('#header-products').each(function(headerImg){
      new Effect.Parallel([
          new Effect.Move(headerImg, { sync: true, x: -470, y: +15}),
            new Effect.Morph(headerImg, { sync: true, style: { width: '808px', height: '265px' }})
          ], {
             duration: 1.2,
             delay: 3
        });
    });
  }
}

function productClick(src){
  $('header-products').hide();
  $$('#header-products-small img').each(function(el){
    el.setStyle({ visibility: 'visible' });
    el.src=src;
  });
}



// Check to see if the dom is loaded
document.observe('dom:loaded', function(){
  //load the sIFR font replacement
  if(typeof sIFR == "function"){
  if($('slogan')){
    sIFR.replaceElement("#slogan", named({sFlashSrc: "/swf/eurostile.swf", sColor: "#7d8186", sWmode: "transparent"}));
  }
  if($('menu')){
    sIFR.replaceElement(".menuItem", named({sFlashSrc: "/swf/eurostile.swf", sColor: "#7d8186", sWmode: "transparent"}));
  }
  };

  // Initiate the functions
  imageSlider.init();
  strictNewWindows.init();
  productSlider = setTimeout("productsSlider.init()",3000);
  menu.init();
  // productCodes.init();
  headerSlide.init();
 });
