jQuery(function () {
  jQuery(".product, div#productImages").each(function () {
    jQuery(this).children("a.galleryImage").ivPrepare();
  });

  bindEvents();
  readyContentRotator();
  readyGoogleMaps();
  toggleDeliveryAddress();
  
  if(Tea.Utils.getURLParam('isTest') != ''){
    jQuery('a:not([href*=isTest])').each(function(){
      var jQEle = jQuery(this),
          href= jQEle.attr('href');
      jQEle.attr('href', href + '?isTest=true');
    });
   }
});

function bindEvents() {

  //Product image zoom click
  jQuery("td#rightColumn img.productLargeImagesIcon").live("click", function () {
    jQuery(this).next().click();
  });
  //Product image click
  jQuery("img#productImage").live("click", function () {
    var productImage = jQuery(this);
    productImage.siblings("div#productImages").children('[imageId='+productImage.attr('imageId')+']').click();
  });
}

function readyContentRotator() {
  var contentRotator = jQuery(".imageRotator");
  if (contentRotator.children("img").length > 1) {
    //Setup buttons
    var contentRotatorControl = jQuery(".imageRotator").readyContentRotator().getContentRotatorControl();
    jQuery('div.imageRotatorControl img.doubleLeft').click(function () {
      contentRotatorControl.start(true);
    });
    jQuery('div.imageRotatorControl img.left').click(function () {
      contentRotatorControl.prev();
    });
    jQuery('div.imageRotatorControl img.middle').click(function () {
      contentRotatorControl.stop();
    });
    jQuery('div.imageRotatorControl img.right').click(function () {
      contentRotatorControl.next();
    });
    jQuery('div.imageRotatorControl img.doubleRight').click(function () {
      contentRotatorControl.start();
    });
  } else {
    jQuery('div.imageRotatorControl img').remove();
  }
}

function readyGoogleMaps() {

  jQuery(".googleMapsLink, .googleMapsAddress").click(function () {
    var address = "", html = "", tdContainer = jQuery(this).closest("td, div"), tdContainerClone = tdContainer.clone();
    tdContainerClone.find(".googleMapsLink").remove();
    html = tdContainerClone.html();
    tdContainer.find(".googleMapsAddress").each(function () {
      address += " " + jQuery(this).text();
    });
    var settingsshowMe = {
      onOpened: function (mwc) {
        mwc.modalContent.gMap({ markers: [{ address: address,
          html: html
        }],
          zoom: 13
        });
        //mwc.modalContent.css("display", "block");
        mwc.modalContent.append('<img class="closeModal" src="http://jscripts.teasolutions.dk/plugins/rg.jQuery.imageViewer/gfx/close.png" title="Luk Vinduet" />');
      }
    };

    jQuery('#map').html("").openModalWindow(settingsshowMe);

  });
  jQuery(".closeModal").live("click", function () {
    jQuery(this).closeModalWindow();
  });
}

function toggleDeliveryAddress() {
  var otherShippingAddress = jQuery('#otherShippingAddress');
  if(otherShippingAddress[0]){
    jQuery('#shippingDetails').css({ display: (otherShippingAddress.is(':checked') ? 'block' : 'none') });
  }
}

jQuery('#otherShippingAddress').live('click', function(){
  toggleDeliveryAddress();
});

jQuery('#miniCart:not(.isEmpty)').live('click', function() {
  window.location.href = jQuery(this).attr('cartlink');
  return false;
});

jQuery('#sharePurchase tr').live('click', function(){
  window.location.href = jQuery(this).find('a:first').attr('href');
});

