var currSource	= "";
var currTarget	= "box0";
var oldHeight	  = 0;


function innerH(){
	var vpX;
	var vpY;

	if (self.innerHeight) // all except Explorer
	{
		vpX = self.innerWidth;
		vpY = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		vpX = document.documentElement.clientWidth;
		vpY = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		vpX = document.body.clientWidth;
		vpY = document.body.clientHeight;
	}
	return vpY;
}


function notSelected(id){
	if ($(id) != undefined) Element.classNames(id).remove('selectedItem');
}

function doOnLoad(){
	//new Effect.Opacity("box0", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box97", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box1", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box98", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box99", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box2", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box3", { to: 0.0, from: 1.0, duration:0.025 });
	new Effect.Opacity("box4", { to: 0.0, from: 1.0, duration:0.025 });

}

function posCenter(){
		var e = $('center');
		var p = Position.cumulativeOffset(e);
		if (p[0] < 20){
			p[0] = 20;
		}
		e.setStyle({left: p[0]+"px", marginLeft: "auto"});
		var el = Position.positionedOffset(e)[0];
}

function relCenter(){
		var e  = $('center');
		e.setStyle({left: 50+"%", marginLeft: "-350px"});	
}

function loadContent(theURL, sourceID, targetID) {
	 
	 var id1 = "item" + sourceID;
	 var id2 = "box" + targetID;
	 var id3 = "bottom" + targetID;
	 
	 if (currSource != null && currTarget != null) {
	 	//window.setTimeout("notSelected('"+currSource+"')", 250);
	 	//posCenter();
		
	 	if (id2 != currTarget || id1 == currSource){
			var collTarget = currTarget;
			if(currSource){
				new Effect.Morph(currSource, { style: 'background:#ffffff; color: #000000;', duration:0.33});
				new Effect.Morph($(currSource).down('img'), { style: 'border-color: #a8a8a8;', duration:0.33});
				new Effect.Morph($(currSource).down('a',1), { style: 'color: #a8a8a8;', duration:0.33});
				new Effect.Morph($(currSource).down('a',2), { style: 'color: #a8a8a8;', duration:0.33});
			}
			new Effect.Opacity(collTarget, { 
													 to: 0.0,
													 from: 1.0,
													 duration:0.33,
													 afterFinish:function(eff){
														 var elementDimensions = $(collTarget).getDimensions();
														 new Effect.Scale(eff.element.id, 0, Object.extend({ 
																			scaleContent: false, 
																			scaleX: false,
																			scaleFrom: 100,
																			//scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
																			restoreAfterFinish: true,
																			//afterSetup: function(effect) {
																				//effect.element.makeClipping().setStyle({height: '0px'}).show(); 
																			//},  
																			afterFinishInternal: function(effect) {
																				effect.element.undoClipping().hide();
																			}
																		}, {duration:0.1} || {}));
													 }})
			/*
			new Effect.Fade(currTarget, {
											duration: .25
											});
			*/
		} else {
				new Effect.Morph(currSource, { style: 'background:#ffffff;', duration:0.33});
				new Effect.Morph($(currSource).down('img'), { style: 'border-color: #a8a8a8;', duration:0.33});
				new Effect.Morph($(currSource).down('a',1), { style: 'color: #a8a8a8;', duration:0.33});
				new Effect.Morph($(currSource).down('a',2), { style: 'color: #a8a8a8;', duration:0.33});
				new Effect.Opacity(id2, { 
													 to: 0.0,
													 from: 1.0,
													 duration:0.33
													 })
		}
	 }
	 
	 if (currSource != id1){
		var time = 650;
		if (id2 == currTarget){
			time = 650;
		}
		
		var t = window.setTimeout("ajUp('"+theURL+"', '"+id1+"', '"+id2+"', '"+id3+"')", time);
		//ajUp(theURL, id1, id2);
		currSource = id1;
	 } else {
		 var t = window.setTimeout("start()", 650);
		currSource = "";
		currTarget = "box0";
	}
}

