var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function changeInputType(
	  oldElm, // a reference to the input element
	  iType, // value of the type property: 'text' or 'password'
	  iValue, // the default value, set to 'password' in the demo
	  blankValue, // true if the value should be empty, false otherwise
	  noFocus) {  // set to true if the element should not be given focus
	  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
	    !document.getElementById || !document.createElement) return;
	  var isMSIE=/*@cc_on!@*/false; //http://dean.edwards.name/weblog/2007/03/sniff/
	  if(!isMSIE){
	    var newElm=document.createElement('input');
	    newElm.type=iType;
	  } else {
	    var newElm=document.createElement('span');
	    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
	    newElm=newElm.firstChild;
	  }
	  var props=['name','id','className','size','tabIndex','accessKey'];
	  for(var i=0,l=props.length;i<l;i++){
	    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
	  }
	  newElm.onfocus=function(){return function(){
	    if(this.hasFocus) return;
	    var newElm=changeInputType(this,'password',iValue,
	      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
	    if(newElm) newElm.hasFocus=true;
	  }}();
	  newElm.onblur=function(){return function(){
	    if(this.hasFocus)
	    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
	      changeInputType(this,'text',iValue,false,true);
	    }
	  }}();
	 // hasFocus is to prevent a loop where onfocus is triggered over and over again
	  newElm.hasFocus=false;
	  // some browsers need the value set before the element is added to the page
	  // while others need it set after
	  if(!blankValue) newElm.value=iValue;
	  oldElm.parentNode.replaceChild(newElm,oldElm);
	  if(!isMSIE && !blankValue) newElm.value=iValue;
	  if(!noFocus || typeof(noFocus)=='undefined') {
	    window.tempElm=newElm;
	    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
	  }
	  return newElm;
}



function populateElement(selector, defvalue, inputtype) {
	if (typeof selector == "string")
		selector = $(selector); 
 	
	if($(selector).value == "") {
		$(selector).value = defvalue;
	}
	
    var ua=navigator.userAgent.toLowerCase();
	
	if(inputtype=='password' && 
	  !((ua.indexOf('konqueror')!=-1) && /khtml\/3\.[0-4]/.test(ua)) && 
	  !(((ua.indexOf('safari')!=-1) && !window.print))) {
	    // Set the third value to the text you want to appear in the field.
	    changeInputType(selector,'text',defvalue,false,true);
	 }
	
	$(selector).addEvent('blur',function(){
		if($(selector).value == "") {
			$(selector).value = defvalue;
			$(selector).setStyle('font-style', 'italic');
		}
	});

	$(selector).addEvent('focus',function(){
		if($(selector).value == defvalue) {
			$(selector).value = "";
			$(selector).setStyle('font-style', 'normal');
		}
	});
}
		
BrowserDetect.init();

var Site = {
	syncSizes: function(){
			
				
				var cnt_inner = $(document.body).getElement('div.content_wraper');
				if (BrowserDetect.browser=="Explorer" &&
					BrowserDetect.version < 7)
				{
					prop = 'height';
				}
				else
				{
					prop = 'min-height';
				}
				
				cnt_inner.setStyle('height', 'auto');
				cnt_inner.setStyle(prop, '1px');
				
				cnt_inner.setStyle(prop, (2+cnt_inner.offsetHeight+$(document.body).getElement('div.header_tile_container').offsetHeight-$(document.body).getElement('#container').offsetHeight-$(document.body).getElement('#footer_menu').offsetHeight)+'px');

				var l2 = $$('ul.secondLevel');
			    t = $(document.body).getElement('div.header').offsetHeight;
			    x = 0;
				l2.each(function(element, index) {
					if (x<element.offsetHeight) x = element.offsetHeight;
				});				
				if (t+x>$(document.body).offsetHeight-36) {
					cnt_inner.setStyle(prop,(cnt_inner.offsetHeight-$(document.body).offsetHeight+t+x+36)+'px');
				}
				
	}	
};

