/**************************************************************************************\
*   Name:           Router                                                             *
*   Author:         Philip Rasmussen (DK)                                              *
*   Version:        0.1.0 (beta)                                                       *
*   Description:	Attempts to fight the problems related to frames                   *
\**************************************************************************************/

function FrRouter($frsetBaseAddr, $fr2BaseAddr){				
	this.$frsetBaseAddr = $frsetBaseAddr;
	this.$fr2BaseAddr = $fr2BaseAddr;
	
	this.$fr2InitLocIsSet = 0;
	
	this.$frsetLastAddr = "";
	this.$fr2LastAddr = "";	
	
	this.$frsetNewAddr = "";
	this.$fr2NewAddr = "";

	this.$fr2IsBusy = 0;
	
	this.$fastTick = setInterval("$frRouter.fastTick();",1);
	
	this.$normalTick = null;
	
	this.$regExp01 = new RegExp('^'+this.$frsetBaseAddr+'#?/?','gi');
	this.$regExp02 = new RegExp('^' + this.$fr2BaseAddr, 'gi');
	this.$regExp03 = new RegExp('^\#\/' + this.$frsetBaseAddr + '\#\/', 'gi');
	this.$regExp04 = new RegExp('^' + this.$frsetBaseAddr + '\#\/', 'gi');
}	

FrRouter.prototype.fastTick = function(){
	this.$busy = 1;
	
	if(typeof(fr2) == "undefined"){
		this.$busy = 0;
		return;
	}
	
	//If permission denied.
	try{
		if( typeof(fr2.document) != undefined){}
	}catch(e){
		document.getElementById("fr2").src = "";		
		clearInterval(this.$fastTick);
		this.$fastTick = null;
		return;		
	}
	
	//Switch content page to anchored address given to frameset. Otherwise default/first content page will load.
	if(this.$fr2InitLocIsSet == 0){

		if(this.$frsetBaseAddr != document.location.href){
			//Stop loading current page in fr2 (experimental)
			fr2.document.location.href = "about:blank";
			
			//Create new address for fr2
			this.$fr2NewAddr =
				this.$fr2BaseAddr 
				+ document.location.href.replace(this.$regExp01,"")
				  .replace("%23","#");//%23 replace with # handles osx safari bug.;
				
			//Reload fr2 to new location	
			fr2.document.location.href = this.$fr2NewAddr;			
			
			this.$fr2LastAddr = this.$fr2NewAddr; 		
			
			this.$fr2NewAddr = "";		
		}else{
			this.$fr2LastAddr = this.$fr2BaseAddr;
		}
		
		this.$frsetLastAddr = document.location.href;
		
		//Mark as done
		this.$fr2InitLocIsSet = 1;
		
	}else if( typeof(fr2.document) != "undefined" && "about:blank" != fr2.document.location.href){
		//Mark as finished
		this.$fr2InitLocIsSet = 2;
		
		//Fix IE7 iframe height issue
		if( parseInt(document.getElementById("fr2").clientHeight) < parseInt(document.body.clientHeight) ){
			document.getElementById("fr2").style.height = parseInt(document.body.clientHeight)+"px";
		}		
	}
	
	//If both initialisations is set, stop fast tick.
	if(this.$fr2InitLocIsSet == 2){
		clearInterval(this.$fastTick);
		this.$fastTick = null;	
	}	 

	this.$busy = 0;
};


FrRouter.prototype.normalTick = function(){
	if (this.$fr2InitLocIsSet != 2 || this.$busy == 1) {
		return;
	}
		
	this.$busy = 1;
	
	//If permission denied.
	try{
		if( typeof(fr2.document.location.href) != undefined){}
	}catch(e){
		if (!this.$fr2IsBusy) {
			//Stop loading current page in fr2 (experimental)
			document.getElementById("fr2").src = "about:blank";
			
			//Create new address for fr2
			this.$fr2NewAddr = this.$fr2BaseAddr +
			document.location.href.replace(this.$regExp01, "").replace("%23", "#");//%23 replace with # handles osx safari bug.;
			//Reload fr2 to new location	
			document.getElementById("fr2").src = this.$fr2NewAddr;
			
			this.$fr2LastAddr = this.$fr2NewAddr;
			
			this.$fr2NewAddr = "";
			
			this.$fr2IsBusy = 1;
		}
		
		this.$busy = 0;
		
		return;		
	}
		
	//If fr2 adress has changed
	if(this.$fr2LastAddr != fr2.document.location.href){
		if (!this.$fr2IsBusy) {
			//Create new address for frset
			this.$frsetNewAddr = 
				'#/' 
				+ fr2.document.location.href.replace(this.$regExp02, "");
			
			//"#/http://dev3.pickupsongs.odry/#/privacy.html"
			if(this.$regExp03.exec(this.$frsetNewAddr)){
				this.$frsetNewAddr = 
					'#/' 
					+ fr2.document.location.href.replace(this.$regExp04, "");
				
				fr2.document.location.href =
					this.$fr2BaseAddr
					+ fr2.document.location.href.replace(this.$regExp04, "");
						 
				this.$fr2IsBusy = 1;				
			}
			
			//Store new address		 
			this.$frsetLastAddr = this.$frsetBaseAddr + this.$frsetNewAddr;
			
			//If new frset address now is different from current	
			if (this.$frsetLastAddr != document.location.href) {
				//Update frset location hash	
				if (typeof(window.history.replaceState) == "function") {
					window.history.replaceState("", "", this.$frsetLastAddr);
				}
				else {
					document.location.hash = this.$frsetNewAddr;
				}
			}
			
			this.$frsetNewAddr = "";
			
			//Store new fr2 address
			this.$fr2LastAddr = fr2.document.location.href;
		}
	}else if(this.$fr2IsBusy){
		this.$fr2IsBusy = 0;	
	}
	
	//console.log((this.$frsetLastAddr != document.location.href?"!=":"=="));
		
	//If frameset anchored address has changed
	if(this.$frsetLastAddr != document.location.href){
		//Create new address for fr2
		this.$fr2NewAddr =
			this.$fr2BaseAddr + 
			document.location.href.replace(
				new RegExp('^'+this.$frsetBaseAddr+'#?/?','gi'),
				""
			).replace("%23","#");//%23 replace with # handles osx safari bug.
		
		//If new fr2 address is different from current	
		if(this.$fr2NewAddr != fr2.document.location.href){
			//Reload fr2 to new location	
			fr2.document.location.href = this.$fr2NewAddr;			
			this.$fr2LastAddr = this.$fr2NewAddr;
			this.$fr2IsBusy = 1;
		}	
		
		this.$fr2NewAddr = "";	
			
		//Store new frset address
		this.$frsetLastAddr = document.location.href;
	}
	
	this.$busy = 0;
};


