﻿var timeout = 0;
var timeout1 = 0;
$(document).ready(function() {
  $('.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();
      }
    });
  });

  $('.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');
  });
});

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 = '';
    //Ongoing/OOS
    if (status == 0 && onhand == 0) {
      message = 'Available for despatch in approximately ' + leadTime + ' days'; messageId = 1;
    }
    //Ongoing/In Stock
    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;
      }
    }
    //Fashion/OOS
    if (status == 1 && onhand == 0) {
      message = 'Sold Out - this item will not be returning'; messageId = 3;
    }
    //Fashion/In Stock
    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;
      }
    }
    //TempUnavail/In Stock
    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;
      }
    }
    //TempUnavail/OOS
    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")
    }
    //	    if ( messageId==3 || messageId==6){
    //	        $('#divAvailability').attr('innerHTML',message).attr('className','availabilityMessageError').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';
      //swatch.alt = swatch.name + ' (SOLD OUT in selected size)';
    }
    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';
        //swatch.alt = swatch.name;
      }
    }
  }
}
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'),
      //error:function (request,error) {
      //    alert(error);
      //},
      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; //xmlhttp.responseText;
  fadeBackground('cs2', [220, 252, 210], [255, 255, 255], 1500);
}
function displayColour(colour) {
  if (colour == '') {
    document.getElementById('selectedColour').innerHTML = selectedColourName + '&#160;';
  } else {
    document.getElementById('selectedColour').innerHTML = colour;
  }
}

function showDiscountColours() {
  $('#discountColours').slideDown('slow');
}