/*

	IHI
	JavaScript functions
	
	Author: Creuna Danmark A/S / www.creuna.dk
	Copyright: 2008, Creuna Danmark A/S. All rights resevered

-----------------------------------------------------------------------*/

var Ihi = {
	
	options:
	{
		browsers:
		{
			ie6: Browser.Engine.trident4
		}
	},
	
	domready: function()
	{
		if($$('div.box').length > 0)
			Ihi.Box.initialize();
		
		if($$('dl.faq').length > 0)
			Ihi.Faq.initialize();
	},
	
	Element:
	{
		/**
		 * Morphing one Element to match the CSS values
		 *
		 * @param	string	element		A string ID of the Element or an
										Element to apply the style transitions to
		 * @param	number	duration	Duration of effect in milliseconds
		 * @param	array	styles		CSS values
		 */
		morph: function(element, duration, styles)
		{
			var morph = new Fx.Morph(element,
			{
				duration: duration,
				transition: Fx.Transitions.Quad.easeInOut,
				wait: false
			});
			
			morph.start(styles);
		},
		
		/**
		 * Scroll window to any element
		 *
		 * @param	string	element		A string ID of the Element or an Element
		 * var		array	position	Returns position in {x, y}
		 */
		scrollTo: function(element)
		{
			var position = $(element).getPosition();
			var scroll = new Fx.Scroll(window, {
				duration: 650
			});
			
			scroll.start(0, position.y - 100);
		}
	},
	
	Faq:
	{
		initialize: function()
		{
			$$('dl.faq').each(function(e)
			{
				Ihi.Element.morph(e, 1, { backgroundColor: '#ffffff' });
			});
		},
		
		/**
		 * Changes background color and slides to element
		 *
		 * @param	string	element		A string ID of the Element or an 
										Element to apply the style transitions to
		 */
		changeFocus: function(element)
		{
			var nextFaqElement = $(element).getNext('dl.faq');
			
			// Scroll to anchor element
			Ihi.Element.scrollTo(element);
			
			// Wait for scroll
			(function(){
				Ihi.Element.morph(nextFaqElement, 400, { backgroundColor: '#f0f4f7' });
			}).delay(700);
			
			if($('faq'))
			{
				$$('dl.faq').each(function(e)
				{
					if(nextFaqElement != e)
					{
						Ihi.Element.morph(e, 600, { backgroundColor: '#ffffff' });
					}	
				});
			}
		},
		
		toTop: function()
		{
			if($('faq'))
			{
				// Scrolls window to body top
				Ihi.Element.scrollTo(document.body);
				
				$$('dl.faq').each(function(e)
				{
					Ihi.Element.morph(e, 600, { backgroundColor: '#ffffff' });
				});
			}
		}
	},
	
	Box:
	{
		defaultColor: '#2c485c',
		hoverColor: '#426b89',
		
		initialize: function()
		{
			var boxes = $$('#main div.box');
			
			if(boxes)
				Ihi.Box.create(boxes);
		},
		
		/**
		 * Create events for sliding content up/down
		 *
		 * @param	array	elements	Box elements from $$
		 */
		create: function(elements)
		{
			elements.each(function(element, i)
			{
				if(element.getProperty('class').test("boxSmall") == false)
				{
					var wrapper = element.getElement('div.l');
					var toggler = wrapper.getElement('dl dt');
					
					if(wrapper.getElement('dl dd'))
					{
						var content = wrapper.getElement('dl dd');
					}
					
					if(typeof content != "undefined" && Ihi.options.browsers.ie6 != true)
					{
						// Adopt box to get default height and delete it again

						var copy = element.clone().set({'id': 'box'});
						if(copy.getElement('div.l dl dd'))
						{
						    copy.getElement('div.l dl dd').setStyle('display', 'block');
						}
						
						element.getParent().adopt(copy);
						
						var defaultHeight;
                        defaultHeight = $('box').getElement('dd').offsetHeight;
						// Dispose box
						$('box').dispose();
						
						content.setStyles({
							height: 0,
							display: 'block'
						});
					}
			        
					// Make box clickable
					var link = toggler.getElement('a');
					if(link)
					{
						wrapper.addClass('href');
						wrapper.addEvent('click', function()
						{
							window.location = link.getProperty('href');
						});
					}
			        
					var morph = new Fx.Morph(content,
					{
						duration: 500,
						transition: Fx.Transitions.Quad.easeOut,
						wait: false
					});
			        
					// Add sliding morph events
					wrapper.addEvents(
					{
						mouseover: function()
						{
                            
							if(typeof content != "undefined" && Ihi.options.browsers.ie6 != true)
							{
								morph.start({
									'height': defaultHeight
								});
							}
							
							if(link)
								link.getElement('span').setStyle('color', Ihi.Box.hoverColor);
						},
						mouseout: function()
						{
							if(typeof content != "undefined" && Ihi.options.browsers.ie6 != true)
							{
								morph.start({
									'height': 0
								});
							}
							
							if(link)
								link.getElement('span').setStyle('color', Ihi.Box.defaultColor);
						}
					});
				}
			});
		}
	},
	
	Search:
	{
		addDefaultText: function(el, text)
		{
			if(el.getStyle('width') == '200px')
			{
				if(el.value == '')
				{
					el.setStyle('color', '#EFF4F7');
					el.value = text;
				}
				
				Ihi.Element.morph(el, 300, {
					width: '144px',
					color: '#555'
				});
			}
		},
		
		removeDefaultText: function(el, text)
		{
			if(el.value == text)
				el.value = '';
			
			Ihi.Element.morph(el, 300, {
				width: '200px',
				color: '#000'
			});
		},
		
		doSearch: function(firstLevelKey, url, language, defaultValue, searchControl)
		{
			var strParam = "";
			
			if (defaultValue != $(searchControl).value)
			{
				strParam += "?query=" + encodeURIComponent($(searchControl).value);
				self.location = self.location.protocol+'//'+self.location.host+'/'+firstLevelKey+url+strParam;
			}
		}
	}
};

window.addEvent('domready', Ihi.domready);
