// JavaScript Document
//SET GLOBALS
var canvaswidth = 970;
var subnavwidth = '160px';
var timer = 200;
var wheretostartleftalign = 5;
var effectDuration = .1;
var thirdlevelduration = .12;
var topoffsetID = 'header_wrapper';

//NAVIGATION HOLDER
var MainNav = new Array();
		
function initNavEvents()
{
	//init ids
	var mainnav = null;
	var subnav = null;
	for(var a=0; a<MainNav.length; a++)
	{
		MainNav[a].submenu.style.width = subnavwidth;
	}

	//init main nav events
	for(var a=0; a<MainNav.length; a++)
	{				  
		function showsub(event, obj) 
		{    
			clearTimeout(obj.outtimer);
			clearTimeout(obj.subnavtimer);
			obj.overtimer = setTimeout(function () {		
					
					
					
					if(obj.submenu.style.display != 'block')
					{
						new Effect.BlindDown(obj.submenu, 
						{
							duration: effectDuration,
							beforeStart: function ()
							{
								obj.submenu.style.visibility = 'visible';
								obj.submenu.style.display = 'none'; 
								obj.submenu.style.zIndex= '100'; 
							},
							afterFinish: function ()
							{
								obj.submenu.style.visibility = 'visible';
								obj.submenu.style.display = 'block'; 
							}
						});
					}
			}, timer);
			   
		} 
		MainNav[a].parentItem.observe('mouseover', showsub.bindAsEventListener(this, MainNav[a]));   
		
		function hidesub(event, obj) 
		{    
			clearTimeout(obj.overtimer);
			obj.parentItem.className = '';
			obj.outtimer = setTimeout(function () {
					if(obj.submenu.style.display != 'none')
					{
						new Effect.BlindUp(obj.submenu, 
						{
							duration:effectDuration,
							beforeStart: function ()
							{
								
								obj.submenu.style.zIndex= '0'; 
							},
							afterFinish: function ()
							{
								obj.submenu.style.visibility = 'hidden';
								obj.submenu.style.display = 'none'; 
							}
						});
					}
			}, timer);
		} 
		MainNav[a].parentItem.observe('mouseout', hidesub.bindAsEventListener(this, MainNav[a]));  
	}
	
	//init submenu events
	for(var a=0; a<MainNav.length; a++)
	{
		if(MainNav[a].submenu)
		{
			function showsubmenu(event, obj) 
			{    
				obj.parentItem.className = 'main_nav_wrapperOn';

				obj.submenu.style.visibility = 'visible';
				obj.submenu.style.display = 'block';   
				clearTimeout(obj.subnavtimer);
				clearTimeout(obj.outtimer);
			} 
			MainNav[a].submenu.observe('mouseover', showsubmenu.bindAsEventListener(this, MainNav[a]));   
			
			function hidesubmenu(event, obj) 
			{   
				obj.subnavtimer = setTimeout(function () {
					new Effect.BlindUp(obj.submenu, 
						{
							duration:effectDuration,
							beforeStart: function ()
							{
							},
							afterFinish: function ()
							{
								obj.parentItem.className = '';
								obj.submenu.style.visibility = 'hidden';
								obj.submenu.style.display = 'none'; 
							}
						});
				}, timer);
			} 
			MainNav[a].submenu.observe('mouseout', hidesubmenu.bindAsEventListener(this, MainNav[a]));  
		}
				
	}

	//ini thirdnavs events
	for(var y=0; y<MainNav.length; y++)
	{
		for(var m=0; m<MainNav[y].thirdLvL.length; m++)
		{
			if(MainNav[y].thirdLvL[m].thirdmenuNode)
			{
				
				function showthirdsub(event, obj) 
				{    
					clearTimeout(obj.thirdmenuouttimer);
					obj.thirdparentItem.style.backgroundColor =  '#a21411';
					obj.thirdmenuovertimer = setTimeout(function () {
						if(obj.thirdmenuNode.style.visibility != 'visible')
						{
							new Effect.BlindDown(obj.thirdmenuNode, 
							{
								duration: thirdlevelduration,
								scaleX:	true,
								scaleY: true,
								beforeStart: function ()
								{
									obj.thirdmenuNode.style.visibility = 'visible';
									obj.thirdmenuNode.style.display = 'none'; 
									obj.thirdmenuNode.style.zIndex= '100'; 
								},
								afterFinish: function ()
								{
									obj.thirdmenuNode.style.visibility = 'visible';
									obj.thirdmenuNode.style.display = 'block'; 
								}
							});
						}
					}, timer);					   
				} 
				MainNav[y].thirdLvL[m].thirdparentItem.observe('mouseover', showthirdsub.bindAsEventListener(this, MainNav[y].thirdLvL[m]));  
				
				
				function hidethirdsub(event, obj) 
				{    
					clearTimeout(obj.thirdmenuovertimer);
					obj.thirdparentItem.style.backgroundColor =  '#000000';
					obj.thirdmenuouttimer = setTimeout(function () {
							if(obj.thirdmenuNode.style.display != 'none')
							{
								new Effect.BlindUp(obj.thirdmenuNode, 
								{
									duration: thirdlevelduration,
									scaleX:	true,
									scaleY: true,
									beforeStart: function ()
									{
										obj.thirdmenuNode.style.zIndex= '0'; 
									},
									afterFinish: function ()
									{
										obj.thirdmenuNode.style.visibility = 'hidden';
										obj.thirdmenuNode.style.display = 'none'; 
									}
								});
							}
					}, timer);
				} 
				MainNav[y].thirdLvL[m].thirdparentItem.observe('mouseout', hidethirdsub.bindAsEventListener(this, MainNav[y].thirdLvL[m]));  
				
				
				function showthirdsubmenu(event, obj) 
				{      
					obj.thirdparentItem.style.backgroundColor =  '#a21411';
					clearTimeout(obj.thirdmenuouttimer);
					//clearTimeout(thirdobj.thirdmenuovertimer);
					//clearTimeout(thirdobj.thirdmenuovertimer2);
				} 
				MainNav[y].thirdLvL[m].thirdmenuNode.observe('mouseover', showthirdsubmenu.bindAsEventListener(this, MainNav[y].thirdLvL[m]));  
				
				
				function hidethirdsubmenu(event, obj) 
				{    
					
					obj.thirdmenuouttimer = setTimeout(function () {
							if(obj.thirdmenuNode.style.display != 'none')
							{
								new Effect.BlindUp(obj.thirdmenuNode, 
								{
									duration: thirdlevelduration,
									scaleX:	true,
									scaleY: true,
									beforeStart: function ()
									{
										obj.thirdmenuNode.style.zIndex= '0'; 
										obj.thirdparentItem.style.backgroundColor =  '#000000';
									},
									afterFinish: function ()
									{
										obj.thirdmenuNode.style.visibility = 'hidden';
										obj.thirdmenuNode.style.display = 'none'; 
									}
								});
							}
					}, timer);
				} 
				MainNav[y].thirdLvL[m].thirdmenuNode.observe('mouseout', hidethirdsubmenu.bindAsEventListener(this, MainNav[y].thirdLvL[m]));  

			}
		}	
	}
	
}

