$(document).ready(function() {
	/* Track where the mouse is */
	var mouse_is_inside = false;
	$('#login-box').hover(function(){ 
		mouse_is_inside=true; 
	}, function(){ 
		mouse_is_inside=false; 
	});
	$('#login-link').hover(function(){ 
		mouse_is_inside=true; 
	}, function(){ 
		mouse_is_inside=false; 
	});
	$('#reminder-button').hover(function(){ 
		mouse_is_inside=true; 
	}, function(){ 
		mouse_is_inside=false; 
	});
	$('.btn-delete').hover(function(){ 
		mouse_is_inside=true; 
	}, function(){ 
		mouse_is_inside=false; 
	});

	/* If link gets focus  */
	$('#login-link').focus(function(){ 
		if(! mouse_is_inside) {
			$('.notification').hide();
			$('.accountLogin').addClass('down');		
			$('#login-box').show('slow');
		}
	});
	/* If link clicked */
	$('#login-link').click(function(){
		$('.notification').hide();
		$('.accountLogin').toggleClass('down');		
		$('#login-box').toggle('slow');
		$('#reminder-box').hide('slow');
		return false;
	});
	
	$('#reminder-button-success').click(function(){	
		$('#reminder-button-success').attr('disabled','disabled');
		$('.notification').hide();
		var ref = $('.productBasket input[name=ref]').val();

        if(typeof _gaq != 'undefined') {
    		_gaq.push(['_trackEvent', 'basket', 'ajaxNotificationAdd']);
    	}

		$.ajax({
				url: '/account/notifications.asp',
				data: { ref: ref, template: 'ajax' },
				dataType: 'html',
				success: function(response){
					$('#reminder-box-success').html(response);
					$('#reminder-box-success').slideToggle('slow');
					$('#reminder-button-success').attr('disabled','');
				},
				error: function(){
					$('#reminder-box-error').slideToggle('slow');
					$('#reminder-button-success').attr('disabled','');
				}		
		});
		return false;
	});

	$('#reminder-button-login').click(function(){	

        var scrollTop = $(window).scrollTop();
        if (scrollTop > 15) {
            $('#login-box').css('top', scrollTop-5);
        } else {
            $('#login-box').css('top', 15);
        }

		$('.notification').hide();
		$('#reminder-box-login').toggle('slow');
		$('.accountLogin').addClass('down');
		$('#login-box').show('slow');
		return false;
	});

	/* Clicks out of main area */
	$(document.body).click(function(){ 
		if(! mouse_is_inside) {
			$('.accountLogin').removeClass('down');
			$('#login-box').hide('slow');
			/*window.clearTimeout(hideBasket);*/
			$('#basket-notification').slideUp('slow')
			$('.notification').hide('slow');
		}
	});
	/* Fade out on close buttons */
	$(".btn-delete").click(function(){
		$(this).parents('#reminder-box-login').animate({ opacity: 'hide' }, 'slow');
		$(this).parents('#reminder-box-success').animate({ opacity: 'hide' }, 'slow');
	});

	var divHeight = $('.description-container').height();
	if(divHeight > 325) {
		$('.description-container').removeClass('big');
		$('#more-description').show();
	}
	
	$("#more-description").click(function(){
		$('.accountLogin').removeClass('down');
		$('#login-box').hide('slow');
		$('.notification').hide('slow');
		$('.description-container').toggleClass('big');
		
		var currentHeight = $('.description-container').height();
		if(currentHeight > 325) {
			$('#more-description').attr('src','/img/button/read-less.gif');
			$('#more-description').val('Read Less');
			}
		else {
			$('#more-description').attr('src','/img/button/read-more.gif');
			$('#more-description').val('Read More');
		}
			
		return false;
	});
	
	function hideBasketNote() {
		hideBasket = window.setTimeout( function() {
			$('#basket-notification').slideUp('slow');
			$('.jquerybasketbutton').attr('disabled','');
			$('.jquerymultibasketbutton').attr('disabled','');
		}, 4000);
	}
	
	$('.jquerybasketbutton').click(function(){

		$('.jquerybasketbutton').attr('disabled','disabled');
		$('.notification').hide();
		$('.accountLogin').removeClass('down');
		$('#login-box').hide('fast');
		
		var ref = $(this).parent().find('.input-ref').val();
		var promotion = $(this).parent().find('.input-promotion').val();
		
        var scrollTop = $(window).scrollTop();
        if (scrollTop > 55) {
            $('#basket-notification').css('top', scrollTop-25);
        } else {
            $('#basket-notification').css('top', 55);
        }
        
		
        if(typeof _gaq != 'undefined') {
    		_gaq.push(['_trackEvent', 'basket', 'ajaxBasketAdd']);
	    	_gaq.push(['_trackPageview', '/cgi-bin/basket.cgi?action=add']);
	    }

		
		$.ajax({
				url: '/cgi-bin/basket.cgi',
				data: { action: 'add', ref: ref, template: 'ajax', promotion: promotion },
				dataType: 'html',
				async: true,
				success: function(response){
    
                    if (response.indexOf('http') == 0) {
                        document.location = response;
                    } else {
    					$('#basket-notification').html(response);
	    				$('#basket-notification').show('fast');
    					updateBasket = $(response).filter('#ajaxbasketsummary').html();
	    				$('#basket').html(updateBasket);
		    			hideBasketNote();
		    		}
				},
				error: function(response){
					$('.jquerybasketbutton').attr('disabled','');
				}		
		});
		return false;
	});


	$('.jquerymultibasketbutton').click(function(){

		$('.jquerymultibasketbutton').attr('disabled','disabled');
		$('.notification').hide();
		$('.accountLogin').removeClass('down');
		$('#login-box').hide('fast');
		
		var items = $(this).parent().find('.input-items').val();

        var scrollTop = $(window).scrollTop();
        if (scrollTop > 55) {
            $('#basket-notification').css('top', scrollTop-25);
        } else {
            $('#basket-notification').css('top', 55);
        }

        if(typeof _gaq != 'undefined') {
    		_gaq.push(['_trackEvent', 'basket', 'ajaxBasketAddMultiple']);
	    	_gaq.push(['_trackPageview', '/cgi-bin/basket.cgi?action=add']);
	    }
		
		$.ajax({
				url: '/cgi-bin/basket.cgi',
				data: { action: 'quick', items: items, template: 'ajax', delimiter: ',', qtyaction: 'add' },
				dataType: 'html',
				async: true,
				success: function(response){

                    if (response.indexOf('http') == 0) {
                        document.location = response;
                    } else {
    					$('#basket-notification').html(response);
    					$('#basket-notification').show('fast');
    					updateBasket = $(response).filter('#ajaxbasketsummary').html();
    					$('#basket').html(updateBasket);
    					hideBasketNote();
    				}
				},
				error: function(response){
					$('.jquerymultibasketbutton').attr('disabled','');
				}		
		});
		return false;
	});

});
