﻿/** DOM-READY ****************************************************************************/
$(document).ready(function () {
    fields();
    tooltip();
    defaultfields();
    ShoppingBag();
    removelink();
    closeBagNow();
    IE9fix();
});
var IE6 = ($.browser.msie && $.browser.version == 6);
/** [END] DOM-READY **********************************************************************/
/** ALL FIELDS IN SITE *******************************************************************/
function fields() {
    $('input[type="text"], textarea, input[type="password"]').addClass("field");
    $('input[type="text"], textarea, input[type="password"]').focus(function () {
        $(this).removeClass("field").addClass("focusfield");
    });
    $('input[type="text"], textarea, input[type="password"]').blur(function () {
        $(this).removeClass("focusfield").addClass("field");
    });
}
/** [END] ALL FIELDS IN SITE *************************************************************/
/** FIELDS WITH DEFAULT VALUE ************************************************************/
function defaultfields() {
    $(".cleardefault").focus(function () {
        if (this.value == this.defaultValue) {
            this.value = '';
        } 
    });
    $(".cleardefault").blur(function () {
        if ($.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        } 
    });
}
/** [END] FIELDS WITH DEFAULT VALUE ******************************************************/
/** ToolTip ******************************************************************************/
function tooltip() {
    link_title("a.tip", "tooltip");
}
function link_title(target_items, name) {
    if (typeof (_runningFromMicrosite) != "undefined") {
        $(target_items).each(function (i) {
            $("body").append("<div class='mainSiteContent'><div class='" + name + "' id='" + name + i + "'><p>" + $(this).attr('title') + "</p></div></div>");
            var title = $("#" + name + i);
            $(this).removeAttr("title").mouseover(function () {
                title.css({ opacity: 0.8, display: "none" }).fadeIn(400);
            }).mousemove(function (kmouse) {
                title.css({ left: kmouse.pageX + 15, top: kmouse.pageY - 20 });
            }).mouseout(function () {
                title.fadeOut(400);
            });
        });
    } else {
        $(target_items).each(function (i) {
            $("body").append("<div class='" + name + "' id='" + name + i + "'><p>" + $(this).attr('title') + "</p></div>");
            var title = $("#" + name + i);
            $(this).removeAttr("title").mouseover(function () {
                title.css({ opacity: 0.8, display: "none" }).fadeIn(400);
            }).mousemove(function (kmouse) {
                title.css({ left: kmouse.pageX + 15, top: kmouse.pageY - 20 });
            }).mouseout(function () {
                title.fadeOut(400);
            });
        });
    } 
}
/** [END] ToolTip ************************************************************************/
/** Ajax Get *****************************************************************************/
function populateElement(source, open, target, close, div) {
    var url = source;
    $(div).html('<div class="bg_load"><p class="loadPop"><span class="display">Loading...</span></p></div>');
    $.get(url, function (data) {
        var startIndex = data.indexOf(open + target);
        var endIndex = data.lastIndexOf(close);
        cache: true;
        $(div).html(data.substring(startIndex, endIndex + close.length));
    });
}

var basketOldCount = 0;
function GetSingleElement(source, element, target) {
    var basketNewCount = GetSubCookieValue('Basket', 'Indy.Basket.BasketCount');
    if (basketOldCount != basketNewCount) {
        $(target).html('<div class="bg_load"><p class="loadPop"><span class="display">Loading...</span></p></div>');
        $(target).load(source + ' ' + element);
        basketOldCount = basketNewCount;
    }
}

/** [END] Ajax Get ***********************************************************************/
/** Scroll text  *************************************************************************/
function scrollWindow() {
    if ($.browser.opera) { $('html').animate({ scrollTop: $(scrollTo).offset().top }, speedscroll); }
    else $('html,body').animate({ scrollTop: $(scrollTo).offset().top }, speedscroll);
}
/** [END] Scroll text  *******************************************************************/
/** Open Bag   ***************************************************************************/
function ShoppingBag() {
    if (IE6) {
        $(".openBag").click(
   function () {
       $(".placeholderDropdown").css('display', 'block');
   });
    } else {
        $(".openBag").click(
   function () {
       $(".placeholderDropdown").css('display', 'block');
   });
    } 
} function closeNow() { $(".placeholderDropdown").css('display', 'none'); }
function closeBagNow() {
    if (IE6) { $('.placeholderDropdown').css('display', 'block'); }
    else {
        $('.placeholderDropdown').mouseout(function () {
            $('.placeholderDropdown').fadeTo(duration, 1).fadeOut('slow');
        });
    }

}
/** [END] Open Bag   *********************************************************************/

/** Fix for IE9 adding huge amounts of space above the footer on product pages   *********/
function IE9fix() {                                                             // <3 IE
    if ($.browser.msie && $.browser.version > 7) {                              // IE9 emulates IE8
        $('#container').css('position', 'static').css('position', 'relative');  // makes no sense, but it works
    }
}
/** [END] IE9 fix   **********************************************************************/

/** Remove Shopping Bag Call   ***********************************************************/
function removelink() {
    if (typeof (_runningFromMicrosite) != "undefined") {
        $('.removeIf.openBag').removeClass('openBag').addClass('bagLink');
        $(".removeIf").attr("href", BaseURL + "/shopping_bag/ShoppingBag.aspx");
    } 
}
/** [END] Remove Shopping Bag Call   *****************************************************/

