// jquery plugin JS required for addl effects
(function(jQuery) { jQuery.dimensions = { version: '1.2' }; jQuery.each(['Height', 'Width'], function(i, name) { jQuery.fn['inner' + name] = function() { if (!this[0]) return; var torl = name == 'Height' ? 'Top' : 'Left', borr = name == 'Height' ? 'Bottom' : 'Right'; return this.is(':visible') ? this[0]['client' + name] : num(this, name.toLowerCase()) + num(this, 'padding' + torl) + num(this, 'padding' + borr); }; jQuery.fn['outer' + name] = function(options) { if (!this[0]) return; var torl = name == 'Height' ? 'Top' : 'Left', borr = name == 'Height' ? 'Bottom' : 'Right'; options = jQuery.extend({ margin: false }, options || {}); var val = this.is(':visible') ? this[0]['offset' + name] : num(this, name.toLowerCase()) + num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width') + num(this, 'padding' + torl) + num(this, 'padding' + borr); return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0); }; }); jQuery.each(['Left', 'Top'], function(i, name) { jQuery.fn['scroll' + name] = function(val) { if (!this[0]) return; return val != undefined ? this.each(function() { this == window || this == document ? window.scrollTo(name == 'Left' ? val : jQuery(window)['scrollLeft'](), name == 'Top' ? val : jQuery(window)['scrollTop']()) : this['scroll' + name] = val; }) : this[0] == window || this[0] == document ? self[(name == 'Left' ? 'pageXOffset' : 'pageYOffset')] || jQuery.boxModel && document.documentElement['scroll' + name] || document.body['scroll' + name] : this[0]['scroll' + name]; }; }); jQuery.fn.extend({ position: function() { var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results; if (elem) { offsetParent = this.offsetParent(); offset = this.offset(); parentOffset = offsetParent.offset(); offset.top -= num(elem, 'marginTop'); offset.left -= num(elem, 'marginLeft'); parentOffset.top += num(offsetParent, 'borderTopWidth'); parentOffset.left += num(offsetParent, 'borderLeftWidth'); results = { top: offset.top - parentOffset.top, left: offset.left - parentOffset.left }; } return results; }, offsetParent: function() { var offsetParent = this[0].offsetParent; while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static')) offsetParent = offsetParent.offsetParent; return jQuery(offsetParent); } }); function num(el, prop) { return parseInt(jQuery.curCSS(el.jquery ? el[0] : el, prop, true)) || 0; }; })(jQuery);

var xmlData;
var activePosition = 0;
var animationSpeed = 350;
 
var homePageItems = new Array();
var totalCount = 0;
var pauseSlides = false;

var selectorVals = new Object();
selectorVals.MainContent = "#bannerMainCont";
selectorVals.Title = selectorVals.MainContent + " h1";
selectorVals.Body = "#itemBody";
selectorVals.BodyButton = "#itemLink";
selectorVals.MainImage = "#mainImage";
selectorVals.MainImageSel = selectorVals.MainImage + " img";
selectorVals.TextContainer = "#storyContainer";

var cachedObjects = new Object();
cachedObjects.ThumbNails = new Array();

// Start function when DOM has completely loaded 
jQuery(document).ready(function() {
    showPromo();
});


function CreateItems(xmlData) {
    jQuery('FeaturedStory', xmlData).each(function(i) {
        var jxmlData = jQuery(this);
        homePageItem = new Object();
        totalCount++;

        //add values to properties of object
        homePageItem.Position = jxmlData.attr("Position") - 1;

        homePageItem.ThumbID = "#thumb" + homePageItem.Position;
        homePageItem.ThumbImage = jxmlData.find("ThumbUrl").text();
        homePageItem.ThumbHoverImage = jxmlData.find("ThumbHoverUrl").text();
        homePageItem.ThumbLink = jxmlData.find("StoryLink").text();

        homePageItem.BackgroundImage = jxmlData.find("ImageUrl").text();
        homePageItem.Title = jxmlData.find("Title").text();
        homePageItem.BodyText = jxmlData.find("SubTitle").text();
        homePageItem.ButtonLink = homePageItem.ThumbLink;

        //calls methods which builds the thumbnails on the page and binds the mouseenter event
        buildThumbs(homePageItem.ThumbImage, homePageItem.ThumbLink, homePageItem.Position);

        //set default color to the new thumbnail images
        cachedObjects.ThumbNails[homePageItem.Position] = jQuery(homePageItem.ThumbID);

        homePageItems[homePageItem.Position] = homePageItem;
    });
}