function ajUp(theURL, id1, id2,id3){
	if (id2 != currTarget){
		new Ajax.Updater(id2, theURL, {
			method:     'get',
			onComplete: function() {
				//posCenter();
				//Element.classNames(id1).add('selectedItem');
				
				var elementDimensions = $(id2).getDimensions();
				
				new Effect.Scale(id2, 100, Object.extend({ 
												scaleContent: false, 
												scaleX: false,
												scaleFrom: 0,
												scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
												restoreAfterFinish: true,
												afterSetup: function(effect) {
													effect.element.makeClipping().setStyle({height: '0px'}).show(); 
												},  
												afterFinishInternal: function(effect) {
														effect.element.undoClipping();
												},
												afterFinish: function(effect){
													new Effect.Opacity(id2, { to: 1.0, from: 0.0, duration:0.33, afterFinish:checkScroll});
													new Effect.Morph(id1, { style: 'background:#000000;', duration:0.33});
													new Effect.Morph($(id1).down('img'), { style: 'border-color: #000000;', duration:0.33});
													new Effect.Morph($(id1).down('a', 1), { style: 'color: #ffffff;', duration:0.33});
													new Effect.Morph($(id1).down('a', 2), { style: 'color: #ffffff;', duration:0.33});
												}
												}, {duration:.1} || {}));
				/*
				new Effect.Appear(id2, {
													duration: .75,
													afterFinish:checkScroll
													});
				*/
			}
			
		});

	} else {
	oldHeight = $(id2).getDimensions().height;
	new Ajax.Updater(id2, theURL, {
			method:     'get',
			onComplete: function() {
				//posCenter();
				new Effect.Opacity(id2, { to: 1.0, from: 0.0, duration:0.33});
				new Effect.Morph(id1, { style: 'background:#000000;', duration:0.33});
				new Effect.Morph($(id1).down('img'), { style: 'border-color: #000000;', duration:0.33});
				new Effect.Morph($(id1).down('a', 1), { style: 'color: #ffffff;', duration:0.33});
				new Effect.Morph($(id1).down('a', 2), { style: 'color: #ffffff;', duration:0.33});
				var elementDimensions = $(id2).getDimensions();
				var from = (oldHeight/elementDimensions.height)*100;
			
				new Effect.Scale(id2, 100, Object.extend({ 
    		scaleContent: false, 
    		scaleX: false,
    		scaleFrom: from,
    		scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    		restoreAfterFinish: true,
    		afterSetup: function(effect) {
      		effect.element.makeClipping().setStyle({height: '0px'}).show(); 
				},
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
				},
				afterFinish:checkScroll
			}, {duration:.1} || {}));
				
		}
		});
		
	}
	currTarget = id2;
}


function start(){
				
				//posCenter();
			
				var elementDimensions = $("box0").getDimensions();
				
				new Effect.Scale("box0", 100, Object.extend({ 
												scaleContent: false, 
												scaleX: false,
												scaleFrom: 0,
												scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
												restoreAfterFinish: true,
												afterSetup: function(effect) {
													effect.element.makeClipping().setStyle({height: '0px'}).show(); 
												},  
												afterFinishInternal: function(effect) {
														effect.element.undoClipping();
												},
												afterFinish: function(effect){
													new Effect.Opacity("box0", { to: 1.0, from: 0.0, duration:0.33, afterFinish:checkScroll});
												}
												}, {duration:.1} || {}));
				/*
				new Effect.Appear(id2, {
													duration: .75,
													afterFinish:checkScroll
													});
				*/
}
			

function checkScroll(effect){
	//alert("SCROLLCHECK");
	var el = effect.element;
	
	var xOff = Position.page(el)[1];
	var elH  = el.getHeight();
	
	var end = xOff + elH;  
	var ih	 = innerH();
	//alert(end + " > " + ih);
	if(end > ih){
		var over = end-ih;
		var cum = Position.cumulativeOffset(el);
		var newOff = cum[1]-xOff+over-30;
		//alert("scroll");
		new Effect.ScrollTo('topPage', {offset: newOff, duration:.2});
	} else if(xOff < 94){
		var cum = Position.cumulativeOffset(el);
		var newOff = cum[1]-124;
		//alert("scroll");
		new Effect.ScrollTo('topPage', {offset: newOff, duration:.2});
	
	}
	
	
	
}

//#####################################################

	// decrypt helper function
		function decryptCharcode(n,start,end,offset)	{
			n = n + offset;
			if (offset > 0 && n > end)	{
				n = start + (n - end - 1);
			} else if (offset < 0 && n < start)	{
				n = end - (start - n - 1);
			}
			return String.fromCharCode(n);
		}
			// decrypt string
		function decryptString(enc,offset)	{
			var dec = "";
			var len = enc.length;
			for(var i=0; i < len; i++)	{
				var n = enc.charCodeAt(i);
				if (n >= 0x2B && n <= 0x3A)	{
					dec += decryptCharcode(n,0x2B,0x3A,offset);	// 0-9 . , - + / :
				} else if (n >= 0x40 && n <= 0x5A)	{
					dec += decryptCharcode(n,0x40,0x5A,offset);	// A-Z @
				} else if (n >= 0x61 && n <= 0x7A)	{
					dec += decryptCharcode(n,0x61,0x7A,offset);	// a-z
				} else {
					dec += enc.charAt(i);
				}
			}
			return dec;
		}
			// decrypt spam-protected emails
		function linkTo_UnCryptMailto(s)	{
			location.href = decryptString(s,-1);
		}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
