// when the DOM is ready...
var $scroll, scrollOptions, $panels, $container, $navlinksContainer;
var nInitialScrollLeft = 0;
$(document).ready(function() {
    $(".PagingControls a[title]").simpletooltip();
    
    WireupFilteredLists('body');
    WireupDropDownLinkMenus('body');
    WireupMediaPlayerAssetLinks('body');
    WireupInterestDropDownLinkMenus('body');
});

function str_replace(haystack, needle, replacement) {
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

function WireupFilteredLists(parent) {
    $(parent).find('.FilteredArticles').each(function(i) {
        var CurrentFilteredList = $(this);
        var $Selector = CurrentFilteredList.find('.selectedCategory');
        var LinkNavURL = null;
        //$Selector.text(CurrentFilteredList.find('.newsLinks:first').attr('name'));
        CurrentFilteredList.find('.newsLinks').hide();
        //CurrentFilteredList.find('.newsLinks:first').show();

        //Create dropdown.
        $("<div class='DropDownMenu'></div>").appendTo(CurrentFilteredList);
        CurrentFilteredList.find(".newsLinks").each(function(i) {
            if ($(this).hasClass("SelectedNewsCat")) {
                $Selector.text($(this).attr("name"));
                $(this).show();
            }
            LinkNavURL = $(this).attr("href");
            if (LinkNavURL) {
                $("<a href=\"" + LinkNavURL + "\">" + $(this).attr("name") + "</a>").appendTo(CurrentFilteredList.find(".DropDownMenu"));
            } else {
                $("<a>" + $(this).attr("name") + "</a>").appendTo(CurrentFilteredList.find(".DropDownMenu"));
            }
        });
        CurrentFilteredList.find('.DropDownMenu').hide();
        //$Selector.click(function() { $(this).parent().find('.filterDropDown').toggle('fast'); });

        $Selector.click(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                currentOpenDD = dd;
                dd.slideDown(100);
                dd.mouseover(function() { ddcancelclosetime(); });
                dd.mouseout(function() { ddclosetime(); });

            } else {
                dd.hide();
            }
        });

        $Selector.mouseover(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                ddcancelclosetime();
            }
        });
        $Selector.mouseout(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                ddclosetime();
            }
        });

        CurrentFilteredList.find('.DropDownMenu a').click(function() {
            if ($(this).attr('href')) {
                window.location.href = $(this).attr('href');
            } else {
                FilteredListsSelect($(this));
            }
        });

    });

}

function FilteredListsSelect(Item) {
    var ItemName = Item.text();
    Item.parent().parent().find('.newsLinks').each(function(i) {
        if ($(this).attr("name") == ItemName) {
            $(this).show();
        } else {
            $(this).hide();
        }
    });
    Item.parent().parent().find('.selectedCategory').text(ItemName);
    Item.parent().parent().find('.DropDownMenu').hide();
}


function WireupDropDownLinkMenus(parent) {
    $(parent).find('.DropDownLinkMenu').each(function(i) {
        var CurrentDropDownMenu = $(this);
        var $Selector = CurrentDropDownMenu.find('.selectedCategory');
        //$Selector.text('Select a Case Study');
        CurrentDropDownMenu.find('ul').hide();

        //Create dropdown.
        $("<div class='DropDownMenu'></div>").appendTo(CurrentDropDownMenu);
        CurrentDropDownMenu.find('a').each(function(i) {
            $("<a href=\"" + $(this).attr("href") + "\">" + $(this).text() + "</a>").appendTo(CurrentDropDownMenu.find(".DropDownMenu"));
        });
        CurrentDropDownMenu.find('.DropDownMenu').hide();
        //$Selector.click(function() { $(this).parent().find('.filterDropDown').toggle('fast'); });

        $Selector.click(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                currentOpenDD = dd;
                dd.slideDown(100);
                dd.mouseover(function() { ddcancelclosetime(); });
                dd.mouseout(function() { ddclosetime(); });

            } else {
                dd.hide();
            }
        });

        $Selector.mouseover(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                ddcancelclosetime();
            }
        });
        $Selector.mouseout(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                ddclosetime();
            }
        });
    });

}
var currentOpenDD = null;
var ddclosetimer = null;

function ddclosetime() {
    window.clearTimeout(ddclosetimer);
    ddclosetimer = window.setTimeout(ddclose, 2000);
}
function ddclose() {
    window.clearTimeout(ddclosetimer);
    ddclosetimer = null;
    if (currentOpenDD) {
        currentOpenDD.hide();
        currentOpenDD = null;
    }
}

function ddcancelclosetime() {
    if (ddclosetimer) {
        window.clearTimeout(ddclosetimer);
        ddclosetimer = null;
    }
}

//Stub Lightbox Handler for Media Player
function ShowImage(imageUrl, Description) {
    jQuery.slimbox(imageUrl, Description);
}

