// JavaScript Document

var Navigator = Class.create({
	initialize: function(target,page,trackingPage,history,opacity,loadingImg) {	//célobjektum neve*, kezdőlap*, google analitics oldalnév, előzmények (0/1),áttetszőség (0-1), töltőcsík neve
		this.name=target;
		this.target=null;
		if (typeof(trackingPage)=='undefined') trackingPage='';
		this.pages=new Array(page);
		this.trackingPages=new Array(trackingPage);
		this.index = 0;
		this.endOfQueue = 0;
		if (typeof(history)!='undefined') this.history=history; else this.history=0;
		if (typeof(opacity)!='undefined') this.opacity=opacity; else this.opacity=1;
		if (typeof(loadingImg)!='undefined') this.loadingImgName=loadingImg;
		this.loadingImg=null;
		

		this.navID=Navigator.navs.length;
		Navigator.navs[this.navID]=this;
	},
	initObjs: function(){
		this.target=$(this.name);
		if (typeof(this.loadingImgName)!='undefined') this.loadingImg=$(this.loadingImgName);
	},
	navigate: function(page,trackingPage) {
		this.initObjs();
		if (typeof(trackingPage)=='undefined') trackingPage='';
		
		this.setEffectOn();
		
		var target=this.target;
		var obj=this;
		new Ajax.Request(page, {
			method: 'get',
			onComplete: function(){
				obj.setEffectOff();
			},
			onSuccess: function(response){
				obj.setContent(response.responseText);
				obj.initNavigatorButtonStates();
				Navigator.track(trackingPage);
				try{
			        FB.XFBML.parse(); 
			    }catch(ex){}
			},
			onFailure: function(){
				target.innerHTML='Az oldalt nem sikerült betölteni. <a onclick="Navigator.navs[' + obj.navID + '].refresh()">Újra</a> / <a onclick="Navigator.navs[' + obj.navID + '].prev()">Vissza</a>';
			}
		});
	},
	go: function(page,trackingPage) {
		if (typeof(trackingPage)=='undefined') trackingPage='';
    	if (this.history==1 && this.pages[this.index]!=page){
			this.index++;
			if (this.index==Navigator.HISTORY_SIZE) this.index=0;
			this.endOfQueue = this.index;
			this.trackingPages[this.index]=trackingPage;
		}
		this.pages[this.index]=page;
		this.navigate(page,trackingPage);
		var q=eval(Navigator.NAVIGATOR_FRAME_NAME).location.href.toQueryParams();
		if (typeof(q['HISTORY_INDEX'])=='undefined') q['HISTORY_INDEX']=1;
		q['HISTORY_INDEX']++;
		eval(Navigator.NAVIGATOR_FRAME_NAME).location.href=Navigator.NAVIGATOR_FRAME_SRC  + '?HISTORY_INDEX=' + q['HISTORY_INDEX'] + '&NEW_PAGE=1';
		
	},
	refresh: function(){
		this.go(this.pages[this.index],this.trackingPages[this.index]);
	},
	next: function(){
		if (this.history!=1 || this.index==this.endOfQueue) return;
		this.index++;
		if (this.index==Navigator.HISTORY_SIZE) this.index=0;
		this.navigate(this.pages[this.index],this.trackingPages[this.index]);
		//eval(Navigator.NAVIGATOR_FRAME_NAME).history.forward();
	},
	prev: function(){
		if (this.history!=1 || this.index==this.endOfQueue+1 || 
			(this.index==0 && (typeof(this.pages[Navigator.HISTORY_SIZE-1])=='undefined') || (this.endOfQueue==Navigator.HISTORY_SIZE-1))) return;
		this.index--;
		if (this.index==-1) this.index=Navigator.HISTORY_SIZE-1;
		this.navigate(this.pages[this.index],this.trackingPages[this.index]);
		//eval(Navigator.NAVIGATOR_FRAME_NAME).history.back();
	},
	hasNext: function(){
		if (this.history!=1 || this.index==this.endOfQueue) return false;
		return true;
	},
	hasPrev: function(){
		if (this.history!=1 || this.index==this.endOfQueue+1 || 
			(this.index==0 && (typeof(this.pages[Navigator.HISTORY_SIZE-1])=='undefined') || (this.endOfQueue==Navigator.HISTORY_SIZE-1))) return false;
		return true;
	},
	setEffectOn: function(){
		this.initObjs();
		if (this.opacity!=1) this.target.setOpacity(this.opacity);
		if (typeof(this.loadingImgName)!='undefined') this.loadingImg.style.display='block';		
	},
	setEffectOff: function(){
		this.initObjs();
		if (this.opacity!=1) this.target.setOpacity(1);
		if (typeof(this.loadingImgName)!='undefined') this.loadingImg.style.display='none';
	},
	setContent: function(content){
		this.target.innerHTML=content;
		content=content.extractScripts();
		for (var __script_index=0;__script_index<content.length;__script_index++){
			eval(content[__script_index]);
		}
	},
	initNavigatorButtonStates: function(){
		if (this.history==1){

			if (!this.hasNext()){if($(this.name+'NextButton')) $(this.name+'NextButton').setOpacity(Navigator.NavigatorButtonOpacity);}
			else {if($(this.name+'NextButton')) $(this.name+'NextButton').setOpacity(1);}
			if (!this.hasPrev()) {if($(this.name+'PrevButton')) $(this.name+'PrevButton').setOpacity(Navigator.NavigatorButtonOpacity);}
			else {if($(this.name+'PrevButton')) $(this.name+'PrevButton').setOpacity(1);}
		}
	},
	getCurrentPage: function(){
		return this.pages[this.index];
		
	}
});
Navigator.track=function(trackingPage){
	if (typeof(pageTracker)=='undefined' || trackingPage=='') return;
	if (trackingPage.charAt(0)!="/") trackingPage="/" + trackingPage;
	pageTracker._trackPageview(trackingPage);
}

Navigator.navs=new Array();
Navigator.MAIN_NAV_ID=0;
Navigator.HISTORY_SIZE=40;
Navigator.NavigatorButtonOpacity=0.4;
Navigator.DefaultContentOpacity=0.4;
Navigator.NAVIGATOR_FRAME_NAME='NavigatorFrame';
Navigator.NAVIGATOR_FRAME_SRC='libs/Utils/Navigator.php';

