/**
 * Window It - jQuery plugin by Gary Manfredi
 * Copyright (c) 2008 Firehose
 * Take a query object and create a window with its html contents 
 * Arguments: width, height, windowTitle
 *
 * @example $('.myContent').windowit(500,   300, "My Content");
 * @cat plugin
 * @type jQuery 
 *
 */
 
// override these in your code to change the default messages and styles
var _WIdefaults = {
	border: 5, 
	titleHeight: 10, 
	paddingTop: 4, 
	paddingBottom: 4, 
	imagesPath: "images/"
}

var _WI;
_WI = jQuery.fn.windowit = function(options){
	
	// default options
	options = jQuery.extend({
	     w: 420,
	     h: 300,
		 title:"Window",
		 modal:false,
		 forceSize: false,
		 callback: function(){}
	  }, options);
	
	// store options in object for function reference
	_WI.options = options;
	
	// bound w & h if forceSize is false
	if (!options.forceSize) {
		var maxW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		var maxH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		options.w = Math.min(options.w, maxW - 90);
		options.h = Math.min(options.h, maxH - 60);
	}
	
	// apply overlay
	$("body","html").css({height: "100%", width: "100%"}); // IE 6 fix
	if($("#WI_overlay").length == 0){
		var overlay = $('<div id="WI_overlay"></div>').appendTo($(document.body));
		if (!options.modal) overlay.css({ cursor: "pointer" }).click(_WI.close);
	}
	
	// if IE6 then set position of overlay to absolute instead of fixed
	if($.browser.msie ){
		var n = (navigator.appVersion).indexOf("MSIE") + 5;
		var version = parseInt((navigator.appVersion).substr(n, n + 3));
		if(version == 6) $("#WI_overlay").css("position", "absolute");
	}
	
	// create new window and set width/height
	_WI.win = $('<div class="WI_win"><div class="WI_title"></div><div class="WI_content"></div></div>')
		.width(options.w).height(options.h);
 
	// if forceSize then set position of WI_win to absolute instead of fixed (to scroll)
	if(options.forceSize){
		_WI.win.css("position", "absolute");
	}
	
	// set title and content and size content
	$(".WI_title", _WI.win).html(options.title); // should add id or alt or something
	$(".WI_content", _WI.win).append(this).height(options.h - _WIdefaults.border*2 - _WIdefaults.titleHeight - _WIdefaults.paddingTop - _WIdefaults.paddingBottom);
	
	/* draggable disabled because window started appearing on bottom for some reason
	// if interface draggables if installed, make window draggable
	if(!options.modal && typeof(_WI.win.draggable) == "function"){
		_WI.win.draggable({
			handle:".WI_title",
			zIndex:2001
		});
		$(".WI_title", _WI.win).css("cursor", "move");
	}
	*/
	
	// set close actions
	$(".WI_title", _WI.win).append($('<div class="WI_close">Esc key or <a href="javascript:void(0)">Close</a>' +
		'<img src="' + _WIdefaults.imagesPath + 'close.gif" /></div>'));
	$(".WI_close img", _WI.win).click(_WI.close);
	$(".WI_close a", _WI.win).click(_WI.close);
	
    // set escape to trigger keypress
    $(document).keydown(function(event){
        if (event.keyCode == 27) {
            $(document).unbind("keydown");
            _WI.close();
        }
        return true;
    });
	
	// attach window to body and center
	$(document.body).append(_WI.win.hide());
	_WI.center();
	_WI.win.show();
	//_WI.win.width(w).height(h);
			
	return this;
}

_WI.center = function() {
	var maxW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	var maxH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	_WI.win.css({
		left: Math.max((maxW - 20 - _WI.win.width())/2, 0) + "px",
		top: Math.max((maxH - _WI.win.height())/2.3, 0) + "px"
	});
}

// close window and remove it
_WI.close = function(effectSpeed){
	var s = effectSpeed || 300; // (effectSpeed == null) ? 300 : effectSpeed;
	$("#WI_overlay").remove();

	//$(".WI_win").hide(s, function(){  // TODO check if hiding effect speed works in upgraded jQuery
        $(".WI_win").remove();
	//});
	
	// remove key press listening
    $(document).keypress(function(e){});
	
	// do optional callback on close
	_WI.options.callback();
}

// resize and center to fit new window size
_WI.resize = function(){
	if (_WI.win == null) return;
	
	// bound w & h
	var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
	var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	
	// reset window & content area's width and/or height
	_WI.win.width(w-90).height(h-60);
	$(".WI_content", _WI.win).height(h - _WIdefaults.border*2 - _WIdefaults.titleHeight - _WIdefaults.paddingTop - _WIdefaults.paddingBottom);
 	_WI.center();
}