function setSubnavLeft(index)
{
	var mainnav_items = $$('#main_nav a', '#sidebar a'); 
	var leftoffset = null;
	var pageDim = $('body').getDimensions();
	if(pageDim.width < canvaswidth) { pageDim.width = canvaswidth; }
	var pageMiddle = pageDim.width/2;
	var canvasleft = pageMiddle + canvaswidth/2;
	
	var navwidths = new Array();
	
	for(var a=0; a < mainnav_items.length; a++)
	{	
		var temp = mainnav_items[a].getWidth();
		navwidths.push(temp);
	}
	navwidths.reverse() 
	var temp = 0;
	var rightoffset = 0;
	for(var a=0; a < (navwidths.length-index); a++)
	{	
		temp = temp + navwidths[a];
	if(a > (navwidths.length-wheretostartleftalign-1))
		{
			rightoffset = 0;
		}
		else
		{
			rightoffset = navwidths[a] - parseInt(subnavwidth) - 1;
		}
	}
	canvasleft = canvasleft - temp + rightoffset-1;
	return canvasleft;
}


function reposNav()
{
	//alert(MainNav.length);
	var mainnav_items = $('main_nav').getElementsByTagName('a');
	for(var i=0; i < mainnav_items.length; i++)
	{
		MainNav[i].submenu.style.left = setSubnavLeft(i) + 'px';
	}
	
	for(var i=0; i < MainNav.length; i++)
	{
		for(var r=0; r < MainNav[i].thirdLvL.length; r++)
		{
			if(i >= wheretostartleftalign)
			{
				MainNav[i].thirdLvL[r].thirdmenuNode.style.left = -MainNav[i].thirdLvL[r].thirdmenuleft -2 + 'px';
			}
			else
			{
				MainNav[i].thirdLvL[r].thirdmenuNode.style.left = MainNav[i].thirdLvL[r].thirdmenuleft + 'px';	
			}
			
			MainNav[i].thirdLvL[r].thirdmenuNode.style.top = MainNav[i].thirdLvL[r].thirdmenutop + 'px';
		}
	}
}

