//=====================================
// SoliterCMS
// File: functions.js
// Last Updated: 21.04.2007
//=====================================

var CurrentHash = '';
var will_redirect = false;
HashRedirect();

function PollHashChange(){
// alert(CurrentHash);
	if(CurrentHash != window.location.hash){
		Url = url+'/'+window.location.hash.substring(1)+'/';
		if(window.location.hash == ''){
			Url = window.location.href;
		}
//		var Url = "";
//		UrlHash = window.location.hash.substring(1,window.location.hash.length);
//		Trailing = window.location.hash.substring(window.location.hash.length-1,window.location.hash.length);
//		NoHash = window.location.href.substring(0,window.location.href.indexOf('#'));
//		if(NoHash == ''){
//			NoHash = window.location.href;
//		}
//		if(Trailing != "/"){
//			Trailing = "/";
//		} else Trailing = "";
//		if(UrlHash != "") {
//
//			if(NoHash.indexOf('/normal/') != -1){
//				Url = NoHash.substring(0,NoHash.indexOf('/normal'))+'/ajax/'+UrlHash+Trailing;
//			} else if(NoHash.indexOf('index.php') != -1){
//				Url = NoHash.substring(0,NoHash.indexOf('index.php'))+'ajax/'+UrlHash+Trailing;
//			} else if(NoHash.substring(NoHash.length-1,NoHash.length) != '/'){
//				Url = NoHash+'/ajax/'+UrlHash+Trailing;
//			} else {
//				Url = NoHash+'ajax/'+UrlHash+Trailing;
//			}
//		} else {
//			Url =  window.location.href.replace(/normal/,'ajax');
//		}
//
//		if(NoHash.indexOf('/normal/') != -1){
//		} else if(NoHash.indexOf('index.php') != -1){
//			Url = NoHash.substring(0,NoHash.indexOf('index.php'))+'ajax/homepage/';
//		} else if(NoHash.substring(NoHash.length-1,NoHash.length) != '/'){
//			Url = NoHash+'/ajax/homepage/';
//		} else {
//			Url = NoHash+'ajax/homepage/';
//		}


		ajaxUpdater('modcontent',Url,1,0,"",'get','');
		CurrentHash = window.location.hash;
 	}
}

//setTimeout('PollHashChange.periodical(1000)',5000);
window.addEvent('domready', function(){

	Loading();
	PollHashChange.periodical(1000)

});



function ajaxUpdater(Div,Url,Up,DoHash,UrlHash,Method,FormElement){
	Loading();
	var timestamp = new Date();
	var PostData ;
	if(FormElement != ''){
		PostData = $(FormElement);
	} else PostData = '';
	if(Method == 'post'){
		new Request.HTML({update: Div,data: PostData, evalScripts: true,onComplete: FinishedLoading}).post(Url+timestamp.getTime());
	} else {
		//new Request.HTML({evalScripts: false,onComplete: function(a,b,c,d){alert(c);FinishedLoading();}}).get(Url+timestamp.getTime());
		new Request.HTML({ evalScripts: false,onComplete: function(a,b,c,d){$(Div).innerHTML=c;FinishedLoading();$exec(d);}}).get(Url+timestamp.getTime());
	}
	//new Ajax(Url,{data: PostData, method: Method,update: Div, evalScripts: true,onComplete: FinishedLoading}).request();
	//new Ajax.Updater(Div,Url,{asynchronous:true,evalScripts:true,method:'get',onComplete:FinishedLoading});
	if(Up == 1){
		new Fx.Scroll(window,{transition: Fx.Transitions.Quad.easeInOut}).toTop();
	}
	if(DoHash) {
		CurrentHash = '#'+UrlHash;
		window.location.hash = '#'+UrlHash;
	}
}

function Loading(){
	//$('loading').style.visibility = 'visible';
	if(!will_redirect){
		$('loading').setStyle('display','block');
	}
	//$('loading').setStyle('visibility','visible');

}

function FinishedLoading(){
	//$('loading').style.visibility = 'hidden';
	$('loading').setStyle('display','none');
	//$('loading').setStyle('visibility','hidden');
	FixPng($('modcontent'));
	//window.fireEvent('complete');


}
function AjaxPeriodic(){
// 	var timestamp = new Date();
// 	var periodic = new Request.HTML({update: 'notifier', evalScripts: true, onComplete: AjaxPeriodic}).get(url+'/notifier/'+timestamp.getTime());
}

function HashRedirect(){
	//Hash without #
	UrlHash = window.location.hash.substring(1);
	if(UrlHash != ''){
		will_redirect = true;
		window.location.href = url+'/'+UrlHash+'/';
	}
	//if(url.index)

//	//Get last character
//	Trailing = window.location.hash.substring(window.location.hash.length-1,window.location.hash.length);
//	//Remove hash
//	NoHash = window.location.href.substring(0,window.location.href.indexOf('#'));
//	if(Trailing != "/"){
//		Trailing = "/";
//	} else Trailing = "";
//
//	if(UrlHash != ""){
//		if(NoHash.indexOf('/normal/') != -1){
//			window.location.href = NoHash.substring(0,NoHash.indexOf('/normal'))+'/normal/'+UrlHash+Trailing;
//		} else if(NoHash.indexOf('index.php') != -1){
//			window.location.href = NoHash.substring(0,NoHash.indexOf('index.php'))+'normal/'+UrlHash+Trailing;
//		} else if(NoHash.substring(NoHash.length-1,NoHash.length) != '/'){
//			window.location.href = NoHash+'/normal/'+UrlHash+Trailing;
//		} else {
//			window.location.href = NoHash+'normal/'+UrlHash+Trailing;
//		}
//
//	}
}

function UpdateDiv(Div,Content){
	if($(Div)){
		$(Div).innerHTML=Content;
	}
}

function FixPng(){
	if(Browser.Engine.trident4){
		Images = document.getElements('img[src$=.png]');

		Images.each(function(value,key){
			//if(value.getParent('span[class=png]') == null){
				var imgID = (value.id) ? "id='" + value.id + "' " : "";
				var imgClass = (value.className) ? "class='" + value.className + "' " : "";
				var imgTitle = (value.title) ? "title='" + value.title + "' " : "title='" + value.alt + "' "
				var imgStyle = "margin:0px; pading:0px; display:inline-block;" + value.style.cssText
				var imgOnClick = (value.getProperty('onclick')) ? "onclick='" + value.getProperty('onclick') + "FixPng();' ":"";
				//var imgOnMouseOver = (value.getProperty('onmouseover')) ? "onmouseover='" + value.getProperty('onmouseover') + "FixPng();' ":"";
				//var imgOnMouseOut = (value.getProperty('onmouseout')) ? "onmouseout='" + value.getProperty('onmouseout') + "FixPng();' ":"";
				if (value.align == "left") imgStyle = "float:left;" + imgStyle
				if (value.align == "right") imgStyle = "float:right;" + imgStyle
				//if (value.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span class=\"png\" " + imgID +imgOnClick + imgClass + imgTitle
				+ " style=\"" + "width:" + value.width + "px; height:" + value.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + value.src + "\', sizingMethod='scale');\"></span>"
				value.outerHTML = strNewHTML
			//}

		});

	}

}

window.addEvent('load', function() {
	FixPng(document);
	AjaxPeriodic();
	//AjaxPeriodic.periodical(20000);
	FinishedLoading();
});


