
var timeout = 0;
var timeout1 = 0;
$(document).ready(function(){
    if (window.location.hash == '#purchasereview') {
        $('#submitting').hide();
        $('#reviewBox').slideDown('fast');
    }
    $('.matchingStyles img').hover(function(){
        $('#matchingTitle').slideDown('fast').text($(this).attr('alt'));
        timeout = 1;
    }, function(){
        $(this).delay(200, function(){
            $('#matchingTitle').text('');
        });
        timeout = 0;
        $(this).delay(5000, function(){
            if (timeout == 0) {
                $('#matchingTitle').slideUp();
            }
        });
    });
    $("#stars-wrapper").stars({
        inputType: "select",
        oneVoteOnly: true,
        callback: function(ui, type, value){
            if ($('#stars-wrapper').attr('data_member') != 1) {
                alert('Sorry, you must be a member to review products');
                return false;
            }
            $('#reviewBox').slideUp('fast');
            $.ajax({
                type: "POST",
                url: '/products/style.asp',
                data: 'styleid=' + URLEncode($('#styleid').attr('value')) + '&rating=' + value,
                complete: function(responseText, textStatus, XMLHttpRequest){
                    $('#customerReviews').html(parseInt($('#customerReviews').html()) + 1);
                    $('#ratingResponse').slideDown('fast').delay(5000, function(){
                        $('#ratingResponse').slideUp();
                    });
                }
            });
						_gaq.push(["_setAccount","UA-213745-1"],["_trackPageview","/goals/rating.html"])
        }
    });
    $("#stars-wrapper2").stars({
        inputType: "select",
        cancelValue: '',
        callback: function(ui, type, value){
            $('#reviewrating').val(value);
        }
    });
    $('.AlsoBought img').hover(function(){
        $('#alsoBoughtTitle').slideDown('fast').text($(this).attr('alt'));
        timeout = 1;
    }, function(){
        $('#alsoBoughtTitle').text('');
        timeout = 0;
        $(this).delay(5000, function(){
            if (timeout == 0) {
                $('#alsoBoughtTitle').slideUp();
            }
        })
    });
    $('.matchingStyles a').click(function(){
        ga_trackEvent('Navigation/Style', 'MatchingStyles');
    });
    $('.AlsoBought a').click(function(){
        ga_trackEvent('Navigation/Style', 'AlsoBought');
    });
    $('#zoom a').lightBox({
        imageLoading: '/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
        containerResizeSpeed: 0,
        overlayOpacity: 0.2
    });
    $('#zoom a').click(function(){
        closeSwatchPreview();
        ga_trackEvent('Navigation/Style', 'Zoom');
    });
    $('#addReview').click(function(){
        $('#reviewBox').slideDown('fast');
        $('#submitting').hide();
        $('#reviewLoader').hide();
    })
    $('#hideReview').click(function(){
        $('#reviewBox').slideUp('fast');
        $('#reviewLoader').show();
    })
    $('#submitReview').click(function(){
        if ($('#reviewrating').attr('selectedIndex') == 0) {
            alert('Enter a rating from 1-5');
            return false;
        }
        if ($('#reviewText').val() == '') {
            alert('Please enter a review');
            return false;
        }
        if ($('#reviewName').val() == '') {
            alert('Please enter your name');
            return false;
        }
        if ($('#location').val() == '') {
            alert('Please enter your location');
            return false;
        }
        $('#submitting').show();
        $.ajax({
            type: "POST",
            url: '/products/style.asp',
            data: 'styleid=' + URLEncode($('#styleid').attr('value')) + '&reviewrating=' + URLEncode($('#reviewrating').val()) + '&review=' + URLEncode($('#reviewText').val()) + '&name=' + URLEncode($('#reviewName').val()) + '&location=' + URLEncode($('#location').val()) + '&type=review',
            complete: function(responseText, textStatus, XMLHttpRequest){
                $('#reviewBox').slideUp('fast');
                $('#reviewResponse').slideDown('fast').delay(5000, function(){
                    $('#reviewResponse').slideUp();
                });
            }
        });
    });
});
function RateItem(){
    if (document.rating.rating.selectedIndex > 0) {
        document.rating.submit();
    }
    else {
        alert('Enter a rating');
    }
}