window.addEvent('domready', Site.syncSizes);
window.addEvent('load', Site.syncSizes);
window.addEvent('resize', Site.syncSizes);

var Horoscope = new Class({
	initialize: function(elementHoroscope) {
		this.cycle = true;
		this.cyclePeriod = 4000;

		this.elementHoroscope = elementHoroscope;
		
		this.items = this.elementHoroscope.getElements('div.single_sign');
		//this.elementHoroscope.getElement('div.buttons_sign_outer').setStyle('visibility', 'visible');
		
		this.fx = new Fx.Elements(this.items, {wait: false, duration: 200, transition: Fx.Transitions.quadOut});

		this.currentItem = 0;
		for (var i = 0; i<this.items.length; i++){
			if (this.items[i].getStyle('visibility')=='visible') {
				this.items[i].setStyle('opacity', 1);
				this.currentItem = i;
			}
			else {
				this.items[i].setStyle('opacity', 0);
			}
		}

		this.buttonUp = this.elementHoroscope.getElement('div.buttons_sign_outer a.up');
		if($defined(this.buttonUp)) {
			//this.buttonUp.removeEvents('click');
			//this.buttonUp.removeProperty('href');
			this.buttonUp.addEvent('click', this.eventUp.bind(this));
		}
		
		this.buttonDown = this.elementHoroscope.getElement('div.buttons_sign_outer a.down');
		if($defined(this.buttonDown)) {
			//this.buttonDown.removeEvents('click');
			//this.buttonDown.removeProperty('href');
			this.buttonDown.addEvent('click', this.eventDown.bind(this));	
		}
		
		setTimeout(this.nextItem.bind(this), this.cyclePeriod);
	},
	showItems: function(itemNumToShow) {
		function showElement(){
			this.itemToShowEff.start({'opacity':1});
		};

		if (this.itemToShowEff){
			this.itemToShowEff.stop();
		}
		this.fx.stop();
		this.itemToShowEff = new Fx.Styles(this.items[itemNumToShow], {duration: 500, wait: false, transition: Fx.Transitions.quadIn});

		var obj = {};
		this.items.each(function(other, j){
			obj[j] = {'opacity': 0};
		});
		this.fx.start(obj).chain(
			showElement.bind(this)
		);

		/*
		this.fx.stop();
		var obj = {};
		obj[itemNumToShow] = {
			'opacity': [this.items[itemNumToShow].getStyle('opacity').toFloat(), 1]
		};
		var itemToShow = this.items[itemNumToShow];
		this.items.each(function(other, j){
			if (other != itemToShow){
				var w = other.getStyle('opacity').toFloat();
				if (w != 0) obj[j] = {'opacity': [w, 0]};
			}
		});
		this.fx.start(obj);
		*/
	},
	nextItem: function() {
		if (this.cycle) {
			if(this.currentItem>0)
				this.currentItem = this.currentItem - 1;
			else 
				this.currentItem = this.items.length - 1;

			this.showItems(this.currentItem);
			setTimeout(this.nextItem.bind(this), this.cyclePeriod);
		}
	},
	eventUp: function() {
		this.cycle = false;

		if(this.currentItem>0)
			this.currentItem = this.currentItem - 1;
		else 
			this.currentItem = this.items.length - 1;

		this.showItems(this.currentItem);
		
		return false;
	},
	
	eventDown: function() {
		this.cycle = false;

		if(this.currentItem<this.items.length-1)
			this.currentItem = this.currentItem + 1;
		else
			this.currentItem = 0;
		
		this.showItems(this.currentItem);
		
		return false;
	}
	
});

