/*
	*	--------------------------
	* 	Requires:
	*	co/uk/pntestbed/core.js
	*	--------------------------
*/
var uk; // Required in GLOBAL scope - does not overwrite any exisiting objects!
if(!uk){ uk = {}; } else if(typeof uk != "object"){ throw new Error("uk already exists and is not an object"); }
if(!uk.co){ uk.co = {}; } else if(typeof uk.co != "object"){ throw new Error("uk.co already exists and is not an object"); }
if(!uk.co.pntestbed){ uk.co.pntestbed = {}; } else if(typeof uk.co.pntestbed != "object"){ throw new Error("uk.co.pntestbed already exists and is not an object"); }
if(!uk.co.pntestbed.core){ throw new Error("uk.co.pntestbed.core does not exist"); }

var ap; // Required in GLOBAL scope - does not overwrite any existing objects!
if(!ap){ ap = {}; } else if(typeof ap != "object"){ throw new Error("ap already exists and is not an object"); }

// Utilities
ap.$ = function(sId){ return document.getElementById(sId); }
ap.$$ = function(v){ var obj = typeof v == 'string'? document.getElementById(v): v; return obj.style? obj.style:obj; }

// Set on index.php
ap.page = '';
ap._sub = '';
ap._var = '';
ap._par = '';
ap._agdef = true;
ap.pncf = [];
ap.pncf_current = -1;
ap.pncf_new = 0;
ap.pncf_msDuration = 1000;
ap.pncf_msDurationImage = 1000;
ap.pncf_msDurationText = 1000;
ap.pncf_msRunningImage = 1000;
ap.pncf_msRunningText = 500;
ap.pncf_msCurDuration = 0;
ap.pncf_intIdDuration = false;
ap.pncf_intIdRunning = false;
ap.PNCF_MS_FADEINTERVAL = 50;
ap.pncf_bCurrentIsText = false;

uk.co.pntestbed.core.window.onload(function(){

	if(ap.page == 'area_guide'){
		ap.$('div'+ap._sub).style.display = 'block';
	}
	if(ap.page == 'home'){
		if(typeof ap.$$('pncf_static').opacity != 'undefined' || typeof ap.$$('pncf_static').filter != 'undefined'){	
			ap.pncf = uk.co.pntestbed.core.document.getElementsByClassName('pncf');
			for(var i = 0; i < ap.pncf.length; i++){
				ap.$$(ap.pncf[i]).opacity = 0;
				ap.$$(ap.pncf[i]).filter = 'alpha(opacity=0)';
				ap.$$(ap.pncf[i]).display = 'block'; 
			}
			ap.fadeToNextSlide();
		}
	}



	// --------------------------------------------------------
	// Handle links
	var aa = document.getElementsByTagName('a');
	for(var i = 0; i < aa.length; i++){
		aa[i].onclick = function(e) { this.blur(); return true; };

		if(ap.page == 'home'){// find hcimg and hctext
			if(aa[i].className.match(/\bhc(title|img|text)\b/g)){
				aa[i].onmouseover = function(e){ var hctitle = this.parentNode.parentNode.getElementsByTagName('a')[0]; hctitle.style.backgroundColor = '#ACA196';	};
				aa[i].onmouseout = function(e){ var hctitle = this.parentNode.parentNode.getElementsByTagName('a')[0]; hctitle.style.backgroundColor = '#FFD24F';	};
			}
		}

		if(ap.page == 'area_guide'){// find pu a's
			if(aa[i].className.match(/\baga\b/g)){// && !aa[i].className.match(/\bselected\b/g)){
				aa[i].onmouseover = function(e){ 
					var agaSel = ap.aga_getSelected();
					if(agaSel) agaSel.style.backgroundPosition = '0px 0px';

					if(ap._agdef == true){
						ap.$('div'+ap._sub).style.display = 'none';	
						ap._agdef == false;
					}
					ap.$('div'+this.parentNode.id.substr(2)).style.display = 'block';	
				};
				aa[i].onmouseout = function(e){ 
					var agaSel = ap.aga_getSelected();
					if(agaSel) agaSel.style.backgroundPosition = '0px -22px';
					
					ap.$('div'+this.parentNode.id.substr(2)).style.display = 'none';	
				};
			}
		}
		
		if(ap.page == 'property' && ap._sub == 'item'){
			if(aa[i].className.match(/\bpia\b/g)){
				aa[i].onclick = function(e) { 
					this.blur(); 
					//pia
					var pia = uk.co.pntestbed.core.document.getElementsByClassName('pia');
					for(var i = 0; i < pia.length; i++){
						pia[i].className = 'pia';
					}
					this.className = 'pia selected';
					var sImgId = this.id.substr(3);
					var sAlt = ap.$('piimage').innerHTML.match(/alt="([^"]*)"/);
					sAlt = (sAlt != null)? sAlt[0]: '';
					ap.$('piimage').innerHTML = '<img src="./manager/jpg/img/ap_img_'+sImgId+'.jpg" width="423" alt="'+sAlt+'" />';
					return false; 
				};
			}		
		}
	}
	// End (Handle links)
	// --------------------------------------------------------
	
});

