/* IE Image flicker fix */
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
/* End */
 
$(document).ready(function(){
	// Open external link in a new window
	$('a[rel="external"]').click(function(clickEvent){
		clickEvent.preventDefault();
		window.open(this.href);		
	});
	
	// Display sample in forms
	$('form').each(function() {	
		className = 'hasExampleValue';
		
		$(this).find('input[type="text"][title],textarea[title]').each(function(){
			$(this).focus(function(eventFocus){
				if($(this).hasClass(className)) {					
					$(this).val('');
					$(this).removeClass(className);
				}
			});
				
			$(this).blur(function(eventBlur){				
				if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			});
			
			$(this).change(function(eventChange){
				if ($(this).val() != '' && $(this).val() != $(this).attr('title')) {
					$(this).removeClass(className);
				}
			});
					
			if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				if(!$(this).hasClass(className)) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			}
		});
		
		$(this).submit(function(eventSubmit){	
			$(this).find('.' + className).each(function(){
				$(this).val('');
			});
		});	
		
		// "a" html entity to trigger form submission
		$(this).find('div.buttons a:not([rel="external"])').click(function(e){		
			e.preventDefault();
			
			// Set the 'rel' of the link to the hidden 'input[action]'
			$(this).parents('form').find('input[name="action"]').attr('value', $(this).attr('rel'));
			
			// Submit the parent form
			$(this).parents('form').trigger('submit');
		});
	});
	
	// Animations
	if ($.support.opacity) {
		// Main menu animation
		$('#link-list-container-main li.lvl1 > a[class!="highlight"]').each(function(){
			$(this).mouseenter(function(){
				$(this).css('opacity', 0);
				$(this).fadeTo(300, 1.0);
			});
		});
		
		// Package menu animation
		$('#link-list-package.dynamic a[class!="highlight"]').each(function(){
			$(this).mouseenter(function(){
				$(this).fadeTo(200, 1.0);
			});
			$(this).mouseleave(function(){
				$(this).fadeTo(200, 0.4);
			});
		});
	}
	
	if ($('li#link-list-container-extra-2').get(0) && $('div#container').get(0)) {
		var adContainer = $('li#link-list-container-extra-2').get(0);
		var pageContentContainer = $('div#container').get(0);
		
		$(adContainer).data('minimumTopValue', $(adContainer).position().top);
		$(adContainer).data('maximumTopValue', $(pageContentContainer).height() + $(pageContentContainer).offset().top - $(adContainer).outerHeight());		

		$(adContainer).data('function', {followWindowScroll: function() {
			$(adContainer).stop();
			
			if ($(window).scrollTop() > $(adContainer).position().top) {
				$(adContainer).animate({
					top : $(window).scrollTop() > $(adContainer).data('maximumTopValue') ? $(adContainer).data('maximumTopValue') : $(window).scrollTop() + 'px'
				}, 750);
			} else if($(window).scrollTop() < $(adContainer).position().top && $(window).scrollTop() > $(adContainer).data('minimumTopValue')) {
				$(adContainer).animate({
					top : $(window).scrollTop() + 'px'
				}, 750);								
			} else {
				$(adContainer).animate({
					top : $(adContainer).data('minimumTopValue') + 'px'
				}, 750);
			}
			}});
				
		$(window).scroll(function(scrollEvent) {
			$(adContainer).data('function').followWindowScroll();
		});
		
		$(window).each(function() {													
			$(adContainer).data('function').followWindowScroll();		
		});
	}	
	
	// Quick menu for main menu
	if ($('.link-list ul').is('#link-list-container-main')) {
		$('#link-list-container-main ul').each(function(){
			$.data(this, 'outerHeight', $(this).outerHeight());
			$(this).css('height', 0).hide();
		});
		
		$('#link-list-container-main > li.lvl1').mouseenter(
			function(){				
				if ($(this).find('ul').get(0)) {
					$(this).find('ul').each(function(){
						// Close any opened sub-menus
						toggleSlideUp();
										
						// Open the sub-menu
						$(this).stop(true).animate({height:$.data(this, 'outerHeight')}, 500);
					});
				} else {
					toggleSlideUp();
				}
			});
		
		// Closes any visible sub menus
		function toggleSlideUp() {
			$('#link-list-container-main').find('ul:visible').each(function(){
				$(this).stop(true).animate({height:'0px'}, 500);
			});
		}
		
		// Check if visible sub menus should be closed and closes them
		function triggerAutoClose() {
			if (!(typeof $.data(document, 'eventMousemove') == 'undefined')) {
				if (!$($.data(document, 'eventMousemove').target).parents().is('#link-list-container-main')) {
					toggleSlideUp();
				}
			}		
		}
	}
	
	$().mousemove(function(e){
		$.data(document, 'eventMousemove', e);
		triggerAutoClose();
	});
});