var selectedColourId = 0;
var selectedColourName = '';
var selectedSizeIndex = 0;
var selectedSize = '';
function selectColour(id, colourName, swatch){
    if (!$('#img_' + id).hasClass('swatchSelectorUnavailable')) {
        if (id != selectedColourId) {
            document.getElementById('mainImage').src = document.getElementById('originalImage').value;
            $('#swatchPreview').hide();
            $('#swatchPreview').fadeIn('slow');
            document.getElementById('zoom').style.display = 'block';
            document.getElementById('swatchPreviewImage').src = '/images/products/style/swatches/' + swatch + '.jpg';
            document.getElementById('colourName').innerHTML = colourName;
            document.getElementById('selectedColour').innerHTML = colourName;
            var mainImage = document.getElementById('mainImage');
            try {
                mainImage.style.opacity = 0.2;
                mainImage.filters.alpha.opacity = 20;
            } 
            catch (err) {
            }
            document.getElementById('img_' + id).style.border = '2px #868379 solid';
            if (selectedColourId > 0) {
                document.getElementById('img_' + selectedColourId).style.border = '2px #e8e2cf solid';
            }
            selectedColourId = id;
            selectedColourName = colourName;
            document.getElementById('colour').value = colourName;
            changeSizeColour();
        }
    }
}

function closeSwatchPreview(){
    $('#swatchPreview').fadeOut('normal');
    var mainImage = document.getElementById('mainImage');
    try {
        mainImage.style.opacity = 1;
        mainImage.filters.alpha.opacity = 100;
    } 
    catch (err) {
    }
}

function setMainImage(id, name, desc){
    document.getElementById('colour').value = desc;
    document.getElementById('zoom').style.display = 'none';
    document.getElementById('swatchPreview').style.display = 'none';
    document.getElementById('selectedColour').innerHTML = desc;
    var mainImage = document.getElementById('mainImage')
    mainImage.src = '/images/products/alternate/' + name;
    try {
        mainImage.style.opacity = 1;
        mainImage.filters.alpha.opacity = 100;
    } 
    catch (err) {
    }
    document.getElementById('img_' + id).style.border = '2px #868379 solid';
    if (selectedColourId > 0) {
        document.getElementById('img_' + selectedColourId).style.border = '2px #e8e2cf solid';
    }
    selectedColourId = id;
    selectedColourName = desc;
    changeSizeColour();
}

function zoomLarge(style){
    window.open('/products/zoom.asp?mode=large&style=' + style, '', 'height=475,width=475,status=no,toolbar=no,menubar=no,location=no');
}

function zoomBack(style){
    window.open('/products/zoom.asp?mode=back&style=' + style, '', 'height=475,width=475,status=no,toolbar=no,menubar=no,location=no');
}

function viewAvailability(){
    window.open('/products/availability.asp', '', 'height=400,width=400,status=no,toolbar=no,menubar=no,location=no');
}

var messageId = 0;
function changeSizeColour(){
    if (selectedColourId > 0 && selectedSize != '') {
        var status = avail[selectedSize + '|' + selectedColourName];
        var onhand = stock[selectedSize + '|' + selectedColourName];
        var rtsDate = rts[selectedSize + '|' + selectedColourName];
        var message = '';
        if (status == 0 && onhand == 0) {
            message = 'Order now for despatch in approximately ' + leadTime + ' days';
            messageId = 1;
        }
        if (status == 0 && onhand > 0) {
            if (onhand <= 5) {
                message = onhand + ' in Stock and ready for immediate despatch. <br/> More available for despatch in approximately ' + leadTime + ' days';
                messageId = 2;
            }
            else {
                message = '5+ in Stock and ready for immediate despatch. <br/> More available for despatch in approximately ' + leadTime + ' days';
                messageId = 2;
            }
        }
        if (status == 1 && onhand == 0) {
            message = 'Sold Out - this item will not be returning';
            messageId = 3;
        }
        if (status == 1 && onhand > 0) {
            if (onhand <= 5) {
                message = onhand + ' in Stock and ready for immediate despatch (limited stock)';
                messageId = 4;
            }
            else {
                message = '5+ in Stock and ready for immediate despatch (limited stock)';
                messageId = 4;
            }
        }
        if (status == 2 && onhand > 0) {
            if (onhand <= 5) {
                message = onhand + ' in Stock and ready for immediate despatch';
                messageId = 5;
            }
            else {
                message = '5+ in Stock and ready for immediate despatch';
                messageId = 5;
            }
        }
        if (status == 2 && onhand == 0) {
            if (rtsDate == 'N/a') {
                message = 'Temporarily unavailable - no return date available ';
                messageId = 6;
            }
            else {
                message = 'Temporarily unavailable - expected to return ' + rtsDate;
                messageId = 6;
            }
        }
        var availDiv = document.getElementById('divAvailability');
        if (message != '') {
            $('#divAvailability').attr('innerHTML', message).attr('className', 'availabilityMessage').slideDown("fast")
        }
    }
}

