﻿var TimeoutInterval = 6000;
var TimeoutRef = null;
var RotatorCount = 0;

$(document).ready(function() {

    // for colorbox to disable images x of xx
    $('a[rel="lightbox_iframe"]').colorbox({ rel: 'nofollow' });

    //Rotator Scripts
    RotatorCount = $(".ui-tabs-nav-item").not(".ui-tabs-nav-control").length;
    TimeoutRef = setTimeout("ScheduleNextTab(1)", TimeoutInterval);

    $(".ui-tabs-nav-item").each(function(index) {
        $(this).click(function() {
            clickedIndex = $(this).attr("id").split("-")[2];
            clearTimeout(TimeoutRef);
            if ($(this).is(".ui-tabs-nav-control")) {//move left or right             
                $(".ui-tabs-panel").not(".ui-tabs-hide").fadeOut('slow', function() {
                    index = $(this).attr("id").split("-")[1];
                    index = (clickedIndex == 0 ? (index == 1 ? RotatorCount : index - 1) : (index == RotatorCount ? 1 : parseInt(index) + 1));
                    ScheduleTab(index);
                });
            }
            else if ($(this).not(".ui-tabs-selected")) { //click on thumbnail
                $(".ui-tabs-panel").not(".ui-tabs-hide").fadeOut('slow', function() { ScheduleTab(index); });
            }
        })
    });

    $(".ui-tabs-panel").each(function(index) {
        $(this).mouseover(function() {
            if (TimeoutRef != null)
                clearTimeout(TimeoutRef);
        });
        $(this).mouseout(function() {
            clearTimeout(TimeoutRef);
            TimeoutRef = setTimeout("ScheduleNextTab(" + (index + 1).toString() + ")", TimeoutInterval);
        });
    });

    var MenuCount = 0;
    var MenuTimeOutObj = {};

    //Menu Scripts
    $("li.mainNavItem,li.subMenu").each(function() {
        $(this).attr("id", "submenu_" + MenuCount);
        MenuCount++;
        $(this).mouseover(function() {
            $(this).children("ul").addClass("open");
            $(this).addClass("expanded");
            if (MenuTimeOutObj[$(this).attr("id")])
                clearTimeout(MenuTimeOutObj[$(this).attr("id")]);
        })
    })

    $("li.mainNavItem,li.subMenu").each(function() {
        $(this).mouseout(function() {
            MenuTimeOutObj[$(this).attr("id")] = setTimeout("CloseMenu('" + $(this).attr("id") + "')", 0);
        })
    })

    //Lightbox scripts
    $(function() {
        $('a[rel*=lightbox_iframe]').each(function() {
            var linkwidth = $(this).attr("width");
            var linkheight = $(this).attr("height");
            $(this).colorbox({ iframe: true, width: linkwidth, height: linkheight });
            //IsLoadedIframeContent()
        });
    });
})

function CloseMenu(Id) {
    $("#" + Id).children("ul").removeClass("open");
    $("#" + Id).removeClass("expanded");
}

//Assumes that the document being loaded in the iframe has a form tag in it.
function IsLoadedIframeContent() {
    var FormTestResults = $("#cboxContent iframe").contents().find("body").find("form");
    if (FormTestResults == null) { //document not loaded
        $("#cboxContent iframe").contents().find("body").attr("innerHTML", "<img src='images/loading.gif' />");
        //setTimeout("IsLoadedIframeContent", 500);
    }
}

//Rotator Functions
function ScheduleNextTab(CurrentIndex) {
    CurrentIndex = (CurrentIndex > RotatorCount ? 1 : CurrentIndex);
    var NextIndex = (CurrentIndex >= RotatorCount ? 1 : CurrentIndex + 1);
    $("#fragment-" + CurrentIndex.toString()).fadeOut('slow', function() {
        ScheduleTab(NextIndex);
    });
}

function ScheduleTab(index) {
    $("#fragment-" + (index).toString()).fadeIn('slow', function() {
        $("#fragment-" + (index).toString()).removeClass("ui-tabs-hide");
        clearTimeout(TimeoutRef);
        TimeoutRef = setTimeout("ScheduleNextTab(" + (index).toString() + ")", TimeoutInterval);
    });
    $(".ui-tabs-selected").removeClass("ui-tabs-selected");
    $(".ui-tabs-panel").not(".ui-tabs-hide").addClass("ui-tabs-hide");
    $("#nav-fragment-" + (index).toString()).addClass("ui-tabs-selected");
}

//Taxonomy Search
function DoTaxonomySearch(obj) {
    var Value = $(obj).attr("id");
    // alert("Value: " + Value);
    $("#solution_taxonomy").attr("value", Value);
    __doPostBack();
}

