/* parseQueryString.js - a function to parse and decode query strings
 *
 * The author of this program, Safalra (Stephen Morley), irrevocably releases
 * all rights to this program, with the intention of it becoming part of the
 * public domain. Because this program is released into the public domain, it
 * comes with no warranty either expressed or implied, to the extent permitted
 * by law.
 *
 * For more public domain JavaScript code by the same author, visit:
 * http://www.safalra.com/web-design/javascript/
 */
function parseQueryString(_1){var _2={};if(_1==undefined){_1=location.search?location.search:"";}if(_1.charAt(0)=="?"){_1=_1.substring(1);}_1=_1.replace(/\+/g," ");var _3=_1.split(/[&;]/g);for(var i=0;i<_3.length;i++){var _5=_3[i].split("=");var _6=decodeURIComponent(_5[0]);var _7=decodeURIComponent(_5[1]);if(!_2[_6]){_2[_6]=[];}_2[_6].push((_5.length==1)?"":_7);}return _2;}

function  LoadCssFile(filename) {
	var fileref=document.createElement("link")
  	fileref.setAttribute("rel", "stylesheet")
  	fileref.setAttribute("type", "text/css")
  	fileref.setAttribute("href", filename)
}


/* ************************************************************************************* */
/*						JavaScript Modal Popup Extension								 */
/*	adapted from http://www.queness.com/post/77/simple-jquery-modal-window-tutorial		 */
/* ************************************************************************************* */
function ModalContent(value) {
	this.Value = value;
}

function Modal(parent, displayHtml) {
	//LoadCssFile(
	var _parentDomPath = parent;
	var _innerPreContent = '<div id="boxes">' + 
		'<!-- #customize your modal window here -->' + 
		'<div id="dialog" class="window"> ' + 
			'<!-- close button is defined as close class -->  ' +
			'<a href="#" style="float:right;" class="close">[X]</a>  ' +
			'<div id="modalContent">';		
	var _innerPostContent = '' + 
			'</div>' +
		'</div>' +     
		'<!-- Do not remove div#mask, because you\'ll need it to fill the whole screen --> ' +   
		'<div id="mask"></div>'
		'</div>';
	var _innerHtml = _innerPreContent + displayHtml + _innerPostContent;
	
	if (typeof jQuery == 'undefined') {
		throw 'jQuery is undefined';
	}
	else {
		this.Parent = $(_parentDomPath);
		this.InnerHtml = _innerHtml;
		this.Initialize = Initialize;		
		this.Show = show;
		this.Hide = hide;
		this.ClearContents = function()  {
			this.InnerHtml = _innerPreContent + _innerPostContent;
			this.Parent.html(this.InnerHtml);
		};
		this.ResetContents = function(content) {
			this.InnerHtml = _innerPreContent + content + _innerPostContent;
			this.Parent.html(this.InnerHtml);
		};
		this.Initialize();	
	}
	
	function Initialize() {
		//set innerHtml of modal popup
		this.ResetContents(displayHtml);
		
		
		//if close button is clicked
		$('.window .close').bind( 'click', function (e) {
			//Cancel the link behavior
			e.preventDefault();
			hide();
		});		
		
		//if mask is clicked
		$('#mask').bind( 'click', function () {
			hide();
		});			

		//if any key is pressed close the popup
		$(document).keyup(function(e) {
			if(e.keyCode == 13) {
				hide();
			}
		});
	}
	
	//e is the anchor as a jquey object
	function show(e) {
		//Cancel the link behavior
		if (!e)	{ }
		else	{ 
			e.preventDefault(); 
		}
		//Get the A tag
		//var id = $(obj).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
			  
		//Set the popup window to center
		$('#boxes #dialog').css('top',  winH/2-$('#boxes #dialog').height()/2);
		$('#boxes #dialog').css('left', winW/2-$('#boxes #dialog').width()/2);
	
		//transition effect
		$('#boxes #dialog').fadeIn(2000); 
	}
	
	function hide() {
		$('#mask').hide();
		$('.window').hide();
	}
}


$(document).ready(function() {	
	//add listener on click of anchor tag:
	$('a[name=modal]').bind('click', function(e) {
		e.preventDefault();
		var qs = parseQueryString($(this).attr('href'));
		var error = false;
		if (!qs.parent) {
			throw 'parent query string is not defined.';
			error = true;
		}
		if (!qs.html && !qs.obj && !qs.jObj) {
			throw 'html content of modal popup is not defined.';
			error = true;
		}
		
		if (typeof qs.preback !== 'undefined') {
			eval(qs.preback[0]+';');
		}
		
		if (!error) {
			//if we want to take the html right from the querystring:
			if (!qs.obj && !qs.jObj) {
				var m = new Modal(qs.parent[0], qs.html);
				m.Show();
			}
			//if we want to take the html from a modalContent object:
			else if (qs.obj && !qs.jObj)  {
				var m = new Modal(qs.parent[0], eval(qs.obj[0]).Value);
				m.Show();
			}
			//if we want to take the html from a jquery object:
			else if (qs.jObj) {
				var m = new Modal(qs.parent[0], $(qs.jObj[0]).html());
				m.Show();
			}
		}
		
		if (typeof qs.callback !== 'undefined') {
			eval(qs.callback[0]+';');	
		}
		
	});

	
});
