$(document).ready(function(){
	var sailingHeight =	$('#what-type-sailing').height();
	var kitBagFormHeight = $('#kit-bag-form').height();
	var whatTypeWeatherLeft = $('#what-type-weather').css('left');
	var yourKitBadLeft = $('#your-kitbag').css('left');
	$('#kit-bag-form').css({'top':-(kitBagFormHeight+30)+'px'})
	var linkClass;
	$('#what-type-sailing').css({'height':sailingHeight+'px'});

	// Get Weather
	$('#what-type-sailing a').click(function(){
		$('#reset').show();
		$('#what-type-sailing > .selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$('.selected').animate({'opacity':'1'});

		// Get Weather
		var activityId = $('#what-type-sailing h4.selected a').attr('id').split('-')[1];

		$('#activityId').val(activityId);

		hideWeather();

		$.get('technical/kit-bag/weather/' + activityId, function(data) {
			if (data['html'].length != 0) {
				$('#what-type-weather-options').empty(); // Remove Weather Types
				$('#what-type-weather-options').html(data['html']); // Insert Weather Types
			
				$('#what-type-sailing h4:not(".selected")').animate({'opacity':'0.5'},function(){
					loadInWeather();
				});

				if (loadWeatherFromSession) {
					$('#weather-' + weatherId).trigger('click');
					loadWeatherFromSession = false;
					if ($('.kit-bag-form p').hasClass('errorText') || $('.kit-bag-form p').hasClass('successText')) {
						//errorSuccess();
						loadInForm();
					}
				}
				else {
					// Hide Kitbag
					hideKitbag();
				}
			}
		});
	});

	// Get Products
	$('#what-type-weather-options a').live('click', function(){
		$('#reset').show();
		var activityId = $('#what-type-sailing h4.selected a').attr('id').split('-')[1];
		var weather = $(this).attr('id').split('-')[1];

		$('#activityId').val(activityId);
		$('#weatherId').val(weather);

		var url = 'technical/kit-bag/products/' + activityId + '/' + weather;

		$.get(url, function(data) {
			if (data['html'].length != 0) {
				$('#kitbag-main').empty(); // Remove Current Products
				$('#kitbag-main').html(data['html']); // Insert New Products

				if (data.empty) {
					$('#email-results-btn').hide();
				} else {
					$('#email-results-btn').show();
				}

				$('#kitbag-main img').GillProductHover($('#main-content'), $('#main-content'));

				$('#productIds').attr('value',$.toJSON(data.productIds));
			}
		});

		$('img:first', '#what-type-weather-options a').show();
		$('img:nth-child(2)', '#what-type-weather-options a').hide();

		$('img:first', this).hide();
		$('img:nth-child(2)', this).show();

		$('#what-type-weather-options a').removeClass('selected');
		$(this).addClass('selected');

		$('#what-type-weather-options > .selected').animate({'opacity':'1'});
		$('#what-type-weather-options a:not(".selected")').animate({'opacity':'0.5'});

		loadInKitbag();
	});

	$('#what-type-weather-options a').live('mouseover mouseout', function(event){
		if (event.type == 'mouseover') {
			$('img:first', this).hide();
			$('img:nth-child(2)', this).show();
		}
		else {
			if ($(this).attr('class') != 'selected') {
				$('img:first', this).show()
				$('img:nth-child(2)', this).hide();
			}
		}
	});
	
	$('#your-kitbag a.wide-btn').click(function(){
		loadInForm();
	});

	function loadInWeather() {
		//$('#what-type-weather').animate({'left':'0px'});
		$('#what-type-weather').animate({'left':'0px'});
	}

	function hideWeather() {
		$('#what-type-weather').animate({'left':'-770px'});
	}
	
	function loadInKitbag() {
		$('#your-kitbag').animate({'left':'0px'});
	}

	function hideKitbag() {
		$('#your-kitbag').animate({'left':'-770px'});
	}
	
	function loadInForm() {
		$('#kit-bag-main-content').animate({'left':'410px'},function(){				 
			$('#kit-bag-form').animate({'top':'0px'});
		});

		// Prevent user being able to change kitbag when on form, as they can only see half of it, force them to click back to use it properly.
		$('#reset, #what-type-sailing a').unbind();
		$('#what-type-weather-options a').die();
	}
	
	function errorSuccess() {
		//$('#your-kitbag').css({'left':'0px'});
		//$('#what-type-weather').css({'left':'0px'});
		//$('#weather-' + weatherId).trigger('click');
		//$('#kit-bag-main-content').css({'left':'410px'});
		//$('#kit-bag-form').css({'top':'0px'});
		loadInForm();
	}
	
	$('#reset').click(function(){
		$('#what-type-sailing h4').each(function(){
			$(this).animate({'opacity':'1'})
		});
		$('h4.selected').removeClass('selected');
		$('#what-type-weather a').removeClass('selected ' + linkClass).each(function(){
			$(this).animate({'opacity':'1'})
		});
		$('#what-type-weather').animate({'left':whatTypeWeatherLeft});
		$('#your-kitbag').animate({'left':yourKitBadLeft});
		$('#kit-bag-main-content a.change').html('Select');

		$('#reset').hide();

		// Wipe session variables
		$.get('technical/kit-bag/reset');

		return false;
	});

	$('#kit-bag-form > .heading-underline a.change').click(function(){
		kitBagFormHeight = $('#kit-bag-form').height();
		$('#kit-bag-form').animate({'top':-kitBagFormHeight +'px'},function(){
			$('#kit-bag-main-content').animate({'left':'0px'});			
		});											
	});


	//trigger
	var loadWeatherFromSession = false;
	
	var activityId = $('#activityId').attr('value');
	if (activityId != '') {
		var weatherId = $('#weatherId').attr('value')
		if (weatherId != '') {
			loadWeatherFromSession = true;
		}
		$('#activity-' + activityId).click();
	}
});
