/*
 * ------------------------------------------------------------
 *  Copyright (c) 2011 I-COM.
 *  This software is the proprietary information of I-COM
 *  All Rights Reserved.
 *  ------------------------------------------------------------
 *
 *  SVN revision information:
 *  $HeadURL$
 *  $Revision$
 *  $Author$
 *  $Date$
 *
 */
jQuery(document).ready(function($) {
	// Super nav height matching and hover functionality.
	$('#nav :hidden').each(function() {
		$(this).show();
	});

	$('#nav ul ul').each(function() {
		var height = 0;

		$(this).parent().parent().find('ul').each(function() {
			height = Math.max(height, $(this).height());
		});

		$(this).height(height);
	});

	$('#nav > li > ul').hide();

	$('#nav > li').hover(
		function() {
			$(this).find('ul').show();
		},
		function() {
			$(this).find('ul').hide();
		}
	);

	// Add plusses to nav titles with sub menus
	$('#nav > li:gt(0) > a').each(function() {
		$(this).text($(this).text() + ' +');
	});

	// Social media tab
	var socialTab = $('#social-tab');
	var socialClosed = parseInt(socialTab.outerWidth()) * -1;
	socialTab.css('left', socialClosed + 'px');

	$('#social-tab .tag').click(function(e) {
		e.preventDefault();

		var left = parseInt(socialTab.css('left')) < 0 ? '0px' : socialClosed + 'px';

		socialTab.animate({
			'left': left
		});
	});

	// Fancy fonts with Cufon
	Cufon.replace('#nav > li > a, h1, #sidebar .title, #contactForm h2', {
		fontFamily:'Avenir LT Std',
		hover:true
	});

	$('#nav > li').mouseleave(function() {
		Cufon.refresh();
	});

	// Background switcher
	$('#banners-background li a').each(function(i) {
		// Preload image
		var bgPreloader = [];
		bgPreloader[i] = new Image();
		bgPreloader[i].src = $(this).attr('href');

		// Change background on click
		$(this).click(function(e) {
			e.preventDefault();

			var href = $(this).attr('href');
			var link = $(this);

			$('#banners-background a').removeClass('active');

			$('#banners-background').animate({
					'opacity':0
				},
				'fast',
				null,
				function () {
					$('#banners-background').css({
						'background-image':'url(' + href + ')'
					});

					link.addClass('active');

					$('#banners-background').animate({
							'opacity':1
						},
						'fast'
					);
				}
			);
		});
	});

	// SEO filler slide
	$('#seo-filler :not(h2)').hide();
	$('#seo-filler h2').click(function() {
		$(this).parent().find(':not(h2)').toggle();
	})

	// Newsletter form slider
	var newsletter = $('#newsletter-validate-detail');
	var newsletterFullWidth = newsletter.width();
	var newsletterShrunkWidth = (newsletterFullWidth - ($('#newsletter-validate-detail .form-subscribe').width() + parseInt($('#newsletter-validate-detail').css('padding-left'))));
	newsletter.width(newsletterShrunkWidth);

	$('#newsletter-validate-detail .label').click(function() {
		var width = newsletter.width() == newsletterFullWidth ? newsletterShrunkWidth : newsletterFullWidth;

		newsletter.animate({
			'width': width
		}, 'slow');
	});

	// Enables centre aligning of quick access lists
	$('.quick-access > ul').each(function() {
		var width = $(this).width();
		$(this).width(width);
		$(this).css('float', 'none');
	});

	$('.basket-info').width(272);

	// Locations map ski/trek links
	$('#ski-map').click(function() {
		if (map) {
			var location = new GLatLng(45.89000815866184, 7.3388671875);
			map.panTo(location);
			map.setCenter(location, 5);
		}
	});

	$('#trek-map').click(function() {
		if (map) {
			var location = new GLatLng(53.69670647530323, -1.93359375)
			map.panTo(location);
			map.setCenter(location, 5);
		}
	});

	// Open certain links in new window
	$('a.new-window').click(function(e) {
		e.preventDefault();

		window.open($(this).attr('href'));
	});
});
