var activeStoryIndex = 0;
var fadeComplete = true;
var fadeTimer = 10000;
var timeoutId;
var myModal = null;
var defaultSearchText = "Type here...";
var defaultSearchColor = '#999999';
var defaultSearchColorActive = '#333333';

jQuery.noConflict();

function Init(){
  jQuery("div#primaryNav ul").superfish();
  InitPopup();
  InitHomeImageFade();
  InitEmailToFriend();
  InitPrint();
  InitSearch();
  FixImages();
  SearchDisplayEvt();
  if(typeof correctPNG == 'function') {
    correctPNG();
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Popup
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitPopup(){
  var background = new Element('div', {id:'modalBackground'});
  background.setStyle('display', 'none');
  background.setStyle('width', window.getScrollWidth() + 'px');
  background.setStyle('height', window.getScrollHeight() + 'px');
  background.setStyle('background-color', '#000');
  background.setStyle('opacity', '0.7');
  background.setStyle('position', 'absolute');
  background.setStyle('z-index', 10000);   
  background.onclick = HidePopup;
  background.inject($('body'), 'top');
  
  var popupContent = new Element('div', {id:'popupContent'});
  popupContent.setStyle('position', 'absolute');
  popupContent.setStyle('z-index', 10001);
  popupContent.inject($('body'), 'top');

  $$('div.popup-intro').each(function(item, index){
    item.setStyle('display', 'block');
  });
  
  $$('h3.popupHeading').each(function(item, index){
    item.setStyle('display', 'block');
  });
  
  $$('div.popup-content').each(function(item, index){
    var id = item.id;
    var copy = item.clone();
    var left = (((window.getSize().x / 2) + window.getScroll().x) - 250) + 'px';
    
    item.destroy();
    
    var closeLink = new Element('a', {'href': '#', 'html': 'Close'});
    closeLink.onclick = HidePopup;
    closeLink.inject(copy);
        
    copy.inject($('popupContent'), 'top');
    copy.setStyle('display', 'none');
    copy.setStyle('background-color', '#ffffff');
    copy.setStyle('width', '500px');
    copy.setStyle('padding', '10px 10px 5px');
    copy.setStyle('position', 'absolute');
    copy.id = id;
    copy.setStyle('left' , left);
  });
}

function ShowPopup(nodeId){
  $(nodeId).setStyle('display', 'block');
  $(nodeId).setStyle('top' , (((window.getSize().y / 2) + window.getScroll().y) - $(nodeId).getSize().y) + 'px');
  $('modalBackground').setStyle('display', 'block'); 
  return false;
}

function HidePopup(){
  $$('div#popupContent').getChildren().each(function(item, index){
    item.setStyle('display', 'none');
  });

  $('modalBackground').setStyle('display', 'none');
  return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Search
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitSearch(){
  var searchField = $('searchText');
  
  if(searchField){
    searchField.style.color = defaultSearchColor;
    searchField.value = defaultSearchText;
    searchField.onfocus = function() { FocusSearchField(); };
    searchField.onblur = function() { BlurSearchField(); };
  }
}

function FocusSearchField(){
  var searchField = $('searchText');
  
  if(searchField){
    if(searchField.value == defaultSearchText){
      searchField.style.color = defaultSearchColorActive;
      searchField.value = "";
    }
  }
}

function BlurSearchField(){
  var searchField = $('searchText');
  
  if(searchField){
    if(searchField.value == ""){
      searchField.style.color = defaultSearchColor;
      searchField.value = defaultSearchText;
    }
  }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Home Image Fade
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitHomeImageFade(){
  jQuery('#banner').cycle({timeout: 10000});
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Email To Friend
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function InitEmailToFriend(){
  var link = $('emailLink');

  if(link){
    link.onclick = EmailToFriend;
  }
}

function EmailToFriend(){
  if(!myModal){
    myModal = new Modal();
  }

  myModal.show($('emailToFriend'), {title : 'Email to friend', width: '457px'});
  return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Indexer - Using Mootools
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UpdateIndexer(pageId, indexerId, pageNumber){
  var updateNode = $('indexer-' + indexerId);
  var url = 'default.csf?id=' + pageId + '&indexer=' + indexerId + '&page=' + pageNumber;

  new Ajax(url, {
    method: 'get',
    update: updateNode
  }).request();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FixImages
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function FixImages(){
  $$('div.imageHolder').each(function(item, index){
    var width = item.getElements('div.imgInner img').getWidth();
    item.getElements('div.imgInner').setStyle('width', width);
    item.setStyle('width', width);
  });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Search Box Display
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function getElementStyle(elemID, IEStyleProp, CSSStyleProp){
  var elem = document.getElementById(elemID);
  if (elem.currentStyle) {
    return elem.currentStyle[IEStyleProp];
  } else if (window.getComputedStyle) {
    var compStyle = window.getComputedStyle(elem, "");
    return compStyle.getPropertyValue(CSSStyleProp);
  }
  return "";
}

function toggleLogIn(){
  var searchElem = document.getElementById("searchPopUp");
  var searchVis = getElementStyle("searchPopUp", "display", "display");
  if (searchVis == "none") {
    searchElem.style.display = "block";
  } else {
    searchElem.style.display = "none";
  }
  document.getElementById("searchLink").blur();
  return false;
}

function SearchDisplayEvt(){
  var elem = document.getElementById("searchLink");
  if (elem) {
    elem.onclick = toggleLogIn;
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// print this page
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Check for print value in URL and trigger print dialogue */
/* ------------------------------------------------------- */

function InitPrint(){
  var link = $('printLink');

  if(link){
    //link.onclick = PrintTest;
  }
}

function GetSearchData() {
  var results = new Object();
  var srchString = unescape(location.search.substring(1, location.search.length));
  if (srchString.length > 0) {
    var srchArray = srchString.split("&");
    var tempArray = new Array();
    for (var n = 0 ; n < srchArray.length ; n++) {
      tempArray = srchArray[n].split("=");
      results[tempArray[0]] = tempArray[1];
    }
  }
  return results;
}

function SetPrintStyleSheets(title) {
  var j = 0;
  var b = new Array;
  var a = document.getElementsByTagName("link");

  //disable all style sheets
  for (var i = 0; i < a.length ; i++) {
    if (a[i].getAttribute("type").indexOf("css") != -1) {
      a[i].disabled = true;
      //create a list of all print style sheets that were present
      if ((a[i].getAttribute("media").indexOf("print") != -1) || (a[i].getAttribute("media").indexOf("all") != -1)) {
        b[j] = a[i].href;
        j++;
      }
    }
  }

  //re-load print style sheets to force activation in page
  for (var i = 0; i < b.length ; i++) {
    var headID = document.getElementsByTagName("head")[0];
    var cssNode = document.createElement("link");
    cssNode.type = "text/css";
    cssNode.rel = "stylesheet";
    cssNode.href = b[i];
    cssNode.media = "all";
    headID.appendChild(cssNode);
  }
}

function PrintTest() {
  SetPrintStyleSheets();
  setTimeout("window.print()", 500);
  return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

window.onload = Init;