/*
	myWindow.js
	Created by James Snook ( jimothy@whoisjimothy.com )
	
	This file contains all of the functions needed to create my window objects.  
	The windows are created on demand and are given relevant content to display.
	The goal was to make the windows remember what content to display, thus avoiding conflicting requests when opening 5 windows at once.
	Also, this makes keeping track of each window much, much easier.
	
	Think you can improve this?  Please let me know.  I'm new to creating custom javascript objects...
	
	Thank you for your interest in my web site :)
*/
	
///////////////////////////////////////////////////////////
popupWindow.prototype.showProperties = function(){
	alert(this.page);
	alert(this.windowText);
	alert(this.baseURL);
	alert(this.refreshLoc);
}
///////////////////////////////////////////////////////////
popupWindow.prototype.openWindow = function(x, y, z){
	$("#desktop").append(this.windowText);
	this.content=$("."+this.page+"_window .window .content");
	this.window=$("."+this.page+"_window");
	this.refreshLink=$("."+this.page+"_window .titlebar .refresh");
	this.homeLink=$("."+this.page+"_window .titlebar .home");
	this.window.css('z-index', z);
	this.window.css('left', x);
	this.window.css('top', y);
}
///////////////////////////////////////////////////////////
popupWindow.prototype.moveToTop = function(){
	this.window.css('z-index', baseIndex++);
}
///////////////////////////////////////////////////////////
popupWindow.prototype.showWindow = function(){
	this.window.removeClass("minimized");
	this.window.children(".window").css('display', 'block');
	this.window.children(".titlebar").children(".min").attr('src', '/graphics/button_min.png');
	this.window.css('height', windowHeight);
	this.window.css('z-index', baseIndex++);
	this.content.hide();
	this.window.animate({opacity: "show"}, "slow");	
}
popupWindow.prototype.handleDataRequest = function(newData){
	
	var windowObject=this;
	this.pageLoaded = true;
	this.content.hide();
	this.content.html(newData).fadeIn("medium");
	var page=this.page;
	$("."+page+"_window a").click(function(event) {
		if($(this).attr("href")!="#"){
			if(isInternal($(this).attr("href")))
			{
				event.preventDefault();
				link=convertHref($(this).attr("href"));
				windowObject.openURL(link, true);
				updateHash(link, true);
			}
			else if($(this).attr("href").indexOf("#")!=-1){
				event.preventDefault();
				var link = $(this).attr("href").substring(1);
				var windowName = stCap(link);
				rootlinks(link, windowName);
			}
		}
		else if($(this).attr("href")=="#"){ event.preventDefault(); }
		else window.location=$(this).attr("href");
	});
}
///////////////////////////////////////////////////////////
popupWindow.prototype.openURL = function(section, doFade){
	this.refreshLoc=section;
	var page=this.page;
	var windowObject=this;
	this.pageLoaded=false;
	var theWindow="."+this.page+"_window .window .content";
	if(doFade){
		$(theWindow).hide();
		if (!this.pageLoaded) {
			$(theWindow).html('<p class="page-loading"><img src="/graphics/loading.gif" alt=""/></p>').fadeIn("medium");
		}
	}
	$.get("http://"+location.host+"/" + section, {ajax: true}, function(data) {
		windowObject.handleDataRequest(data);
	});
}

///////////////////////////////////////////////////////////
popupWindow.prototype.setActions = function()
{
	var windowObject=this;
	this.refreshLink.click(function()
	{
		windowObject.openURL(windowObject.refreshLoc, true);
	});
	
	this.homeLink.click(function()
	{
		windowObject.openURL(windowObject.homeLoc, true);
		updateHash(windowObject.homeLoc, true);
	});

	$('.popup').draggable({ handle: '.titlebar', containment: '#body-mimic' });
	$('.popup').mousedown(function() 
	{
		$(this).css('z-index', baseIndex++);
	});
	
	this.window.children(".titlebar").children(".close").click(function() 
	{
		$(this).parent().parent().animate({opacity: "hide"}, "medium");
		updateHash(windowObject.page,false);
	});
	
	
	this.window.children(".titlebar").children(".min").click(function() 
	{
		if($(this).attr('src')=="/graphics/button_min.png")
		{ 
			$(this).parent().parent().addClass("minimized");
			$(this).parent().parent().children(".window").css('display', 'none');
			$(this).attr('src', '/graphics/button_max.png');
			$(this).attr('title', 'Maximize window');
			$(this).parent().parent().css('height', '28px');
		}
		else{
			$(this).parent().parent().removeClass("minimized");
			$(this).parent().parent().children(".window").css('display', 'block');
			$(this).attr('src', '/graphics/button_min.png');
			$(this).attr('title', 'Minimize window');
			$(this).parent().parent().css('z-index', baseIndex++);
			$(this).parent().parent().css('height', windowHeight);
		}
	});
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
function popupWindow(name) 
{
	this.page=name.toLowerCase();
	this.windowText='<div class="popup '+this.page+'_window">';
	this.windowText+='<div class="titlebar"><span>'+name+'</span>';
	this.windowText+='<img src="/graphics/button_close.png" class="title-button close" title="Close window" alt=""/>';
	this.windowText+='<img src="/graphics/button_min.png" class="title-button min" title="Minimize window" alt=""/>';
	//if(name=="Blog") this.windowText+='<img src="/graphics/button_refresh.png" class="title-button refresh" title="Refresh page" alt=""/>';
	this.windowText+='<img src="/graphics/button_home.png" class="title-button home" title="Go to this window\'s home page" alt=""/>';
	this.windowText+='</div><div class="window"><div class="content"><div class="loading"></div></div></div></div>';	
	if(this.page=="blog") this.baseURL="blog/main";
	else this.baseURL=this.page;
	this.refreshLoc=this.baseURL;
	this.homeLoc=this.baseURL;
	this.pageLoaded=false;
	this.data="";
}