// JavaScript Document
var currentItem;
var currentLink;
var currentMenu;
var menuTimer;

var currentShare = null;
var currentShareMenu = null
var shareTimer = null;

var extraFunctions = new Array();

var overlayBackground;
var overlay;
var overlayActive = false;

$(".flash_movie").each(function(i,obj){
  var tid = obj.id;
  swfobject.registerObject( tid, "9.0.115", "expressInstall.swf" );
});

$(document).ready(function() {
  $(window).scroll(function(){
    windowScrolled();
  })
  $(window).resize(function(){
    windowResized();
  });
	//$(window).load(function(){
    $("#hub .hub_links .icon").each(function(i,obj){
      replaceImageLink( obj );
    });

    $(".black_button").each(function(i,obj){
      processBlackButton( obj );
    });

    $(".inactive_button").each(function(i,obj){
      $(".content", obj).wrap( "<div class=\"inactive_button_left\"><div class=\"inactive_button_right\"></div></div>" );
    });

    $(".active_button").each(function(i,obj){
      $("a", obj).wrap( "<div class=\"active_button_left\"><div class=\"active_button_right\"></div></div>" );
    });

    $(".standard_input").each(function(i,obj){
      $("input",obj).wrap( "<div class=\"standard_input_left\"><div class=\"standard_input_right\"><div class=\"standard_input_padding\"></div></div></div>" );
    });

    /*var currentMenu;
    $(".hub_nav .out").each(function(i,item){
      var link = $("a",item).get(0);
      var linkMenu;
      if( $(link).hasClass("menu") ) {
        var linkId = $(link).get(0).id;
        linkMenu = $("#"+linkId+"_menu").get(0);
      }
      $(link).hover(function(){
        link.mouseOver = true;
        showHubMenu( item, link, linkMenu );
      },function(){});
    });*/

  $(".menu").each(function(i,obj){
    var link = obj;
    var menu = $("#" + link.id + "_menu").get(0);
    $(link).hover(function(){
      if( !currentShare || currentShare != menu ) {
        if( shareTimer ) {
          clearTimeout( shareTimer );
        }
        if( currentShareMenu ) {
          currentShareMenu.style.display = "none";
        }
        currentShare = link;
        currentShareMenu = menu;
        currentShareMenu.style.display = "";
        link.mouseOver = true;
      }
    },function(){
      if( currentShareMenu && !currentShareMenu.mouseOver ) {
        startShareTimer();
      }
      link.mouseOver = false;
    });
    $(menu).hover(function(){
      menu.mouseOver = true;
      if( shareTimer ) {
        clearTimeout( shareTimer );
      }
    },function(){
      menu.mouseOver = false;
      if( currentShare && !currentShare.mouseOver ) {
        startShareTimer();
      }
    });
  });

    try {
      if( page_start ) {
        page_start();
      }
    } catch( e ) {}

    var totalFunctions = extraFunctions.length;
    for( var i = 0; i < totalFunctions; i++ ) {
      extraFunctions[i]();
    }
	//});
});

function startShareTimer() {
  shareTimer = setTimeout( function(){checkShareMenu()}, 500 );
}

function checkShareMenu() {
  if( !currentShareMenu.mouseOver && !currentShare.mouseOver ) {
    currentShareMenu.style.display = "none";
    currentShareMenu = null;
    currentShare = null;
  } else {
    startShareTimer();
  }
}

function showHubMenu( item, link, menu ) {
  if( currentItem && currentLink && currentMenu && currentMenu != menu && currentLink != link && currentItem != item ) {
    currentLink.mouseOver = false;
    currentMenu.mouseOver = false;
    hideHubMenu( currentItem, currentLink, currentMenu, true );
  }
  $(item).removeClass( "out" );
  $(item).addClass( "over" );
	if( menu ) {
		menu.style.display = "";
		menu.style.zIndex = "2";
    currentItem = item;
    currentLink = link;
		currentMenu = menu;
		$(menu).hover(function(){
			if( menuTimer ) {
        clearTimeout( menuTimer );
        menuTimer = null;
			}
			menu.mouseOver = true;
		},function(){
			menu.mouseOver = false;
	   	menuTimer = setTimeout( function(){hideHubMenu(item, link, menu);}, 500 );
		});
    $(link).hover(function(){},function(){
      link.mouseOver = false;
      menuTimer = setTimeout( function(){hideHubMenu(item, link, menu);}, 500 );
    });
	} else {
    $(link).hover(function(){},function(){
      link.mouseOver = false;
      hideHubMenu( item, link, menu );
    });
  }
}

