/* subscribe / fire custom events */
function Observer() {
    this.fns = [];
	this.subscribe=function(fn){this.fns.push(fn)}
	this.unsubscribe=function(fn){this.fns = this.fns.filter(function(el){if( el !== fn )return el})}
	this.fire=function(o, thisObj) {
        var scope = thisObj || window;
		for(var i=0;i<this.fns.length;i++) 
			this.fns[i].call(scope, o);
    }
}
hideWipeEvent=new Observer();
showWipeEvent=new Observer();
onLoadEvent=new Observer();
showWipeEvent.subscribe(function(){pageWipeInit();scroll(0,0);});

//onload
$(function(){
	onLoadEvent.fire();
});

/* Popup window handling */
function pageWipeInit(){
		try {
			$('#pageWipe').click(function(){hideWipeBox()});
			$('#pageWipe').css('height',parseInt($('#pageWipe').css('height'))-20);
			if (isNaN(window.innerHeight + window.scrollMaxY)){
				var yScroll = document.body.scrollHeight;
				var xScroll = document.body.scrollWidth;
			}else {
				var yScroll = window.innerHeight + window.scrollMaxY;
				var xScroll = window.innerWidth + window.scrollMaxX;
			};
			var pwb=document.getElementById('pageWipeBox')
			pwb.style.width='850px';
			//document.getElementById('pageWipeBox').style.width='699px';
			$('#pageWipe').css('height',yScroll+20);
			$('#pageWipeBox').css('margin-left',0);
			var boxleft=parseInt(xScroll/2-parseInt($("#pageWipeBox").css('width'))/2);		
			$("#pageWipeBox").css('left',boxleft);
		} catch(e) {
			// for IE's benefit
			//alert(e.message);
			return false;
		}	
		/*
		try {
			$('#pageWipe').click(function(){hideWipeBox()});
			$('#pageWipe').css('height',parseInt($('#pageWipe').css('height'))-20); 
			var yScroll =  $(window).height();
			var xScroll =  $(window).width();
			var pwb=document.getElementById('pageWipeBox')
			pwb.style.width='850px';
			$('#pageWipe').css('height',yScroll+20);
			$('#pageWipeBox').css('margin-left',0);
			var boxleft=parseInt(xScroll/2-parseInt($("#pageWipeBox").css('width'))/2);		
			$("#pageWipeBox").css('left',boxleft);
		} catch(e) {
			// for IE's benefit
			//alert(e.message);
			return false;
		}
		*/
	}
function showWipeBox(){
		$('#PopupWipe').css('display','block'); 
		$('.flashArea').css('display','none');		
	}
function hrefToPopup(aTag){
	try{
		loadWipeBox(aTag.href);
	}catch(e){
		alert(e.message);
	}
	return false;
}
function loadWipeBox(url){
		scroll(0,0);
		pageWipeInit();
		showWipeBox();
		$("#pageWipeBox").html('<div id="loadingIcon"></div>'); 
		$("#pageWipeBox").load(url,function(){scroll(0,0);showWipeEvent.fire();pageWipeInit();});		
	}
function hideWipeBox(){
		$('#PopupWipe').css('display','none'); 
		$("#pageWipeBox").html(''); 
		$('.flashArea').css('display','block');	
		hideWipeEvent.fire();
		
	}
window.onresize=function(){pageWipeInit();};