var browsers = 'all';
var config = {
	hovers: new Array(
		{
			parentId:'nav',
			tagName:'li',
			tagPosition:'all',
			className:'hover'
		}
	),
	inputFields:new Array(
		{
			id:'login',
			setBack:true
		},
		{
			id:'searchProducts',
			setBack:true
		},
		{
			id:'searchSite',
			setBack:true
		}
	),
	togglers:new Array(
		{
			buttonId:'lang-button',
			blockId:'language',
			propertyType:'class',
			values:new Array('opened','closed'),
			defaultValue:'closed'
		}
	)
};

/* CODE AREA */

/* inputFields */

var initInputFields = function()
{
	currentState.inputFields = new Array();

	for (var index in config.inputFields){
		var conf = config.inputFields[index];
		var el = document.getElementById(conf.id);
		if (el){
			el.configIndex = index;
			currentState.inputFields[index] = el.value;
			el.onfocus = function(){
				var prevValue = currentState.inputFields[this.configIndex]; 
				if (this.value == prevValue){
					this.value = '';
				}
			}
			if (conf.setBack){
				el.onblur = function(){
					var prevValue = currentState.inputFields[this.configIndex];
					if (this.value == ''){
						this.value = prevValue;
					}
				}
			}
		}
	}
}

/* hovers */

var initHovers = function()
{
	for (var index in config.hovers){
		var conf = config.hovers[index];
		var topLevelElement = document.getElementById(conf.parentId);
		if (topLevelElement){
			var subElements = topLevelElement.getElementsByTagName(conf.tagName);
			for (var i = 0; i < subElements.length; i++){
				if (conf.tagPosition == 'all' || (conf.tagPosition == 'children' && subElements[i].parentNode.id == conf.parentId))
				{
					subElements[i].configIndex = index;
					subElements[i].onmouseover = function(){
						this.className += ' ' + config.hovers[this.configIndex].className;
					};
					subElements[i].onmouseout = function(){
						this.className = this.className.replace(config.hovers[this.configIndex].className,'');
					};
				}
			}
		}
	}
}

/* togglers */
/* depends on indexOf */

var initTogglers = function()
{
	currentState.togglers = {
		buttons:new Array(),
		blocks:new Array()
	}; 

	for (var index in config.togglers){
		var conf = config.togglers[index];
		var button = document.getElementById(conf.buttonId);
		var block = document.getElementById(conf.blockId);
		if (button && block){
			button.configIndex = index;
			
			currentState.togglers.buttons[index] = button;
			currentState.togglers.blocks[index] = block;
			
			if (conf.propertyType == 'class'){
				block.className = conf.defaultValue;
			} else if (conf.propertyType == 'style'){
				block.style[conf.property] = conf.defaultValue;
			}
			
			button.onclick = function(){
				var propertyType = config.togglers[this.configIndex].propertyType;
				var property = config.togglers[this.configIndex].property;
				var values = config.togglers[this.configIndex].values;
				var button = currentState.togglers.buttons[this.configIndex];
				var block = currentState.togglers.blocks[this.configIndex];

				var currentIndex = 0;
				
				if (propertyType == 'class'){
					var currentStyle = block.className;
				} else if (propertyType == 'style'){
					var currentStyle = block.style[property];
				}				
				
				if (currentStyle){
					var ind = indexOf(values,currentStyle);
					if (ind + 1 < values.length){
						currentIndex = ind + 1;
					}
				}
				
				if (propertyType == 'class'){
					block.className = values[currentIndex];
				} else if (propertyType == 'style'){
					block.style[property] = values[currentIndex];
				}
				return false;
			}
		}
	}	
}

/* lightbox */

var initLightBox = function(){
	var fader = document.getElementById('fader');
	var container = document.getElementById('container');
	
	if (fader && container){
		if (opera){
			var windowHeight = document.body.clientHeight;
			var windowWidth = document.body.clientWidth;
		} else if (ie || ff) {
			var windowHeight = document.documentElement.clientHeight;
			var windowWidth = document.documentElement.clientWidth;
		} else if (safari) {
			var windowHeight = self.innerHeight;
			var windowWidth = self.innerWidth;
		}
		
		if (opera || ie || ff){
			var scrollerTop = document.documentElement.scrollTop;
			var scrollerLeft = document.documentElement.scrollLeft;
		} else if (safari) {
			var scrollerTop = window.pageYOffset;
			var scrollerLeft = window.pageXOffset;
		}
	
		var pageWidth = document.body.offsetWidth;
		var pageHeight = document.body.offsetHeight;
		
		if (pageHeight < windowHeight) pageHeight = windowHeight;
		
		var containerWidth = container.offsetWidth;
		var containerHeight = container.offsetHeight;

		fader.style.width = pageWidth + 'px';
		fader.style.height = pageHeight + 'px';

		var left = Math.round((pageWidth - containerWidth) / 2) + scrollerLeft;
		var top = Math.round((windowHeight - containerHeight) / 2) + scrollerTop;
		if (left < 0) left = 0;
		if (top < 0) top = 0;

		container.style.left = left + 'px';
		container.style.top = top + 'px';
	}
}

