/**
 * DeepLink
 * (http://f6design.com/journal/2006/11/18/deeplink-flash-deep-linking/)
 *
 * By Jonathan Nicol (f6design.com)
 *
 * Inspired by the work of:
 * Theo Hultberg (http://blog.iconara.net/2006/06/21/bookmarkability-in-flash/) 
 * and Asual(http://www.asual.com/swfaddress/)
 *
 */
if(typeof f6design == "undefined") var f6design = new Object();
f6design.DeepLink = function() {
	var o = this;
	var flashObj;
	o.currentHash = window.location.hash;
	var ftScr;
	o.xPos = 0;
	o.yPos = 0;
	o.ftReady = 0;
	this.setObject = function(obj){
		o.flashObj = obj;
	}
	o.changeState = function(newState){
		if(window.location.hash != "#"+newState){
			o.currentHash = "#"+newState;
			if(newState == "home" && window.location.hash == ""){
				//alert("Welcome");
			} else {
				o.checkTitle(o.currentHash.split("#")[1]);
			}
			window.location.hash=newState;
		}
	}
	o.sendState = function(){
		hashValue = decodeURI(window.location.hash).substring(1);
		o.currentHash = window.location.hash;
		if (hashValue){
			document.getElementById(o.flashObj).receiveState(hashValue);
		}
		o.checkTitle(o.currentHash.split("#")[1]);
	}
	o.listenForURLChange = function(){
		if(window.location.hash == "" && o.currentHash != ""){
			window.location.hash = "home";
		}
		if (window.location.hash != o.currentHash){
			//alert(window.location.hash);
			o.sendState();
		}
	}
	o.loadImg = function(img){
			var image= new Image(0,0);
			image.src = "loadimg.php?img="+img;
	}

	o.flashLoaded = function(){
		// send initial hash state to flash (in case initial URL contains deeplink)
		o.sendState();
	}
	
	o.initFT = function(){
		var ftSect = o.currentHash.split("#")[1];
		if(ftSect == undefined){
			ftSect = "home";
		}
		o.writeFT(ftSect,document.title);
		o.ftReady = 1;
	}
	o.writeFT = function(section,desc){
		var getFTScr = document.getElementById("dbfacebook");
		if(getFTScr != null){
			getFTScr.parentNode.removeChild(getFTScr);
		}
		ftScr = document.createElement('div');
		ftScr.id = "dbfacebook";
		ftScr.style.zIndex=1000;
		ftScr.style.position='absolute';
		ftScr.style.left = o.xPos+"px";
		ftScr.style.top = o.yPos+"px";
		ftScr.innerHTML = '<div style="float:left;margin-top:-0.5px"><iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3a%2f%2fwww.dimitriya.net%2f%3fid%3d'+section+'&text='+escape(desc)+'&lang=en" style="width:110px; height:22px;"></iframe></div><div style="float:left;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3a%2f%2fwww.dimitriya.net%2f%3fid%3d'+section+'&amp;layout=button_count&amp;show_faces=true&amp;width=110&amp;action=like&amp;colorscheme=light&amp;height=22" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:22px; " allowTransparency="true"></iframe></div>';
		document.body.appendChild(ftScr);
	}
	o.moveFT = function(ftXPos,ftYPos){
		var getFTScr = document.getElementById("dbfacebook");
		if(getFTScr != null && getFTScr != undefined){
			getFTScr.style.left = ftXPos+"px";
			getFTScr.style.top = ftYPos+"px";
		}
			o.xPos = ftXPos;
			o.yPos = ftYPos;
	}

	o.writeCounter = function(section){
		var getoScr = document.getElementById("dbcounter");
		if(getoScr != null){
			getoScr.parentNode.removeChild(getoScr);
		}
		var oScr = document.createElement('iframe');
		oScr.id = "dbcounter";
		oScr.style.display = "none";
		document.body.appendChild(oScr);
		var f = document.getElementById("dbcounter");
		var d = f.contentDocument || f.contentWindow.document;
		var txtFileC = new XMLHttpRequest();
		txtFileC.open("GET", "statcounter/"+section+".txt", true);
		txtFileC.onreadystatechange = function() {
			if (txtFileC.readyState === 4) {		// Makes sure the document is ready to parse.
				if (txtFileC.status === 200) {  // Makes sure it's found the file.
					var allTextC = txtFileC.responseText;
					//oScr.innerHTML = allTextC;
					//o.loadImg(allTextC);
					d.open();
					d.write(allTextC);
					d.close();
					//document.body.appendChild(oScr);
			   } else if(txtFileC.status === 404){
					//alert("Counter not found");
			   }
			}
		}
		txtFileC.send(null);
	}
	o.checkTitle = function(tit){
		if(tit == undefined){
			tit = "home";
		}
		
		var txtFile = new XMLHttpRequest();
		txtFile.open("GET", "array.php?addr="+tit, true);
		txtFile.onreadystatechange = function() {
			if (txtFile.readyState === 4) {		// Makes sure the document is ready to parse.
				if (txtFile.status === 200) {  // Makes sure it's found the file.
					var allText = txtFile.responseText;
					document.title = allText;
					o.writeCounter(tit);
					if(o.ftReady == 1){
						o.writeFT(tit,allText);
					}
				 } else if(txtFile.status === 404){
					//файлът го няма
				 }
			}
		}
		txtFile.send(null);
	}
	setInterval(o.listenForURLChange, 150);
}
DeepLink = new f6design.DeepLink();