/* START - Liveclicker video */

function LiveclickerVideoTab() {
    $(".image").click(function () {
        var ProductImageClass = ".productimage_" + this.className.split(' ')[2];
        var PlayerboxClass = ".playerbox_" + this.className.split(' ')[2];
        $(ProductImageClass).show();
        $(PlayerboxClass).hide();

    });

    $(".tab").click(function () {

        var productId = this.className.split(' ')[2];
        for (var i = 1; i < $(".tab").length; i++) {
            var tabselectionclass = $(".tab")[i].className.split(' ');

            if (tabselectionclass.length >= 3 && tabselectionclass[2] == productId) {
                if (tabselectionclass[1] == "video") {
                    $(".tab")[i].className = "tab video " + productId + " tabdeselected";
                }
                else if (tabselectionclass[1] == "image") {
                    $(".tab")[i].className = "tab image " + productId + " tabdeselected";
                }
            }

        }

        $(this).removeClass("tabdeselected");
        $(this).addClass("tabselected");
    });

    $(".video").click(function () {
        var ProductImageClass = ".productimage_" + this.className.split(' ')[2];
        var PlayerboxClass = ".playerbox_" + this.className.split(' ')[2];
        $(ProductImageClass).hide();
        $(PlayerboxClass).show();

    });

    $('div.playerbox').each(function () {
        if ($(this).find('div.Liveclicker_video').length > 0 || $(this).find('iframe').length > 0) {
            $(this).show();
        }
    });

    SwatchImageHandling();
}

function DisplayLiveClickerVideo() {
    for (var i = 1; i < $(".tab").length; i++) {
        var productID = $(".tab")[i].className.split(' ')[2];
        var liveclickerclass = ".playerbox_" + productID;
        var ProductImageClass = ".productimage_" + productID;
        var tabcontainerclass = ".tabscontainer_" + productID;

        if ($(liveclickerclass).find(".Liveclicker_video").length > 0) {
            $(tabcontainerclass).show();
        }
        else {
            $(ProductImageClass).show();
        }
    }
}

function SwatchImageHandling() {
    $(".swatches").click(function () { // Show the product image as per swatch image selection

        var productId = this.className.split(' ')[1];
        var outfitSwatchFlag = "0";
        for (var i = 1; i < $(".tab").length; i++) {
            var tabselectionclass = $(".tab")[i].className.split(' ');

            if (tabselectionclass.length >= 3 && tabselectionclass[2] == productId) {
                outfitSwatchFlag = "1";
                if (tabselectionclass[1] == "video") {
                    $(".tab")[i].className = "tab video " + productId + " tabdeselected";
                }
                else if (tabselectionclass[1] == "image") {
                    $(".tab")[i].className = "tab image " + productId + " tabselected";
                }
            }

        }

        var ProductImageClass = ".productimage_" + this.className.split(' ')[1];
        var PlayerboxClass = ".playerbox_" + this.className.split(' ')[1];
        $(ProductImageClass).show();
        $(PlayerboxClass).hide();

    });
}
/* END - Liveclicker video */

/* START - Liveclicker-Omniture v0.2 */

var OMTRcustomEventTag = 'Customer/Liveclicker Player';
var currentPlayer;

var timer_set = 0;
var sentMessageStarted = 0;
var sentMessage10PercentCompletion = 0;
var sentMessage90PercentCompletion = 0;
var sentMessageFullCompletion = 0;
var whatsupcounter = 1;
var lengthInSeconds = 30; // default. this value is changed on player loaded 

function onLCPlayerLoaded(player) {
    currentPlayer = player;
    lengthInSeconds = currentPlayer.getSettings().totalTime; // resets the proper play time 
}

function openMovie(productID, videoName, lengthInSeconds) {
    // alert('Sending "video open" message to Omniture, video name: ' + videoName + ' - length: ' + lengthInSeconds + ' - Event tag: ' + OMTRcustomEventTag);
    s.products = ";" + productID;
    s.Media.open(videoName, lengthInSeconds, OMTRcustomEventTag);
    s.Media.play(videoName, 0);
}

function endMovie(productID, videoName, lengthInSeconds) {
    // alert('Sending "video ended" message to Omniture');
    s.products = ";" + productID;
    s.Media.stop(videoName, lengthInSeconds);
    s.Media.close(videoName);
}

function update_timer() {
    var time;
    var timeinvideo;
    var videoLength = lengthInSeconds;
    try {
        time = Number(currentPlayer.getSettings().playTime);
    }
    catch (e) {
    }

    if ((time != undefined) && (time > 0) && (videoLength > 0)) {
        var percentComplete = time / videoLength;
        if (sentMessageStarted == 0) {
            sentMessageStarted = 1;
            openMovie(productID, videoName, videoLength);
        }
        if ((sentMessageFullCompletion == 0) && ((percentComplete) > 0.98)) {
            endMovie(productID, videoName, videoLength);
            sentMessageFullCompletion = 1;
        }
    }
}

setTimeout(function () { window.setInterval('update_timer()', 500); }, 1000);

/* END - Liveclicker-Omniture v0.2 */
