﻿$(document).ready(function() {
    Cufon.replace('#primary-nav li a', { hover: true });
    Cufon.replace('#secondary-nav li a', { hover: true });
    Cufon.replace('a.lrg', { hover: true });
    Cufon.replace('a.lrg-right', { hover: true });
    Cufon.replace('a.sml', { hover: true });
    Cufon.replace('a.sml-right', { hover: true });
    Cufon.replace('.filter-a ul li a', { hover: true });
    Cufon.replace('.filter-b ul li a', { hover: true });
    Cufon.replace('.filter-e ul li a', { hover: true });
    Cufon.replace('.slide p', { hover: true });
    Cufon.replace('.nav-right-lrg');
    Cufon.replace('h1');
    Cufon.replace('h2');
    Cufon.replace('h3');

    // Put a 1 in product quantity when clicked
    $('#product .quantity input.textbox').click(function() {
        if ($(this).val() == "") {
            $(this).val("1");
        }
    });

    // Hide anything that needs to appear for non-java script users
    if ($('#jsHide').length > 0) {
        $('#jsHide').hide();
    }

    var $contheight = $('#wrapper').height() - 69;
    $('#product-nav').css({ top: $contheight + 'px' });

    // Show anything that needs to appear for java script users
    $('.jsShow').show();

    $('.magnifyswatch').height('30px');
    $('.nomagnifyswatch').height('30px');

    $('#product-downloads').attr("z-index", "500").slideToggle(0);

    $('#product-download').click(function() {
        $('#product-downloads').slideToggle(500);
        return false;
    })

    var $rand = Math.floor(Math.random() * 3);

    $('#slideshow-images').cycle({
        fx: 'fade', //scrollHorz
        pause: 0,
        speed: 4000,
        timeout: 5000,
        random: 0,
        next: '#next',
        startingSlide: $rand
    });

    $('#slideshow-intros').cycle({
        fx: 'fade',
        pause: 0,
        speed: 4000,
        timeout: 5000,
        random: 0,
        next: '#next',
        startingSlide: $rand
    });

    $('#slideshow-icons').cycle({
        fx: 'fade',
        pause: 0,
        speed: 4000,
        timeout: 5000,
        random: 0,
        next: '#next',
        startingSlide: $rand
    });

    if ($('.prod-gallery').length > 0) {
        var galleries = $('.prod-gallery').adGallery();
        galleries[0].settings.description_wrapper = $('#mainSlideIntro');
    }

    if ($('.film-gallery').length > 0) {
        var galleries = $('.film-gallery').adGalleryFilm();
        galleries[0].settings.description_wrapper = $('#mainSlideIntro');
    }

    $('#area-slideshow #area-slideshow-images').cycle({
        fx: 'fade',
        pause: 0,
        speed: 3000,
        timeout: 5000,
        random: 0,
        next: '#area-slideshow-next',
        prev: '#area-slideshow-prev'
    });

    $('.ad-image-wrapper').hover(function() {
        if ($(this).find('.Image').attr('src') != null) {
            $('#magnify-image').show();
            //$('#gallery').addClass('magnify-image');
        }
    }, function() {
        //$('#gallery').removeClass('magnify-image');
        $('#magnify-image').hide();
    });

    $('#magnify-image').hover(function() {
        $(this).show();
    });

    $('.ad-image-wrapper').click(function() {

        if ($(this).find('.Image').attr('src') != null) {

            $('#blackout').show().css({ 'opacity': 0.7 });
            $('#largeimagemodal').fadeIn();
            $('#largeimagecontainer').html('<img src="' + $(this).find('.Image').attr('src').replace('product_', '') + '" style="border: solid 10px #FFF" />');

            var img = new Image();
            $(img).load(function() {
                var h = $('#largeimagecontainer img').height() + 20;
                var b = $(window).height();

                var top = (b - h) / 2;

                if (top < 0) {
                    $('#largeimagemodal').css({ 'top': '10px' });
                    $('#largeimagecontainer').height(b - 80 + 'px');
                    $('#largeimagecontainer img').height(b - 100 + 'px');
                }
                else {
                    $('#largeimagemodal').css({ 'top': '10px' });
                    $('#largeimagecontainer').height('auto');
                }
            }).attr('src', $(this).find('.Image').attr('src').replace('product_', ''));
        }

        if ($(this).find('.Video').attr('longdesc') != null) {
            alert($(this).find('.Video').attr('longdesc'));
        }

        if ($(this).find('.Flash').attr('longdesc') != null) {
            window.open('/360_html/' + $(this).find('.Flash').attr('longdesc') + '.html', '', 'status=yes,height=740');
        }
    });

    $('#largeimagemodal').click(function() {
        $(this).fadeOut();
        $('#blackout').fadeOut();
    });

    $('.magnifyswatch').hover(function() {
        var id = $(this).attr('id').substring(11);

        $('.swatchimagegallery').hide();

        if ($('#swatchGallery' + id + ' ul').children().length > 0) {

            $('#swatchGallery' + id).show().css('top', $(this).position().top);
            $('#swatchGallery' + id + ' ul li:first-child .Image').trigger('click');
        }
    }, function() {

    });

    $('.swatchimagegallery').hover(function() {
        $(this).show();
    }, function() {
        $('.swatchimagegallery').hide();
    });

    $('.swatchimagegallery .Image').click(function() {
        $('#showswatchimage').html('<img class="Image" src="' + $(this).attr('href') + '" />');

        var img = new Image();
        $(img).load(function() {
            var h = $('#showswatchimage img').height();

            if (h > 395) {
                $('#showswatchimage img').height('395px');
            }
        }).attr('src', $(this).attr('href'));

        return false;
    });

    // Load without caching
    $.fn.loadWithoutCache = function() {
        var elem = $(this);
        var func = arguments[1];
        $.ajax({
            url: arguments[0],
            cache: false,
            dataType: "html",
            success: function(data, textStatus, XMLHttpRequest) {
                elem.html(data);
                if (func != undefined) {
                    func(data, textStatus, XMLHttpRequest);
                }
            }
        });
        return elem;
    }

    $('#basket-items').loadWithoutCache('/cart.aspx', function() {
        $('#basket-items').css({ 'background': 'none' });
    });

    $('#basket-nav').click(function() {
        toggleBasket();
        return false;
    });

    $('#continue').click(function() {
        $('#basket-container').slideUp(250);
        $('#basket-nav').toggleClass('selectedMenu');
        return false;
    });

    if ($("#tweet").length > 0) {
        $("#tweet").tweet({
            username: "polarisbikewear",
            join_text: "auto",
            avatar_size: 32,
            count: 1,
            auto_join_text_default: "we said,",
            auto_join_text_ed: "we",
            auto_join_text_ing: "we were",
            auto_join_text_reply: "we replied to",
            auto_join_text_url: "we were checking out",
            loading_text: "loading tweets..."
        });
    }

    if ($("#tweets").length > 0) {
        $("#tweets").tweet({
            username: "polarisbikewear",
            join_text: "auto",
            avatar_size: 0,
            count: 3,
            auto_join_text_default: "we said,",
            auto_join_text_ed: "we",
            auto_join_text_ing: "we were",
            auto_join_text_reply: "we replied to",
            auto_join_text_url: "we were checking out",
            loading_text: "loading tweets..."
        });
    }

    $("#shippingSame").click(function() {
        if ($('input[name=shippingSame]').is(':checked')) {
            $('[name=shippingAddress]').val($('[name=address]').val());
            $('[name=shippingAddress]').attr("disabled", true);
            $('[name=shippingAddress2]').val($('[name=address2]').val());
            $('[name=shippingAddress2]').attr("disabled", true);
            $('[name=shippingTown]').val($('[name=town]').val());
            $('[name=shippingTown]').attr("disabled", true);
            $('[name=shippingPostcode]').val($('[name=postcode]').val());
            $('[name=shippingPostcode]').attr("disabled", true);
            $('[name=shippingCountry]').val($('[name=country]').val());
        }
        else {
            $('[name=shippingAddress]').val('');
            $('[name=shippingAddress]').attr("disabled", false);
            $('[name=shippingAddress2]').val('');
            $('[name=shippingAddress2]').attr("disabled", false);
            $('[name=shippingTown]').val('');
            $('[name=shippingTown]').attr("disabled", false);
            $('[name=shippingPostcode]').val('');
            $('[name=shippingPostcode]').attr("disabled", false);
        }
    });
});

function toggleBasket() {
    $left = $('#basket-nav').position().left;
    $leftpos = $left - 500;
    $('#basket-container').css('left', $leftpos + 'px');
    $('#basket-container').slideToggle(250);
    $('#basket-nav').toggleClass('selectedMenu');
}

function removeItem(id) {
    $('#itemquantity' + id).value = '0';

    document.getElementById("itemquantity" + id).value = '0';
    
    //document.getElementById("shoppingCartForm").submit();
    //return false;
}
