// Gestisce i frame di player mp3 e contenuto - basato su http://kalakutaprod.org/gwap/gwap.js
// Created by Daniele Bartocci ( net02.it@gmail.com )

var forceiFrame = {
	iFrameParent : "http://pillbox.it/",
	defaultPage  : "index.php",
	framePage    : "index.html",
	hash : parent.location.hash,
	
	updateChildURL : function() {
		var myurl = location.hash;
		if(myurl == "") {
			myurl = forceiFrame.iFrameParent+forceiFrame.defaultPage;
		} else {
			myurl = forceiFrame.decodeUrl(myurl);
		}
		window.parent.frames['mainIFrame'].document.location.replace(myurl);
	},

	
	captureIframeLinks : function() {
		//alert('capture');
		for(i = 0; i < document.links.length; i++) {
		    if(document.links[i].onclick){
		        continue; //don't clobber existing onclicks
		    }
		    if(document.links[i].target == "" && document.links[i].rel == "") {
		    	document.links[i].onclick = function(){return parent.forceiFrame.updateParentURL(this.href);}
		    }
		}
		/*elimino gli a di ajax e lightbox
		var tabs = document.getElementsByClassName('viewsdisplaytabs-tab');
		for(i = 0; i < tabs.length; i++) {
		    tabs[i].onclick = null;
		}
		var div = document.getElementById('allineasx')
		tabs = div.getElementsByTagName('a');
		for(i = 0; i < tabs.length; i++) {
		    tabs[i].onclick = null;
		}*/
	},
	
	pollHash : function () {
	// shitty hack to update child iframe when back / forward buttons are pressed
		if(forceiFrame.hash != location.hash) {
			forceiFrame.hash = location.hash;
			forceiFrame.updateChildURL();
		}
	},

	updateParentURL : function(childUrl) {
		//alert('updateParentURL');
		var res = forceiFrame.checkIfInScope(childUrl);
		if(res){
			var encurl = forceiFrame.encodeUrl(res);
			if (encurl.match('#|\\?')) {
				return true;
			}
			var url = ""+window.location;
			var baseurl = url.split('#', 1);
			window.location = baseurl + '#' + encurl;
    		return false;
		 } else {
			window.location = childUrl;
			return false;
		}

	},
    
    checkIfInScope : function(url) {
		if(url == forceiFrame.iFrameParent) {
			return true;
		}
		var ScopeRe = new RegExp('^'+forceiFrame.iFrameParent);
		var m = url.match(ScopeRe);
		if(m == null) {
			//not in domain
			return false;
		}
		//in domain, return relative path
		return url.substr(forceiFrame.iFrameParent.length);

	},
	
	encodeUrl : function(s) {
		if(s.length > 0){
			return encodeURI(s);
		} else {
			return '';
		}
	},
	
	decodeUrl : function(s){
		var u = decodeURI(s);
		return u.substr(1);
	},
	
	parentWindowResize : function(){
		var i = document.getElementById? document.getElementById('mainIFrame'): document.all? document.all['mainIFrame']: null;
		if (i) {
			var h = document.documentElement.clientHeight || document.body.clientHeight;
			i.style.height = h + "px";
		}
	}
}

if(myIFrame == 'child') {
	// Embed the page as a child iFrame in the defined parent document
	var id = 0;
	try {
		id = window.frameElement.id;
	} catch(err) { }
	if(id == 0){
		// we're not in an frame, embed self in iframe
		var myurl = ""+document.location;
		var relurl = forceiFrame.checkIfInScope(myurl);
		var urlenc = forceiFrame.encodeUrl(relurl);
		if(urlenc.length > 0) {
			urlenc = '#'+urlenc;
		}
		var dest = forceiFrame.iFrameParent+urlenc;
		window.location = dest;
	};

	jQuery(document).ready(function(){
		/*$("a").click(function () { 
      		alert("cliccato link "+$(this).attr('href'));
      	});*/
		parent.document.title = document.title;
		forceiFrame.captureIframeLinks();
	});
}

if(myIFrame == 'parent') {
	jQuery(document).ready(function() {
		forceiFrame.parentWindowResize();
		forceiFrame.updateChildURL();
		setInterval(forceiFrame.pollHash, 400);			
	});

	window.onresize = forceiFrame.parentWindowResize;
}

