var pNum = 0;
var slideTotal = 0;

function ShowAddReview() {
    $('#productTabs li').removeClass('tabon');
    $('#fullReview').parent().addClass('tabon');
    $('.productTabContainer .tab').hide();
    $('#readReviewBox').hide();
    $('#readreview').show();
    $('#writeReviewBox').fadeIn('slow');
}

$(document).ready(function () {
	// Colour-ways animation
    $('p.animation_2').hide();
    animation_state = 1;
    function colour_ways_animation(carousel) {
	    if ( animation_state == 1 ) {
		    $('p.animation_1').fadeOut(1000);
		    $('p.animation_2').fadeIn(1000);
		    animation_state = 0;
	    }
	    else {
		    $('p.animation_1').fadeIn(1000);
		    $('p.animation_2').fadeOut(1000);
		    animation_state = 1;
	    }
    }
	window.setInterval(colour_ways_animation, 4000);
    // Sorts the product page info panes and image
    if (($('.productInfoPane').height() - 134) < 297) {
        $('.productImage').height(297);
        var newMargin = (432 - $('.productInfoPane').height()) / 2;
        $('.dottedBox').css('margin-top', newMargin + 10);
    } else {
        //$('.productImage').height($('.productInfoPane').height() - 134);
        var newMargin = $('.productInfoPane').height() - $('.productImagePane').height();
        $('.productGallery').css({ 'margin-top': newMargin + 6 });
    }

    // For the right aligned category menu
    $(document).ready(function () {
        $(".jewellery ul").addClass("onRight");
        $(".collections ul").addClass("onRight");
        $(".interiors ul").addClass("onRight");
    });

    // Inline send to a friend
    $(".productEmailFriend").colorbox({ width: "500", height: "500", inline: true, iframe: true });

    // Add to Basket Popup
    $('.addbag').click(function (ev) {
        $('#addToBasketAlert').css({ top: '330px', left: '50%' }).fadeIn('fast');

    });
    // To close the basket
    $('.checkoutClose, .checkoutContinue').click(function () {
        $('#addToBasketAlert').fadeOut();
    });

    $('#AddToFav').jFav();

    $('.productSlash').transform({ rotate: 45 });
    $('.productListSlash').transform({ rotate: 45 });

    $('.customerCommentsSlides').cycle({
        fx: 'fade',
        cleartypeNoBg: true
    });

    /* for product image gallery  (will need changing)
    $('ul.small_product_images a').click(function(){
    $('div#productimagecontainer img.product_image').attr('src', $(this).children('img').attr('src').replace('width=150', 'width=350').replace('height=150', 'height=400'));
    $('div#productimagecontainer a.avzoom').attr('href', $(this).children('img').attr('src').replace('width=150', 'width=0').replace('height=150', 'height=0'));
    $('div#productimagecontainer a.clicktoenlarge').attr('href', $(this).children('img').attr('src').replace('width=150', 'width=0').replace('height=150', 'height=0'));
    return false;
    });
    */


    var jqzoom_options = {
        zoomWidth: 434,
        zoomHeight: 434,
        xOffset: 19,
        yOffset: -1,
        position: "right",
        showEffect: "fadein",
        hideEffect: "fadeout",
        fadeoutSpeed: "fast",
        preloadImages: false,
        title: false
    }

    $('.jqzoom').jqzoom(jqzoom_options);


    // Colorbox for product page
    $("a.altImages").colorbox({ rel: 'altImages' });

    /* Not needed anymore
    $(".productGallery a[rel='productimages']").click(function () {
        // Get the 2 URL's to switch
        var $thisImg = $(this).attr('href');
        var $mainImg = $('.productImage a').attr('href');

        // Swap and replace them both
        $('.productImage a').attr('href', $thisImg).children('img').attr('src', $thisImg.replace('=594', '=297').replace('=594', '=297'));
        $(this).attr('href', $mainImg).children('img').attr('src', $mainImg.replace('=594', '=98').replace('=594', '=98'));

        // Return false so the Colorbox doesn't kick in
        $('.jqzoom').unbind();
        $('.jqzoom').jqzoom(jqzoom_options);
        return false;
    });
    */

    // Default values for input box's (store in title)
    $('input[title]').each(function () {
        if ($(this).val() === '') {
            $(this).val($(this).attr('title'));
        }

        $(this).focus(function () {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }
        });
        $(this).blur(function () {
            if ($(this).val() === '') {
                $(this).val($(this).attr('title'));
            }
        });
    });

    $('#fullRead').click(function () {
        $('#productTabs li').removeClass('tabon');
        $('#fullTab').parent().addClass('tabon');
        $('.productTabContainer .tab').hide();
        $('#fulldesc').fadeIn('slow');
    });

    $('#writeReviewBtn').click(function () {
        ShowAddReview();
    });

    $('#readReviewBtn').click(function () {
        $('#productTabs li').removeClass('tabon');
        $('#fullReview').parent().addClass('tabon');
        $('.productTabContainer .tab').hide();
        $('#writeReviewBox').hide();
        $('#readreview').show();
        $('#readReviewBox').fadeIn('slow');
    });

    $('#productTabs li').hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); });
    $('#productTabs li a').click(function () {
        $('#productTabs li').removeClass('tabon');

        $(this).parent().addClass('tabon');
        $('.productTabContainer .tab').hide();
        $($(this).attr('href')).show();
        return false;
    });
    $('.productTabContainer .tab').hide();





    // IE6 fixes
    if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) < 7) {
        // Fix the background rollover for search bar
        $('#headerInfo .hSearch').hover(function () {
            $(this).css('background-position', '0px -26px');
        }, function () {
            $(this).css('background-position', '0px 0px');
        });

        // Fix menu hover for main menu
        $('#mainMenu li').hover(function () {
            $('ul', this).show();
        }, function () {
            $('ul', this).hide();
        });

        // background rollover fix
        $('#mainMenu li.crafts').hover(function () { $(this).css('background-position', '0px 0px'); }, function () { $(this).css('background-position', '0px -31px'); });
        $('#mainMenu li.haberdashery').hover(function () { $(this).css('background-position', '-141px 0px'); }, function () { $(this).css('background-position', '-141px -31px'); });
        $('#mainMenu li.fashion').hover(function () { $(this).css('background-position', '-341px 0px'); }, function () { $(this).css('background-position', '-341px -31px'); });
        $('#mainMenu li.interiors').hover(function () { $(this).css('background-position', '-482px 0px'); }, function () { $(this).css('background-position', '-482px -31px'); });
        $('#mainMenu li.jewellery').hover(function () { $(this).css('background-position', '-648px 0px'); }, function () { $(this).css('background-position', '-648px -31px'); });
        $('#mainMenu li.collections').hover(function () { $(this).css('background-position', '-806px 0px'); }, function () { $(this).css('background-position', '-806px -31px'); });
    }


    // Make the side menu work
    $('.menuoption').click(function () {
        $('.menuoption').removeClass('on');
        $('.submenuContent').slideUp('normal');
        $('.submenuContentLinks').slideUp('normal');
        if ($(this).next().is(':hidden') == true) {
            $(this).addClass('on');
            $(this).next().slideDown('normal');
        }
    });
    $('.submenuContent').hide();

    //    $('.menuselect li ul li').toggle(function () {
    //        if ($(this).hasClass('tickon')) {
    //            $(this).removeClass('tickon');
    //        } else {
    //            $(this).addClass('tickon');
    //        }
    //    }, function () {
    //        if ($(this).hasClass('tickon')) {
    //            $(this).removeClass('tickon');
    //        } else {
    //            $(this).addClass('tickon');
    //        }
    //    });


    // Code for the product scroller at the bottom of the page
    $('#carouselScroller').jcarousel({
        auto: 2,
        scroll: 1,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });


    // Code for the SEO content panels in the footer
    slideTotal = $('#seocontent .panels').size() - 1;
    updateSlides();

    $('#nextSlide').click(function () {
        pNum++;
        updateSlides();
        return false;
    });

    $('#prevSlide').click(function () {
        pNum--;
        updateSlides();
        return false;
    });


    $('.newsLetterSignupButton').hover(function () {
        $('.newsLetterSignup').css('background-position', 'bottom left');
    }, function () {
        $('.newsLetterSignup').css('background-position', 'top left');
    }).click(function () {
        $('#newsLetterSignupForm').submit();
        return false;
    });

    $('.newsLetterSignup').hover(function () {
        $(this).css('background-position', 'bottom left');
    }, function () {
        $(this).css('background-position', 'top left');
    });


    $('.hSearchButton').hover(function () {
        $('.hSearch').css('background-position', 'bottom left');
    }, function () {
        $('.hSearch').css('background-position', 'top left');
    }).click(function () {
        $('#hSearchForm').submit();
        return false;
    });

    $('.hSearch').hover(function () {
        $(this).css('background-position', 'bottom left');
    }, function () {
        $(this).css('background-position', 'top left');
    });

});

function updateSlides() {
    if (pNum < 0) pNum = slideTotal;
    if (pNum > slideTotal) pNum = 0;
    $('#seocontent .panels').each(function(index) {
        if (pNum != index) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });
}


function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


