/*
-----------------------------------------------
Horizon Shutters Shutters
Author: Chad Spencer & Jeremy Boles
----------------------------------------------- */

window.addEvent('domready', function() {
  window.store('hash', '');
  
  /* Generic Tab/Links Swither to Avoid Page Reloads
  ----------------------------------------------- */
  var container = $$('body.shutters #sections');
  if (container.length == 1) {
    container = container[0];
    // Grab all of the links
    var links = container.getElements('a');
    // Then grab all of the sections they link to
    var sections = $$(links.map(function(link) { return $(link.get('href').replace('#', '')); }));
    // Add the click event for the links we grabed
    links.addEvent('click', function(event) {
      // Stop normal behavior
      event = new Event(event);
      event.stop();
      // Add help methods to the clicked on element
      var link = $(this);
      // Hide all of the sections
      sections.setStyle('display', 'none');
      // Show the proper section
      sections.filter(link.get('href')).setStyle('display', 'block');
      // Unmark all links as current
      links.getParent('li').removeClass('current');
      // Set the link as current
      link.getParent('li').addClass('current');
      // Change the hash
      var scroll = window.getScroll();
      window.location.hash = link.get('href');
      window.store('hash', window.location.hash);
      window.scrollTo(scroll.x, scroll.y);
    });
    
    // Check if the hash has changed
    var check = function() {
      if (window.location.hash != window.retrieve('hash')) {
        window.store('hash', window.location.hash.toString());
        var section = $(window.retrieve('hash').replace('#', ''));
        if (section) {
          // Hide all the sections
          sections.setStyle('display', 'none');
          // Show the proper section
          section.setStyle('display', 'block');
          // Unmark all links as current
          links.getParent('li').removeClass('current');
          // Filter the links to find the one that is linking to the current hash (selector wasn't working)
          var link = links.filter(function(l) { return (l.get('href') == ('#' + section.get('id'))); })[0];
          // Set the link as current
          link.getParent('li').addClass('current');
        }
      }
    };
    check.run();
    (function(){ 
      if (window.location.hash != window.retrieve('hash')) {
        // Make the browser go to the top
        var scroll = container.getPosition();
        window.scrollTo(scroll.x, (scroll.y - 100));
      }
      check.run();
    }).periodical(500);
  }
  
  /* Overview Page
  ----------------------------------------------- */
  // Controlling the tabs
  var widths = $$('body.plantation .louver-widths');
  if (widths.length == 1) {
    widths = widths[0];
    
    var links = widths.getElements('a');
    var sections = $$(links.map(function(width) { return $(width.get('href').replace('#', '')); }));
    
    links.addEvent('click', function(event) {
      // Stop normal behavior
      event = new Event(event);
      event.stop();
      // Add help methods to the clicked on element
      var link = $(this);
      // Hide all of the sections
      sections.setStyle('display', 'none');
      // Show the proper section
      sections.filter(link.get('href')).setStyle('display', 'block');
      // Unmark all links as current
      links.getParent('li').removeClass('current');
      // Set the link as current
      link.getParent('li').addClass('current');
    });
  }
  
  // Selectors and id for display the flash files
  var flash_selectors = [
    { 'selector' : 'body.shutters.traditional #traditional-louver', 'id' : 'traditional' },
    { 'selector' : 'body.shutters.plantation #plantation-louver-25', 'id' :  '2' },
    { 'selector' : 'body.shutters.plantation #plantation-louver-3', 'id' :  '3' },
    { 'selector' : 'body.shutters.plantation #plantation-louver-35', 'id' :  '3.5' },
    { 'selector' : 'body.shutters.plantation #plantation-louver-45', 'id' :  '4' }
  ];
  // Go through all of the selectors and display the proper flash file
  flash_selectors.each(function(flash) {
    var el = $$(flash.selector);
    if (el.length == 1) { // If we found an element
      el = el[0]; // Since its an array, just grab the first element
      var swf = new Swiff('/media/flash/louverView.swf', {
        'container' : el,
        'height' : 460,
        'params' : { 'base' : '.' },
        'vars' : { 'louverId' : flash.id },
        'width' : 658
      });
    }
  });
  
  /* Photos Page
  ----------------------------------------------- */
  var photo_ids = ['slideshow-traditional', 'slideshow-plantation'];
  photo_ids.each(function(id) {
    var el = $(id);
    if (el) {
      var type = id.replace('slideshow-', '');
      var swf = new Swiff('/media/slideshow/' + type + '/' + type + '.swf', {
        'container' : el,
        'height' : 532,
        'params' : { 'base' : '.' },
        'width' : 670
      });
    }
  });
  
  /* Configurations
  ----------------------------------------------- */
  var config_ids = ['panel-config-traditional', 'panel-config-plantation'];
  config_ids.each(function(id) {
    var el = $(id);
    if (el) {
      
      var xml = '';
      // Send the correct HTML for the style of shutter
      if (document.body.hasClass('traditional')) {
        xml = '_xml/dataPanelConfigsTraditional.xml'
      } else {
        xml = '_xml/dataPanelConfigsPlantation.xml';
      }
      
      var swf = new Swiff('/media/flash/wrapConfigView.swf', {
        'container' : el,
        'height' : 425,
        'params' : { 'base' : '.' },
        'vars' : { 'xmlUrl' : xml },
        'width' : 670
      });
    }
  });
  
});