function initNavPos()
{
	$('subnav_wrapper').style.top = $('header_wrapper').getHeight() -1 + 'px';
	//Main Nav Items Array
	var mainnav_items = $('main_nav').getElementsByTagName('a');

	//Get 2ndLevel subnav and put node references in array
	for(var i=0; i < mainnav_items.length; i++)
	{		
		var mainsubnav = {
			parentItem : null,
			submenu : null,
			submenuleft : null,
			overtimer : null,
			outtimer : null,
			subnavtimer : null
		}
		
		//look for subnav wrapper
		var subnavID = mainnav_items[i].id + '_subnav';
		
		if($(subnavID))
		{
			$(subnavID).cleanWhitespace(); 
			mainsubnav.parentItem = mainnav_items[i];
			mainsubnav.submenu = $(subnavID);
			
			if($(subnavID).getElementsByTagName('div'))
			{
				var subnavitems = $(subnavID).childElements();
				var temp = new Array();
				var top = 0;
				for(var q=0; q<subnavitems.length; q++)
				{
					if(subnavitems[q].nextSibling)
					{
						if(subnavitems[q].nextSibling.nodeName == 'DIV')
						{
							subnavitems[q].id = 'subnav_third';
							subnavitems[q].onclick = function () {return false; }
							subnavitems[q].onclick  = function (e)
								{return false; }	
							if (subnavitems[q].captureEvents) subnavitems[q].captureEvents(Event.ONCLICK);
							
							var thirdsubnav = {
								thirdparentItem : subnavitems[q],
								thirdmenuNode : subnavitems[q].nextSibling,
								thirdmenuleft : parseInt(subnavwidth),
								thirdmenutop : top,
								thirdmenuovertimer : 'null2',
								thirdmenuouttimer : null,
								thirdmenuouttimer2 : null
							}
							temp.push(thirdsubnav);
						}
						if(subnavitems[q].nodeName == 'A')
						{
							top = top + subnavitems[q].getHeight();
						}
						
					}
				}
				mainsubnav.thirdLvL = temp;
			}
			
			MainNav.push(mainsubnav);
			
		}
	}
}
function initNav()
{	
	initNavPos();
	reposNav();
	initNavEvents();
}

function initselectcountry()
{
	if($('select_country'))
	{
		new Autocompleter.SelectBox('select_country', {submit : 'selectcountry_form'});	
	}
}
function initfilters()
{
	if($('filter'))
	{
	var filters = $('filter').getElementsByTagName('select');
	for(var i=0; i<filters.length; i++)
	{
		filters[i].onchange = function (e)
		{
			if (!e) var e = window.event;
			$('filter').submit();
		}
		if (filters[i].captureEvents) filters[i].captureEvents(Event.ONCHANGE);

	
	}
	}
}

// Copyright (c) 2006 - 2008 Gabriel Lanzani (http://www.glanzani.com.ar)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// SEE CHANGELOG FOR A COMPLETE CHANGES OVERVIEW
// VERSION 0.4

Autocompleter.SelectBox = Class.create();
Autocompleter.SelectBox.prototype = Object.extend(new Autocompleter.Base(), {
  initialize: function(select, options) {
	this.element = "<input type=\"text\" id=\"" + $(select).id + "_combo\" />"
	new Insertion.Before(select, this.element)
	var inputClasses = Element.classNames(select);
	
	this.update = "<div id=\"" + $(select).id + "_options\" class=\"" + inputClasses + "\"></div>"	
	new Insertion.Before(select, this.update)
			
    this.baseInitialize($(select).id + "_combo", $(select).id + "_options", options);
    this.select = select;
	this.selectOptions = [];
		
	$(this.element.id).setAttribute('readonly','readonly');
	this.element.readOnly = true;
	
	if(this.options.debug) var debugText = 'Debug input ' + this.element.id + ' and div ' + this.update.id + ' created, Autocompleter.Base() initialized\r\n';
	if(!this.options.debug)Element.hide(select);
	Element.addClassName(this.element.id, this.options.css) ;
	
	var optionList = $(this.select).getElementsByTagName('option');
	var nodes = $A(optionList);

	for(i=0; i<nodes.length;i++){
		this.selectOptions.push("<li id=\"" + nodes[i].value + "\">" + nodes[i].innerHTML + '</li>');
		if (nodes[i].getAttribute("selected")) this.element.value = nodes[i].innerHTML;
		
		if(this.options.debug) debugText += 'option ' + nodes[i].innerHTML + ' added to '+ this.update.id + "\r\n";
	}
	
	Event.observe(this.element, "click", this.activate.bindAsEventListener(this));
	
	if ($(select).selectedIndex >= 0)this.element.value = $(select).options[$(select).selectedIndex].innerHTML;
	
	var self = this;
	this.options.afterUpdateElement = function(text, li) {
		$('select_country_combo').blur();
		var optionList = $(select).getElementsByTagName('option');
		var nodes = $A(optionList);

		var opt = nodes.find( function(node){
			return (node.value == li.id);
		});
		$(select).selectedIndex=opt.index;

		if(self.options.redirect) document.location.href = opt.value;
		if(self.options.submit != '') $(self.options.submit).submit();
	}
	
	if(this.options.debug) alert(debugText);
  },

  getUpdatedChoices: function() {
  		this.updateChoices(this.setValues());
  },

  setValues : function(){

		return ("<ul>" + this.selectOptions.join('') + "</ul>");
  },
  
  setOptions: function(options) {
    this.options = Object.extend({
		//MORE OPTIONS TO EXTEND THIS CLASS
		submit		: false, //form Id to submit after change 
		redirect	: false, // redirects to option value
		debug		: false, //show alerts with information
		css			: 'combo'	 //css class for new element 
	}, options || {});
  }
})