﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="jquery-ui-1.8.7.custom.min.js" />
/// <reference path="jquery.tools.min.js" />

function stopRotator(refreshIntervalId) {
    //alert('Stopping ' + refreshIntervalId.toString());
    clearInterval(refreshIntervalId);
}

function startRotator(pnlRotatorClientID, animateInterval, lblItemNoClientID, lnkItemNameClientID, headCSSInfo, refreshInterval) {
//    alert('pnlRotatorClientID: ' + pnlRotatorClientID);
//    alert('lblItemNoClientID: ' + lblItemNoClientID);
//    alert('lnkItemNameClientID: ' + lnkItemNameClientID);
//    alert('headCSSInfo: ' + headCSSInfo);

    var pnlRotator = $("#" + pnlRotatorClientID).eq(0);
    var divRotator = $(pnlRotator).children().eq(0);
    if ($(divRotator).children().hasClass('show')) {
    }
    else {
        // This means we are on the first time through
        var divDetails = $(divRotator).children().first();
        $(divDetails).addClass('show')
    	    .animate({ opacity: 1.0 }, animateInterval);
        //        $('div.rotator div:first').removeClass('hide')
        //            .addClass('show')
        //	        .animate({ opacity: 1.0 }, animateInterval);
        rotate(pnlRotatorClientID, animateInterval, lblItemNoClientID, lnkItemNameClientID, headCSSInfo);
    }

    var currentInterval = setInterval("rotate('" + pnlRotatorClientID + "', " + animateInterval + " , '" + lblItemNoClientID + "', '" + lnkItemNameClientID + "' , '" + headCSSInfo + "')", refreshInterval);
    //alert('Starting ' + currentInterval.toString());
    return currentInterval;
}

function rotate(pnlRotatorClientID, animateInterval, lblItemNoClientID, lnkItemNameClientID, headCSSInfo) {
    // This is information from image_rotator.ascx
    //var pnlRotator = document.getElementById(pnlRotatorClientID);
    var pnlRotator = $("#" + pnlRotatorClientID).eq(0);
    var divRotator = $(pnlRotator).children().eq(0); // same as $(pnlRotator).children().first();
    var divDetails = $(divRotator).find('.show');
    //var divDetails = $(divRotator).children().eq($(divRotator).children().hasClass('show'));

    // This is the original image rotator code
    //Get the first image
    var current = ($(divDetails) ? $(divDetails) : $(divRotator).first());
    if (current.length == 0) current = $(divRotator).first();

    //Get next image, when it reaches the end, rotate it back to the first image
    //var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $(divRotator).first() : current.next()) : $(divRotator).first());
    //These 3 lines below to get the images in random order
    var sibs = current.siblings();

    // Nothing to rotate if the siblings is 0 - basically this stops the rotator
    if (sibs.length > 0) {
        var rndNum = Math.floor(Math.random() * sibs.length);
        var next = $(sibs[rndNum]);

        //Hide the current image
        current.css({ opacity: 0.0 })
            .removeClass('show');

        //Set the fade in effect for the next image, the show class has higher z-index
        next.css({ opacity: 0.0 })
	        .addClass('show')
	        .animate({ opacity: 1.0 }, animateInterval);
    }
        
    // Retrieve the child item that has the class currently set to 'show'
    var divDetails = $(divRotator).find('.show');
    var hdnPartNo = divDetails.find('input').eq(0);
    var hdnPartName = divDetails.find('input').eq(1);
    var hdnNavigateUrl = divDetails.find('input').eq(2);

    var partNo = hdnPartNo.get(0).value;
    var partName = hdnPartName.get(0).value;
    var navigateUrl = hdnNavigateUrl.get(0).value;
    
    // Do whatever is desired with this information collected above or do nothing
    //  This allows the Item No and Part Name to be on a separate control
    if (lblItemNoClientID != "") {
        var lblItemNo = document.getElementById(lblItemNoClientID);
        lblItemNo.innerHTML = partNo;
    }
    if (lnkItemNameClientID != "") {
        var lnkPartName = document.getElementById(lnkItemNameClientID);
        lnkPartName.innerHTML = partName;
        lnkPartName.href = navigateUrl;
    }

    // Nothing to rotate if the siblings is 0 - basically this stops the rotator
    if (sibs.length > 0) {
        // This assumes the CSS Class is "head"
        if (headCSSInfo != "") {
            var head = $(headCSSInfo);
            head.css({ opacity: 0.0 })
               .animate({ opacity: 1.0 }, animateInterval);
        }
    }
    
};

/**
 *
 * Must be used on parent page wherever bii_rotator control is used
 *
 */
//var TimerClientID = "";
//function startTimer(TimerClientID)
//{
//    //alert(TimerClientID.toString());
//    var timer = $find(TimerClientID.toString());
//    timer._startTimer(); 
//}
//function stopTimer(TimerClientID)
//{
//    //alert(TimerClientID.toString());
//    var timer = $find(TimerClientID.toString());
//    timer._stopTimer();
//} 