window.addEvent('domready', function(){

			/*
			 * Logic for listing single view horoscope
			*/
	
		var horoscopeitems = $$('div.horoscope_item_single');
	    
		horoscopeitems.each(function(element, index) {
			
			var navCheck = element.getElement('div.buttons_sign_outer');
			
			if(navCheck.getStyle('display')!='none') {
				var tHoroscope = new Horoscope(element);
			}
		});
		
		     /*
			 * Logic for user input
			 */
	
	    if ($('user')!=null &&
			$('user')!="null")
		{
			r = $('user').value;
			$('user').value = "";
			populateElement('user', r, 'text');
		}			
	
	         /*
			 * Logic for pass input
			 */
	    if ($('pass')!=null &&
			$('pass')!="null")
		{
			p = $('pass').value;
			$('pass').value = "";
			populateElement('pass', p, 'password');
		}	
	    
			/*
			 * Logic for search input
			 */
			
		t = $('search_sword').value;
		$('search_sword').value = "";
		populateElement('search_sword', t);
		
			/*
			 * Logic for main menu
			 */
		
		var teasers = $$('li.firstLevel a.firstLevel');
			
 		var totalWidth = -1;
		
 		teasers.each(function(teaser) {
				teaser.setStyle('width', teaser.offsetWidth);
 				totalWidth += teaser.offsetWidth +1 ;
 		});

		if (979>totalWidth)
		{
			/*
			* Add additional padding
			*/
			
			var addPadding = (979.00-totalWidth)/teasers.length;
			var cumulatedError = 0.0;
				
			var total = 0;
			var diff = 979.00-totalWidth;
			teasers.each(function(teaser, i) {
 				if (i!=teasers.length-1) {
	 				add = Math.floor(addPadding);
	 				cumulatedError += addPadding - Math.floor(addPadding);
	 				if (cumulatedError>1)
	 				{
						cumulatedError--;
						add++;
					}
	 				teaser.setStyle('width', teaser.offsetWidth + add);	
 				}
			});

	 		totalWidth = 0;
			
	 		teasers.each(function(teaser) {
	 				totalWidth += teaser.offsetWidth;
	 		});
	 		teasers[teasers.length-1].setStyle('width', teasers[teasers.length-1].offsetWidth + 979-totalWidth-1);	
		};
		
			/*
			* Logic for footer menu
			*/
		
		var footerTeasers = $$('li.bottomMenuFirstLevel');
			
 			if (footerTeasers.length > 0)
 			var totalWidth = 0;
		 	footerTeasers.each(function(footerTeasers) {
 				totalWidth += footerTeasers.offsetWidth;
 			});
		
 		if (963>totalWidth)
		{
			/*
			* Add additional padding
			*/
			var addPadding = (963-totalWidth)/(footerTeasers.length*2);
			var cumulatedError = 0.0;

 			footerTeasers.each(function(teaser) {
 				add = Math.floor(addPadding);
 				cumulatedError += addPadding - Math.floor(addPadding);
 				if (cumulatedError>1)
 				{
				cumulatedError--;
				add++;
				}
 				//teaser.setStyle('padding-left', teaser.getStyle('padding-left').replace('px', '').toInt() + add);
 				teaser.setStyle('width', teaser.offsetWidth + add);

 				add = Math.floor(addPadding);
 				cumulatedError += addPadding - Math.floor(addPadding);
			 	if (cumulatedError>1)
			 	{
				cumulatedError--;
				add++;
				}
			 	//teaser.setStyle('padding-right', teaser.getStyle('padding-right').replace('px', '').toInt() + add);
			 	teaser.setStyle('width', teaser.offsetWidth + add);
			});

 			footerTeasers.each(function(teaser) {
 				//teaser.getElement('a.first').setStyle('width', teaser.offsetWidth);
 			});

				/*
				* Check if everything went fine, if not correct (by maximum 1px)
				* Note: this should not happen at all
				*/
			
 			totalWidth = 0.0;
			footerTeasers.each(function(teaser) {
				totalWidth += teaser.offsetWidth;
				//alert(teaser.offsetWidth);
			});
			//alert(totalWidth);
			if (963!=totalWidth)
			{
				//alert(963 - totalWidth);
				//teasers[0].setStyle('padding-right', teasers[0].getStyle('padding-right').replace('px', '').toInt() + 800 - totalWidth);
				//teasers[0].setStyle('width', teasers[0].offsetWidth + add);
			}
		};
});
