﻿/*
    Essential Sales Products
    Dynamic Effects and Functions
    Created by Climbing Turn Ltd (www.climbingturn.co.uk)
    November 15th 2010
    These functions are dependent upon jQuery 1.4.4 and Jquery UI 1.8.6
*/

/* Globals */

var rvpScrollFactor = 154;
var maxRVPLeftPos;
var formFF = 500; // form control Fade Factor timing
var smallDynFooter;
var dynFooterHeight = 69;
var dynFooterCookie = 'dfsmall';

/* Scroll the RVP List */


function scrollRVPLeft() {
    calculateMaxRVPLeft();
    var x = getRVPLeftPos();
    if (x > maxRVPLeftPos) {
        x -= rvpScrollFactor
        $('#rvpScrollContainer').animate({ left: x }, 1000, 'easeOutCirc');
    }
}

function scrollRVPRight() {
    var x = getRVPLeftPos();
    if(x < 0) {
        x += rvpScrollFactor
        if (x > 0) x = 0;
        $('#rvpScrollContainer').animate({ left: x }, 1000, 'easeOutCirc');
    }
}

function getRVPLeftPos() {
    var pos = $('#rvpScrollContainer').position();
    return pos.left;
}

function calculateMaxRVPLeft() {
    var numRVPs = Number($('#NumberOfRVPs').val()) + 1;
    maxRVPLeftPos = 1078 - (numRVPs * 154);
}

function showHideRVPNavs() {
    var numRVPs = Number($('#NumberOfRVPs').val());
    var pos = $('#rvpScrollContainer').position();
    var width = $('#rvpScrollContainer').width();
    if (numRVPs > 6 ) {
        $('#rvpScrollLeft').css('display', '');
        $('#rvpScrollRight').css('display', '');
    }
    else {
        $('#rvpScrollLeft').css('display', 'none');
        $('#rvpScrollRight').css('display', 'none');
    }
}

// remove an item from the RVP
function rvpRemove(id) {
    PVHService.RemoveItem(id, itemRemoved)
    return false;
}

// item was removed succesfully from the RVP list
function itemRemoved(result) {
    showRVP(result);
}


/* Show and hide the RVP List */

// toggles the recently viewed products
function toggleRVP(bHide) {
    if (($('#rvpContainer').css('display') == 'none') && (!bHide)) {
        var pos = $('#dynFooter').position();
        var y = pos.top - 94;
        $('#rvpDisplay').html('click to hide your recently viewed products');
        $('#rvpContainer').css('top', y + 'px');
        $('#rvpContainer').fadeIn(500);
        $('#rvpScrollWindow').fadeIn(1000);
        $('#rvpScrollWindow').html('<p>Please Wait</p>');
        PVHService.GetPVHDisplay(showRVP, RVPError);
    }
    else {
        $('#rvpDisplay').html('click to show your recently viewed products');
        $('#rvpContainer').fadeOut(500);
        $('#rvpScrollWindow').fadeOut(1000);
    }
}

function showRVP(result) {
    $('#rvpScrollWindow').html(result);
    showHideRVPNavs();
}

function RVPError(error) {
    $('#rvpScrollWindow').html(error.get_message());
}

/* Show and Hide the Dynamic Footer */

function hideFooters() {
    $('#dynFooter').hide();
    toggleRVP(true);
}

function showFooters() {
    var y = $(window).height() - dynFooterHeight + $(window).scrollTop();
    if ($('#dynFooter').length > 0) {
        var dynfooter = $('#dynFooter');
        dynfooter.css('top', y + 'px');
        dynfooter.fadeIn('slow');
    }
}

// Switches between the small and large dynamic footer
function toggleDynamicFooters() {
    hideFooters();
    if (!smallDynFooter) {
        $('#DynamicFooterContentSmall').css('display', 'none');
        $('#DynamicFooterContent').css('display', '');
        $('#dynFooter').css('width', '100%');
    }
    else {
        $('#DynamicFooterContent').css('display', 'none');
        $('#DynamicFooterContentSmall').css('display', '');
        $('#dynFooter').css('width', '60px');
    }
    showFooters();
    return false;
}

function showFooterLink_Click() {
    smallDynFooter = false;
    setDynamicFooterCookie('0');
    return toggleDynamicFooters();
}

function hideFooterLinkClick() {
    smallDynFooter = true;
    setDynamicFooterCookie('1');
    return toggleDynamicFooters();
}

function setDynamicFooterCookie(val) {
    document.cookie = dynFooterCookie + '=' + getCookie(dynFooterCookie) + '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=' + document.domain;
    var expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + 120);
    document.cookie = dynFooterCookie + '=' + escape(val) + '; expires=' + expiryDate.toUTCString() + '; path=/; domain=' + document.domain;
}

function getCookie(cookieName) {
    var i, x, y, arrCookies = document.cookie.split(";");
    for (i = 0; i < arrCookies.length; i++) {
        x = arrCookies[i].substr(0, arrCookies[i].indexOf("="));
        y = arrCookies[i].substr(arrCookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == cookieName) {
            return unescape(y);
        }
    }
}

/* Save Basket */

// Saves the basket with site id = 3 = Desktop
function saveBasket() {
    IsenLyftBasketService.SaveBasket(3, onSavebasketComplete, onSaveBasketError);
}

function onSavebasketComplete(result) {
    window.alert(result);
}

function onSaveBasketError(error) {
    window.alert(error.get_message());
}


/* Go To Checkout */

// redirects to the checkout with siteid = 3 = Desktop
function goToCheckout() {
    IsenLyftBasketService.RedirectToCheckout(3, onRedirectSuccess, onRedirectFail);
}

function onRedirectSuccess(result) {
    window.location.href = result;
}

function onRedirectFail(error) {
    window.alert(error.get_message());
}

/* Search */


function checkSearch() {
    if ($.trim($('#searchText').val()) == '') {
        alert('Please enter something to search for!');
        return false;
    } else {
        return true;
    }
}


function testForEnter(e) {
    var evt = e != null ? e : event;
    if (evt.keyCode == 13) {
        if (checkSearch()) {
            $("input[name=__EVENTTARGET]").val('ctl00$espHeader$cmdSearch');
            $('#espForm').submit();
        }
    }
}

/* Event Handlers */

$(document).ready(function () {
    smallDynFooter = getCookie(dynFooterCookie) == '1' ? true : false;
    $('#rvpDisplay').click(function () { toggleRVP(); });
    $('#rvpScrollLeft').click(function () { scrollRVPLeft(); });
    $('#rvpScrollRight').click(function () { scrollRVPRight(); });
    if ($('#dynFooter'))
        $('#dynFooter').css('position', 'absolute');
    $('#mbSaveBasket').click(function () { saveBasket(); });
    $('#mainCheckoutLink').click(function () { goToCheckout(); });
    $('#lbGoToCheckout').click(function () { goToCheckout(); });
    $('#ShowFooterLink').click(function () { return showFooterLink_Click(); });
    $('#HideFooterLink').click(function () { return hideFooterLinkClick(); });
    $('a[rel="external"]').attr('target', '_blank');
    $('#cmdSearch').click(function () { return checkSearch(); });
    $('#searchText').keydown(function () { return testForEnter(); });
    toggleDynamicFooters();
    if ($('#HeaderPrintThis').length > 0)
        $('#HeaderPrintThis').click(function () { window.print(); return false; });
});

$(window).scroll(function () {
    hideFooters();
    if (window.dynFooterTimeout) clearTimeout(window.dynFooterTimeout);
    window.dynFooterTimeout = setTimeout('showFooters()', 1000);
});

$(window).resize(showFooters);

