var activeStoryIndex = 0;
var fadeComplete = true;
var fadeTimer = 10000;
var timeoutId;
var myModal = null;
var defaultSearchText = "Search";

jQuery.noConflict();

function Init(){
  InitSearch();
  InitStoryNavigator();
  InitProductSelector();
  InitAccordion();  
  jQuery("ul#menu").superfish();
  jQuery("ul#menu-modules").superfish();
  FixImages();
}

jQuery(document).ready(function(){
  jQuery(".disclaimer-wrapper a").hover(function() {
    jQuery(this).next("div").stop(true, true).animate({opacity: "show", top: "20"}, "slow");
  }, function() {
    jQuery(this).next("div").animate({opacity: "hide", top: "40"}, "fast");
  }).click(function(){return false;})
});

function InitAccordion(){
  var accordion = new Accordion('div.companyTitle', 'div.companyPeople', {opacity: false, alwaysHide: true}, $('accordion'));
}

function loadFile(playerId, fileName) {
  var player = document.getElementById(playerId);
  
  if(player){
    player.sendEvent('LOAD', fileName);
    player.sendEvent('PLAY', 'true');
  }
  
  return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FixImages
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function FixImages(){
  $$('div.image-right, div.image-left, div.image-top').every(function(item, index){
    var width = item.getElements('div.image-wrapper img').getWidth();
    item.getElements('div.image-wrapper').setStyle('width', width);
    item.setStyle('width', width);
  });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Search
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitSearch(){
  var searchField = $('search-header');
  
  if(searchField){
    searchField.style.color = '#999999';
    searchField.value = defaultSearchText;
    searchField.onfocus = function() { FocusSearchField(); };
    searchField.onblur = function() { BlurSearchField(); };
  }
}

function FocusSearchField(){
  var searchField = $('search-header');
  
  if(searchField){
    if(searchField.value == defaultSearchText){
      searchField.style.color = '#333333';
      searchField.value = "";
    }
  }
}

function BlurSearchField(){
  var searchField = $('search-header');
  
  if(searchField){
    if(searchField.value == ""){
      searchField.style.color = '#999999';
      searchField.value = defaultSearchText;
    }
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Product Selector
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitProductSelector(){
  var selector = $('product-selector');

  if(selector){
    selector.onchange = function(){ FilterSelection(); };
  }
}

function FilterSelection(){
  var selector = $('product-selector');

  var selectedValue = selector.value;
  
  var array = new Array();

  $$('div#main-content li').forEach(function(product){
    if(product.className.indexOf('pt-' + selectedValue) > -1 || selectedValue == 0){
      product.style.display = 'block';
      var parentId = product.parentNode.id;
      if(!array.contains(parentId)){
        product.addClass('first');
        array.include(parentId)
      } else {
        product.removeClass('first');
      }
    } else {
      product.style.display = 'none';
      product.removeClass('first');
    }
  });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Story
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitStoryNavigator(){
  var previous = $('story-navigator-previous');
  var next = $('story-navigator-next');

  if(previous && next){
    previous.onclick = function(){ StoryNavigate(-1); };
    next.onclick = function(){ StoryNavigate(1); };
    timeoutId = setTimeout("StoryNavigate(" + 1 + ")", fadeTimer);
  }
}

function StoryNavigate(direction){
      
  if(fadeComplete){    
    
    clearTimeout(timeoutId);
    
    var stories =	$$('div.story');
    var newActiveStoryIndex = activeStoryIndex + direction;
      
    if(newActiveStoryIndex >= stories.length){
      newActiveStoryIndex = 0;
    }

    if(newActiveStoryIndex < 0){
      newActiveStoryIndex = stories.length - 1;
    }
        
    stories[activeStoryIndex].style.zIndex = 2;
    stories[newActiveStoryIndex].style.zIndex = 1;
    
    fadeActiveStory(stories[activeStoryIndex], stories[newActiveStoryIndex]);
      
    activeStoryIndex = newActiveStoryIndex;
  }
}

function fadeActiveStory(activeStory, newActiveStory){
  fadeComplete = false;
  var thisFx = new Fx.Tween(activeStory, 'opacity');
  thisFx.options.duration = 2000;
  var thisFx2 = new Fx.Tween(newActiveStory, 'opacity');
  thisFx2.options.duration = 2000;
  
  thisFx.start(0.99, 0);
  thisFx2.start(0, 0.99);
  
  activeStory.style.display = 'block';
  newActiveStory.style.display = 'block';
  
  thisFx.onComplete = function(){
    thisFx.set(0);
    activeStory.style.zIndex = '0';
    timeoutId = setTimeout("StoryNavigate(" + 1 + ")", fadeTimer);
    fadeComplete = true;
    return;
  }
  return;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Indexer
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UpdateIndexer(indexerId, querystring){
  var url = 'default.csf?' + querystring;
  new Ajax(url, {
    method: 'get',
    update: $('indexer-' + indexerId)
  }).request();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// My Wholesale
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function EditMyModules(){
  return EditBase('my-module-selector', 'My Modules', '250px');
}

function EditMyProducts(){
  return EditBase('product-edit', 'Update My Products', '750px');
}

function EditMyDialogues(){
  return EditBase('dialogue-edit', 'Update My Dialogue', '350px');
}

function EditMySubscriptions(){
  return EditBase('subscription-edit', 'Update My Subscription', '350px');
}

function Disclaimer(id){
  return EditBase(id, 'Disclaimer', '350px');
}

function EditBase(id, title, width){
if(!myModal){
    myModal = new Modal();
  }
  
  myModal.show($(id), {title : title, width: width});
  return false;
}

function CloseModal(){
  if(myModal){
    myModal.hide();
  }
  
  return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Page Init
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
window.onload = Init;
