//////////////////////////////////////////////////////////////////////////////////////////////// GLOBAL VARS
////for boxes on home
var left=0.5;						// root X position in percent (1=100%)
var top=0.5;						// root Y position in percent
var box= new Array(); 
box['pad']=4;						// general padding of the box
box['marg']=10;						// general margin of the box
box['margTop']=33;						// top margin of the box
box['zindex']=0;
box['openSpeed']=2; 				// bigger is slower
box['closeSpeed']=4; 				// bigger is slower
box['initAlpha']=0.8;
if (navigator.appName.indexOf('Explorer')>-1) box['maxAlpha']=1;
else { box['maxAlpha']=0.99; }
box['alphaSpeed']=0.4;				// steps (+=alpahSpeed)
box['on']=false;					// switch 
box['splash']=0;					// if == 1 => check splash boxes

////for scrollzone
var scrollzone=0;
var maxScrollHeight=0;
var scrollFrameHeight=395; 			//check the css
var scrollWay=0;
var scrollCurrent=0;
var scrollSpeed=5;

//////////////////////////////////////////////////////////////////////////////////////////////// GLOBAL VARS


//////////////////////////////////////////////////////////////////////////////////////////////// GENERAL FUNCTIONS
function cleanVar(x) { if (x.toString().indexOf('px')>-1) { x=parseInt(x.toString().substring(0,x.toString().indexOf('px'))); } return x; }
function gbid(id) { return document.getElementById(id); }
function trace(x) { 
	if (!gbid('trace')) { document.body.innerHTML+='<div id="trace"></div>'; }
	gbid('trace').innerHTML+='<br>'+x; }
//////////////////////////////////////////////////////////////////////////////////////////////// GENERAL FUNCTIONS


