/**
 * Actonjs Library v. 1.0
 * Author: Actonbit.gr < sales@actonbit.gr >
 * Copyrights: Actonbit.gr < sales@actonbit.gr >
 */

Actonjs.namespace("Actonjs.ui");

Actonjs.ui = function () {
	
	var vHeight,vWidth,maskC,maskArray,pending=false,modal,modalIns,modalError=false;
	var googleAnalytics,googleAds;
	
	function getViewportHeight() {if (window.innerHeight!=window.undefined) return window.innerHeight;if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;if (document.body) return document.body.clientHeight;return window.undefined;}
	function getViewportWidth() {var offset = 17;var width = null;if(window.innerWidth!=window.undefined) return window.innerWidth;if(document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;if(document.body) return document.body.clientWidth;}

	function getScrollTop() {var yoff=null;if (self.pageYOffset){yoff=self.pageYOffset;}/*not Explorer*/else if (document.documentElement && document.documentElement.scrollTop){yoff=document.documentElement.scrollTop;}/*Explorer 6 strict*/else if (document.body){yoff=document.body.scrollTop;}/*All other Explorers*/return yoff}
	function getScrollLeft() {var xoff=null;if (self.pageXOffset){xoff=self.pageXOffset;}/*not Explorer*/else if (document.documentElement && document.documentElement.scrollLeft){xoff=document.documentElement.scrollLeft;}/*IE6 strict*/else if (document.body){xoff=document.body.scrollLeft;}/*IE*/return xoff}
	
	function scrollTop() {var yoff=null;
		if (self.pageYOffset){
			yoff=self.pageYOffset;
		}/*not Explorer*/
		else if (document.documentElement && document.documentElement.scrollTop){
			yoff=document.documentElement.scrollTop;
		}/*Explorer 6 strict*/
		else if (document.body){
			yoff=document.body.scrollTop;
		}/*All other Explorers*/
		
		self.pageYOffset=0;
		document.documentElement.scrollTop=0;
		document.body.scrollTop=0;
	}
	

	function resetModalIns() {
		var el = Actonjs.get('ac-mod-msgBox');
		if (modalIns&&el&&modalError==false) el.innerHTML = modalIns;
	}
	
	/*
	 * Αυτή η μέθοδος διαωάζει τη γλώσσα προτίμησης του επισκέπτη και κάνει δρομολόγηση 
	 * του επισκέπτη στην σελίδα που βρίσκεται τώρα στο κομμάτι της σελίδας που καλύπτει
	 * τη δική του γλώσσα.
	 */
	function setLang(lang) {var uri=location.href;uriParts=uri.split('/');var _location = uriParts[4];if (lang) {location.href = '/'+lang+'/'+_location;}}
	
	/**
	 * Αυτή η μέθοδος χρησιμοποιείται για να ανανεώσουμε το περιέχομενο τον διαφημίσεων 
	 * μαζί με κάθε ανανέωση του περιεχομένου του κεντρικού πάνελ, πχ. κάθε φορά που 
	 * γίνεται αλλαγή στα κριτήρια βελτίωσης αναζήτησης.
	 */
	function renewAd() {
		var html = false;
		var divaki = Actonjs.get('some_ad');
		if(divaki) {
			/*var d = new Date();
			var myTime = d.getTime();
			divaki.innerHTML = myTime;
			divaki.innerHTML = googleAds;*/
			divaki.innerHTML = '<iframe class="ad_space" src="../includes/googleAds.php" frameborder=0 scrolling=no></iframe>';
			/*divaki.innerHTML = '<img width="250" src="../images/jobb24.gif" />';
			divaki.attributes.src = 'http://www.in.gr/';*/
		}
		//console.log(divaki.innerHTML);
		
	}
	
	function loadMask(el) {
		var elm = Actonjs.get(el);
		/*Den kanoume tipota an prota den pistopoiisoume oti to elm uparxei*/
		if (elm) {
			h = elm.clientHeight;
			if(isMasked(el)) {
				/*Idi foraei maska, peirazoume mono tis diastaseis an xreiazetai*/
				switch(el) {
					case 'centre_panel':maskArray['cp'].style.height=h+'px';
										handleMask(el,'+');
										break;
					default:break;
				}
			}
			else {
				switch(el) {
					case 'centre_panel':maskArray['cp'].className='po-dun-mask active';
										maskArray['cp'].style.height=h+'px';
										handleMask(el,'+');
										break;
					default:break;
				}
			}
		}
	}
		
	function killMask(el) {
		handleMask(el,'-');
		var elm = Actonjs.get(el);
		/*Den kanoume tipota an prota den pistopoiisoume oti to elm uparxei*/
		if (elm) {
			h = elm.clientHeight;
			if (!isMasked(el)) {
				switch(el) {
					case 'centre_panel':maskArray['cp'].className='po-dun-mask inactive';
										break;
					default: break;
				}
			}
			else {
				switch(el) {
					case 'centre_panel':maskArray['cp'].style.height=h+'px';break;
					default:break;
				}
			}
		}
	}
	
	/* to action mporei na einai '+' i '-' analogos me to an pame na vgaloume i na valoume to mask pano sto elm */
	function handleMask(el,action) {
		switch(action) {
			case '+':
				if(isMasked(el)) ++maskC[el];
				else maskC[el]=1;
			break;
			case '-':
				if(isMasked(el)) maskC[el]-=2;
				else maskC[el]=0;
				if (maskC[el]<0)maskC[el]=0;
			break;
			default:break;
		}
	}
	
	function handleModal(){if(Actonjs.isIE()&&!Actonjs.isIE7()) {Actonjs.addEvent(window,'scroll',centerPopIE6);}}
	function isMasked(el) {return maskC[el]?true:false;}
	function maskPage() {var mask = addDiv('pmask',false);mask.className = 'acpmask';mask.style.display='inline';}
	function clearPage() {if(Actonjs.get('pmask')) {removeDiv('pmask');killPop();pending=false;}}
	function killPop(){if (modal) {modal.style.display='none';removeDiv(modal.id);modalIns=false;pending=false;}}
	function acpop(type,args) {
		var l = Actonjs.getLang();
		// Auto einai to default keimeno pou tha fortonoume mexri na fortosei to periexomeno 
		// tou kathe modal parathurou
		var html = '<div>'+
					'<table width="100%" cellspacing="0" cellpadding="3">'+
						'<tr>';
		if (l=='en') { html += '<td align="left" style="font-size:1.4em;color:#3af;font-weight:bold;">Connecting to server</td>'; }
		else if (l=='de') { html += '<td align="left" style="font-size:1.4em;color:#3af;font-weight:bold;">German connection</td>'; }
		else if (l=='se') { html += '<td align="left" style="font-size:1.4em;color:#3af;font-weight:bold;">Kontaktar server</td>'; }
		else { /* default ta ellinika */ html += '<td align="left" style="font-size:1.4em;color:#3af;font-weight:bold;">Σύνδεση με διακομιστή</td>'; }		
		html += '<td align="right"><img src="../images/cross.png" onclick="Actonjs.ui.clearPage();" style="cursor:pointer;cursor:hand;"></td><tr><tr><td colspan="2" style="width:100%;height:40px;text-align:left;background:#fd9;border-top:solid 2px #bdf; border-bottom:solid 2px #bdf;"><div id="ac-mod-msgBox" style="width:100%;height:100%">';
		
		if (l=='en') { html += 'Connecting ...'; }
		else if (l=='de') { html += 'German connecting ...'; }
		else if (l=='se') { html += 'Kontaktar ...'; }
		else { /* default ta ellinika */ html += 'Σύνδεση με τον διακομιστή'; }
		html +=	'</div></td></tr><tr><td align="center" colspan="2" style="height:200px;"><img src="../images/loading_2.gif"></td></tr></table></div>';
		
		// Edo tha ginetai ena switch gia na doume poio div tha fortosoume, ti class tha einai, periexomeno klp klp
		var el = false;
		var post_t = false;
		switch(type) {
			case 'ac-mod-login': el = addDiv(type,false);post_t='login'; break;
			case 'ac-mod-signup': el = addDiv(type,false);post_t='signup';break;
			case 'ac-mod-share': el = addDiv(type,false); post_t='share';break;
			case 'ac-mod-reply': el = addDiv(type,false); post_t='reply';break;
			default:break;
		}
		if (el) {
			el.innerHTML = html;
			// Amesos meta tin dimiourgia tou pop kai amesos prin valoume to periexomeno mesa ..
			if (modal) removeDiv(modal.id);
			modal=el;
			centerPop();
			//el.innerHTML=html;
			if(Actonjs.isIE()&&!Actonjs.isIE7()) {modal.style.position='absolute';}
			var con = Actonjs.AJAX();
			var params= 't='+post_t;
			if(args.length > 0) 
				params += '&'+args;

			if (con.init()) {
				con.request({
					url:'/'+l+"/controllers/form_builder.php",
					method:"POST",
					params: params,
					success:function(){
						var response=con.getResponse();
						var html=false;
						modal.className=type;
						html=response.html;
						modalIns = response.modalIns;
						if (html) Actonjs.get(type).innerHTML=html;
						if (Actonjs.isIE()&&!Actonjs.isIE7()) centerPopIE6();
						else centerPop();
					},
					failure:function(){clearPage();}
				});
			}
			
		}
		else {clearPage();}
	}
	
	function showModalErrors(e_a) {
		if (e_a.length&&modalError==true) {
			var el = Actonjs.get('ac-mod-msgBox');
			if (el) {
				var html = '<span style="color:red;">';
				switch (Actonjs.getLang()) {
					case 'en':html+='[EN] Please fix the errors in fields:<br>';break;
					case 'de':html+='[DE] Please fix the errors in fields:<br>';break;
					case 'se':html+='Vänligen åtgärda fel i fält:<br>';break;
					case 'nl':html+='[NL] Please fix the errors in fields:<br>';break;
					default:html+='Παρακαλώ διορθώστε τα σφάλματα στα πεδία:<br>';break;
				}
				html += e_a[0];
				for (var i=1; i<e_a.length;i++) html += ', '+e_a[i];
				html += '</span>';
				el.innerHTML = html;
			}
		}
	}
	
	function showLoadingModal() {
		var el = Actonjs.get('ac-mod-msgBox');
		if (el) {
			var html = '<div style="text-align:center;">';
			switch(Actonjs.getLang()) {
				case 'en':html += '[ΕΝ] Loading<br><img src="../images/loading_2.gif">';break;
				case 'de':html += '[DE] Loading<br><img src="../images/loading_2.gif">';break;
				case 'se':html += 'Laddar<br><img src="../images/loading_2.gif">';break;
				case 'nl':html += '[NL] Loading<br><img src="../images/loading_2.gif">';break;
				default:html += 'Φόρτωση<br><img src="../images/loading_2.gif">';break;
			}
			html += '</div>';
			el.innerHTML=html;
		}
	}
	
	function clearLoadingModal() {
		var el = Actonjs.get('ac-mod-msgBox');
		if (el) {
			if (modalIns) el.innerHTML=modalIns;
			else el.innerHTML = '';
		}
	}
	
	function centerPop(){if (modal) {reCalc();if (modal.style.display!='inline') modal.style.display='inline';var elW = modal.clientWidth;var elH = modal.clientHeight;modal.style.top = (vHeight/2 - elH/2)+'px';modal.style.left = (vWidth/2 - elW/2)+'px';}}
	function centerPopIE6(){if(modal){reCalc();if (modal.style.display!='inline') modal.style.display='inline';var elW = modal.clientWidth;var elH = modal.clientHeight;var sTop = document.documentElement.scrollTop;modal.style.top = (vHeight/2 - elH/2)+sTop+'px';modal.style.left = (vWidth/2 - elW/2)+'px';}}
	function addDiv(id,cname){if(!Actonjs.get(id)){var s=document.createElement('div');s.setAttribute('id',id);if(cname){s.className=cname;}document.getElementsByTagName("body")[0].appendChild(s);}else{removeDiv(id);addDiv(id,cname);}return Actonjs.get(id);}
    function removeDiv(targetelement){document.getElementsByTagName("body")[0].removeChild(document.getElementById(targetelement));if(modal.id==targetelement)modal=null;}
    
    // Epistrefei ena array sto [0] vrisketai to X kai sto [1] to Y tou element tou opoiou pername to id san argument.
    function getElPos(el) {
    	var obj = Actonjs.get(el);
    	var x = getElLeft(obj);
    	var y = getElTop(obj);
    	return (new Array(x,y));
    }
    
    function getElLeft(obj) {
		var curleft = 0;
	    if(obj.offsetParent) {
	    	while(true) {
	        	curleft += obj.offsetLeft;
	          	if(!obj.offsetParent) break;
	          	obj = obj.offsetParent;
	        }
	    }
	    else if(obj.x) curleft += obj.x;
	    return curleft;
	}
	
	function getElTop(obj) {
    	var curtop = 0;
    	if(obj.offsetParent) {
        	while(true) {
          		curtop += obj.offsetTop;
          		if(!obj.offsetParent) break;
          		obj = obj.offsetParent;
        	}
    	}
    	else if(obj.y) curtop += obj.y;
    	return curtop;
  	}
  	
  	// Dinei to platos enos element otan perasoume to id tou element
  	// An to element den uparxei, tote epistrefei false.
  	function getElWidth(el) {
  		var result = false;
  		var obj = Actonjs.get(el);
  		if (obj) result = obj.offsetWidth;
  		
  		return result;
  	}
  	
  	// Dinei to Ypsos enos element otan perasoume to id tou element
  	// An to element den uparxei, tote epistrefei false.
  	function getElHeight(el) {
  		var result = false;
  		var obj = Actonjs.get(el);
  		if (obj) result = obj.offsetHeight;
  		
  		return result;
  	}

	function reset() {
		reCalc();
		maskC=Array();
		maskArray=Array();
		modal=false;
		startMasks();
		Actonjs.addEvent(window,'resize',centerPop);
		setGoogleAnalytics();
		setGoogleAds();
	}
	
	/* Εδώ ορίζουμε το script το οποίο μας επιτρέπει να ελέγχουμε την κίνηση στον τόπο. */
	function setGoogleAnalytics(){googleAnalytics='<script type="text/javascript">var gaJsHost=(("https:"==document.location.protocol)?"https://ssl.":"http://www.");'+'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));</script>'+'<script type="text/javascript">var pageTracker=_gat._getTracker("UA-1043241-5");pageTracker._trackPageview();</script>';}
	
	/* Εδώ ορίζουμε το script το οποίο μας επιτρέπει να εμφανίζουμε διαφημίσεις Google Adsense. */
	function setGoogleAds(){googleAds='<script type="text/javascript"><!--google_ad_client="pub-7313524808650276";google_ad_slot="7714048641";google_ad_width=250;google_ad_height=250;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';}
	
	/*Edo kanoume upologismous ton diastaseon tis selidas. Mia fora stin arxi kai meta se kathe resize kai centerPop().*/
	function reCalc() {vHeight=getViewportHeight();vWidth=getViewportWidth();}
	
	function startMasks() {
		maskArray['cp'] = Actonjs.get('centre_panel_mask');
	}
	
	function divdisplay(objRef,bool) {
		if (bool) objRef.style.display="inline";
		else objRef.style.display="none";
	}
	
	function toggledisplay(objRef) {
		if(objRef.style.display == "inline") 
			objRef.style.display = "none";
		else
			objRef.style.display = "inline"
	}
	
	
	return {
		init:	function() {
			reset();
			handleModal();
		},
		loadMask:	function(el) {loadMask(el);},
		killMask:	function(el) {killMask(el);},
		/*noResults: 	function(query,l) {return noResults(query,l);},*/
		/*showResults:function(res) {return qv(res);},*/
		/*assignActions:function(res){assignActions(res);},*/
		maskPage:	function() {maskPage();},
		clearPage:	function() {clearPage();},
		acpop:		function(type, args) {acpop(type, args);},
		canSubmit:	function() {return (modalError==true)?false:true;},
		login:		function() {login();},
		logout:		function() {logout();},
		myPage:		function() {myPage();},
		/*toggleSP:	function(o) {toggleSP(o);},*/
		divdisplay:		function(objRef,bool) {divdisplay(objRef,bool);},
		toggledisplay:		function(objRef) {toggledisplay(objRef);},
		scrollTop:		function() {scrollTop();},
		getElementPosition:	function(el) { return getElPos(el); },
		renewAd:	function() { renewAd(); },
		setLang: 	function(lang) {setLang(lang);},
		
		getViewportHeight: 	function() {return getViewportHeight();},
		getViewportWidth: 	function() {return getViewportWidth();},
		getElWidth:			function(target) {return getElWidth(target);},
		getElHeight:		function(target) {return getElHeight(target);}
	}
}();

Actonjs.onReady(Actonjs.ui.init);

	/* Auti i methodos einai upeuthuni na anathesei ta onclick actions pano sta eikonidia gia to add/remove saved properties. */
	/* Sto simeio auto mporoume na kanoume assign kai oti allo action theloume .. */
	/*function assignActions(d) {
		if (d) {
			var properties = d.properties;
			for (var i=0; i<properties.length;i++) {
				
				var img1 = Actonjs.get('qv_handle_saved_'+properties[i].id);
				if (img1) Actonjs.addEvent(img1,'click',Actonjs.ui.toggleSP);
				var img2 = Actonjs.get('qf_handle_saved_'+properties[i].id);
				if (img2) Actonjs.addEvent(img2,'click',Actonjs.ui.toggleSP);
			}
		}
	}*/
	
	/**
	 * Auti i function kanei tin douleia tis parapano methodou gia to perivallon tou qv omos
	 */
	/*function toggleSP (o) {
		if (typeof o == 'object') {
			if(Actonjs.isIE())var i=o.srcElement.id; //,v=o.srcElement.value;
			else var i=o.target.id; //,v=o.target.value;
			var img = Actonjs.get(i);
			var imgSrc = img.src;
			var pid = img.id.substring(16);
			var msg = '';
			if (imgSrc!=undefined) {
				if (imgSrc.indexOf("save.png")>-1) { addSP(pid); }
				else if (imgSrc.indexOf("delete.png")>-1) {
					switch (Actonjs.getLang()) {
						case 'en': msg = 'Are you sure you want to delete this property from your saved properties?';break;
						case 'de': msg = '[DE] You want to save this property?';break;
						case 'se': msg = 'Är du säker på att du vill radera det här objektet?';break;
						case 'nl': msg = '[NL] You want to save this property?';break;
						default: msg = 'Σίγουρα θέλετε να διαγράψετε αυτό το ακίνητο απο τα αποθηκευμένα ακίνητα σας;';break;
					}
					if (confirm(msg)) {removeSP(pid);}
				}
			}
		}
	}*/
	
	/*function login() {
		var l = Actonjs.getLang();
		modalError=false;
		resetModalIns();
		var email=Actonjs.trim(Actonjs.get('login_email').value);
		var password=Actonjs.trim(Actonjs.get('login_password').value);
		var e_a = Array();
		if (!Actonjs.validator.isEmail(email)) {
			var error = '';
			switch (l) {
				case 'en':e_a[e_a.length]='[EN] Email';break;
				case 'de':e_a[e_a.length]='[DE] Email';break;
				case 'se':e_a[e_a.length]='Email';break;
				case 'nl':e_a[e_a.length]='[NL] Email';break;
				default:e_a[e_a.length]='[EL] Email';break;break;
			}
			modalError=true;
		}
		if (!Actonjs.validator.isPasswd(password)) {
			var error = '';
			switch (l) {
				case 'en':e_a[e_a.length]='[EN] Password';break;
				case 'de':e_a[e_a.length]='[DE] Password';break;
				case 'se':e_a[e_a.length]='Lösenord';break;
				case 'nl':e_a[e_a.length]='[NL] Password';break;
				default:e_a[e_a.length]='Κωδικός';break;break;
			}
			modalError=true;
		}
		if (!modalError) {
			var con = Actonjs.AJAX();
			if (con.init()) {
				con.request({
					url:'/controllers/user-login.php',
					params:'e='+email+'&p='+password,
					method:'POST',
					success:function() {
						var response=con.getResponse();
						var status = response.status;
						if (status==1) { // Ola epituxos
							if (pending) eval(pending); // Edo kanoume oti exei meinei sto queue energeion na ginei
							pending=false;
							//allagiHeader(1); // άχρηστο αφού γίνεται δρομολόγηση του χρήστη ούτος ή άλλως
							setLang(response.user_data.lang);
							clearPage();
						}
						else { // Egine sfalma
							var el = Actonjs.get('ac-mod-msgBox');
							if (el) {
								var html = '<span style="color:red;">';
								switch (l) {
									case 'en':html+='[EN] Unknown user';break;
									case 'de':html+='[DE] Unknown user';break;
									case 'se':html+='Ökänd användare';break;
									case 'nl':html+='[NL] Unknown user';break;
									default:html+='Άγνωστος χρήστης';break;
								}
								html += '</span>';
								el.innerHTML = html;
							}
						}
					},
					failure:function(){login();}, // To vazoume na ksanaprospathisei
					mask:function(){showLoadingModal();}
				});
			}
		}
		if (modalError) {
			showModalErrors(e_a);
		}
	}*/
	
	//function logout() {var con = Actonjs.AJAX();if (con.init()) {con.request({url:'/controllers/user-login.php',method:'POST',params:'logout=true',success:function(){/*allagiHeader(0);*/ /* Άχρηστο αφού κάνουμε δρομολόγηση του χρήστη στην κεντρική σελίδα*/location.href = '/'+Actonjs.getLang()+'/';},failure:function(){logout();}});}}
	//function myPage() {location.href = '/'+Actonjs.getLang()+'/profile.php?menu=profile&tab=1';}
