// Global variables
var submenu_active = false;
var submenu_height = new Array();
var open_popups = new Array();
var open_urls = new Array();
var wcount = 0;

// Functions

var submenu_show = function(name) {
  var menu = 'ul#menu-' + name;
  if(!$(menu).is(':visible')) {
    var main = $('ul#menu-main')
    $(menu + ' ul').hide();
    if($.browser.msie && $.browser.version == '6.0') {
      main_height = main.outerHeight();
    } else {
      main_height = main.height();
    }
    if(!submenu_active)
      main.fadeOut(1000, function() {
	main.css({
	  'left': $(window).width() / 2 - (main.outerWidth() + 10) + 'px'
	});
	$(menu).css({
	  'left': (main.width() + 20).toString() + 'px',
	  'top': 0,
	  'height': main_height + 'px'
	});
	// Menu items color
	$('#menu-main > li > a').css({color: '#999'});
	$('a#'+name).css({color: '#fff'});
	main.fadeIn(1000);
	$(menu).fadeIn(1000);
      });
    else
      $('ul.sec:visible').fadeOut(1000, function() {
	$(menu).css({
	  'left': main.outerWidth() + 20 + 'px',
	  'top': 0,
	  'height': main_height + 'px'
	});
	// Menu items color
	$('#menu-main > li > a').css({color: '#999'});
	$('a#'+name).css({color: '#fff'});
	$(menu).fadeIn(1000);
      });
    submenu_active = true;
  }
}

var submenu_reset = function() {
  if(submenu_active) {
    var main = $('ul#menu-main');
    $('ul.sec').fadeOut(1000);
    main.fadeOut(1000, function() {;
      main.css({
	left: $(window).width() / 2 - main.outerWidth() / 2 + 'px',
	top: $(window).height() / 2 - main.outerHeight() / 2 + 'px'
      });
      // Menu items color
      $('#menu-main > li > a').css({color: '#fff'});
      main.show();
    });
    submenu_active = false;
  }
}

var centering = function() {
  var main = $('#menu-main');
  var w_width = $(window).width();
  var w_height = $(window).height();
  var e_width = main.outerWidth();
  var e_height = main.outerHeight();
  if(submenu_active)
    main.css({
      left: w_width / 2 - (e_width + 10) + 'px',
      top: w_height / 2 - e_height / 2 + 'px'
    });
  else
    main.css({
      left: w_width / 2 - e_width / 2 + 'px',
      top: w_height / 2 - e_height / 2 + 'px'
    });
  var cwindow = $('div#cwindow');
  var cw_height = cwindow.outerHeight();
  var cw_width = cwindow.outerWidth();
  cwindow.css({
    'left': w_width / 2 - cw_width / 2 + 'px',
    'top': w_height / 2 - cw_height / 2 + 'px'
  });
  var lwindow = $('div#lwindow');
  var lw_height = lwindow.outerHeight();
  var lw_width = lwindow.outerWidth();
  lwindow.css({
    'left': w_width / 2 - lw_width / 2 + 'px',
    'top': w_height / 2 - lw_height / 2 + 'px'
  });
}

var onTransition = function() {
  $('a#prev').css({display: 'block'});
  $('a#next').css({display: 'block'});
  if($(this).is(':first-child')) {
    $('a#prev').css({display: 'none'});
  } else if($(this).is(':last-child')) {
    $('a#next').css({display: 'none'});
  }
}

