$(document).ready(function()
{
	//Start Menu Scripts
	var newWindowHeight = $(window).height();
	var newWindowWidth = $(window).width();
	menuOpen=false;
	baseIndex=4;
	baseX=410;
	baseY=70;
	pages = new Array();
	windowHeight='555px';
	
	//pull down menu click	
	$(".btn-slide").click(function()
	{
		if(!menuOpen)
		{	$("#panel").animate({ top: 99	}, "medium");
			menuOpen=true;
		}
		else
		{	$("#panel").animate({ top: -220	}, "medium");
			menuOpen=false;
		}
	});
	
	
	//start menu clicks
	$(".start-menu li").click(function(event) {
		var linkText = $(this).text().toLowerCase();
		var windowName = $(this).text();
		rootlinks(linkText, windowName);
	});
	
	$('a').click(function(){
		var linkhref = $(this).attr('href');
		if(linkhref=="#") return false;
		if(linkhref.indexOf('#')!=-1) 
		{
			linkhref=linkhref.substring(1); 
			var windowName = stCap(linkhref);
			rootlinks(linkhref, windowName);
			return false;
		}
	});
	
	//beta button
	 $("#beta-info").hover(function() {
		$(this).children('.beta-bubble').fadeIn("fast");
	}, function() {
		$(this).children('.beta-bubble').fadeOut("fast");
	});
	
	//--------------- GET INITIAL HASH AND OPEN WINDOWS --------------------
	if(location.hash!=""){
		var a=location.hash.split("&");
		a[0]=a[0].substring(1);
		for (x in a) {
			var section=a[x];
			linkText=getPageFromSegment(section);
			if(linkText=="blog"||linkText=="about"||linkText=="contact"||linkText=="portfolio"||linkText=="gallery"){
				windowName=linkText;
				pages[linkText]=new popupWindow(windowName);
				pages[linkText].openWindow(baseX, baseY, baseIndex);
				pages[linkText].showWindow();
				pages[linkText].setActions();
				baseX+=30;
				baseY+=30;
				baseIndex++;
				pages[linkText].openURL(a[x], true);
			}
		}
	}
	
	$(window).bind("resize", resizeWindow);
	if($(window).height()>750) newWindowHeight = $(window).height();
	newWindowWidth = $(window).width();
	$("#body-mimic").css("height", newWindowHeight );
	$("#body-mimic").css("width", newWindowWidth );
	//resizeWindow();
	
	
});
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
function resizeWindow( ) 
{
	if($(window).height()>750) newWindowHeight = $(window).height();
	newWindowWidth = $(window).width();
	$("#body-mimic").css("height", newWindowHeight );
	$("#body-mimic").css("width", newWindowWidth );
}
////////////////////////////////////////////////////////////////
function stCap(strObj){
	return(strObj.charAt(0).toUpperCase()+strObj.substr(1).toLowerCase());
}
////////////////////////////////////////////////////////////////
function rootlinks( pagelink, windowTitle ) 
{
	if ( $("."+pagelink+"_window").length )
	{
		pages[pagelink].showWindow();
		pages[pagelink].openURL(pages[pagelink].baseURL, true);
		updateHash(pages[pagelink].baseURL, true);
	}
	else {
		pages[pagelink]=new popupWindow(windowTitle);
		pages[pagelink].openWindow(baseX, baseY, baseIndex);
		pages[pagelink].showWindow();
		pages[pagelink].setActions();
		baseX+=30;
		baseY+=30;
		baseIndex++;
		pages[pagelink].openURL(pages[pagelink].baseURL, true);
		updateHash(pages[pagelink].baseURL, true);
	}
}
////////////////////////////////////////////////////////////////
function updateHash(segment, add){
	var str=location.hash;
	if(str=="") location.hash=segment;
	else
	{
		found=false;
		var page = getPageFromSegment(segment);
		var segmentToReplace="";
		a=str.split("&");
		a[0]=a[0].substring(1);
		for (x in a) {
			if (page==getPageFromSegment(a[x])) 
			{
				found=true;
				segmentToReplace=a[x];
			}
		}
		if(found){
			if(add)	location.hash=location.hash.replace(segmentToReplace, segment);
			else {
				if(location.hash.replace(segmentToReplace, "")=="") location.hash="#";
				else location.hash=location.hash.replace(segmentToReplace, "");
				if(location.hash.replace("&&", "&")=="") location.hash="#";
				else location.hash=location.hash.replace("&&", "&");
				if(location.hash[1]=="&") {
					newStr = location.hash.substring(2);
					if(location.hash[0]+newStr=="") location.hash="#";
					else location.hash=location.hash[0]+newStr;
				}
				if(location.hash[location.hash.length-1]=="&") 
				{
					if(location.hash.substring(0,location.hash.length-1)=="") location.hash="#";
					else location.hash=location.hash.substring(0,location.hash.length-1);
				}
			}
		}
		if(!found) location.hash=location.hash+"&"+segment;
	}
	
}
////////////////////////////////////////////////////////////////
function getPageFromSegment(segment){
	if(segment.search("/")!=-1)
	{
		return segment.substring(0,segment.indexOf("/"));
	}
	else return segment;
}
////////////////////////////////////////////////////////////////
function isInternal(url){
	str=""+url;
	return (str.indexOf(location.host)>=0);
}
////////////////////////////////////////////////////////////////
function convertHref(url){
	return url.replace("http://www.whoisjimothy.com/","");
}
