﻿$(document).ready(function() {
    //show the loading icons
    $(".ajaxLoading").show();
    $(".ajaxData").hide();

    //load the carousel
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "_handlers/index.ashx",
        data: { 'type': 'carousel' },
        success: function(data) {
            getCarouselSuccess(data);
        },
        error: function() { }
    });                     
    
    //load the news stories
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "_handlers/index.ashx",
        data: { 'type': 'newsstories' },
        success: function(data) {
            getNewsStoriesSuccess(data);
        },
        error: function() {
        }
    });
    
    //load the press releases    
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "_handlers/index.ashx",
        data: { 'type': 'pressreleases' },
        success: function(data) {
            getPressReleasesSuccess(data);
        },
        error: function() {
        }
    });

    //load the global data
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "_handlers/index.ashx",
        data: { 'type': 'globaltraining' },
        success: function(data) {
            getGlobalTrainingSuccess(data);
        },
        error: function() {
        }
    }); 
});

function popUp(href) {
    window.open(href, '_blank', 'width=725,height=505,resizable=yes,toolbar=no,directories=no,location=no ,menubar=no,status=no,left=400,top=100');
    return false;
}

function getCarouselSettingsSuccess(data) {
    if (typeof data != "undefined") {
        var c = eval(data);

        $(".hr-slider").CarouSlide({
            slideTime: parseInt(c[0].carouselslidetime),
            animTime: parseInt(c[0].carouselanimationtime),
            showPauseButton: c[0].carouselanimate,
            autoAnim: c[0].carouselanimate
        });
    }
    
    $("#divCarouselLoading").hide();
    $("#divCarousel").show();
}

function getCarouselSuccess(data) {
    if (typeof data != "undefined") {
        var c = eval(data);
        var html = "";
        var target;

        $("#ulCarousel").html("");
        $("#ulCarouselButtons").html("");

        for (var i in c) {
            target = "_self";

            html = "<li id='slide" + c[i].priority + "'>";
            html += "<div class='slide-image'>";

            //check if this is an hrpolicy file
            if ((c[i].link.indexOf("http://") != -1) && (c[i].link.indexOf("hrpolicy.org") != -1)) {
                //check that it is a webpage and not a pdf document
                if (c[i].link.indexOf(".aspx") == -1 && c[i].link.indexOf(".htm") == -1) {
                    target = "_blank"
                }
            }

            //set the image
            html += "<a href='" + c[i].link + "' target='" + target + "'>";
            html += "<img src='" + c[i].imagesrc + "' alt='" + c[i].title + "' />";
            html += "</a><br />" + c[i].imageinfo + "</div>";

            //add the content
            html += "<div class='slide-content'>";
            html += "<h1>" + c[i].title + "</h1>";
            html += c[i].firstsentence + " <a href='" + c[i].link + "' target='" + target + "'>Read&nbsp;more&nbsp;&#187;</a></div></li>";

            $("#ulCarousel").append(html);
            $("#ulCarouselButtons").append("<li><a href='javascript:void(0);'>" + c[i].priority + "</a></li>");
        }
    }

    //get the settings for the carousel
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "_handlers/index.ashx",
        data: { 'type': 'carouselsettings' },
        success: function(data) { getCarouselSettingsSuccess(data) },
        error: function() {
        }
    });
}

function getGlobalTrainingSuccess(data) {
    if (typeof data != "undefined") {
        var c = eval(data);
        if (c.length > 0) {
            var html;
            var target = "_self";
            var link = ""

            html = "<span class='home-showcase'><a href='/events_calendar.aspx?type=global'>Upcoming Events</a></span><br />";

            if ((c[0].link.indexOf("http://") != -1) && (c[0].link.indexOf("hrpolicy") == -1)) {
                target = "_blank"
                link = c[0].link;
            }
            else {
                if (c[0].meetingtype == "conf") {
                    link = "event_call.aspx?id=" + c[0].productid
                }
                else if (c[0].meetingtype == "training") {
                    link = "event_training.aspx?id=" + c[0].productid
                }
                else {
                    link = "event_meeting.aspx?id=" + c[0].productid
                }
            }

            html += "<a href='" + link + "' target='" + target + "'>" + c[0].title + "</a><br />";
            html += $.format.date(new Date(c[0].startdate), "M/d/yyyy");

            $("#liGlobal").html(html).removeClass("hidden");
        }
    }

    $("#divGlobal").show();
}

function getNewsStoriesSuccess(data) {
    if (typeof data != "undefined") {
        var c = eval(data);
        var html;
        var target = "";

        for (var i in c) {
            html = "<li>";
            target = "_self";

            // if an issue is specified, then display a link for it                    
            if (c[i].issue.length > 0) {
                html += "<span class='home-showcase' style='display: inline;')><a href='" + c[i].issuelink + "' target='" + c[i].issuetarget + "'>" + c[i].issue + "</a></span><br />";
            }

            //determine if the link should open in a new window or not
            if ((c[i].link.indexOf("http://") != -1) && (c[i].link.indexOf("hrpolicy") == -1)) {
                target = "_blank"
            }

            //add the news story link and the date                                        
            html += "<a href='" + c[i].link + "' target='" + target + "'>" + c[i].title + "</a><br />";
            html += "<span>" + $.format.date(new Date(c[i].date), "M/d/yyyy") + "</span></li>";

            $("#ulNews").append(html);
        }
    }

    $("#divNewsLoading").hide();
    $("#divNews").show();
}

function getPressReleasesSuccess(data) {
    if (typeof data != "undefined") {
        var c = eval(data);
        var html;
        var target = "";

        for (var i in c) {
            html = "<li>";
            target = "_self";

            // if an issue is specified, then display a link for it                    
            if (c[i].issue.length > 0) {
                html += "<span class='home-showcase' style='display: inline;')><a href='" + c[i].issuelink + "' target='" + c[i].issuetarget + "'>" + c[i].issue + "</a></span><br />";
            }

            //determine if the link should open in a new window or not
            if ((c[i].link.indexOf("http://") != -1) && (c[i].link.indexOf("hrpolicy") == -1)) {
                target = "_blank"
            }

            //add the news story link and the date                                        
            html += "<a href='" + c[i].link + "' target='" + target + "'>" + c[i].title + "</a><br />";
            html += "<span>" + $.format.date(new Date(c[i].date), "M/d/yyyy") + "</span></li>";

            $("#ulPress").append(html);                    
        }
    }

    $("#divPressLoading").hide();
    $("#divPress").show();
}