var popup_content = function(data, textStatus) {
  if(textStatus == 'success') {
    var elem;
    var page = $(data);
    var title = page.filter("h1");
    var content = page.filter("div#content");
    if($(content).hasClass('list')) {
      elem = $('div#lwindow');
      $(content).find("a.popup").click(function() {
	var url = $(this).attr('href');
	popup_open(url, $('div#cwindow'));
	return false;
      });
      elem.append(content);
    }
    else {
      elem = $('div#cwindow');
      $(content).find("a").each(function() {
	$(this).attr('target', 'new');
	if($(this).attr('href').match(/^\//))
	  $(this).addClass('internal');
	else
	  $(this).addClass('external');
      });
      elem.append(content);
      // Prev/Next Buttons
      if($('div#images img').length > 1) {
        // var buttons = '<div id="buttons"><div id="prev"><a href="#">Prev</a></div> <div id="next"><a href="#">Next</a></div></div>';
        // $('div#slideshow').append(buttons);
	$('a#next').css({display: 'block'});
      }
      // Cycle Lite Plugin
      $('div#images').cycle({height: '466px', timeout: 0, prev: '#prev', next: '#next', after: onTransition});

    }
    elem.children('div.wtitle').append(title);

    // jScrollPane
    $('div#content').jScrollPane({scrollbarMargin: 0});
  }
}

var overlay_zindex = function(action) {
  var ltop, lbottom;
  if(action == 'open') {
    ltop = $('div#overlay');
    lbottom = $('div#lwindow');
  } else {
    ltop = $('div#lwindow');
    lbottom = $('div#overlay');
  }
  var ztop = ltop.css('zIndex');
  var zbottom = lbottom.css('zIndex');
  ltop.css('zIndex', zbottom);
  lbottom.css('zIndex', ztop);
}

var popup_open = function(url, elem) {
  $.get(url, false, popup_content, "html");
  if(open_popups.length == 0)
    $('div#overlay').show().click(popup_close);
  else
    overlay_zindex('open');
  open_popups.push(elem);
  elem.show();
  elem.contents().find('p.wclose a').click(popup_close);
  open_urls.push(url);
  document.location.hash = '#' + url;
}

var popup_close = function() {
  var elem = open_popups.pop();
  elem.hide();
  elem.children().not('div.wtitle').remove();
  elem.contents().find('h1').remove();
  if(open_popups.length == 0)
    $('div#overlay').hide();
  else
    overlay_zindex('close');
  open_urls.pop();
  if(open_urls.length == 0)
    document.location.hash = '#';
  else
    document.location.hash = '#' + open_urls[open_urls.length - 1];
}

$(document).ready(function() {

  // Preloads prev/next arrows for the content images
  // This is a hack, there has to be a better solution
  var prev_img = new Image();
  var next_img = new Image();
  var prev_img_ie = new Image();
  var next_img_ie = new Image();
  $(prev_img).attr('src', '/static/images/misc/arrow-left.png');
  $(next_img).attr('src', '/static/images/misc/arrow-right.png');
  $(prev_img_ie).attr('src', '/static/images/misc/arrow-left.gif');
  $(next_img_ie).attr('src', '/static/images/misc/arrow-right.gif');

  // Background Loading
  var img = new Image();
  $(img).load(function() {
    $(this).hide();
    $('#background').removeClass('loading').append(this);
    $(this).show();
  }).attr('src', bg_image);

  // Adds "Enter Site" element
  $("body").prepend('<div id="enter"><p>Enter Site</p></div>');
  $('#enter').hide();
  $('#enter').mouseover(function() {
    $(this).fadeOut();
    $('#menu-main').fadeIn();
    $(this).unbind('mouseover');
  });

  // Menu centering
  $(window).resize(centering);

  // Submenu show/hide event bind
  $("a.submenu").click(function() {
    submenu_show($(this).attr("id"));
  });

  // PopUp
  $("a.popup").click(function() {
    var url = $(this).attr('href');
    if($(this).hasClass('list'))
      popup_open(url, $('div#lwindow'));
    else
      popup_open(url, $('div#cwindow'));
    document.location.hash = '#' + url;
    return false;
  });
  
  $("ul#menu-main > li > a.popup").click(submenu_reset);

});

$(window).load(function() {

  // Centering
  centering();

  // "Enter Site" element
  var enter = $('#enter');
  enter.css({
    top: $(window).height() / 2 - enter.outerHeight() / 2,
    left: $(window).width() / 2 - enter.outerWidth() / 2
  });
  enter.show();

  // Saves offsetHeight of each submenu
  $('ul.sec').each(function() {
    submenu_height[$(this).attr('id')] = $(this).height();
  });
  
  if(window.location.hash && window.location.hash.length > 1) {
    url = window.location.hash.substring(1);
    if(url.substring(0,5) == '/list')
      popup_open(url, $('div#lwindow'));
    else
      popup_open(url, $('div#cwindow'));
  }

});