function hideHubMenu( item, link, menu, h ) {
	var hideAnyway = ( h == null ) ? false : true;
	var hideObject = true;
	if (!link.mouseOver && (!menu || !menu.mouseOver)) {
    if (menu) {
      menu.style.display = "none";
      menu.style.zIndex = "1";
		}
    $(item).addClass( "out" );
    $(item).removeClass( "over" );
    $(link).hover(function(){
      link.mouseOver = true;
      showHubMenu( item, link, menu );
    },function(){});
	}
	if( menuTimer ) {
		clearTimeout( menuTimer );
		menuTimer = null;
	}
	if( !hideObject ) {
		menuTimer = setTimeout( function(){hideHubMenu(item, link, menu);}, 500 );
	}
}

function replaceImageLink( obj ) {
  var linkImg = $("img",obj).get(0);
  var linkObj = $(obj).get(0);
  linkImg.style.display = "";
  /*linkObj.style.width = linkImg.width;
  linkObj.style.height = linkImg.height/2;*/
  linkImg.style.display = "none";
  var srcSplit = linkImg.src.split("/");
  var imageDir = "";
  var imagesFound = false;
  for( i = 0; i < srcSplit.length; i++ ) {
    if( imagesFound ) {
      imageDir += srcSplit[i];
      if( i < srcSplit.length-1 ) {
        imageDir += "/";
      }
    }
    if( srcSplit[i] == "images" ) {
      imagesFound = true;
    }
  }
  linkObj.style.backgroundImage = "url('" + ( imagesFound ? styleDir + "/images/" + imageDir : linkImg.src ) + "')";
}

function setCookie( name, value, days ) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+days);
	document.cookie=name+ "=" +escape(value)+((days==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie( name ) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(name + "=");
		if (c_start!=-1) {
			c_start=c_start + name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function submitForm( id ) {
  $("form[name="+id+"]").get(0).submit();
  return false;
}

function processBlackButton( obj ) {
  $("a", obj).wrap( "<div class=\"black_button_left\"><div class=\"black_button_right\"></div></div>" );
}

function popoutPlayer( id ) {
  window.open("http://" + domain + root + "/index.php?extension=podcasts&action=get_podcast&podcast_id=" + id + "&template=podcast/show_podcast",'Darth Hater Podcast','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=44');
}

function showOverlay( id, onLoad, params, pass ) {
  $("#shell").after('<div id="' + id + '" class="overlay_bg"></div>');
  overlayBackground = $("#" + id).get(0);
  $(overlayBackground).css( "top", $(document).scrollTop() );
  $(overlayBackground).click(function(){
    if( !overlayActive ) {
      closeOverlay();
    }
  });
  var data = "";
  var current = 0;
  for( var key in params ) {
    if( current > 0 ) {
      data += "&";
    }
    data += key + "=" + params[key];
    current++;
  }
  $.ajax({
    type: "POST",
    url: root + "/index.php",
    data: data,
    dataType: 'html',
    success: function(data) {
      if( overlayBackground ) {
        $(overlayBackground).html(data);
        overlay = $(overlayBackground).children().get(0);
        $(overlay).css( "position", "absolute" );
        $(".black_button",overlayBackground).each(function(i,button){
          $("a", button).wrap( "<div class=\"black_button_left\"><div class=\"black_button_right\"></div></div>" );
        });
        $(overlay).hover(function(){
          overlayActive = true;
        },function(){
          overlayActive = false;
        });
        positionOverlay();
        onLoad( pass );
      }
    },
    error: function(data) {
      alert( "Overlay template could not be loaded." );
    }
  });
}

function closeOverlay() {
  $(overlayBackground).remove();
  overlayBackground = null;
  overlay = null;
  overlayActive = false;
}

function positionOverlay() {
  $(overlayBackground).css( "top", $(document).scrollTop() );
  $(overlay).css( "top", ( $(overlayBackground).height() / 2 ) - ( $(overlay).height() / 2 ) );
  $(overlay).css( "left", ( $(overlayBackground).width() / 2 ) - ( $(overlay).width() / 2 ) );
}

function windowScrolled() {
  if( overlayBackground ) {
    positionOverlay();
  }
}

function windowResized() {
  if( overlayBackground ) {
    positionOverlay();
  }
}