FrRouter.prototype.frsetOnload = function(){
	setTimeout(function(){
		if (this.$fastTick != null) {
			clearInterval(this.$fastTick);
			this.$fastTick = null;
		}		
	},5000);
	
	this.$normalTick = setInterval("$frRouter.normalTick();",1000);
	
};	
			
FrRouter.prototype.frsetUnload = function(){

};

FrRouter.prototype.fr2DomOnLoad = function(){
	this.normalTick();
};

jQuery(function(){

	var subdirectory = 'en';
	
	var dir = '';
	
	var title = "Loading ..."; // Title to show between frame loads
	var url = location.protocol+'//'+location.host+'/'+dir+subdirectory+(location.hash).substring(1); // Set src of iframe based on hash

	jQuery('#fr2').attr('src',url).load(function(){

		var path = fr2.location.pathname;
		var loc = fr2.location.href;
		var pos = (fr2.location.protocol+'//'+fr2.location.host+'/'+dir+subdirectory).length;

		top.location.hash = (pos != -1 ? loc.substring(pos) : ''); // Set top hash value to make links and bookmarks possible
		top.document.title = fr2.document.title; // Update title

		fr2.window.onunload = function(){
			top.document.title = title;
		}

		var link = jQuery('link[rel="shortcut icon"]').remove().attr("href");
		jQuery('<link href="'+ link +'" rel="shortcut icon" type="image/x-icon" />').appendTo('head');

	});
});


function sharePopup(title, url) {
		fr2.PUS.DivPopUp.clear().setName("share").setTitle('<div class="share-popup-title">Share</div>');
		
		/*var content = '<div class="share-body"><div><a href="mailto:?Subject=Awesome%20Track&body='+title+': '+ url +'"><img src="skin/frontend/pus_new/pus_theme/images/share_email.png" alt="Email" /></a></div> <div><a target="_blank" href="http://www.facebook.com/share.php?u='+ escape(url) +'"><img src="skin/frontend/pus_new/pus_theme/images/share_fb.png" alt="Facebook" /></a></div> <div><a target="_blank" href="http://twitter.com/home?status='+ escape(title) +': '+ escape(url) +'"><img src="skin/frontend/pus_new/pus_theme/images/share_twitter.png" alt="Twitter" /></a></div>   </div>';
		fr2.PUS.DivPopUp.setContent(content).show();*/
		title = title.replace("'","\\'");
		url = url.replace("'","\\'");
		var content = '<div class="share-body"><div><a href="javascript:;" onclick="top.shareViaEmailPopup(\''+title+'\',\''+ url +'\')"><img src="skin/frontend/pus_new/pus_theme/images/share_email.png" alt="Email" /></a></div> <div><a target="_blank" href="http://www.facebook.com/share.php?u='+ escape(url) +'"><img src="skin/frontend/pus_new/pus_theme/images/share_fb.png" alt="Facebook" /></a></div> <div><a target="_blank" href="http://twitter.com/home?status='+ escape(title) +': '+ escape(url) +'"><img src="skin/frontend/pus_new/pus_theme/images/share_twitter.png" alt="Twitter" /></a></div>   </div>';
		fr2.PUS.DivPopUp.setContent(content).show();
}
function shareViaEmailPopup(title,url){
	url = url.replace('/#/','/en/');
	var loc = top.location;
	var splitted = loc.toString().split('#');
	var customerEmail;
	var reloadurl = splitted[0] +'en/email';
	//Element.show('loadingmask');
	fr2.PUS.DivPopUp.clear().setName("shareViaEmail").setTitle('<div class="share-popup-title">Share via Email</div>');
	fr2.PUS.DivPopUp.setContent('<div style="position:absolute;top:50%;left:50%;"><img alt="Please wait..." style="position:relative;margin:-50% 0 0 -50%;" src="http://10.16.16.56/magento/pickupsongs/en/skin/frontend/pus_new/pus_theme/images/adj-nav-progress.gif"></div>').show();
	new fr2.Ajax.Request(reloadurl, {
	method: 'post',
	parameters: 'title='+title+'&url='+url,
	onComplete: function(transport) {
	content = transport.responseText;
		fr2.PUS.DivPopUp.setContent(content).show();
	}
	});
	
}
