﻿
//  don't delete this -  it is a VS script reference to the jQuery file, and allows intellisense to almost work
/// <reference path="~/scripts/jquery-1.3.2.js" />

var lclFeatureItem = new Array(4);
lclFeatureItem[0] = new Array();
lclFeatureItem[1] = new Array();
lclFeatureItem[2] = new Array();
lclFeatureItem[3] = new Array();

var lclCurrentFeature = new Array(4);
lclCurrentFeature[0] = 0;
lclCurrentFeature[1] = 0;
lclCurrentFeature[2] = 0;
lclCurrentFeature[3] = 0;

var lclOddOrEvenFeature = new Array(4);
lclOddOrEvenFeature[0] = "a";
lclOddOrEvenFeature[1] = "a";
lclOddOrEvenFeature[2] = "a";
lclOddOrEvenFeature[3] = "a";

// we don't start with the open feature one
var lclCurrentPosition = 0;
var maxDivHeight = 66;

function lclRotateFeatureItems(frequency) {
    setInterval(function() {
        if (++lclCurrentPosition == 4)
            lclCurrentPosition = 0;
        if (lclFeatureItem[lclCurrentPosition].length > 1)
            lclRotateFeature(lclCurrentPosition);
    }, frequency);

    // code for triggering rotate by button
    //    if (++lclCurrentPosition == 4)
    //        lclCurrentPosition = 0;
    //    if (lclFeatureItem[lclCurrentPosition].length > 1)
    //        lclRotateFeature(lclCurrentPosition);
}


function lclRotateFeature(selected) {
    
    // get the next feature
    if (++lclCurrentFeature[selected] >= lclFeatureItem[selected].length)
        lclCurrentFeature[selected] = 0;

    // calculate which divs we're talking about
    var currentFeatureID = "#featureID" + selected + lclOddOrEvenFeature[selected];
    lclOddOrEvenFeature[selected] = (lclOddOrEvenFeature[selected] == "a" ? "b" : "a");
    var newFeatureID = "#featureID" + selected + lclOddOrEvenFeature[selected];
    
    // load the new feature
    lclDisplayFeature(selected, true);   

    // then fade the current panel out and the new one in
    $(currentFeatureID).fadeOut(2500);
    $(newFeatureID).fadeIn(1500);

}

function lclDisplayFeature(selected, hidden) {
    var feature = lclFeatureItem[selected][lclCurrentFeature[selected]];
    var featureID = "#featureID" + selected + lclOddOrEvenFeature[selected];

    // change the image
    $(featureID + " > .spotlightImage").each(function() {
        this.innerHTML = feature.Image;
    });
    
    // change the title and summary
    $(featureID + " > .spotlightContent").each(function() {
        this.innerHTML = feature.Title + feature.Summary;
    });
    
    // add the onclick function to the whole div
    $(featureID).unbind("click").css("cursor", "pointer").click(function() {
        if (feature.Link.substring(0, 1) != '/')
            eval(feature.Link);
        else
            window.location.href = feature.Link;
    });

    // allow code below to get size of hidden box
    if (hidden) {
        var opacity = $(featureID).css("opacity");
        $(featureID).css("opacity", opacity == 0 ? "0.0001" : opacity);
    }
    
    // determine the max height of the feature boxes
    $(".homeSpotlights > .spotlight > .spotlightContent").each(function() {
        $(this).css('height', '');
    });
    
    $(".homeSpotlights > .spotlight > .spotlightContent").each(function() {
        if (maxDivHeight < $(this).height())
            maxDivHeight = $(this).height();
    });

    // and set them all to the same
    $(".homeSpotlights > .spotlight > .spotlightContent").each(function() {
        $(this).height(maxDivHeight);
    });

    // then handle the container too
    $(".homeSpotlights").each(function() {
        $(this).height(maxDivHeight+190);
    });
}


function lclAddFeatureItem(position, image, link, title, summary) {
    var featureItem = new Object();
    featureItem.Link = link.replace(/&apos;/g, "'");
    featureItem.Title = title.replace(/&apos;/g, "'");
    featureItem.Image = image.replace(/&apos;/g, "'");
    featureItem.Summary = summary.replace(/&apos;/g, "'");

    lclFeatureItem[position][lclFeatureItem[position].length] = featureItem;
}