ap.aga_getSelected = function(){
	var aga = uk.co.pntestbed.core.document.getElementsByClassName('aga');
	for(var i = 0; i < aga.length; i++){
		if(aga[i].className.match(/\bselected\b/g)){
			return aga[i];
		}
	}
	return false;
}


ap.fadeToNextSlide = function(){
	ap.pncf_bCurrentIsText = ap.pncf_current % 2;
	if(ap.pncf_bCurrentIsText == 1){
		ap.pncf_msDuration = ap.pncf_msDurationText; 
	} else {
		ap.pncf_msDuration = ap.pncf_msDurationImage; 
	}
	ap.pncf_new = ap.pncf_current + 1;
	if(ap.pncf_new == ap.pncf.length){ ap.pncf_new = 0; }
	ap.fadeToSlide();
}
ap.fadeToSlide = function(){
	ap._clearIntIdRunning();
	if(ap.pncf_intIdDuration == false){
		ap.pncf_msCurDuration = 0;
		ap.pncf_intIdDuration = setInterval(function(){ ap._fade(); }, ap.PNCF_MS_FADEINTERVAL);
	}
}
ap._fade = function(){
	ap.pncf_msCurDuration += ap.PNCF_MS_FADEINTERVAL;
	if(ap.pncf_msCurDuration >= ap.pncf_msDuration){
		ap.pncf_msCurDuration = ap.pncf_msDuration;
	}
	// Use Ease
	var dOpacity = ap._easeInOut(ap.pncf_msCurDuration, ap.pncf_msDuration);
	
	ap._setOpacity(ap.pncf_new, dOpacity); 
	
	//if(ap.pncf_current != -1){
	if(ap.pncf_bCurrentIsText == 1){
		this._setOpacity(ap.pncf_current, 1 - dOpacity); 
	}
	if(ap.pncf_msCurDuration == ap.pncf_msDuration){
		if(ap.pncf_bCurrentIsText == 0){
			this._setOpacity(ap.pncf_current, 0); 
		}
		ap._clearIntIdDuration();
		if(ap.$$('pncf_static').display != 'none'){
			ap.$$('pncf_static').display = 'none';
		}
		ap.pncf_current = ap.pncf_new;
		ap.pncf_intIdRunning = setInterval(function(){ ap.fadeToNextSlide(); }, ap.pncf_new % 2 == 0? ap.pncf_msRunningImage: ap.pncf_msRunningText);
	}
}

ap._setOpacity = function(n, dOpacity){	
	ap.$$(ap.pncf[n]).opacity = dOpacity;
	ap.$$(ap.pncf[n]).filter = 'alpha(opacity=' + Math.round(dOpacity*100) + ')';
}
ap._getOpacity = function(n){	
	return ap.$$(ap.pncf[n]).opacity;
}
ap._clearIntIdDuration = function(){ clearInterval(ap.pncf_intIdDuration); ap.pncf_intIdDuration = false; }
ap._clearIntIdRunning = function(){ clearInterval(ap.pncf_intIdRunning); ap.pncf_intIdRunning = false; }
ap._easeInOut = function(t, d){ return (1 - Math.cos(Math.PI*t/d))/2; }