﻿// ****** Script for the highlights usercontrol rotator ******* //

var jsTimer = null;
var bDoingFade = false;

function ButClickedPlayPause() {
    if ($("img.hlPause").length > 0) {

        ShowPlayButton();
    }
    else {
        ShowPauseButton();
    }
}

function ShowPlayButton() {

    var pauseBut = $("img.hlPause");
    if (pauseBut.length > 0) {
        // currently showing the pauseButton
        hlStopTimer();
        var imgPath = pauseBut.attr('src').replace('pause', 'play');
        pauseBut.attr("src", imgPath);
        pauseBut.removeClass("hlPause").addClass("hlPlay");
    }
}

function ShowPauseButton() {
    var playBut = $("img.hlPlay");
    if (playBut.length > 0) {
        // currently showing the playbutton
        hlStartTimer();
        var imgPath = playBut.attr('src').replace('play', 'pause');
        playBut.attr("src", imgPath);
        playBut.removeClass("hlPlay").addClass("hlPause");
    }
}

function ButClicked(targetItem) {
    if (bDoingFade == true) {
        return;
    }

    bDoingFade = true;
    hlStopTimer();
    GoToImage(targetItem);
    ShowPlayButton();
}

function GoToImage(targetItem) {
    var currentDiv = $("div.hlSelected");
    var currentBut = $("img.hlSelected");
    var targetDiv = $("#highlightDiv" + targetItem);
    var targetBut = $("#highlightButton" + targetItem);


    // swap images on the buttons
    var currentImgSRC = currentBut.attr('src').replace('Buttonon', 'Buttonoff');
    var targetImgSRC = targetBut.attr('src').replace('Buttonoff', 'Buttonon');
    currentBut.attr("src", currentImgSRC);
    targetBut.attr("src", targetImgSRC);


    targetDiv.css("z-index", 10);
    targetDiv.fadeIn("slow", function() {
        targetDiv.css("opacity", "0.97");
        currentDiv.fadeOut("slow");
        currentDiv.css("opacity", "0.0");
        currentDiv.css("z-index", 9);
        bDoingFade = false;
    }
);

    currentBut.removeClass("hlSelected").addClass("hlUnselected");
    currentDiv.removeClass("hlSelected").addClass("hlUnselected");
    targetBut.removeClass("hlUnselected").addClass("hlSelected");
    targetDiv.removeClass("hlUnselected").addClass("hlSelected");
}

function GoToNextImage() {
    // get max number
    var maxValue = $("#hlCount").val();
    var intMaxValue = parseInt(maxValue, 10);

    // get the current DIV
    var currentDiv = $("div.hlSelected");
    var curItemId = currentDiv.attr("id");
    var itemNumber = curItemId.substring(curItemId.length - 1);
    var intValue = parseInt(itemNumber, 10);

    if ((intValue + 1) == intMaxValue) {
        GoToImage(0);
    }
    else {
        GoToImage(intValue + 1);
    }
    hlStartTimer();
}

// do the javascript timer
function hlStartTimer() {
    var maxValue = $("#hlCount").val();
    if (maxValue <= 1) {
        return;
    }

    if (jsTimer != null) {
        hlStopTimer();
    }

    jsTimer = setTimeout("GoToNextImage ()", 5000);
}

function hlStopTimer() {
    if (jsTimer != null) {
        clearTimeout(jsTimer);
        jsTimer = null;
    }
}
$("img.hlSelected").click(function(e) { 
    e.preventDefault(); }); 
