var drambuie = 
{
	//*------------------------------------------------------------------//
	ieZIndexFix: function()
	{
		if(Browser.Engine.trident)
		{
			var relativelyPositionedElements = $$('.sections li');
			var zIndex = 1000;
			relativelyPositionedElements.each
			(
				function(el)
				{
					el.setStyle('z-index', zIndex--);
				}
			)
		}
  	},


	//*------------------------------------------------------------------//
	prepareDocument: function()
	{
		$$('.secondary-navigation').addClass('disabler');
	},


	//*------------------------------------------------------------------//
	carousels: function()
	{
		$$('.carousel').each
		(
			function(el)
			{
				el.setStyle('overflow', 'hidden');
				var myScroll = new Fx.Scroll(el, {duration: 450, transition: Fx.Transitions.Quart.easeOut});
				var scrollWidth = parseInt(el.getElement('li').getStyle('width'));
				
				

				if(el.getElement('.current'))
				{
					myScroll.set(el.getElement('.current').getPosition(el).x, 0);
				}

				var next = new Element
				(
					'a',
					{
    						'class': 'replaced control next',
    						'title': 'View the next item in the carousel.'
					}
    				);

    				var prev = new Element
				(
					'a',
					{
    						'class': 'replaced control prev',
    						'title': 'View the previous item in the carousel.'
					}
    				);

    				next.inject(el.getParent(), 'top');
				prev.inject(el.getParent(), 'top');

				var scrollPos;
				var buttons = function(scroll)
				{
					scroll ? scrollPos = scroll : scrollPos = el.getScroll().x;
					if(scrollPos <= 0)
					{
						prev.addClass('prev-inactive');
					}
					else
					{
						prev.removeClass('prev-inactive');
					}
					
					if (scrollPos >= el.getElement('.carousel-content-wrapper').getSize().x  - ((el.getSize().x / scrollWidth)  * scrollWidth))
					{
						next.addClass('next-inactive');
					}
					else
					{
						next.removeClass('next-inactive');
					}
				}
				buttons();

				next.addEvent
				(
					'click',
					function(e)
					{
						e.preventDefault();
						var currentScroll = el.getScroll().x;
						myScroll.start(currentScroll + scrollWidth);
						buttons(currentScroll + scrollWidth);
					}
				);

				prev.addEvent
				(
					'click',
					function(e)
					{
						e.preventDefault();
						var currentScroll = el.getScroll().x;
						myScroll.start(currentScroll - scrollWidth);
						buttons(currentScroll - scrollWidth - scrollWidth);
					}
				)
			}
		)
	},


	//*------------------------------------------------------------------//
	linkTransform: function()
	{
		$$('.link-transform li').each
		(
			function(el)
			{
				el.getElement('a').setStyle('visibility', 'hidden');
				el
				.addEvent
				(
					'mouseenter',
					function()
					{
						this.addClass('link-transformation');
					}
				)
				.addEvent
				(
					'mouseleave',
					function()
					{
						this.removeClass('link-transformation');
					}
				)
				el.addEvent
				(
					'click',
					function()
					{
						window.location.href=this.getElement('a');
					}
				)

				el.getElement('a').addEvent
				(
					'focus',
					function()
					{
						el.addClass('link-transformation');
					}
				)
				.addEvent
				(
					'blur',
					function()
					{
						el.removeClass('link-transformation');
					}
				);
			}
		);
	},


	//*------------------------------------------------------------------//
	inputClear: function()
	{
		$$('.dob input').each
		(
			function(el)
			{
				var value = el.getProperty('value');
				el
				.addEvent
				(
					'focus',
					function()
					{
						this.value="";
					}
				)
				.addEvent
				(
					'blur',
					function()
					{
						if(this.value=="")
						{
							this.value=value;
						}
					}
				)
			}
		)
	},


	//*------------------------------------------------------------------//
	secondaryContent: function()
	{
		if($('secondary-content'))
		{
			$$('.preloader').removeClass('hidden').fade('hide');
			var myVerticalSlide = new Fx.Slide( ('ajax-placeholder'), {duration: 500, transition: Fx.Transitions.Quad.easeOut});
			var links = $$('a.window');
			if(!($('secondary-content').hasClass("open")))
			{
				myVerticalSlide.slideOut();
			}
			$$('a.close').removeClass('hidden');
			links.each
			(
				function(link)
				{
					link.addEvent
					(
						'click',
						function(e)
						{
							links.removeClass('current');
							link.addClass('current');
							$$('.preloader').fade('in');
							e.preventDefault();
							myVerticalSlide.slideOut();
							var req = new Request.HTML
							(
								{
									url: this.href,
									onSuccess: function(html)
									{
										var delayFunc = function()
										{
											$('ajax-placeholder').getElement('.embed-wrapper').empty();
											$('ajax-placeholder').getElement('.embed-wrapper').adopt(html);
											$$('a.close').setStyle('width', $('ajax-placeholder').getElement('.embed').getStyle('width'));
											myVerticalSlide.slideIn();
											$$('.preloader').fade('out');
										}
										delayFunc.delay(1000);
									},
									onFailure: function()
									{
										$('ajax-placeholder').innerHTML="The request failed.";
										alert("failed");
									}
								}
							);
							req.send();
						}
					);
				}
			);

			$$('a.close').addEvent
			(
				'click',
				function(e)
				{
					links.removeClass('current');
					e.preventDefault();
					myVerticalSlide.slideOut();
				}
			)
		}
	},


	//*------------------------------------------------------------------//
	panelPages: function()
	{
		var panels = $('panels').getElement('.panel-wrapper');
		var myFx = new Fx.Scroll(panels, {duration: 400, transition: Fx.Transitions.Quad.easeOut, wheelStops : false});
		$$('.tertiary-navigation a')[0].addClass('current');

		$$('.tertiary-navigation a').each
		(
			function(el)
			{
				el.addEvent
				(
					'click',
					function(e)
					{
						$$('.tertiary-navigation a').removeClass('current');
						el.addClass('current');
						e.preventDefault();

						var base = this.getProperty('href');
						var element = $(base.substr(base.lastIndexOf("#"), base.length).replace(/#/,""));

						myFx.start(element.getPosition($('panels').getElement('.panel-sub-wrapper')).x);
					}
				)
			}
		);
	},


	//*------------------------------------------------------------------//
	form:function()
	{
		if($('email-panel'))
		{
 			$('email-panel').getElement('form').addEvent
			(
				'submit',
				function(e)
				{
					e.preventDefault();
					$('email-panel').addClass('ajax-load');
					this.getChildren().fade('out');
					this.set
					(
						'send',
						{
							onComplete: function(response)
							{
								var myFunc = function()
								{
									$('email-panel').removeClass('ajax-load');
									$('email-panel').getElement('form').set('html', response)
								}.delay(1000);
							}
						}
					);
					this.send()
				}
			)
		}
	},


	//*------------------------------------------------------------------//
	setBackgroundSize: function()
	{
		if($(document.body).hasClass('dynamic'))
		{

			var windowDimensions = window.getSize();		//Used for opera support
			var scrollDimensions = window.getScrollSize();		//Used for xbrowser
		
			//REMOVE FALLBACK BACKGROUND
			$(document.body).setStyle('background-image', 'none');

			//CREATE BACKGROUND CONTAINER
			var backgroundContainer = new Element
			(
				'div',
				{
    					'id'   : 'parralax',
    					'class': 'background-container'
				}
    			);
    			backgroundContainer.inject($(document.body), 'top');

			//CREATE GRAPHIC
			var path ="/site-media/img/interface/backgrounds/{page}/{page}.jpg";
			var page = {page: $(document.body).getProperty('id')}
			var img = new Element
			(
				'img',
				{
    					'src'  : path.substitute(page),
    					'alt'  : ''
				}
    			);
          img.setStyle('position', 'fixed');
    			img.inject(backgroundContainer, 'top');

			//SET WIDTHS
			if (windowDimensions.x > 1010)
			{
				backgroundContainer.setStyle('width', windowDimensions.x);
				img.setStyle('width', windowDimensions.x + 25);
			}
			else
			{
				backgroundContainer.setStyle('width', 1010);
				img.setStyle('width', 1010 + 25);
			}

			//SET HEIGHTS
			var aspect = (1049 / 693).round(1);
			var height = (parseInt(img.getStyle('width')) / aspect).round();
			img.setStyle('height', height);
			backgroundContainer.setStyle('height', height);
			backgroundContainer.appendText(".");
		}
	},


	//*------------------------------------------------------------------//
	navigationPin: function()
	{
		if($('navigation') && $('primary-content').getElement('.module'))
		{
			//GET ELEMENTS
			var backgroundContainer       = $$('.background-container');
			var header                    = $('header');
			var primaryContent            = $('primary-content');
			var navigation                = $('navigation').getElement('.sub-wrapper');
			var module                    = $('primary-content').getElement('.module');

			//RESET ELEMENTS
			navigation.setStyle('top', 'auto');
			module.setStyle('top', 'auto');	

			//GET DIMENSIONS
			var windowHeight              = window.getSize().y;
			var backgroundContainerHeight = parseInt($$('.background-container').getStyle('height'));
			var headerHeight              = header.getSize().y;
			var primaryContentHeight      = primaryContent.getSize().y;
			var navigationHeight          = navigation.getSize().y;
			var moduleHeight              = module.getSize().y;

			var scrollBarHeight = 0
			if(Browser.Engine.webkit || Browser.Engine.presto)
			{
				scrollBarHeight = 20;
			}

			var pin = function()
			{
				if (windowHeight > backgroundContainerHeight)
				{
					navigation.setStyle('top', backgroundContainerHeight - navigationHeight - headerHeight - scrollBarHeight);
				}
				else
				{
					navigation.setStyle('top', windowHeight - navigationHeight - headerHeight - scrollBarHeight);
				}
				module.setStyle('top', parseInt(navigation.getStyle('top'))/2 - (moduleHeight/2) - parseInt(module.getStyle('margin-top')));
			}

			var determine = function()
			{
				if(windowHeight > (headerHeight + primaryContentHeight + navigationHeight))
				{
					pin();
				}
			}
			determine();
		}
	},


	//*------------------------------------------------------------------//
	parallax: function()
	{
		if($('parralax'))
		{
			new mParallax($('parralax'));
		}
	},


	//*------------------------------------------------------------------//
	linkTooltips: function()
	{
		var tipLinks = $$('a[title], label[title], input[title]');
		var tooltips = new Tips(tipLinks,{className:'tooltip-wrapper', offset:{'x':-98, 'y': 14}});
	},



	//*------------------------------------------------------------------//
	popupLinks: function()
	{
		var anchors = $$('a[rel="pop-up"]');
		anchors.each
		(
			function(anchor)
			{
				anchor.addEvent
				(
					'click',
					function(e)
					{
						var left = (screen.width/2  - 300);
						var top  = (screen.height/2 - 200);
						e.preventDefault();
						window.open(anchor.href, "Drambuie", 'width=600,height=400,scrollbars=1,location=0,toolbar=0,resizable=0,top='+top+',left=' +left+"'");
					}
				)
			}
		)
	},


	//*------------------------------------------------------------------//
	externalLinks: function()
	{
		var anchors = $$('a[rel="external"]');
		anchors.each
		(
			function(anchor)
			{
				anchor.addEvent
				(
					'click',
					function(e)
					{
						e.preventDefault();
						window.open(anchor.href);
					}
				)
			}
		)
	},
	killBlog: function()
	{

		$('blog_heading').getElements('span').addEvent('click', function(event) {
			$('blog_window').setStyle('display', 'none');
		});

	},


	//*------------------------------------------------------------------//
	videoPlayer: function()
	{
		if($('video-player'))
		{
			var player = $('video-player');
			player.inject($('wrapper'), 'top');
			player.addClass('active');
			var windowDimensions = window.getSize();
			var videoWidth;
			windowDimensions.x < 1010 ? videoWidth = 1010 : videoWidth = windowDimensions.x;
			player.setStyle('height', windowDimensions.y - 57);
			player.setStyle('width', videoWidth);		
			$('navigation').getElement('.sub-wrapper').setStyle('top', windowDimensions.y - 57);
		}

	},

	//*------------------------------------------------------------------//
	clearMask: function()
	{
		if($('mask'))
		{
			var maskFx = new Fx.Tween($('mask'), {duration: 1500, transition: Fx.Transitions.Quad.easeOut});
			maskFx.start('opacity', 0);
			(
				function()
				{
					$('mask').destroy();
				}
			).delay(1500)
		}
	},


	//*------------------------------------------------------------------//
	init: function()
	{
		this.ieZIndexFix();
		this.prepareDocument();
		this.carousels();
		this.linkTransform();
		this.inputClear();
		this.secondaryContent();
		this.setBackgroundSize();
		this.navigationPin();
		//this.parallax();
		this.linkTooltips();
		this.popupLinks();
		this.externalLinks();
		this.videoPlayer();
		
		this.clearMask();
	}
}


/*------------------------------------------------------------------
REGISTER MAIN EVENTS
------------------------------------------------------------------*/
window
.addEvent
(
	'domready',
	function()
	{
		drambuie.init();
	}
)
.addEvent
(
	'resize',
	function()
	{
		$$('.background-container').dispose();
		drambuie.setBackgroundSize();
		var profile;
		Browser.Engine.trident ? profile = "ie": profile = "w3c";
		if(profile == "ie")
		{
			drambuie.setBackgroundSize();
		}
		if(profile == "w3c")
		{
			//drambuie.parallax();
		}
		drambuie.navigationPin();
		
		drambuie.videoPlayer();
	}
);