var enabled = true;
var base_url = 'http://www.elementbyfuego.com/index.php/ajax/';
var base_url2 = 'http://www.elementbyfuego.com/index.php/';
var last_url = '';
var tag = '';
var links;
var queuedPage = "";
var productIndex = 0;
var newsIndex = 0;
var pressIndex = 0;
var lightbox = false;
var subnavobject = null;
function flashLink(url) {
  if(url.indexOf('upgrade') != -1) {window.open('http://www.elementbyfuego.com/index.php/upgrade/');
    return;
  }
  if(enabled) {
    enabled = false;
    queuedPage = url;
    changePage(url);
  } else
    queuedPage = url;
}
function hardcodedLink(anchor) {
        anchor.blur();
        if(enabled) {
            enabled = false;
            queuedPage = anchor.href;
            changePage(anchor.href);
        } else
          queuedPage = anchor.href;
        return false;
}
function changePage(url) {
        urchinTracker(url);
        if($('social_icons').getStyle('opacity') != 1)
          socialMorph.start({ 'opacity' : 1});
        productIndex = 0;
        newsIndex = 0;
        pressIndex = 0;
        last_url = url;
        if(url.lastIndexOf('index.php/') < 0)
          return;
        var href = url.substring(url.lastIndexOf('index.php/')+10);
        if((href == 'press' || href == 'press/') && $('announcement') != null)
          $('announcement').setStyle('display', 'none');
        if(href.indexOf('lightbox') != -1)
          lightbox = true;
        else {
          lightbox = false;
          hideLightbox();
        }
        new Request({
          url: base_url+href,
          onSuccess: function(responseText, responseXML) {
            window.location.hash = last_url.substring(last_url.lastIndexOf('index.php/')+9);
            if($('navhack'))
              $('navhack').src = base_url2+"iframe?url="+last_url.substring(last_url.lastIndexOf('index.php/')+9);
if(!lightbox) {
            new Element('div', { 'id' : 'new', 'class' : 'container', 'styles' : { 'left' : 970, overflow: 'hidden' }}).inject('main_inner');
            $('new').innerHTML = '<img src="http://t5.trackalyzer.com/trackalyze.asp?r=None&p='+escape(last_url)+'&i=16913"/>'+responseText;
      if (navigator.appName != "Microsoft Internet Explorer") {
            var text_replace = $$('div#new h3', 'div#new p');
            replace_text(text_replace, 'gotham_book');
            gotham_medium = gotham_medium.concat(text_replace);

            var submenu = $$('div#new div.sub_nav a');
if(submenu.length > 0) {
            submenulinks = [];
            for(var i=0; i<submenu .length; i++) {
              submenulinks.push(new Array(submenu[i].innerHTML, submenu[i].href));
            }
            $$('div#new div.sub_nav')[0].innerHTML = "";
            subnavobject = $$('div#new div.sub_nav')[0];
            new Swiff(flashPath+'sub_navigation.swf', {
              width: 536,
              height: 48,
              container: $$('div#new div.sub_nav')[0],
              params: {
                allowscriptaccess: 'always'
              }
            });
}

        }
            updateMenu(last_url.substring(last_url.lastIndexOf('index.php/')+10).split('/')[0]);
            stopLinks($('new').getElements('a'));
            new Fx.Morph('main_inner', { transition : Fx.Transitions.Quint.easeOut, duration : 1500 }).start({'left' : [0, -970]}).chain(function() {
              swatches();
              $('main_inner').setStyle('left', 0);
              $('main_inner').getElements('div.container')[0].dispose();
              $('new').setStyle('left', 0);
              if(queuedPage != last_url) {
                $('new').id = '';
                changePage(queuedPage);
              } else {
                enabled = true;
                $('new').id = '';
              }
            });
} else {
$('lightbox').setStyle('display', 'block');
$('lightbox').innerHTML = '<img src="http://t5.trackalyzer.com/trackalyze.asp?r=None&p='+escape(last_url)+'&i=16913"/>'+responseText;

      if (navigator.appName != "Microsoft Internet Explorer") {
//var text_replace = $$('div#lightbox h2', 'div#lightbox p');
//replace_text(text_replace, 'gotham_book');
//gotham_medium = gotham_medium.concat(text_replace);
}
enabled = true;
if(queuedPage != last_url) {changePage(queuedPage);} else
  showLightbox();
  enabled = true;
}
          },
          onFailure: function(instance) {
            enabled = true;
          }
        }).send();
}
var lightboxMorph;
var whiteoutMorph;
function hideLightbox() {
  $('close').setStyle('visibility', 'hidden');
  if(lightboxMorph) {
    lightboxMorph.cancel();
    whiteoutMorph.cancel();
    lightboxMorph = new Fx.Morph('lightbox', {duration : 500}).start({ opacity: 0 });
    whiteoutMorph = new Fx.Morph('whiteout', {duration : 500}).start({ opacity: 0 }).chain(function() {
      $('lightbox').setStyle('display', 'none');
      $('whiteout').setStyle('display', 'none');
      $('whiteout').removeEvents('click');
    });
  }
}
function showLightbox() {
  $('close').setStyle('visibility', 'visible');
  if(lightboxMorph) lightboxMorph.cancel();
  if(whiteoutMorph) whiteoutMorph.cancel();
  $('lightbox').setStyle('display', 'block');
  $('whiteout').setStyle('display', 'block');
  whiteoutMorph = new Fx.Morph('whiteout', {duration : 500}).start({ opacity: .5 }).chain(function() {
  lightboxMorph = new Fx.Morph('lightbox', {duration : 500}).start({ opacity: 1 });
    $('whiteout').addEvent('click', function() {hideLightbox();});
    if (navigator.appName != "Microsoft Internet Explorer" && lightboxpage) {
      lightboxpage = false;
      //var text_replace = $$('div#lightbox h2', 'div#lightbox p');
      //replace_text(text_replace, 'gotham_book');
      //gotham_medium = gotham_medium.concat(text_replace);
    }
  });
}
function stopLinks(links) {
  for(var i=0; i<links.length; i++) {
    if(links[i].href.indexOf('mailto') == -1 && links[i].href.indexOf('javascript:') == -1 && links[i].href.indexOf('elementbyfuego.com/index.php') != -1) {
      links[i].onclick = function() {
        this.blur();
        if(enabled) {
          if(this.href.substring(this.href.lastIndexOf('index.php/')+10) == "" && queuedPage == this.href)
            next();
          else {
            enabled = false;
            queuedPage = this.href;
            changePage(this.href);
          }
        } else
          queuedPage = this.href;
        return false;
      }
    } else {
      links[i].addEvent('click', function() {this.blur();});
    }
  }
}
window.addEvent('domready', function() {
  links = $$('div#menu a');
  stopLinks(links);
  setTimeout(checkURL, 200);
});
function updateMenu(page) {
  for(var i=0; i<links.length; i++) {
    links[i].getParent().removeClass('here')
    if(links[i].href.substring(links[i].href.lastIndexOf('index.php/')+10).split('/')[0] == page) {
      document.title = links[i].innerHTML.replace('&amp;', '&');
      if (navigator.appName != "Microsoft Internet Explorer")
        $$('div#header object')[0].changeHeader(links[i].innerHTML);
      var temp = $(links[i].parentNode);
      do {
        if(!temp.hasClass('sub'))
          temp.addClass('here');
        temp = $(temp.parentNode);
      } while(temp.id != 'nav');
    }
  }
}
function checkURL() {
  var url = window.location.href.split('#');
  if(url[1] == null) url[1] = '';
  if(tag != url[1] || (document.getElementById('navhack') && tag != document.frames['navhack'].getLocation())) {
    if(document.getElementById('navhack'))
      tag = document.frames['navhack'].getLocation();
    else
      tag = url[1];
    window.location.hash = tag;
    queuedPage = base_url2.substr(0, base_url2.length-1)+tag;
    if(enabled)
      changePage(base_url2.substr(0, base_url2.length-1)+tag);
  }
  setTimeout(checkURL, 200);
}
function loadPage(url) {alert(url);}