function SetDefaultItem(defaultPosition) {
    var homePageItem = homePageItems[defaultPosition];
    (cachedObjects.ThumbNails[defaultPosition]).find("img").attr("src", homePageItem.ThumbHoverImage);
    //add the selected h1, h2 & paragraph to the div
    cachedObjects.MainContent = jQuery(selectorVals.TextContainer);
    var txtContent = '<h1>' + homePageItem.Title + '</h1><span id="itemBody">' + homePageItem.BodyText + '</span>';
    (cachedObjects.MainContent).append(txtContent);
    (cachedObjects.MainContent).append('<p class="readMoreBtn"><a id="itemLink" href="' + homePageItem.ButtonLink + '"><img border="0" src="/_layouts/images/ausa/rotator_more.png" /></a>');


    cachedObjects.Title = jQuery(selectorVals.Title);
    cachedObjects.BodyText = jQuery(selectorVals.Body);
    cachedObjects.BodyButton = jQuery(selectorVals.BodyButton);
    cachedObjects.ThumbCont = jQuery("#thumbCont");
    var bgImage = jQuery('<img>').addClass("mainBgImage").attr("src", homePageItem.BackgroundImage);

    cachedObjects.MainImage = jQuery(selectorVals.MainImage);
    (cachedObjects.MainImage).prepend(bgImage);
    cachedObjects.MainImageSel = jQuery(selectorVals.MainImageSel);
    (cachedObjects.MainImageSel).eq(defaultPosition).animate({ opacity: 'toggle' }, { duration: animationSpeed, queue: false });
}

function OnThumbEnter(itemPosition) {
    if (itemPosition != activePosition) {
        var hoveredItem = homePageItems[itemPosition];

        //clear previous thumbs
        $.each(cachedObjects.ThumbNails, function(index, value) {
            value.find("img").attr("src", homePageItems[index].ThumbImage);
        });
        (cachedObjects.ThumbNails[itemPosition]).find("img").attr("src", hoveredItem.ThumbHoverImage);
        (cachedObjects.Title).html(hoveredItem.Title);
        (cachedObjects.BodyText).html(hoveredItem.BodyText);
        (cachedObjects.BodyButton).attr("href", hoveredItem.ButtonLink);

        var img1 = jQuery("<img>").addClass("mainBgImage").attr("src", hoveredItem.BackgroundImage);
        (cachedObjects.MainImage).append(img1);

        var selectedImage = (cachedObjects.MainImage).children("img");
        var numItems = selectedImage.length - 1;

        selectedImage.eq(numItems).animate({ opacity: 'toggle' }, { duration: animationSpeed, queue: false });
        selectedImage.eq(numItems - 1).animate({ opacity: 'toggle' }, { duration: animationSpeed, queue: false, complete: function() { jQuery(this).remove() } });

        //update activePosition    
        activePosition = itemPosition;
    }
}

function showPromo() {
    //function that's called onload and onmouseover of thumbs
    jQuery.get(xmlFileLoc, {}, function(xml) {
        var xmlData = xml;
        activePosition = 0;
        CreateItems(xmlData);
        if (totalCount > 0) {
            SetDefaultItem(activePosition);
            window.setTimeout("ShowNextStory()", slideTimeout);
        }
    });
}

function ShowNextStory() {
    if (!pauseSlides) {
        if (activePosition == totalCount - 1) {
            OnThumbEnter(0);
        } else
            OnThumbEnter(activePosition + 1);
    }
    window.setTimeout("ShowNextStory()", slideTimeout);
}

function buildThumbs(thumbImage, thumbLink, itemPosition) {
    //function that builds the thumbs on page load
    var thumbCont = jQuery('<a id="thumb' + itemPosition + '" class="roThumb" href="' + thumbLink + '"></a>');
    jQuery(thumbCont).append('<img src="' + thumbImage + '" width="107" height="61" />');
    //bind mouseover function
    jQuery(thumbCont).mouseenter(function() {
        OnThumbEnter(itemPosition);
        pauseSlides = true;
    });

    jQuery(thumbCont).mouseleave(function() {
        pauseSlides = false;
    });

    //write the thumb to the DIV
    jQuery("#thumbCont").append(thumbCont);
}
 
