﻿////////////////////////////////
// Top shot animation support //
////////////////////////////////

Array.prototype.shuffle = function () {
    var s = [];
    while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
    while (s.length) this.push(s.pop());
    return this;
}

// Constants
// Changed by Aditya v1.2.0.0  [previous value of TS_ITEM_WIDTH=240;]
// Used in Top Shot Teaser for xsllayout149.xsl.
var TS_ITEM_WIDTH = 260;
var TS_DISPLAY_INTERVAL = 6000;
var TS_ANIMATION_INTERVAL = 16;

// Animation support
var requestAnimationFrame = window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
var startTime = window.mozAnimationStartTime || (new Date()).getTime();

// Flow control variables
var tsItemIndex = 0;
var tsItemOffset = TS_ITEM_WIDTH;
var tsElementIds = [];
var tsIsPaused = false;

function tsInitTopShots() {

    // Count and store animatable element ids
    $(".topShotElement").each(function (index, value) {

        // Add elements to array
        tsElementIds[index] = "#" + value.id;

    });

    // Randomize order in array
    tsElementIds.shuffle();

    for (var i = 0; i < tsElementIds.length; i++) {

        if (i == 0) {
            $(tsElementIds[i]).css("left", "0px");
            $(tsElementIds[i]).css("clip", "rect(auto, " + TS_ITEM_WIDTH + "px, auto, 0)"); // trbl
            $(tsElementIds[i]).css("visibility", "visible");
        }
        else {
            $(tsElementIds[i]).css("left", TS_ITEM_WIDTH + "px");
            $(tsElementIds[i]).css("clip", "rect(auto, 0px, auto, " + TS_ITEM_WIDTH + "px)"); // trbl
            $(tsElementIds[i]).css("visibility", "visible");
        }
    }

    if (tsElementIds.length > 1 && !tsIsPaused)
        setTimeout(function () { tsAnimate(); }, TS_DISPLAY_INTERVAL);
}

function tsAnimate() {

    if (tsItemOffset > 0 && !tsIsPaused) {

        tsItemOffset -= 5;

        $(tsElementIds[tsItemIndex]).css("left", -(TS_ITEM_WIDTH - tsItemOffset) + "px");
        $(tsElementIds[tsItemIndex]).css("clip", "rect(auto, " + TS_ITEM_WIDTH + "px, auto, " + (TS_ITEM_WIDTH - tsItemOffset) + "px)");
        $(tsElementIds[tsGetNextIndex()]).css("left", tsItemOffset + "px");
        $(tsElementIds[tsGetNextIndex()]).css("clip", "rect(auto, " + (TS_ITEM_WIDTH - tsItemOffset) + "px, auto, 0)");

        // Next step
        if (requestAnimationFrame)
            requestAnimationFrame(tsAnimate);
        else
            window.setTimeout(function () { tsAnimate(); }, TS_ANIMATION_INTERVAL);
    }
    else {
        // Reset item offset
        tsItemOffset = TS_ITEM_WIDTH;
        tsItemIndex = tsGetNextIndex();

        // Display item
        setTimeout(function () { tsAnimate(); }, TS_DISPLAY_INTERVAL);
    }
}

function tsGetNextIndex() {

    return tsItemIndex >= (tsElementIds.length - 1) ? 0 : tsItemIndex + 1;
}

//###############################################################
// Created By : Sagar
// Created On : 11.08.2011
// This function is used to check whether the string contains special characters or not
function jWDIsValidString(data) {
    var iChars = "!#%*+\\'/\"?|[]{}:<>,()";
    for (var i = 0; i < data.length; i++) {
        if (iChars.indexOf(data.charAt(i)) != -1)
            return false;
    }
    return true;
} // jWDIsValidString

//###############################################################
// Created By : Amit
// Created On : 19.12.2011
//              To allow the character as [ ]  and : in module name
function jWDIsValidModuleName(data) {
    var iChars = "!#%*+\\'/\"?|{}<>,()";
    for (var i = 0; i < data.length; i++) {
        if (iChars.indexOf(data.charAt(i)) != -1)
            return false;
    }
    return true;
} // jWDIsValidModuleName