function swfLoaded() {
    $('.MediaPlayerObject').each(function(i) {
        try {
            log.debug('About to tell ' + $(this).attr('id') + ' to play intro');
            this.playIntro();
        } catch (e) {
            log.error('Error occured:' + e.message);
        }
        log.debug('Told');
    });
}

function triggerSWFs(pageName) {
    //Find all media players and stop any active playback
    $('.MediaPlayerObject').each(function(i) {
        try {
            log.debug('About to tell ' + $(this).attr('id') + ' to stop any playback');
            this.stopPlayback();
        } catch (e) {
            log.error('Error occured:' + e.message);
        }
        log.debug('Told');
    });    

    //Find Flash movies in target content area
    $('#' + pageName + ' .MediaPlayerObject').each(function(i) {
        try {
            log.debug('About to tell ' + $(this).attr('id') + ' to play intro');
            this.playIntro();
        } catch (e) {
            log.error('Error occured:' + e.message);
        }
        log.debug('Told');
    });

}


//Handle Asset-Selection in MediaPlayer
function activeMedia(id) {
    //Called when Flash MediaPlayer has focused on a specific asset
    log.info('Focused on ID: ' + id);
    $('H4.MediaPlayerAssetLink').each(function(i) {
        if ($(this).attr('ID') != null) {
            var arrPlayerAsset = $(this).attr('ID').split("|");
            $(this).removeClass('FocusedAsset');
            if (arrPlayerAsset.length > 1) {
                log.info('Found link for ' + arrPlayerAsset[1]);
                if (arrPlayerAsset[1] == id) {
                    $(this).addClass('FocusedAsset');
                }
            }
        }
    });
}

function setAssetFocus(flashMovieID, AssetID) {
    log.info('Setting MediaPlayer (' + flashMovieID + ') to ' + AssetID);
    $('body').find('#' + flashMovieID).each(function(i) { this.focusMedia(AssetID); }); 
}

//Wireup MediaPlayerAssetLink Links

function WireupMediaPlayerAssetLinks(parent) {
    log.debug('wiring up MediaPlayerAssetLinks for ' + parent);
    $(parent).find('H4.MediaPlayerAssetLink').each(function(i) {
        log.warn('found ' + $(this).attr('ID'));
        var sElementID = $(this).attr('ID');
        if ($(this).attr('ID') != null) {
            var arrPlayerAsset = sElementID.split("|");
            if (arrPlayerAsset.length > 1) {
                log.info('Wiring up link for ' + arrPlayerAsset[1]);
                $(this).click(function() { setAssetFocus(arrPlayerAsset[0], arrPlayerAsset[1]); });
            }
        }
    });
}

/* Start Interest Wireup */

function WireupInterestDropDownLinkMenus(parent) {
    $(parent).find('.InterestDropDownLinkMenu').each(function(i) {
        var CurrentDropDownMenu = $(this);
        var $Selector = CurrentDropDownMenu.find('.selectedInterest');
        //$Selector.text('Select a Case Study');
        CurrentDropDownMenu.find('ul').hide();

        //Create dropdown.
        $("<div class='DropDownMenu'></div>").appendTo(CurrentDropDownMenu);
        CurrentDropDownMenu.find('a').each(function(i) {
            $("<a href=\"" + $(this).attr("href") + "\">" + $(this).text() + "</a>").appendTo(CurrentDropDownMenu.find(".DropDownMenu"));
            
//            if($(this).hasClass("Active"))
//            {
//                //alert("active");
//                $(this).click();
//            }
        });
        CurrentDropDownMenu.find('.DropDownMenu').hide();
        //$Selector.click(function() { $(this).parent().find('.filterDropDown').toggle('fast'); });

        $Selector.click(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                currentOpenInterestDD = dd;
                dd.slideDown(100);
                dd.mouseover(function() { InterestDDcancelclosetime(); });
                dd.mouseout(function() { InterestDDclosetime(); });

            } else {
                dd.hide();
            }
        });
        
        CurrentDropDownMenu.find('a').click(function() {
            //alert('click');
            InterestDDclose();
        });

        $Selector.mouseover(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                InterestDDcancelclosetime();
            }
        });
        $Selector.mouseout(function() {
            var dd = $(this).parent().find('.DropDownMenu');
            if (dd.is(":hidden")) {
                // do nothing
            } else {
                InterestDDclosetime();
            }
        });
    });

}
var currentOpenInterestDD = null;
var InterestDDclosetimer = null;

function InterestDDclosetime() {
    window.clearTimeout(InterestDDclosetimer);
    InterestDDclosetimer = window.setTimeout(InterestDDclose, 250);
}
function InterestDDclose() {
    window.clearTimeout(InterestDDclosetimer);
    InterestDDclosetimer = null;
    if (currentOpenInterestDD) {
        currentOpenInterestDD.hide();
        currentOpenInterestDD = null;
    }
}

function InterestDDcancelclosetime() {
    if (InterestDDclosetimer) {
        window.clearTimeout(InterestDDclosetimer);
        InterestDDclosetimer = null;
    }
}

/* End Interest Wireup */