$(document).ready(function() {
    getActivePage();

    $('#officeBox').click(function() {
        window.open("http://www.hminsurancegroup.com/contact/regional_sales.shtml", 'HMI');
        pageTracker._trackPageview('/external/regionaloffice');
    });

    $("#subNav img").hover(function() {
        $(this).attr("src", $(this).attr("src").split(".").join("_ovr."));
    }, function() {
        $(this).attr("src", $(this).attr("src").split("_ovr.").join("."));
    });

    $("#mainNav img").hover(function() {
        if ($(this).attr("src").indexOf("_act") > 0) {
            $(this).attr("src", $(this).attr("src").split("_act.").join("_ovr."));
        } else {
            $(this).attr("src", $(this).attr("src").split(".").join("_ovr."));
        }
    }, function() {
        if ($(this).attr("src").indexOf("_act") > 0) {
            $(this).attr("src", $(this).attr("src").split("_ovr.").join("_act."));
        } else {
            $(this).attr("src", $(this).attr("src").split("_ovr.").join("."));
        }
        getActivePage();
    });

    setTimeout("getRightHeight()", 1000);

    //Set Location and News Arrays
    var locations = ['Atlanta', 'Charlotte', 'Chicago', 'Cincinnati', 'Cleveland', 'Dallas', 'Erie', 'Hartford', 'Houston', 'Kansas', 'New York', 'Philadelphia', 'Phoenix', 'Pittsburgh', 'San Francisco', 'Seattle', 'Southern California', 'Syracuse', 'Tampa', 'Washington D.C.'];
    var newsItems = new Array();
    $('#ctl00_DataList1 tbody tr td').each(function() {
        newsItems.push($(this).html());
    });

    $('.videoLink').colorbox({ iframe: true, innerWidth: 502, innerHeight: 338, opacity: .33 });

    //Pause animations on the homepage when the video link is clicked
    $('.videoLinkHome').click(function() {
        $('.officeText').stop(true, true);
        $('.newsText').stop(true, true);
        $('.officeText').attr('class', 'officeTextPause');
        $('.officeTextPause').text(locations[0]);
        $('.newsText').attr('class', 'newsTextPause');
        $('.newsTextPause').html(newsItems[0]);
        $.fn.colorbox({ iframe: true, innerWidth: 502, innerHeight: 338, opacity: .33, href: '/videoPlayerHome.aspx', title: 'What makes HM different?', onClosed: cboxClosed });
    });

    //$(".videoLink").colorbox({ iframe: true, innerWidth: 502, innerHeight: 338, opacity: .33 });

    var locationsCount = locations.length;
    loopage();
    function loopage() {
        //alert($('#hidStop').val());
        $.each(locations, function(index, val) {
            setTimeout(function() {
                $('.officeText').fadeOut(function() {
                    $(this).html(locations[index]).fadeIn();
                    //$(this).text( index + ' - ' + locationsCount ).slideDown();
                    if (index == (locationsCount - 1)) {
                        setTimeout(function() {
                            loopage();
                        }, 2500);
                    }
                });
            }, index * 2500);
        });
    }


    var newsCount = newsItems.length;
    news1();
    //loopNews();

    function news1() {
        $('.newsText').fadeOut(function() {
            $(this).html(newsItems[0] + '<br/><br/>' + newsItems[1]).fadeIn();
            //if (index == (newsCount - 1)) {
            //setTimeout(function() { loopNews(); }, 8000);
            //}
        });

        setTimeout(function() {
            news2();
        }, 8000);

    }

    function news2() {
        $('.newsText').fadeOut(function() {
            $(this).html(newsItems[2] + '<br/><br/>' + newsItems[3]).fadeIn();
            //if (index == (newsCount - 1)) {
            //setTimeout(function() { loopNews(); }, 8000);
            //}
        });

        setTimeout(function() {
            news1();
        }, 8000);
    }

    var linkedInHeight = $('#linkedIn').height();
    $('#newsBox').css('height', parseInt(linkedInHeight) - 50);

    function loopNews() {
        $.each(newsItems, function(index, val) {
            setTimeout(function() {
                $('.newsText').fadeOut(function() {
                    $(this).html(newsItems[index]).fadeIn();
                    //$(this).text( index + ' - ' + locationsCount ).slideDown();
                    if (index == (newsCount - 1)) {
                        setTimeout(function() { loopNews(); }, 8000);
                    }
                });
            }, index * 8000);
        });
    }

    function cboxClosed() {
        $('.officeTextPause').attr('class', 'officeText');
        $('.newsTextPause').attr('class', 'newsText');
        loopage();
        news1();
    }

});

function randomSWF() {
    var swfItems = ['cougar.swf', 'bulb.swf', 'pie.swf', 'cow.swf'];
    var swfCount = swfItems.length;
    var randNum = Math.floor(Math.random() * swfCount);
    return swfItems[randNum];
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function getRightHeight() {
    if ($('#rightColumn').height() > $('#mainContainer').height()) {
        $('#mainContainer').height($('#rightColumn').height());
    }
    if ($('#mainContainer').height() > $('#rightColumn').height()) {
        $('#rightColumn').height($('#mainContainer').height());
    }
}

function getActivePage() {
    var tmpString = String(window.location);
    tmpString = tmpString.toLowerCase();
    if (tmpString.indexOf("aboutus") > 0) {
        //$('#flashArea').html("<img src='/images/cougarNF.jpg'/>");
        if ($(".m1 a img").attr("src").indexOf("_act") < 0) {
            $(".m1 a img").attr("src", $(".m1 a img").attr("src").split(".").join("_act."));
        }
    }
    if (tmpString.indexOf("stoploss") > 0) {
        //$('#flashArea').html("<img src='/images/cowNF.jpg'/>");
        if ($(".m2 a img").attr("src").indexOf("_act") < 0) {
            $(".m2 a img").attr("src", $(".m2 a img").attr("src").split(".").join("_act."));
        }
    }
    if (tmpString.indexOf("worksite") > 0) {
        //$('#flashArea').html("<img src='/images/bulbNF.jpg'/>");
        if ($(".m3 a img").attr("src").indexOf("_act") < 0) {
            $(".m3 a img").attr("src", $(".m3 a img").attr("src").split(".").join("_act."));
        }
    }
    if (tmpString.indexOf("medicalbenefit") > 0) {
        //$('#flashArea').html("<img src='/images/pieNF.jpg'/>");
        if ($(".m4 a img").attr("src").indexOf("_act") < 0) {
            $(".m4 a img").attr("src", $(".m4 a img").attr("src").split(".").join("_act."));
        }
    }
    if (tmpString.indexOf("eventlisting") > 0) {
        if ($(".m5 a img").attr("src").indexOf("_act") < 0) {
            $(".m5 a img").attr("src", $(".m5 a img").attr("src").split(".").join("_act."));
        }
    }
}

function openExtFromFlash(s){
    window.open(s, 'HMI');
}