//////////////////////////////////////////////////////////////////////////////////////////////// ROOT OPERATIONS
var windowW=0;
var windowH=0;
var rootW=0;
var rootH=0;
function initRoot() {
	if (gbid('root')) {
		rootW=cleanVar(gbid('root').style.width); 
		rootH=cleanVar(gbid('root').style.height);
		centerBodyDiv(); }
	
}
function centerBodyDiv() {
	if ((rootW!=0)&&(rootH!=0)) {
		if (document.body.clientWidth!=windowW) {
			windowW=document.body.clientWidth;
			newLeft=Math.floor((windowW*left)-(rootW/2));
			if (newLeft<0) { newLeft=0; } 
			document.getElementById('root').style.left=newLeft+'px';
		}
		if (document.body.clientHeight!=windowH) {
			windowH=document.body.clientHeight;
			newTop=Math.floor((windowH*top)-(rootH/2));
			if (newTop<0) { newTop=0; } 
			document.getElementById('root').style.top=newTop+'px';
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////// ROOT OPERATIONS

//////////////////////////////////////////////////////////////////////////////////////////////// LANGUAGE OPERATIONS
function langshow(x) {
	if (gbid('language_dropdown')) {
		if (x==1) { gbid('language_dropdown').style.display='block'; }
		if (x==0) { gbid('language_dropdown').style.display='none'; }
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////// LANGUAGE OPERATIONS

//////////////////////////////////////////////////////////////////////////////////////////////// SCROLL OPERATIONS
function initScroll() {	
	if((maxScrollHeight==0)&&(document.getElementById('scrollbody'))) { 
		maxScrollHeight=document.getElementById('scrollbody').scrollHeight;
		scrollCurrent=cleanVar(document.getElementById('scrollbody').style.top);
	} 
}
function scrollDo(way) { scrollWay=way; }
function scrollZone() {
	if (scrollWay!=0) {
		scrollCurrent+=scrollWay*scrollSpeed;
		if (scrollCurrent>0) { scrollCurrent=0; scrollWay=0; }
		if (scrollCurrent<-(maxScrollHeight-scrollFrameHeight)) { scrollCurrent=-(maxScrollHeight-scrollFrameHeight); scrollWay=0; }
		document.getElementById('scrollbody').style.top=scrollCurrent+'px';
	}
}
function scrollLinkTo(loc) {
	if (document.getElementById('scrollbody')) {
		if (scrollCurrent==0) { scrollCurrent=cleanVar(document.getElementById('scrollbody').style.top); }
		loc+='?scrollTo='+scrollCurrent;
	}
	document.location.href=loc;
}
//////////////////////////////////////////////////////////////////////////////////////////////// SCROLL OPERATIONS

//////////////////////////////////////////////////////////////////////////////////////////////// BOXES

function setAlpha(id,value) {
	if (navigator.appName.indexOf('Explorer')==-1) gbid(id).style.opacity=Math.ceil(value*100)/100;
	if (navigator.appName.indexOf('Explorer')!=-1) gbid(id).style.filter='alpha(opacity='+Math.ceil(value*100)+')';
}
function splash() {
	for (x=0; x<splashBox.length;x++) { 
		if (box[splashBox[x]].status!='disabled') { box[splashBox[x]].status='splash'; } 
	}
}

/*function retractSplash() {
	if (box['splash']==1) { 
		for (x=0; x<splashBox.length;x++) { 
			if (box[splashBox[x]].status!='disabled') { 
			box[splashBox[x]].status='smaller';
			} 
		}
		box['splash']=0;
	}
}*/
function expand(id) { 
	//retractSplash();
	if ((box[id].status!='always')&&(box[id].status!='splash')) { box[id].status='bigger'; }
	else if ( box[id].zindex < box['zindex'] ) { 
		box['zindex']++; 
		box[id].zindex = box['zindex'];
		gbid(box[id].boxID).style.zIndex = box[id].zindex;
	}
}
function retract(id) { if ((box[id].status!='always')&&(box[id].status!='splash')) { box[id].status='smaller'; } }
function boxesResize() { 
	for (x=1; x<=boxNum;x++) { 
		resizeBox(x); 
	}
}
function resizeBox(id) {
	if ((box['on'])&&(box[id].status!='disabled')&&(box[id].status!='always')) {
		if ((box[id].status=='bigger')||(box[id].status=='splash')) {
			// Z-index check
			if ( box[id].zindex < box['zindex'] ) { 
				box['zindex']++; 
				box[id].zindex = box['zindex'];
				gbid(box[id].boxID).style.zIndex = box[id].zindex;}
			// opacity -> must reach maxAlpha
			if (box[id].alpha!=box['maxAlpha']) { 
				box[id].alpha+=box['alphaSpeed'];
				if (box[id].alpha>box['maxAlpha']) { box[id].alpha=box['maxAlpha']; }
				setAlpha(box[id].boxID,box[id].alpha);}
			// MAGNIFY EFFECT if content in the div
			if ((box[id].titleID)&&(box[id].contentID)) {
				if ((box[id].currentW!=box[id].maxW) || (box[id].currentH!=box[id].maxH)) {						
					box[id].currentW+=Math.ceil(((box[id].maxW)-box[id].currentW)/box['openSpeed'])+1;
					box[id].currentH+=Math.ceil(((box[id].maxH)-box[id].currentH)/box['openSpeed'])+1;
					if (box[id].currentW>box[id].maxW) { box[id].currentW=box[id].maxW; }
					if (box[id].currentH>box[id].maxH) { box[id].currentH=box[id].maxH; }
					// resize the content
					gbid(box[id].contentID).style.width=(box[id].currentW-((box['pad']*2)+(box['marg']*2)))+'px';
					gbid(box[id].contentID).style.height=(box[id].currentH-(box[id].initH))+'px';
					// resize the box
					if (box[id].side=='left') { 
						gbid(box[id].boxID).style.left=(box[id].posX-(box[id].currentW-box[id].initW))+'px';
					}
					gbid(box[id].boxID).style.width=box[id].currentW+'px';
					gbid(box[id].boxID).style.height=box[id].currentH+'px';
				} else if (box[id].alpha==box['maxAlpha']) {
					if (box[id].status=='splash') { box[id].status='always';}
					else { box[id].status='open'; }
					
				}
			} else if (box[id].alpha==box['maxAlpha']) { box[id].status='open'; }
		}
		else if (box[id].status=='smaller') {
			// opacity -> must decrease to initAlpha
			if (box[id].alpha>box['initAlpha']) { 
				box[id].alpha-=box['alphaSpeed'];
				if (box[id].alpha<box['initAlpha']) { box[id].alpha=box['initAlpha']; }
				setAlpha(box[id].boxID,box[id].alpha);}
			if ((box[id].titleID)&&(box[id].contentID)) {
				if ((box[id].currentW!=box[id].initW) || (box[id].currentH!=box[id].initH)) {
					box[id].currentW-=Math.ceil((box[id].maxW-box[id].initW)/box['closeSpeed']);
					box[id].currentH-=Math.ceil((box[id].maxH-box[id].initH)/box['closeSpeed']);
					if (box[id].currentW<box[id].initW) { box[id].currentW=box[id].initW; }
					if (box[id].currentH<box[id].initH) { box[id].currentH=box[id].initH; }
					// resize the box
					if (box[id].side=='left') { gbid(box[id].boxID).style.left=(box[id].posX+(box[id].initW-box[id].currentW))+'px';}
					gbid(box[id].boxID).style.width=box[id].currentW+'px';
					gbid(box[id].boxID).style.height=box[id].currentH+'px';
					// resize the content
					gbid(box[id].contentID).style.width=(box[id].currentW-((box['pad']*2)+(box['marg']*2)))+'px';
					gbid(box[id].contentID).style.height=(box[id].currentH-(box[id].initH))+'px';
					if ((box[id].currentH-(box[id].initH))<=0) { gbid(box[id].contentID).style.height='1px'; }
				} else if (box[id].alpha==box['initAlpha']) {
					box[id].status='close';
				}			
			} else if (box[id].alpha==box['initAlpha']) { box[id].status='close'; }
		}
		else { box[id].status='close'; }
		return box[id].status;		
	}
	
}
function boxes_obj(id) {
	this.ID=id;
	this.boxID = 'box'+id;
	this.titleID = 'box'+id+'_title';
	this.contentID = 'box'+id+'_content';
	// check of the box
	// if no box OR no title ->  box is disabled
	// if no content -> contentID = false
	if (gbid(this.boxID)&&gbid(this.titleID)) {
		this.posX = cleanVar(gbid(this.boxID).style.left);
		this.posY = cleanVar(gbid(this.boxID).style.top);
		this.zindex = box['zindex'];
		this.alpha = box['initAlpha'];
		// 2 columns in root -> 'left' or 'right'
		if (this.posX<(rootW/2)) { this.side='left'; }
		if (this.posX>=(rootW/2)) { this.side='right'; }
		this.initW = cleanVar(gbid(this.titleID).scrollWidth)+(box['pad']*2)+(box['marg']*2); 
		this.initH = cleanVar(gbid(this.titleID).scrollHeight)+box['pad']+box['marg']+box['margTop'];
		if (this.side=='left') { 
			this.posX=this.posX-this.initW;
		} else {
			gbid('boxTopLeft'+id).className='right_Tl';
			gbid('boxTopRight'+id).className='right_Tr';
			gbid('spacerTopLeft'+id).style.width='50px';
			gbid('spacerTopRight'+id).style.width='10px';
		}
		if (gbid(this.contentID)) {
			this.maxW = cleanVar(gbid(this.contentID).scrollWidth)+(box['pad']*2)+(box['marg']*2);
			this.maxH = this.initH+cleanVar(gbid(this.contentID).scrollHeight);
			if (this.maxW<this.initW) { this.maxW=this.initW; }
			this.currentW=this.initW;
			this.currentH=this.initH;
		} else { 
			this.contentID=false;
			this.initH+=box['pad'];
		}
		// status = disabled | close | open | bigger | smaller | splash | always
		this.status ='close';
		// initialize the box
		gbid(this.boxID).style.left=this.posX+'px';
		gbid(this.boxID).style.width=this.initW+'px';
		gbid(this.boxID).style.height=this.initH+'px';
		gbid(this.boxID).style.overflow='hidden';
		setAlpha(this.boxID,this.alpha);
		gbid(this.boxID).style.visibility='visible';
		if (this.contentID) {
			gbid(this.contentID).style.width=(this.initW-(box['pad']*2)-(box['marg']*2))+'px';
			gbid(this.contentID).style.height='1px';
		}

	} else { 
		this.boxID=false;
		this.titleID=false;
		this.contentID=false;
		this.status ='disabled';
	}
}
function initBoxes() {
	for (x=1; x<=boxNum;x++) { box[x]=new boxes_obj(x);}
	splash();
	box['zindex']++;
	box['on']=true;
}
//////////////////////////////////////////////////////////////////////////////////////////////// BOXES


//////////////////////////////////////////////////////////////////////////////////////////////// MAIN FUNCTIONS
function init() {
	initRoot();
	initBoxes();
	initScroll();
}
function anim() {
	centerBodyDiv();
	boxesResize();
	scrollZone();
}
window.setInterval('anim()',1);
//////////////////////////////////////////////////////////////////////////////////////////////// MAIN FUNCTIONS



function scrolling(way) {
	
}
window.setInterval('')