//#################################################################
// Created By : Sagar & Aditya
// Created On : 09.06.2011
// This function is used to convert date format from dd.mm.yyyy OR dd/mm/yyyy to mm.dd.yyyy OR mm/dd/yyyy
function jWDGetFormattedDate(dateValue, dateFormat) {
    var splitDate = "";
    if (dateFormat.toLowerCase() == "dd.mm.yyyy") // for German culture
    {
        splitDate = dateValue.split(".");
    }
    else if (dateFormat.toLowerCase() == "dd/mm/yyyy") // for english culture
    {
        splitDate = dateValue.split("/");
    }
    return new Date(splitDate[2], splitDate[1] - 1, splitDate[0]);
}

//###############################################################
// Created By : Sagar & Aditya
// Created On : 09.06.2011
// This function is used to check the date difference between the two given dates
function jWDCheckDateDifference(startDate, endDate) {
    var dateDifference;
    var sDate = new Date(startDate);
    var eDate = new Date(endDate);
    dateDifference = sDate.getTime() - eDate.getTime();
    return dateDifference;
}

//###############################################################
// Created By : Binjan
// Created On : 29.06.2011
//Use regular expression for line break & carrige returns
function ReplaceLineBreakAndCarrigeReturns(text) {
    var regEx= /\n/g;
    text = text.replace(regEx, '\\n');
    regEx = /\r/g;
    text = text.replace(regEx, '\\r');
    /*text = text.replace("\\", "\\\\\");*/
    return text;
}

//////////////////////////
// Font scaling support //
//////////////////////////

var g_index = 0;
var g_sizes = ['1em', '1.25em', '1.5em', '1.75em', '2.0em'];

//////////////////////////////////////////////
// Increase or decrease font scaling factor //
//////////////////////////////////////////////
function jWDZoomFont(magnify) {
    // Current font index
    var oldIndex = g_index;

    if (magnify == true) {
        if (g_index < (g_sizes.length - 1)) {
            g_index++;
        }
    }
    else {
        if (g_index > 0) {
            g_index--;
        }
    }

    // Change applies
    if (g_index != oldIndex) {
        jWDSetFontIndex(g_index);
        jWDSetFontImageStatus(g_index);
    }
} // jWDZoomFont

////////////////////////////////
// Update font session cookie //
////////////////////////////////
$(window).load(function () {
    if (document.cookie) {
        var i = document.cookie.indexOf("fontIndex");

        if (i >= 0) {
            g_index = parseInt(document.cookie.charAt(i + 10));

            jWDSetFontIndex(g_index);
            jWDSetFontImageStatus(g_index);
        }
        else {
            g_index = 0;
        }
    }
});

///////////////////////////////
// Set new font index in css //
///////////////////////////////
function jWDSetFontIndex(index) {

    var wideContainerCol = $("#wideContainerCol");
    var teaserCol = $("#teaserCol");
    var containerCol = $("#containerCol");

    if (wideContainerCol)
        wideContainerCol.css("font-size", g_sizes[g_index]);

    if (teaserCol)
        teaserCol.css("font-size", g_sizes[g_index]);

    if (containerCol)
        containerCol.css("font-size", g_sizes[g_index]);

    // Update cookie
    // path=/ is set in cookie, by doing this if we switch from one language to another
    // we get same cookie.
    document.cookie = 'fontIndex=' + index + ';path=/';
} // jWDSetFontIndex

////////////////////////
// Update font images //
////////////////////////
function jWDSetFontImageStatus(index) {

    var zoom_dec = $("#zoom_dec");
    var zoom_inc = $("#zoom_inc");

    if (index == 0) {
        // Disable negative zoom
        if (zoom_dec)
            zoom_dec.attr("src", "/img/font_zoom_dec_inactive.gif");
    }
    else {
        // Enable negative zoom
        if (zoom_dec)
            zoom_dec.attr("src", "/img/font_zoom_dec_active.gif");
    }

    if (index == (g_sizes.length - 1)) {
        // Disable positive zoom
        if (zoom_inc)
            zoom_inc.attr("src", "/img/font_zoom_inc_inactive.gif");
    }
    else {
        // Enable positive zoom
        if (zoom_inc)
            zoom_inc.attr("src", "/img/font_zoom_inc_active.gif");
    }

} // jWDSetFontImageStatus

//###############################################################
// Created By : Amit
// Created On : 13.12.2011
// Replace the last character \ from string with blank
function jWdReplaceLastCharacter(text) {
    if (text.charAt(text.length - 1) == '\\')
        text = text.replace(/\\+$/, '');
    return text;
} // jWdReplaceLastCharacter