var hideSelects = function(){
	var selects = document.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++){
		selects[i].style.display = 'none';
	}
}

var showSelects = function(){
	var selects = document.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++){
		selects[i].style.display = 'inline';
	}
}

var initLightBoxLinks = function(){
	var allProductsLink = document.getElementById('all-products');
	var allApplicationsLink = document.getElementById('all-applications');
	if (allProductsLink){
		allProductsLink.onclick = function(){
			if(navigator.appName == "Microsoft Internet Explorer") {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				http = new XMLHttpRequest();
			}
			
			http.open("GET", "all-products.html");
			http.onreadystatechange = function(){
				if(http.readyState == 4) {
					var container = document.getElementById('container');
					var fader = document.getElementById('fader');
					container.innerHTML = http.responseText;
					fader.style.display = 'block';
					hideSelects();
					initLightBox();
					initCloseLink();
				}
			}
			http.send(null);
			return false;
		}
	}
	if (allApplicationsLink){
		allApplicationsLink.onclick = function(){
			if(navigator.appName == "Microsoft Internet Explorer") {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				http = new XMLHttpRequest();
			}
			
			http.open("GET", "all-applications.html");
			http.onreadystatechange = function(){
				if(http.readyState == 4) {
					var container = document.getElementById('container');
					var fader = document.getElementById('fader');
					container.innerHTML = http.responseText;
					fader.style.display = 'block';
					hideSelects();
					initLightBox();
					initCloseLink();
				}
			}
			http.send(null);
			return false;
		}
	}
}

var initCloseLink = function(){
	var closeLink = document.getElementById('close-lightbox');
	if (closeLink){
		closeLink.onclick = function(){
			var container = document.getElementById('container');
			var fader = document.getElementById('fader');
			fader.style.display = 'none';
			showSelects();
			return false;
		}
	}
}


  
var initTabs = function(){  
  var tab_buttons = Array();
  var tab_content = Array();
  if(document.getElementById('tab-1')){
  tab_buttons[0] = document.getElementById('tab-1');
  tab_content[0] = document.getElementById('tab-content-1');
  tab_buttons[1] = document.getElementById('tab-2');
  tab_content[1] = document.getElementById('tab-content-2');
  tab_buttons[2] = document.getElementById('tab-3');
  tab_content[2] = document.getElementById('tab-content-3');
  
  var n = tab_buttons.length;
  for (var i=0; i<n; i++)  {
    tab_buttons[i].number = i;
    tab_buttons[i].onclick = function() {
      for (var j=0; j<n; j++) {
        if (this.number==j) {
          tab_content[j].style.display = 'block';
          tab_buttons[j].className = 'active';
        }
        else {
          tab_content[j].style.display = 'none';
          tab_buttons[j].className = '';          
        }
      }
    }
  }  
}
}


/* functions */

var indexOf = function(stack, item){
	var len = stack.length;
	for (var i = 0; i < len; i++){
		if (stack[i] === item) return i;
	}
	return -1;
}

/* common */

var opera = window.opera;
var safari = navigator.appVersion.indexOf("Safari") != -1;
var ie = document.all && !window.opera;
var ff = navigator.userAgent.indexOf("Firefox") != -1;

var currentState = {};

var attachOnLoad = function(browsers,initMethod)
{
	if (opera && (browsers.match(/Opera/) || browsers.match(/all/))) {
		window.addEventListener("load", initMethod, false);
	} else if (safari && (browsers.match(/Safari/) || browsers.match(/all/))) {
		window.addEventListener("load", initMethod, false);
	} else if (ie && (browsers.match(/IE/) || browsers.match(/all/))) {
		window.attachEvent("onload", initMethod);
	} else if (ff && (browsers.match(/FF/) || browsers.match(/all/))) {
		window.addEventListener("load", initMethod, false);
	} else if (!ie && !ff && !safari && !opera) {
		if (window.addEventListener){
			window.addEventListener("load", initMethod, false);
		}
		else if (window.attachEvent){
			window.attachEvent("onload", initMethod);
		}
	} 
}


attachOnLoad(browsers,initInputFields);
attachOnLoad(browsers,initHovers);
attachOnLoad(browsers,initTogglers);
attachOnLoad(browsers,initLightBoxLinks);
attachOnLoad(browsers,initTabs);