$(function () {
	
	$(window)
		.bind('resize', function () {
			var h = $('#header').height(),
				f = $('#footer').height(),
				w = $('#wrapper').height(),
				W = $(window).height();

			var G = W - h - w - f;

			if ( h + w + f < W ) {
				$('#wrapper').css('min-height', w+G-f-85);
			}
		}).trigger('resize');
	
	$('#header .user-menu div a, #wrapper .container .content > .box > .body > .grid .box > .footer .asset a, #wrapper .container .content > .box > .body > .grid .box > .compare a, .super-large-box .education a, .super-large-box .shop a, .large-box div a, .individual-product-header .basket-button a, .individual-product-header .gallery .prev a, .individual-product-header .gallery .next a')
		.bind('mouseover', function () {
			$(this)
				.stop()
				.animate(
					{opacity: 1},
					{duration: 150}
				);
		})
		.bind('mouseout', function () {
			$(this)
				.stop()
				.animate(
					{opacity: 0},
					{duration: 150}
				);
		});
		
	$('#supheader').data('open', false);
		
	$('#header .user-menu .login a')
		.bind('click', function (e) {
			e.preventDefault();
			
			if ( $('#supheader').data('open') ) {
				$(this).parents('.login').eq(0).removeClass('active');

				$('#supheader')
					.data('open', false)
					.stop()
					.animate(
						{height: '0'},
						{duration: 300}
					);
			} else {
				$(this).parents('.login').eq(0).addClass('active');

				$('#supheader')
					.data('open', true)
					.stop()
					.animate(
						{height: '50px'},
						{duration: 300}
					);
			}
		});
		
	$('#wrapper .container .content > .box > .body > .grid .box > .compare input:checkbox')
		.bind('click', function () {
			
			if ( $(this).parents('.grid').eq(0).find('input:checkbox:checked').size() == 3 ) {
				$(this).parents('.grid').eq(0).find('input:checkbox:not(:checked)')
					.attr('disabled', 'disabled');
			} else {
				$(this).parents('.grid').eq(0).find('input:checkbox:not(:checked)')
					.removeAttr('disabled', 'disabled');
			}
			
			if ( $(this).attr('checked') ) {
				$(this).parents('.box').eq(0)
					.addClass('active');
			} else {
				$(this).parents('.box').eq(0)
					.removeClass('active');
			}
		});
		
	$('.individual-product-header .gallery .container')
		.data('refresh', function () {
			var C = $('.individual-product-header .gallery .container');
			var G = $('.individual-product-header .gallery');
			
			G.find('.prev').removeClass('hidden');
			G.find('.next').removeClass('hidden');
			
			if ( C.find('a:first').hasClass('enable') ) {
				G.find('.prev').addClass('hidden');
			}
			
			if ( C.find('a:last').hasClass('enable') ) {
				G.find('.next').addClass('hidden');
			}
			
			if ( C.find('a').size() < 4 ) {
				G.find('.prev').addClass('hidden');
				G.find('.next').addClass('hidden');
			}
		});
		
	$('.individual-product-header .gallery .next a')
		.bind('click', function (e) {
			e.preventDefault();
			
			var C = $('.individual-product-header .gallery .container');
			
			C.find('a.enable:first').removeClass('enable');
			
			var i = C.find('a').index( C.find('a.enable:last') );
			
			C.find('a').eq( i + 1 )
				.addClass('enable')
				.css('opacity', 0)
				.animate(
					{opacity: 1},
					{duration: 300}
				);
			C.data('refresh')();
		});
		
	$('.individual-product-header .gallery .prev a')
		.bind('click', function (e) {
			e.preventDefault();
			
			var C = $('.individual-product-header .gallery .container');
			
			C.find('a.enable:last').removeClass('enable');
			
			var i = C.find('a').index( C.find('a.enable:first') );
			
			C.find('a').eq( i - 1 )
				.addClass('enable')
				.css('opacity', 0)
				.animate(
					{opacity: 1},
					{duration: 300}
				);
			C.data('refresh')();
		});
		
	$('#cancelsearch')
		.bind('click', function () {
			$(this).parents('form').eq(0).find(':input')
				.each(function (i) {
					name = $(this).attr('name');
					if (
						   name == 'c'
						|| name == 'q'
					) { return true; }
					$(this).removeAttr('name');
				});
		});
	
	$('#topmenu > .container > ul > li')
		.bind('mouseover', function () {
			$(this).find('ul').eq(0)
				.css('display', 'block')
				.stop()
				.animate(
					{opacity: 1},
					{duration: 300}
				);
		})
		.bind('mouseout', function () {
			var thiz = this;
			
			$(this).find('ul').eq(0)
				.stop()
				.animate(
					{opacity: 0},
					{
						duration: 300,
						complete: function () {
							$(thiz).find('ul').eq(0).css('display', 'none')
						}
					}
				);
		});
		
	$('#wrapper .container .content > .box > .body > .grid .box > .compare a')
		.bind('click', function( e ) {
			e.preventDefault();
			
			var x = $('#wrapper .container .content > .box > .body > .grid .box > .compare :checkbox:checked').size();
			if ( ! x ) {
				$(this).parents('.compare').eq(0).find(':checkbox').attr('checked', 'checked');
			}
			
			$(this).parents('form').trigger('submit');
		});
		
	$('.product-tabs li')
		.bind('click', function(e) {
			$('.product-tabs li').removeClass('selected');
			$(this).addClass('selected');
			$('#product-specification-tab-content').css('display', 'none');
			$('#product-gallery-tab-content').css('display', 'none');
			$('#product-review-tab-content').css('display', 'none');
			$('#product-score-tab-content').css('display', 'none');
			var id = $(this).attr('id');
			var id = id + '-content';
			$('#' + id).css('display', 'block');
		});
		
	$('.individual-product #product-specification-tab-content ul')
		.each(function () {
			$(this).find('li:last').css('margin', 0);
		});
	
});
