var selectorHover = false;

var Site = {
	
	init: function()
	{	
		Site.initTransparency();
		Site.initHomeSelector();
		Site.initHomeAnimation();
		Site.initSelector();
		Site.initCollapse();
		Site.initHover();
		Site.initClick();
		Site.initSearchBarClick();
		Site.eventMapControl();
	},
		
	initTransparency: function()
	{
		$$('.transparent').setOpacity(.6);
	},
	
	initHomeSelector: function()
	{
		/* INIT VARS */
		if($('home-selector') && $('home-selector-content') && document.getElementById('home-selector') != null)
		{
			$("foryourhome-bg").effect('opacity').set(1);
			$("slide2-bg").effect('opacity').set(0);
			$("slide3-bg").effect('opacity').set(0);
			$("home-selector-content").getElement(".foryourhome").effect('opacity').set(1);
			$("home-selector-content").getElement(".slide2").effect('opacity').set(0);
			$("home-selector-content").getElement(".slide3").effect('opacity').set(0);
			$("home-selector-content").getElement(".slide4").effect('opacity').set(0);
			$("home-selector-content").getElement(".slide5").effect('opacity').set(0);
						
			/* INIT VARS */
			var selectedClass			= "foryourhome";

			var elSelector				= $('home-selector');
			var elContent				= $('home-selector-content');
			
			var elContentForyourHome	= $('home-selector-content').getElement('.foryourhome');
			var elContentSlide2		= $('home-selector-content').getElement('.slide2');
			var elContentSlide3		= $('home-selector-content').getElement('.slide3');
			var elContentSlide4		= $('home-selector-content').getElement('.slide4');
			var elContentSlide5		= $('home-selector-content').getElement('.slide5');
		
			/* INIT SELECTOR */		
			elSelector.addClass(selectedClass);
			
			//$("content").setStyle("width", "600px");
			//$("content").setStyle("padding-right", "0");

			/* ACTIONS CONTENT */
			$('home-selector-content').getElements('.content').each(function(el)
			{
				if(el.hasClass(selectedClass))
				{
					el.setStyle('display','block');
					$(selectedClass + '-bg').effect('opacity').set(1);
					$(selectedClass + '-bg').setStyle('display','block');
				}
				else
				{
					//$(selectedClass + '-bg').effect('opacity').set(0);
					$(selectedClass + '-bg').setStyle('display','block');
				}
			});

			/* ADD EVENTS */
			$('home-selector').getElements('.button').each(function(el)
			{
				el.addEvent('mouseover', function(e){Site.eventHomeSelectorClick(el)});
				el.addEvent('mouseover', function(e){Site.eventHomeSelectorOver()});
				el.addEvent('mouseout', function(e){Site.eventHomeSelectorOut()});
				el.addEvent('click', function(e){Site.eventHomeSelectorClick(el)});
			});
			
			$('home-selector-content').addEvent('mouseover', function(e){Site.eventHomeSelectorOver()});
			$('home-selector-content').addEvent('mouseout', function(e){Site.eventHomeSelectorOut()});
		}
	},
	
	initHomeAnimation: function()
	{
		if($('home-selector') && document.getElementById('home-selector') != null)
		{						
			var fxHomeSelector = $("home-selector").effects({duration: 1000, transition: Fx.Transitions.Quart.easeOut});
			
			fxHomeSelector.start
			({
				'opacity': 1
			}).chain(function()
			{				
				this.start({'opacity': 1});
			}).chain(function()
			{
				if(selectorHover == false)
				{
					if($('home-selector').getProperty('class'))
					{
						var temp_selected = $('home-selector').getProperty('class');
					}
					else
					{
						var temp_selected = "foryourhome";
					}

					if(temp_selected == "foryourhome")
					{ var temp_next = "slide2"; }
					else if(temp_selected == "slide2")
					{ var temp_next = "slide3";	}
					else if(temp_selected == "slide3")
					{ var temp_next = "slide4";	}
					else if(temp_selected == "slide4")
					{ var temp_next = "slide5"; }
					else if(temp_selected == "slide5")
					{ var temp_next = "foryourhome"; }

					(function(){
						$(temp_selected + "-bg").effect('opacity').start(1, 0);
						$("home-selector-content").getElement("." + temp_selected).effect('opacity').start(1, 0);
						$("home-selector").removeClass(temp_selected);
					}).delay(0);


					(function(){
						$(temp_next + "-bg").effect('opacity').start(0, 1);
						$("home-selector-content").getElement("." + temp_next).effect('opacity').start(0, 1);
						$("home-selector").addClass(temp_next);
					}).delay(100);
				}
				
				this.start({'opacity': 1});
				
			}).chain(function()
			{
				Site.initHomeAnimation();
			});
		}
		
	},
	
	initSelector: function()
	{
		$$(".selector").each( function(el)
		{
			/* INIT VARS */
			var temp_el = el;

			/* INIT SELECTOR */	
			var temp_first = temp_el.getElement("div").getProperty("id");
			temp_el.addClass(temp_first.replace("-button",""));
			
			$$('.selector-content').each(function(el)
			{
				if(el.getProperty("id") == temp_first.replace("-button","-content"))
				{
					el.setStyle("display","block");
				}
				else
				{
					el.setStyle("display","none");
				}
			});
			
			/* ADD EVENTS */
			temp_el.getElements('.button').each(function(subel)
			{
				subel.addEvent('mouseover', function(e){Site.eventSelectorClick(subel, el)});
				subel.addEvent('click', function(e){Site.eventSelectorClick(subel, el)});
			});
		});
	},
	
	initCollapse: function()
	{
		$$("h2.collapse").each( function(el)
		{
			/* INIT VARS */
			var temp_el			= el;
			var temp_link		= temp_el.getElement("a");
			var temp_content	= el.getNext();
			var temp_height		= temp_content.getSize().size.y;
			
			/* INIT ACTIONS */
			if( temp_link.hasClass("active") )
			{
				
			}
			else
			{
				temp_content.effect('height').set(0);
				temp_content.effect('margin-bottom').set(0);
			}
			
			/* ADD EVENTS */
			if(temp_el.hasClass("unique"))
			{
				temp_el.addEvent('click', function(e){Site.eventCollapseUniqueClick(temp_link, temp_content, temp_height)});
				temp_el.addEvent('mouseover', function(e){Site.eventCollapseUniqueMouseover(temp_link, temp_content)});
				temp_el.addEvent('mouseout', function(e){Site.eventCollapseUniqueMouseout(temp_link, temp_content)});
			}
			else if(temp_el.hasClass("product"))
			{
				temp_el.addEvent('click', function(e){Site.eventCollapseProductClick(temp_link, temp_content, temp_height)});
			}
			else
			{
				temp_el.addEvent('click', function(e){Site.eventCollapseClick(temp_link, temp_content, temp_height)});
			}
		});
	},
	
	initHover: function()
	{
		$$('.hover').each( function(el)
		{
			var temp_id = el.getProperty("id");
			
			el.setOpacity(1);
			
			if($(temp_id + "-content"))
			{
				$(temp_id + "-content").setStyle("display", "none");
			}
			
			el.addEvent('mouseover', function(e)
			{
				el.setOpacity(1);
				
				if($(temp_id + "-content"))
				{
					$(temp_id + "-content").setStyle("display", "block");
				}
			})
			
			el.addEvent('mouseout', function(e)
			{
				el.setOpacity(.6);
				if($(temp_id + "-content"))
				{
					$(temp_id + "-content").setStyle("display", "none");
				}
			})
		});
	},
	
	initClick: function()
	{
		$$('.click').each( function(el)
		{
			var temp_id 	= el.getProperty("id");
			var do_wait 	= false;
			
			if($(temp_id + "-content"))
			{
				var temp_height	= $(temp_id + "-content").getSize().size.y;
				$(temp_id + "-content").effect('height').set(0);
				$(temp_id + "-content").setStyle("overflow", "hidden");	
			}
			
			el.addEvent('click', function(e)
			{
				do_wait 	= false;
				
				if(el.hasClass("active"))
				{
					if($(temp_id + "-content"))
					{
						el.removeClass("active");
						$(temp_id + "-content").effect('height').start(temp_height, 0);
					}
				}
				else
				{					
					$$('.click').each( function(el2)
					{
						var temp_id2 = el2.getProperty("id");

						if( (temp_id != temp_id2) && ( el.getParent() == el2.getParent() ) )
						{
							el2.removeClass("active");
							
							if($(temp_id2 + "-content"))
							{
								var temp_height2 = $(temp_id2 + "-content").getSize().size.y;
								
								if( 0 < temp_height2)
								{
									$(temp_id2 + "-content").effect('height').start(temp_height2, 0);
									do_wait = true;
								}
							}
						}
					});

					el.addClass("active");
					
					if($(temp_id + "-content"))
					{
						if(do_wait)
						{
							(function(){ $(temp_id + "-content").effect('height').start(0, temp_height); }).delay(800);
						}
						else
						{
							$(temp_id + "-content").effect('height').start(0, temp_height);
						}
					}
				}
				
			})
		});
	},
	
	initSearchBarClick: function()
	{
		if ($('search-site'))
		{
			$$('#nav-site .search').addEvent('click', function(e)
			{
				var currentStatus = $('search-site').getStyle('display');
			
				if (currentStatus == 'none')
				{
					$$('#container-content, #nav-content, #logo').setStyle('margin-top', '20px');
					$$('body').setStyle('background-image', 'url(/images/nav-site-bg-large.jpg)');
					$('search-site').setStyle('display', 'block');
				}
				else
				{
					$$('#container-content, #nav-content, #logo').setStyle('margin-top', '0px');
					$$('body').setStyle('background-image', 'url(/images/nav-site-bg.jpg)');
					$('search-site').setStyle('display', 'none');
				}			
			});
		
			$$('.search-field').addEvent('click', function(e)
			{
				this.value = "";
			});
		}
	},
	
	
	eventHomeSelectorClick: function(el)
	{
		/* INIT VARS */
		var temp_item 				= el.getProperty("id").replace("-button","");
		var elSelector				= $('home-selector');
		var elContent				= $('home-selector-content');
		
		selectorHover				= true;
		
		/* ACTIONS SELECTOR */
		elSelector.removeClass(elSelector.getProperty("class"));
		elSelector.addClass(temp_item);
		
		/* ACTIONS CONTENT */
		$('home-selector-content').getElements('.content').each(function(el)
		{
			if(el.hasClass(temp_item))
			{
				el.effect('opacity').set(1);
			}
			else
			{
				el.effect('opacity').set(0);
			}
		});
		
		$('home-selector').getElements('img').each(function(el)
		{
			if(el.hasClass(temp_item))
			{
				el.effect('opacity').set(1);
			}
			else
			{
				el.effect('opacity').set(0);
			}
		});
	},
	
	eventHomeSelectorOver: function()
	{
		selectorHover = true;
	},
	
	eventHomeSelectorOut: function()
	{
		selectorHover = false;
	},
	
	eventSelectorClick: function(subel, el)
	{
		/* INIT VARS */
		var temp_item 				= subel.getProperty("id").replace("-button","");
		var elSelector				= el;
		//var elContent				= $('home-selector-content');
		
		/* ACTIONS SELECTOR */
		elSelector.removeClass(elSelector.getProperty("class"));
		elSelector.addClass("selector");
		elSelector.addClass(temp_item);
		
		/* ACTIONS CONTENT */
		$$('.selector-content').each(function(el)
		{
			if(el.getProperty("id") == (temp_item + "-content"))
			{
				el.setStyle("display","block");
			}
			else
			{
				el.setStyle("display","none");
			}
		});
	},
	
	eventCollapseClick: function(temp_link, temp_content, temp_height)
	{		
		/* INIT ACTIONS */
		if( temp_link.hasClass("active") )
		{					
			temp_content.effect('height').start(temp_height + 5, 0);
			temp_content.effect('margin-bottom').start(16, 0);
			
			temp_link.removeClass("active");
		}
		else
		{	
			temp_content.effect('height').start(0, temp_height + 5);
			temp_content.effect('margin-bottom').start(0, 16);

			temp_link.addClass("active");
		}
		return false;
	},
	
	eventCollapseProductClick: function(temp_link, temp_content, temp_height)
	{
		console.log("bah");
		var temp_id = temp_link.getParent().getProperty("id");
						
		/* INIT ACTIONS */
		if( temp_link.hasClass("active") )
		{
			temp_content.effect('height').start(temp_height, 0);
			temp_content.effect('margin-bottom').start(16, 0);
			temp_link.removeClass("active");
		}
		else
		{			
			$$("h2.product").each( function(el)
			{
				/* INIT VARS */
				var temp_el			= el;
				var temp_link		= temp_el.getElement("a");
				var temp_content2	= el.getNext();
				var temp_height		= temp_content2.getSize().size.y;
				
				console.log(temp_height);

				/* INIT ACTIONS */
				//temp_content2.effect('height').start(temp_height, 0);
				//temp_content2.effect('margin-bottom').start(16, 0);
				//temp_link.removeClass("active");
			});
			
			temp_content.effect('height').start(0, temp_height);
			temp_content.effect('margin-bottom').start(0, 16);
			temp_link.addClass("active");
			


			$('product-holder').setProperty("src", "/binaries/" + temp_id + ".jpg");
		}
		return false;
	},
	
	eventCollapseUniqueClick: function(temp_link, temp_content, temp_height)
	{
		var temp_id = temp_link.getParent().getProperty("id");
						
		/* INIT ACTIONS */
		if( temp_link.hasClass("active") )
		{
			temp_content.effect('height').start(temp_height, 0);
			temp_content.effect('margin-bottom').start(16, 0);
			temp_link.removeClass("active");
			
			$('unique-holder').removeClass($('unique-holder').getProperty("class"));
			$('unique-holder').addClass("none");
			$('unique-holder').setProperty("src", "/images/spacer.gif");
			$('unique-holder').setStyle("display", "none");
		}
		else
		{
			$('unique-holder').removeClass($('unique-holder').getProperty("class"));
			$('unique-holder').addClass(temp_id);
			$('unique-holder').removeClass("none");
			
			$$("h2.unique").each( function(el)
			{
				/* INIT VARS */
				var temp_el			= el;
				var temp_link		= temp_el.getElement("a");
				var temp_content2	= el.getNext();
				var temp_height2	= temp_content2.getSize().size.y;

				/* INIT ACTIONS */
				if(temp_height2 > 0)
				{
					temp_content2.effect('height').start(temp_height2, 0);
					temp_content2.effect('margin-bottom').start(16, 0);
				}

				temp_link.removeClass("active");
			});
			
			temp_content.effect('height').start(0, temp_height);
			temp_content.effect('margin-bottom').start(0, 16);
			
			temp_link.addClass("active");

			
			$('unique-holder').setProperty("src", "/binaries/" + temp_id + ".jpg");
			$('unique-holder').setStyle("display", "block");
		}
		return false;
	},
	
	eventCollapseUniqueMouseover: function(temp_link, temp_content)
	{
		var temp_id = temp_link.getParent().getProperty("id");

		
		$('unique-holder').setProperty("src", "/binaries/" + temp_id + ".jpg");
		$('unique-holder').setStyle("display", "block");
		//$('unique-bg').setOpacity(0.3);
		return false;
	},
	
	eventCollapseUniqueMouseout: function(temp_link, temp_content)
	{
		var temp_id = temp_link.getParent().getProperty("id");
		
		if($('unique-holder').hasClass("none"))
		{
			$('unique-holder').setProperty("src", "/images/spacer.gif");
			$('unique-holder').setStyle("display", "none");
			//$('unique-bg').setOpacity(01);
		}
		else
		{
			$('unique-holder').setProperty("src", "/binaries/" + $('unique-holder').getProperty("class") + ".jpg");
			$('unique-holder').setStyle("display", "block");
		}		
		return false;
	},
	
	eventMapControl: function()
	{
		$$('.con').each(function (el){
			var name = el.getAttribute('id');
			var pos = name.indexOf("-");
			name.slice(pos);
			
			el.addEvent('mouseenter', function()
			{
				var name = el.getAttribute('id');
				var pos = name.indexOf("-");
				name = name.substring(0,pos);
				$(name + "-img").setStyle('display', 'block');
				$(name + "-l").setStyle('color', '#666');
				$(name + "-l").setStyle('backgroundImage', 'url(/images/link-big-gray.gif)');
			});	
			el.addEvent('mouseleave', function()
			{	
				var name = el.getAttribute('id');
				var pos = name.indexOf("-");
				name = name.substring(0,pos);
				$(name + "-img").setStyle('display', 'none');
				$(name + "-l").setStyle('color', '#0099cc');
				$(name + "-l").setStyle('backgroundImage', 'url(/images/link-big-blue.gif)');
			});
		});
		
	},
	
	showContinent: function(continent)
	{
		
	},
	
	hideContinent: function(continent)
	{
		$(continent).setStyle('display', 'none');
	}

}
window.addEvent('domready', Site.init);

function MM_openBrWindowExtern() { //v2.0
	var theURL, theServer, thePath, theParams, theSplit;
	theURL = encodeURI(location.href);
	theSplit = location.href.split("?");
	theParams = encodeURI(theSplit[1]);
	theServer = location.hostname;
	thePath = location.pathname
	theURL = encodeURI('http://' + theServer + thePath + '?' + theParams)
	window.open('/print.jsp?url='+ theURL ,'' ,'toolbar=yes,location=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=520,height=440');
}

function MM_openBrWindowProduct(theURL) { //v2.0
	var regex, theServer, thePath, theParams, theSplit;
	theSplit = theURL.split("?");
	theParams = encodeURI(theSplit[1]);
	regex = /&/g;
	theParams = theParams.replace(regex, "%26")
	thePath = theSplit[0];
	theURL = encodeURI('/print.jsp?url=' + thePath + '?') + theParams
	window.open(theURL, '','toolbar=yes,location=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=520,height=440');
}