$(function(){

	$('#collapsible-button').each(function() {
		var toToggle = $(this).siblings();
		var cookieOptions = {
		    path: '/'
		};

		var collapsed = parseInt($.cookie('collapsible-collapsed'));
		if (collapsed) {
			toToggle.hide();
		}

		$('a', this).click(function() {

			if (toToggle.is(':hidden')) {
				toToggle.show();
				$.cookie('collapsible-collapsed', '0', cookieOptions);
			} else {
				toToggle.hide();
				$.cookie('collapsible-collapsed', '1', cookieOptions);
			}

			return false;
		});
	});

	$('.add-item-to-basket-button').each(function() {
		var add = $('.add', this);
		var added = $('.added', this);

		add.click(function() {
			var rel = $(this).attr('rel');
	        var id = rel.replace('add-item-to-basket-', '');

	        $.post('/basket', {action: 'add', item: id}, function() {
	            add.hide();
	            added.show();
	        });

	        return false;
		});
	});

	var compare = $('.compare');

	compare.click(function() {
		var self = this;
		var rel = $(this).attr('rel');
        var id = rel.replace('add-item-to-compare-', '');

        $.post('/compare', {action: 'add', item: id}, function() {
        	$(self).hide();
        	$(self).next().show();
        	$('.compare-link').show();
        });

		return false;
	})

	$('#sidebar-left a, #sidebar-right a').focus(function() {
		$(this).blur();
	});

    $('.icon-cart').click(function() {
        var rel = $(this).attr('rel');
        var id = rel.replace('add-item-to-basket-', '');

        $.post('/basket', {action: 'add', item: id}, function() {
            $('.' + rel).hide();
        });

        return false;
    });

    function populateBasket(data) {
		$.map(data.items, function(item) {
			$('#item-row-' + item.id + ' :input').val(item.count);
		});

		$('.item-row').each(function() {
			var exists = false;
			var id = $(this).attr('id').replace('item-row-', '');
			$.map(data.items, function(item) {
				if (item.id == id) {
					exists = true;
				}
			});

			if (!exists) {
				$(this).remove();
			}
		});

		$('#total-price').text(data.total);

		$('.delivery').each(function() {

			var id = $(this).attr('id').replace('delivery-', '');

			var d = data.deliveries[id];
			var freeDeliveryLeft = parseFloat(window.freeDeliveryMin[id] - data.freeDeliverySum).toFixed(2);

			if (freeDeliveryLeft > 0) {
				$('#free-delivery-above-text-' + id).hide();
			    $('#free-delivery-below-text-' + id).show();
				$('#free-delivery-price-' + id).show();
				$('#free-delivery-price-value-' + id).text(freeDeliveryLeft);
			} else {
				$('#free-delivery-above-text-' + id).show();
				$('#free-delivery-below-text-' + id).hide();
				if (d.cost_exists && d.cost > 0) {
				    $('#free-delivery-above-text-' + id).hide();
				} else {
				    $('#free-delivery-above-text-' + id).show();
				}
				$('#free-delivery-price-' + id).hide();
			}

			if (d.cost_exists && d.cost > 0) {
			    $('#delivery-cost-container-' + id).show();

			    $('#delivery-cost-value-' + id).text(d.cost_format);

			} else {
			    $('#delivery-cost-container-' + id).hide();
			}

		});

    }

    function saveBasket(callback) {

    	$.post('/basket/save/?' + $("#basket-from").serialize(), {action: 'save'}, function(json) {
    		populateBasket(json.basket);
    		if (callback) {
    			callback();
    		}
    	}, 'json');
    }

    $('.item-row').each(function() {
		var row = this;
		var count = $(':input', this);

		$('.inc', this).click(function() {
			count.val(
				parseInt(count.val()) + 1
			);
			saveBasket();
		});

		$('.dec', this).click(function() {
			var val = parseInt(count.val()) - 1;

			if (val < 0)
				val = 0;

			count.val(val);
			saveBasket();
		});

		$(count).change(function() {
			saveBasket();
		});

		$('.remove', this).click(function() {
			count.val('0');
			saveBasket();
			$(row).fadeOut().remove();

			return false;
		});
    });

    $('#clear-basket').click(function() {
    	$('#basket-from').remove();
    	$('#empty-bakset').show();

    	saveBasket();

    	return false;
    });

    $('#secondary-menu > ul > li').hover(function() {
        $('.sub', this).show();
    }, function() {
        $('.sub', this).hide();
    });

    // preload rollovers
    /*$('.menu ul li img').each(function(e){
        rollover = $(this).attr('src').replace(/\.jpg/, '-hover.jpg');
        $('<img>').attr('src', rollover);
    });*/

    // bind events
    /*$('.menu ul li img').bind('mouseover mouseout', function(e){
        if($(this).attr('rel') == 'hover') {
            $(this).attr('rel', '');
            basic = $(this).attr('src').replace(/\-hover/, '');
            $(this).attr('src', basic);
        } else {
            $(this).attr('rel', 'hover');
            rollover = $(this).attr('src').replace(/\.jpg/, '-hover.jpg');
            $(this).attr('src', rollover);
        }
    });*/

    $('.menu ul li').hover(function() {
        $('.normal', this).hide();
        $('.hover', this).show();
    }, function() {
        $('.hover', this).hide();
        $('.normal', this).show();
    });

    $('table#special thead tr td').bind('mouseover mouseout', function(e){
        if($(this).attr('rel') == 'hover') {
            $(this).attr('rel', '');
            $(this).css('background-image', '');
        } else {
            $(this).attr('rel', 'hover');
            $(this).css('background-image', 'url(images/special-bg-red.png)');
        }
    });

    $('.modal .close').click(function(e){
        $(this).parents('.modal').fadeOut();
        $('#screen-overlay').fadeOut();
        return false;
    });

    $('#login-link').click(function(e){
        $('#screen-overlay').css({
            backgroundColor:    '#000',
            opacity:                    '.7',
            width:                    '100%',
            height:                    '100%'
        }).fadeIn();
        $('#login-box').fadeIn();
        return false;
    });

    /*$('#banner-large').click(function() {
    	var a = $('.banner-switch a:first');
    	var url = a.attr('rel');
    	if (a.attr('target') == '_blank') {
    		window.open(url);
    	} else {
    		window.location = url;
    	}
    }).css({cursor: 'pointer'});*/

    $('#banner-large .banner-switch a').click(function(e){
        var self = this;
    	$('#banner-large .banner-switch a').removeClass('active');
    	var id = $(this).attr('id').replace('banner-switch-', '');

		$('#banner-large .content').hide();
    	$('#banner-content-' + id).show();

        $(this).addClass('active');
        return false;
    });

    $('.item-preview td.preview a').click(function(e){
    	$(this).blur();

    	$('.item-preview td.preview a').removeClass('active');
        $('.item-preview #item-preview').attr('src', $(this).attr('href'));
        $(this).addClass('active');

        return false;
    });

    $('.filter .collapser').click(function(e){
        if ($(this).hasClass('collapsed')) {
        	$(this).removeClass('collapsed');
        	$('table.order-form').show();
        } else {
        	$(this).addClass('collapsed');
        	$('table.order-form').hide();
        }
        return false;
    });
});


$(document).ready(function(){
	$('#callback').click(function(){$('#callBackForm').fadeIn(300);});
	$('#callBackFormClose').click(function(){$('#callBackForm').fadeOut(300);});
	$('#sendcallBackForm').click(cakkbackForm);
});

function cakkbackForm()
{$.post('/callback/',$('#callBackForm form').serializeArray()
	, function(data)
	{
		$('#callBackForm').html(data);
		$('#callback').click(function(){$('#callBackForm').fadeIn(300);});
		$('#callBackFormClose').click(function(){$('#callBackForm').fadeOut(300);});
		$('#sendcallBackForm').click(cakkbackForm);
	});
}
