/*
* jQuery PopBox Plugin
*
* @name jquery.popbox.js 
* @author William Henry
* @version 0.0.1-alpha
* @date December 13, 2007
* @category jQuery plugin
* @copyright (c) 2007 William Henry
*/


(function($) {
	var strPage = null;
	var arrID = Array();
	
	// plugin code here, use $ as much as you like
	$.fn.popuppage = function(options) {
		
		//First we'll need to store ID's being passed - Passed IDs for for click event to show popuppage
		this.each(function() {
			arrID.push(this.id);
		});
		
		// Load OPTIONS
		strPage = options.page; //page to load

		if(strPage == '') {
			alert('No page passed, please setup popuppage with a page');
		}

		//ajax request to grab page html
		prepMainPage();
	}

	function loadPage()
	{
		//set intial css for popup page (to show loading in the middle of screen
		$('#popuppage').css({
			top: (screen.height/2) - ($('#popuppage').height()/2) - 100,
			left: (screen.width/2) - ($('#popuppage').width()/2)
		});
		
		//make visible
		$('#popuppage').css({visibility: 'visible'});
	
		//get page pas intially to load into popuppage
		$.get(strPage, function(strData) {
			$('#popuppage').html(strData);
	
			//grab child so we know size boxes should be
			var objChild = $('#popuppage').children();

			//calculate top and left to make it center screen
			var intTop = (screen.height/2) - (objChild.height()/2);
			var intLeft = (screen.width/2) - (objChild.width()/2);

			//show some style and make it come in with finess instead of just jumping sizes
			$('#popuppage').animate({
				width: objChild.width(),
				height: objChild.height(), 
				top: intTop,
				left: intLeft
			}, 400);
		});
	}

	function lowerlights()
	{
		$('#popuppage-overlay').css({opacity: '.9'}).fadeIn();
	}

	function raiselights()
	{
		$('#popuppage-overlay').fadeOut();
	}

	function prepMainPage()
	{
		$('head').append('<link rel="stylesheet" type="text/css" href="./css/jquery.popuppage.css" media="screen" />');

		// Append markup
		$('body').append('<div id="popuppage-overlay"></div><div id="popuppage" style="width: 300px; height: 300px; background-color: #fff;"></div>');

		$('#popuppage-overlay').hide();
		$('#popuppage').css({visibility: 'hidden'});

		//add click event to all id's passed
		for( i = 0 ; i < arrID.length ; ++i )
		{
			$('#' + arrID[i]).click(function() {
				$('#popuppage').html('<div id="popuppage-loading"><img src="./images/loading.gif" /></div>');
				lowerlights();
				loadPage();
			});
		}
	
		//add event to user clicking off stage
		$('#popuppage-overlay').click(function() {
			$('#popuppage').css({visibility: 'hidden'}); //hide();
			$('#popuppage').html('')
			raiselights();
		});
	}
})(jQuery);

//allow easy access to close popuppage from the popuppage
function closePopuppage()
{
	$('#popuppage').css({visibility: 'hidden'}); //hide()
	$('#popuppage').html('');
	$('#popuppage-overlay').fadeOut();
}

function resizePopuppage(strWidth, strHeight, strHTML)
{
	var objChild = strHTML.children(); 
	alert(objChild.height());
}