function validate(){
    if (document.style.colour.value == '') {
        alert('Please choose a colour');
        return false;
    }
    if (document.style.size.selectedIndex == 0) {
        alert('Please choose a size');
        return false;
    }
}

function selectSize(id, name){
    if (id != selectedSizeIndex) {
        $('#size_' + id).css({
            backgroundColor: '868379',
            color: '#fff'
        })
        if (selectedSizeIndex > 0) {
            $('#size_' + selectedSizeIndex).css({
                backgroundColor: 'e8e2cf',
                color: '#868379'
            })
        }
        selectedSizeIndex = id;
        selectedSize = name;
        filterColours(name);
        changeSizeColour();
    }
}

function selectSizeDropDown(){
    selectedSize = document.style.size[document.style.size.selectedIndex].value;
    changeSizeColour();
    filterColours(selectedSize);
}

function filterColours(size){
    var colourIds = colourList.split('|');
    if (colourIds.length > 2) {
        for (var j = 0; j < colourIds.length - 1; j++) {
            var swatch = document.getElementById('img_' + colourIds[j]);
            swatch.className = 'swatchSelectorUnavailable';
        }
        if (size != '') {
            var colours = bySize[size].split('|');
            for (var i = 0; i < colours.length - 1; i++) {
                var swatch = document.getElementById('img_' + colours[i]);
                swatch.className = 'swatchSelector';
            }
        }
    }
}

function viewAlternate(href){
    closeSwatchPreview();
    ga_trackEvent('Navigation/Style', 'Alternate');
    $('#mainImage').attr('src', href);
    $('#zoom a').attr('href', href);
}

var xmlhttp;
function addToBag(){
    if (selectedColourId == 0 && selectedSize == '') {
        $('#divAvailability').attr('className', 'availabilityMessageError').attr('innerHTML', 'Please select the required size and colour').slideDown("fast");
        return;
    }
    if (selectedColourId == 0) {
        $('#divAvailability').attr('className', 'availabilityMessageError').attr('innerHTML', 'Please select the required colour').slideDown("fast");
        return;
    }
    if (selectedSize == '') {
        $('#divAvailability').attr('className', 'availabilityMessageError').attr('innerHTML', 'Please select the required size').slideDown("fast");
        return;
    }
    if (messageId == 3 || messageId == 6) {
        fadeBackground('divAvailability', [255, 223, 223], [255, 255, 255], 1000);
    }
    else {
        $('#divAvailability').attr('className', 'availabilityMessage').attr('innerHTML', 'Adding item...please wait').slideDown("fast");
        $.ajax({
            type: "POST",
            url: '/ajax/basket.asp',
            data: 'styleid=' + URLEncode($('#styleid').attr('value')) + '&colour=' + URLEncode(selectedColourName) + '&size=' + URLEncode(selectedSize) + '&quantity=' + $('#selectedQuantity').attr('value'),
            complete: function(responseText, textStatus, XMLHttpRequest){
                confirmAddToBag(responseText.responseText);
            }
        });
    }
}

function confirmAddToBag(responseText){
    $('#divAvailability').attr('className', 'availabilityMessageConfirmation').attr('innerHTML', 'The selected item has been added to your <a href="/cart/basket.asp">shopping bag</a>');
    var cartSummary = document.getElementById('cartSummary');
    cartSummary.innerHTML = responseText;
}

function displayColour(colour){
    if (colour == '') {
        document.getElementById('selectedColour').innerHTML = selectedColourName + '&#160;';
    }
    else {
        document.getElementById('selectedColour').innerHTML = colour;
    }
}

function showDiscountColours(){
    $('#discountColours').slideDown('slow');
}
