var api = {
	minH: 608, //726,
	navH: 33, //151,
	conH: 575,
	pW: 0,
	pH: 0,
	bW: 1440,
	bH: 900,

	initPage: function () {
		//return;
		if(!api.browserCheck()) return;
		api.initResize();
		api.Scroller.Check();
		window.onresize = api.initResize;
	},


	initResize: function () {
		api.pH = $('pageBody').getHeight();
		api.pW = $('pageBody').getWidth();
		if(api.pH < api.minH) api.newH = api.minH;
		else api.newH = api.pH;

		if(api.pH < 726) $$('#TopNavMenu .SubLevel').each(function(ul) { ul.setStyle({'border':"1px #737B7F solid"});} );
		else $$('#TopNavMenu .SubLevel').each(function(ul) { ul.setStyle({'border':"none"});} );


		$('Search').setStyle({minHeight:api.newH +'px'});
		$('View').setStyle({height: api.newH +'px', minHeight:api.newH +'px'});
		$('Page').setStyle({ height: api.newH +'px'});
		//$$('#Search input[name="indexed_search"]')[0].value = api.pW +"x"+ api.newH +" | "+ $('Main').getHeight() +" | "+ $('pageBody').getHeight(); +"";

		if($('bg')) {
			var bgDiv  = $$('#bg div')[0];
			var bgImg  = $$('#bg img')[0];

			var bF = api.bW / api.bH;
			var pF = api.pW / api.pH;

			if(bF > pF) var bgH = api.pH, bgW = api.pH / api.bH * api.bW;
			else var bgW = api.pW, bgH = api.pW / api.bW * api.bH;

			bgDiv.setStyle({margin:0, top:0, left:0, width: api.pW+'px', height: api.pH+'px', overflow:'hidden'});
			bgImg.setStyle({margin:0, top:0, left:0, width: bgW+'px', height: bgH+'px'});
			//$$('#Search input[name="indexed_search"]')[0].value = bgW +"x"+ bgH +" | "+ api.pW +"x"+ api.pH;
		}
	},

	Nav: {
		Curr : false,
		Timer: false,

		Init: function() {
			$('TopNavMenu').select("ul.SubLevel").each( function(item){ item.addClassName('hidden') } );
			$('TopNavMenu').select("li.TopLevel").each( function(item){ item.onmouseover = function(e){api.Nav.ItemOver(item) } } );
		},
		ItemOver: function(node) {
			if(api.Nav.Timer) { window.clearTimeout(api.Nav.Timer); api.Nav.Timer = false; }
			var subNav = Element.select(node,"UL.SubLevel")[0];
			if(!subNav && !api.Nav.Curr) return;
			if(api.Nav.Curr && api.Nav.Curr != subNav) api.Nav.Curr.addClassName('hidden');
			if(!subNav) return;
			var subItems = Element.select(subNav, "LI").each( function(item){ item.onmouseover = function(e){api.Nav.ItemOver(subNav) } } );
			api.Nav.Curr = subNav;
			subNav.removeClassName('hidden');
			subNav.onmouseout  = function() { api.Nav.Timer = window.setTimeout('api.Nav.ItemOut()', 300); }
		},
		ItemOut: function(node) {
			if(api.Nav.Curr) api.Nav.Curr.addClassName('hidden');
			window.clearTimeout(api.Nav.Timer); api.Nav.Timer = false; api.Nav.Curr = false;
		}
	},
	projectItems: {
		View: 295,
		Body: false,
		Init: function() { if($('innerProjectItems').getHeight() > api.projectItems.View) { this.Body = $('innerProjectItems').getHeight(); api.projectItems.Check();}},
		Move: function (to) {
			var Step = 98;
			switch(to){
				case 'down': new Effect.Move($('innerProjectItems'), { duration: 1.0, x:0, y:Step , mode:'relative', afterFinish:function(){ api.projectItems.Check();} }); break;
				case 'up': new Effect.Move($('innerProjectItems'), { duration: 1.0, x:0, y:-Step, mode:'relative', afterFinish:function(){ api.projectItems.Check();}}); break;
			}
		},
		Check: function() {
			var Pos = $('innerProjectItems').positionedOffset(), minTop = api.projectItems.View - api.projectItems.Body, maxTop = 0;
			if(Pos.top >= maxTop) { $('innerProjectItems').setStyle({top: maxTop+'px'}); $("browserPrev").addClassName('hidden'); } else { $("browserPrev").removeClassName('hidden'); }
			if(Pos.top <= minTop) { $('innerProjectItems').setStyle({top: minTop+'px'}); $("browserNext").addClassName('hidden'); } else { $("browserNext").removeClassName('hidden'); }
		}
	},

	trim: function(str){
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	// String
	},

	browserCheck: function () {
		//alert("Browser Check");
		if (!window.RegExp) return false;
		var agentStr = false;
		var AGENTS = ["opera","msie","safari","firefox","netscape","mozilla"];
		var agent  = navigator.userAgent.toLowerCase();
		var browser = navigator.appName.toLowerCase();
		for (var i = 0; i < AGENTS.length; i++) {
			agentStr = AGENTS[i];
			if (agent.indexOf(agentStr) != -1) {
				var versionExpr = new RegExp(agentStr + "[ \/]?([0-9]+(\.[0-9]+)?)");
				if (versionExpr.exec(agent) != null) {
					version = parseFloat(RegExp.$1);
				};
				if (agentStr == "opera") return version >= 8.0;
				if (agentStr == "safari") return version >= 125;
				if (agentStr == "msie") return (version >= 6.0 && agent.indexOf("powerpc") == -1);
				if (agentStr == "netscape") return version > 7.1;
				if (agentStr == "firefox") return version >= 1.5;
				if (browser  == "konqueror") return version >= 5.5;
			};
		};
		return !!document.getElementById;
	},

	Scroller: {
		cObj: {'Main': {'DIV': false, 'BAR': false, 'Width':303} , 'Right': {'DIV': false, 'BAR': false, 'Width':193} },
		pNode : false,
		cNode : false,

		Check: function() {
			$$("#View div.outerContent").each(function(oC){
				var iC = oC.select("div.innerContent")[0];
				if(oC.getHeight() < iC.getHeight()) {
					//alert(oC.id +" Outer = "+ oC.getHeight() +"px AND Inner = "+ iC.getHeight() +"px");
					api.Scroller.Init(oC, oC.parentNode);
				}
			});
		},
		Init: function (cNode, pNode) {
			//sNode = pNode.select("div.ScrollBarContainer");
			//if(!sNode)
			sNode = api.Scroller.Create(pNode, cNode.getHeight());

			cNode.select("div.innerContent").each(function(item) { item.addClassName('Scroller-Container');});
			cNode.setStyle({'overflow': "hidden"});

			var sObjDIV = new Scrolling.Scroller(cNode, cNode.getWidth(), cNode.getHeight());
			var sObjBAR = new Scrolling.Scrollbar( sNode, sObjDIV, new Scrolling.ScrollTween());
			var sObjTWE = new Scrolling.ScrollTween(sObjBAR, true);
  			sObjTWE.stepDelay = 15;
			return;

		},
		Create: function(pNode, h) {

			var divSC = new Element('div', {'class': 'ScrollBarContainer'});
			var divST = new Element('div', {'class': 'Scrollbar-Track'});
			var imgUP = new Element('img', {src: '/spacer.gif', height:5,  width:7, 'class': 'Scrollbar-Up'});
			var imgDW = new Element('img', {src: '/spacer.gif', height:5,  width:7, 'class': 'Scrollbar-Down'});
			var imgHD = new Element('img', {src: '/spacer.gif', height:50, width:7, 'class': 'Scrollbar-Handle'});
			divST.setStyle({height: (h-46)+"px" });

			divST.appendChild(imgHD);
			divSC.appendChild(imgUP);
			divSC.appendChild(divST);
			divSC.appendChild(imgDW);
			pNode.appendChild(divSC);

			return divSC;
		},

		Destroy: function(pNode) {
			var divSC = $$("div.ScrollBarContainer")[0];
			if(!divSC) return;
			pNode.removeChild(divSC);
		}
	},

	Mail:{
		decryptCharcode: function (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);
		},

		decryptString: function (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 += api.Mail.decryptCharcode(n,0x2B,0x3A,offset);	// 0-9 . , - + / :
				} else if (n >= 0x40 && n <= 0x5A)	{
					dec += api.Mail.decryptCharcode(n,0x40,0x5A,offset);	// A-Z @
				} else if (n >= 0x61 && n <= 0x7A)	{
					dec += api.Mail.decryptCharcode(n,0x61,0x7A,offset);	// a-z
				} else {
					dec += enc.charAt(i);
				}
			}
			return dec;
		},
		 _mEncrypt: function(s, p) {
			var link = 'mailto:'+ api.Mail.decryptString(s,-2) + (p ? '?'+ p : '');
			location.href = link;
		}
	},

	Countdown: {
		Dest: 0,
		Curr: 0,
		Offs: 0,
		Digital: false,
		Init: function(v, c) { this.Dest = new Date(); this.Curr = new Date(); this.Offs = new Date(); this.Dest.setTime(v); this.Curr.setTime(c); this.Offs = this.Offs.getTime() - this.Curr.getTime(); this.Dest.setTime(this.Dest.getTime() + this.Offs); api.Countdown.Counter(); },
		Counter: function () {
			this.Curr = new Date();
			var Ja=0, Mo=0, Ta=0, St=0, Mi=0, Se=0, Ze=0, Hu=0;
			if(this.Curr < this.Dest) {
				while(this.Curr < this.Dest) { Ja++; this.Curr.setFullYear(this.Curr.getFullYear()+1); } this.Curr.setFullYear(this.Curr.getFullYear()-1); Ja--;
				while(this.Curr < this.Dest) { Mo++; this.Curr.setMonth(this.Curr.getMonth()+1); } this.Curr.setMonth(this.Curr.getMonth()-1); Mo--;
				while(this.Curr.getTime()+(24*60*60*1000) < this.Dest) { Ta++; this.Curr.setTime(this.Curr.getTime()+(24*60*60*1000)); }
				St = Math.floor((this.Dest-this.Curr)/(60*60*1000));
				this.Curr.setTime(this.Curr.getTime()+St*60*60*1000);
				Mi = Math.floor((this.Dest-this.Curr)/(60*1000));
				this.Curr.setTime(this.Curr.getTime()+Mi*60*1000);
				Se = Math.floor((this.Dest-this.Curr)/1000);
				this.Curr.setTime(this.Curr.getTime()+Se*1000);
				Hu = Math.floor((this.Dest-this.Curr)/10);
				/* this.setDigit(Ja+'', 'jah'); this.setDigit(Mo+'', 'mon'); */
				this.setDigit(Ta+'', 'tag');
				this.setDigit(St+'', 'std');
				this.setDigit(Mi+'', 'min');
				this.setDigit(Se+'', 'sek');
				this.setDigit(Hu+'', 'hun');

				setTimeout('api.Countdown.Counter()',20); } else { $('Counter').fade({duration:2.0}); $('pageCounter').fade({duration:9.0}); }
		},
		setDigit: function(val, id) { if(val < 10) { $(id+'[0]').className = 'ziffer0'; $(id+'[1]').className = 'ziffer'+ (val != 0 ? val : "0"); } else { $(id+'[0]').className = 'ziffer'+ val.substr(0,1); $(id+'[1]').className = 'ziffer'+ val.substr(1,1); }}
	},
	FormSend: function(name, url, funct) {
		new Ajax.Request(url, { parameters: $(name).serialize(true), method: 'post', onComplete: funct });
	}
};
