/*
This file will contain all of your custom skin JavaScript.
*/

$(document).ready( function() {
	$(window).click( function() {
		hideDropMenu();
	});
	
	$(".site-drop-menu a.primary ").each( function() {
		var linkwidth = $(this).width();
		var dropwidth = $(this).find("~ ul").width();
		
		if (dropwidth >= linkwidth) {
			$(this).css("min-width",dropwidth);
		} else {
			$(this).find("~ ul").css("min-width",linkwidth);	
		}
	});
	
	$(".site-drop-menu a.primary").mouseover( function() {
		$(".site-drop-menu > ul ul").hide();
		$(".site-drop-menu a.primary").removeClass("primaryselected");
		$(this).addClass("primaryselected");
		$(this).parent().find(" > ul").show();	
	});
	
	$(".site-home-menu > a").toggle(
      function () {
	    hideDropMenu();
		$("#siteNavTree").slideDown();	
		$(this).addClass("drop-selected");
      },
      function () {
		$("#siteNavTree").slideUp();
		$(this).removeClass("drop-selected");
      }
    );
    
    $(".show-files").toggle(
      function () {
		$(".page-files").slideDown();	
		$(this).html("hide files");
      },
      function () {
		$(".page-files").slideUp();	
		$(this).html("show files");
      }
    );
	
	$(".show-comments").toggle(
      function () {
		$(".page-comments").slideDown();	
		$(this).html("hide comments");
      },
      function () {
		$(".page-comments").slideUp();	
		$(this).html("show comments");
      }
    );
    
    $(".popup").each( function() {
	    var pid = $(this).attr("pid");
		$("." + pid).prepend('<a href="#" pid="' + pid + '" class="popup-close">x</a>');    
	});
    
    $(".popup").click( function() {
	    var pid = $(this).attr("pid");
	    
	    //The Height of the popup BG
	    var pbgheight = $(window).height();
	    var pbgwidth = $(window).width();
	    
	    //The Height of the popup Frame
	    var pheight = $("." + pid).height();
	    var pwidth = $("." + pid).width();
	    
	    var pleft = (pbgwidth - pwidth)/2; 
	    var ptop = (pbgheight - pheight)/2;
	    var ptoppx = ptop + "px";
	    var pleftpx = pleft + "px";
	    
		$(".popup-bg").css("height",$(document).height());    
		$(".popup-bg").show(); 
		$("." + pid).css("top",ptoppx)
		$("." + pid).css("left",pleftpx)
		$("." + pid).fadeIn();	
		return false;
	});
	
	$(".popup-close").live("click", function() {
		var pid = $(this).attr("pid");
		$("." + pid).fadeOut( function() {
			$(".popup-bg").hide();	
		});	
	});

});

function hideDropMenu() {
	$(".site-drop-menu > ul ul").hide();
	$(".site-drop-menu a.primary").removeClass("primaryselected");	
}