﻿// JScript File

var debug = false;

$(document).ready(function (){
    
    // initialise hint.js
    $('input:text').hint();
    bindSearchButtons();
    
    // init wide ad at top of page rotation
    if($('#offerStripTop').find('a').length > 0){
        $('#offerStripTop').show();
        $('#offerStripTop').scrollable({circular: true, vertical: true}).autoscroll({ autoplay: true, autopause: true, interval: 6000 });
    } 
    
    // init home slideslow
    $(".slidetabs").tabs(".images > a", {
	    effect: 'fade',
	    fadeOutSpeed: "slow",
	    rotate: true
    }).slideshow({	    
        autoplay: true,
	    clickable: false
	});

    $(".productTabs").tabs(".productDetailTabs > div");
    
    ProductLinks();
    
    $('.zoomImage').colorbox({opacity:0.5});
    
    // SHARE WIDGET JS
    $('#favourites').jFav();

    $('#stafBtn, .close').click( function(event) {
        $('#sendToAFriendForm').slideToggle();
        event.preventDefault();
    });
    
    //$('a#seenItCheaper').colorbox({inline:true, href:"#cheaper"});
    $('#cheaper').jqm({modal: true, trigger: 'a#seenItCheaper'});
    
    
    if($('a[name*="error"]').length > 0){
        errorToggle();
    }
    
    $('#prodThumbs a').click( function(event) {
        imgSwap(this);
        event.preventDefault();
    });
    
    //if($('.prefDate').length > 0){
        $('.prefDate').datepicker({ minDate: "+1D", maxDate: "+1M", dateFormat: "DD, d MM, yy", beforeShowDay: $.datepicker.noWeekends, firstDay: 1 });
   // }

});

function imgSwap(prodImg){

    var img = $(prodImg).attr('href'); 
    $('#prodImg').attr({src: img});
    $('.zoomImage').attr({href: img});
   

}

function errorToggle(){
    $('#basketErrorInner').delay(100).slideDown(800, 'easeOutElastic');
    $('.closeError').bind('click',function(){
        $('#basketErrorInner').slideToggle(500, 'easeInOutBack');
    });
}
    
function ProductLinks()
{
    if($('.priceAddBasket').find('a').length > 0){
        log("Assigning add to basket links");
        $(".priceAddBasket a").unbind('click');
        $(".priceAddBasket a").click(function (event) { 
            // stop page refresh
            event.preventDefault();
            // get the quantity input box
            var input = $(this).siblings("input");
            // get the value from the input
            var value = input.val();
            // change to a number
            value = Number(value);
            if (isNaN(value))
            {
                // cannot be changed to a number set to 1
                value = 1;
            }
            //log("Clicked product link, quantity is " + value + " product id is " + iProductID);
            AddToBasketWithQty($(this).attr("rel"), value); // exists in basket overview.ascx
        });
    } else
    {
       log("No add to basket links on this page.");
    }
}

// used by myaccount
function AddToBasket(iProductID)
{
    AddToBasketWithQty(iProductID, 1);
}

// used by everything else
function AddToBasketWithQty(iProductID, iQty)
{
    log("AddToBasketWithQty: " + iProductID + ", " + iQty);
    Sagittarius.Webservices.Shop.ShopService.AddToBasket(iProductID, iQty, "", "", "", OnSucceeded2, OnFailed2);
}

//used by quick order for multi items
function AddMultipleItemsToBasket(sProdQtyCSV){
    log("AddMultipleItemsToBasket: " + sProdQtyCSV);
    Sagittarius.Webservices.Shop.ShopService.AddMultiplesToBasket(sProdQtyCSV, OnSucceeded2, OnFailed2);
}

function OnSucceeded2(result, userContext, methodName){

    log("AddToBasketWithQty: OKAY");

    document.getElementById("ttlPrice").innerHTML = result.TotalExcVatExcShipping.toFixed(2).toString();
    document.getElementById("ttlPriceInc").innerHTML = result.TotalIncVatExcShipping.toFixed(2).toString();
    document.getElementById("ttlPrice").title = "Inc. VAT: " + result.TotalIncVatExcShipping.toFixed(2).toString();
    document.getElementById("itemCount").innerHTML = "Items: " + result.TotalQuantity.toString();
    
    $().colorbox({inline:true, href:"#shoppingModal", opacity:0.5});

    // Close Button Highlighting. IE doesn't support :hover. Surprise?
    /*
    $('input.jqmdX')
    .hover(
    function(){ $(this).addClass('jqmdXFocus'); }, 
    function(){ $(this).removeClass('jqmdXFocus'); })
    .focus( 
    function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
    .blur( 
    function(){ $(this).removeClass('jqmdXFocus'); });
    */
    
}
    
function OnFailed2(error){
    log("AddToBasketWithQty: FAIL");
    var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();
    
    log("AddToBasketWithQty: Stack Trace: " +  stackTrace + "<br/>" + "Service Error: " + message + "<br/>" + "Status Code: " + statusCode + "<br/>" + "Exception Type: " + exceptionType + "<br/>" + "Timedout: " + timedout);

    // Display the error.
    var divError = document.getElementById("divError");
    divError.style.display = "block";
    divError.innerHTML =  "Stack Trace: " +  stackTrace + "<br/>" + "Service Error: " + message + "<br/>" + "Status Code: " + statusCode + "<br/>" + "Exception Type: " + exceptionType + "<br/>" + "Timedout: " + timedout;
}

function log(a) {
    if (debug == false) { return; }
    try {
        // Firebug or IE console logging
        console.log(a);
    } catch (er) {
        try {
            // opera built in logging
            window.opera.postError(a);
        } catch (er) {
            // log to area underneath the body
            try {
                var p = document.createElement("p");
                p.innerHTML = a;
                document.body.appendChild(p);
            } catch (er) {
                // Cannot log it so stop.
            }
        }
    }
}

function resetSTAFForm(tbName, tbEmail, tbFriendName, tbFriendEmail, tbMessage)
{
    $('#emailToFriend #pnlMain').attr('style', 'display:block');
    $('#emailToFriend #pnlThanks').attr('style', 'display:none');
    
    $(".sendToAFriendForm input").val('');
    $('input:text').hint();
    $('textarea').hint();
}

function stoafMsgSent()
{
    alert("message send");
}
