﻿      $.noConflict();
      jQuery.noConflict();
      
      var pModal;
      var productAutoComplete;
      var ctrl;
      /*window.onerror=function(msg, url, linenumber){
			// invalid character error fix
			alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
			return true
			}*/
      jQuery(document).ready(function(jQuery) {

          jQuery("#productImage").css('visibility', 'visible');
          jQuery("div[id$='zoomProductImage']").css('left', '-1000px');


          var opts = {
              'width': '50%',
              'height': '50%',
              'autoScale': false,
              'transitionIn': 'none',
              'transitionOut': 'none',
              'type': 'iframe',
              'titleShow': false
          }

          var helpOpts = {
              'autoScale': false,
              'transitionIn': 'none',
              'transitionOut': 'none',
              'titleShow': false
          }
          
          try {
              jQuery("#needHelp").fancybox(helpOpts);
              jQuery("#deliveryInfo").fancybox(opts);
              jQuery("#returnsPolicy").fancybox(opts);
              jQuery("#forgotPassword").fancybox(opts);
              ShippingButtonsOnClick();
          }
          catch (e) {

          }
          try {
              productAutoComplete = new AutoComplete('ctl00_mnuPrimary_tbSearch', 'SearchAutoCompletePopup', 'ctl00_mnuPrimary_btnSearch', '/Ajax/ProductService.asmx/GetSuggestions');
              jQuery('.searchBox input:first').click(function() { if (this.value.toLowerCase() == 'search') this.value = ''; });

          }
          catch (e) {
              //alert(e.message);
          }

          jQuery(function(jQuery) {
              jQuery('.currencyModalLink').click(function(e) {
                  e.preventDefault();
              });
          });
      }
      );
      
    function CreateDelegate(obj, func)
    {
    
        return function() {
            return func.apply(obj, arguments);
        }
    }

      // TODO: Merge to javascript file
      function ShowSubMenu(id)
      {
            var divId = id.toString().concat('_sub');
            if (!jQuery('#' + divId).is(':visible'))
            {
                jQuery('#' + divId).removeClass('contracted');
                jQuery('#' + divId).css('display','none');
                Effect.BlindDown(divId,{ duration: 0.5 });
            }else{
                Effect.BlindUp(divId,{ duration: 0.5 });
            }
      }
     
    function DoFunction(func)
    {
        if (ctrl == null)
        {
            ctrl = new Controls('test');
        }
        ctrl.SetFunction(func);
        ctrl.Show();
    }
      
    function Controls(name)
    {
        this.name = name;
        this.currentFunction;
        this.Revert = function() { RevertAll();};
        this.Show = function() 
        {   
            switch (this.currentFunction)
            {
                case 'zoom' : ShowZoom();
                break;
                case '360' : Show360();
                break;
            }
        } 
        this.SetFunction = function(_function) { this.currentFunction = _function;} 
    }
     
     
        function Scale(url)
        {
            Enlarge(url);
        }
     
    function RevertAll()
    {
        // set all elements back to normal state
        jQuery("#productImage").css('display','block');
        jQuery("div[id$='zoomProductImage']").css('left', '-1000px');
        jQuery('#EnlargeProductImage').css('display','none');
        jQuery("#productImage").css('visibility','visible');
        jQuery("#product360").css('display','none');
        jQuery("#productDetails").css('visibility','visible');
        jQuery("div[id$='zoomProductImage']").css('top', '0px');
        jQuery("#ctl00_contentPlaceHolderListing_ctrlProduct_imgProduct").css("display","block");
        jQuery("#closeButton").css('display','none');
    }

    function ShowZoom()
    {
        jQuery("#ctl00_contentPlaceHolderListing_ctrlProduct_imgProduct").css("display","block");
        
        jQuery("div[id$='zoomProductImage']").css('left', '27px');
        jQuery("div[id$='zoomProductImage']").css('top', '-12px');
        jQuery("#productDetails").css('visibility','hidden');
        jQuery("#productImage").css('visibility','hidden');
        jQuery('#EnlargeProductImage').css('display','none');
        jQuery("#closeButton").css('display','block');
    }
    
    function Show360()
    {
    
        jQuery("#ctl00_contentPlaceHolderListing_ctrlProduct_imgProduct").css("display","none");
        jQuery("#productImage").css('display','none');
        jQuery("div[id$='zoomProductImage']").css('left', '-1000px');
         jQuery("div[id$='zoomProductImage']").css('displa', 'none');
        jQuery('#EnlargeProductImage').css('display','none');
        jQuery("#product360").css('display','block');
        jQuery("#closeButton").css('display','block');
        jQuery("#productDetails").css('visibility','visible');
    }
          
      function Enlarge(url)
      {
        if (ctrl != null)
        {
           ctrl.Revert();
        }
        
        jQuery('#EnlargeProductImage').attr('src',url);

        if (!jQuery('#EnlargeProductImage').is(':visible'))
        {
            jQuery("#ctl00_contentPlaceHolderListing_ctrlProduct_imgProduct").css("display","none");
            jQuery('#EnlargeProductImage').toggle();
            jQuery('#normalProductImage').toggle();
            jQuery("#closeButton").css('display','block');
            jQuery("#productImage").css('visibility','visible');
        }
        
      }
      
      function ChangeUserType(userType)
      {
          jQuery('#userType .box').css('display','none');
          jQuery('#' + userType + '.box').css('display','block');
      }
      
      function SelectUserType()
      {
            ChangeUserType(jQuery('#userTypes :checked').val());
      }
      
      function CheckIfPayPal()
      {
        // not using paypal for go live
        /*if (jQuery('#paymenttyperadio input:radio[checked=true]').val().toLowerCase() == "rbpaypal")
        {
            DisableCard();
        }*/
      }
      
      function IsBillingShippingAddress()
      {     
            if (jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewShippingAddressFlag').val() == "1")
            {
                jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewAddressDiv').removeClass('hide'); 
            }else{
                jQuery('#shippingDetails').css('display','none'); 
            }
      }

      function IsANewDeliveryAddress(_val)
      {
         var display = (_val == '0') ? 'none' : 'block' ;
         
         jQuery('#shippingDetails').css('display',display); 
         
         //flag the new address shipping
         jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewShippingAddressFlag').val(_val);
      }

      function UpdateAdress(obj) {
          debugger;
            var address = jQuery.parseJSON(obj.value);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_label2').val(address.name);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_ddl_title2').val(address.title)
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_first_name2').val(address.fname);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_last_name2').val(address.lname);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_ddl_country2').val(address.countryid);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_post_zip_code2').val(address.postcode);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_city2').val(address.city);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_county2').val(address.county); 
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_address_line12').val(address.address1);
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_txt_address_line22').val(address.address2);
            
            // this means the user has selected a previous address
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewShippingAddressFlag').val('0');
         

      }
  
    function IsNewAddress(bln)
    {
        var newAddress = (bln == true) ? '1' : '0';
        jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewShippingAddressFlag').val(newAddress);
        var shippingDetails = (bln == true) ? 'block' : 'none';
        jQuery('#shippingDetails').css('display',shippingDetails); 
    }
    
      function HideDiv(divId)
      {
        jQuery('#' + divId).css('display','none'); 
      }
          
      function ShowDiv(divId)
      {
        jQuery('#' + divId).css('display','block'); 
      }
      
      function ShippingButtonsOnClick()
      {
        // if a user clicks on a previous address set the flag
        jQuery('#ShippingButtons input[type=radio]').click(function() 
        {
            jQuery('#ctl00_contentPlaceHolderListing_coShipping_NewShippingAddressFlag').val('0');
        });
    }

    function EnableDeliveryOptions(enable, rdo_delivery_address_no_ClientID) {
        if (enable) {
            ShowDiv("delivery");
            ShowDiv("deliveryOptions");
            var rdo_delivery_address_no = document.getElementById(rdo_delivery_address_no_ClientID);
            if (rdo_delivery_address_no.checked) {
                ShowDiv("shippingDetails");
            }
        }
        else {
            HideDiv("deliveryOptions");
            HideDiv("delivery");
            HideDiv("shippingDetails");            
        }
    }
      
    function PopUpImage()
    {
        if (pModal)
            pModal.hide();
        pModal = new modal();
        pModal.afterShow( function () { jQuery('#popUpImage').css('display','block'); });
        pModal.afterHide( function () { jQuery('#popUpImage').css('display','none'); });
        pModal.show();
    }

    function PopUpBespokeInsideImage() {
        jQuery("#lnkShowBespokeInside").click();
    }

    function PopUpBespokeInspirationImage() {
        jQuery("#lnkShowBespokeInspiration").click();
    }
    
    function PopUpVideo(videoPath)
    {
        pModal = new modal();
	    pModal.afterShow( function () 
	    { 
	    
	    var FlashVideo = "<div id='popUpVideo' style='display: none; z-index: 6110'></div>";
	    jQuery(FlashVideo).appendTo('#content_large');
	    
	    jQuery('#popUpVideo').css('display','block'); 
	 
	    var fo = new FlashObject(videoPath, "popUpVideo", "480", "270", "0", "");
        fo.addParam("style", "z-index:9999");
        fo.addParam("wmode", "transparent");
        fo.addParam("allowScriptAccess", "always");
        fo.write("popUpVideo");
        
	    });
	    pModal.afterHide( function () { jQuery('#popUpVideo').css('display','none'); });
        pModal.show();
    }
    
    function SignUpSuccessful()
    {
        jQuery('.hide').toggle();
    }
    
    function Popup(typeOf)
    {
        jQuery('#basketToolTip').fadeIn(2000, function() { jQuery('#basketToolTip').fadeOut(5000); })
    }

    function confirmCurrencyChange(callback) {

        //var position = $("#middle").offset();
        
        jQuery('#confirm').modal({
            closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
            overlayId: 'confirm-overlay',
            containerId: 'confirm-container',
            appendTo:"#wrapper",
            autoPosition: false,
            position: "fixed",
            onShow: function(dialog) {
                var modal = this;
                dialog.container.css({ position: 'absolute', top: '10px', left: '180px' });
                // if the user clicks "yes"
                jQuery('.yes', dialog.data[0]).click(function() {
                    // call the callback
                    if (jQuery.isFunction(callback)) {
                        callback();
                    }
                    // close the dialog
                    modal.close(); // or $.modal.close();
                });
            }
        });
    }

    function showCurrencyModal() {
        // example of calling the confirm function
        // you must use a callback function to perform the "yes" action
        confirmCurrencyChange(function() {
        jQuery("#ctl00_Footer_lnkCurrencyChanger").click()
        });
    }

    function updateDivHeight() {

        var divh = document.getElementById('ctl00_MainContentPlaceHolder_ImageMap1').style.height;

        jQuery("#bespoke_content_right").css('min-height', divh);
    }
