// JavaScript Document
$(document).ready(function(){
    var IE6 = false;

    // only utilize if browser is IE6
    if(navigator.appVersion.indexOf("MSIE 6") > 0){
        IE6 = true;
    }

    // -----------------------------------------------------------------

    /*
     *  Add TITLE to all external facing links
     */
    var externalLinks = $("a[target='_blank'][title='']");

    if(externalLinks.length > 0)
    {
        $(externalLinks).each(function(){
            $(this).attr('title', "Clicking on this link will take you from MerchantInsider.com to this organization's own Web site, which is governed by that organization's own Privacy Policy.");
        })
    }
    
    // -----------------------------------------------------------------

    /*
     * DYNAMIC DROP DOWN MENU (MAIN NAVIGATION)
     *
     * The original 'hover' functionality in jquery is overriden inside
     * of jquery.event.hover-1.0.js and the original 'hover' event is
     * renamed to '_hover'.  This 'hover' event is used in order to prevent
     * accidental hovers on the drop down menus.
     */
    $("#mainNav > li").hover(
        function(){
            // if not active
            if(!$(this).hasClass("on"))
                $(this).children("a").addClass("clicked");

            // create iframe within dropdown menu so
            // selects and flash won't bleed through.
            // NOTE: only creates iframe if select <select> elements are present
            if(IE6 && $('select').length > 0){
                var left = ($(this).attr("id") == 'about') ? '25px' : 'auto';
                var width = $(this).children("ul").width() + 'px';
                var height = parseInt($(this).children("ul").height()) + parseInt($(this).children("ul").css("padding-top")) + 'px';
                var html = '<iframe class="bgiframe" frameborder="0" src="javascript:false;" '+
		               'style="display:block; position:absolute; z-index:9999; '+
					   'top: 58px; left:'+left+'; width:'+width+'; height:'+height+';"/>';

                if ( $('> iframe.bgiframe', this).length == 0 )
                    this.insertBefore( document.createElement(html), this.firstChild );
            }

            $(this).children("ul").fadeIn("fast");
        },
        function()
        {
            $(this).children("a").removeClass("clicked");

            if (IE6) {
				$(".bgiframe").remove(); // remove iframe
			}
			
			$(this).children("ul").fadeOut("fast");
        }
    );

    // -----------------------------------------------------------------

    // HOME PAGE FADE CYCLE
    if($("#hmFeatureCntnr").length > 0)
    {
        $("#hmFeatureCntnr").hide().cycle({
            fx: 'fade',
            timeout: 10000,
            speed: 50,
            cleartype: true,
            cleartypeNoBg: true,
            sync: false
        }).show();
    }

    if($("#li-topLvlFeatureLeft").length > 0)
    {
        $("#li-topLvlFeatureLeft").hide().cycle({
            fx: 'fade',
            timeout: 10000,
            speed: 50,
            cleartype: true,
            cleartypeNoBg: true,
            sync: false
        }).show();
       
    }
    
    // Account Changes Forms
    $(".acctformlnk a").click(function(e)
    {
        e.preventDefault();
        
        // change link colors
        $('.acctformlnk a.on').removeClass('on');
        $(this).addClass('on');
        
        var attr_rel = $(this).attr("rel");
        
        $('#accountchangeforms div.on').slideUp('normal', function()
        {
            $(this).removeClass('on');
            $('#'+attr_rel).slideDown().addClass('on');
            $("#accountchangeforms input[type='text']").each(function(){$(this).val("")});
            $("input[name='form_type']").val(attr_rel);
        });
    });
});

/**
 * Helper function, removes the akmodal iframe
 */
function remove_iframe(){
    $('#ak_modal_div').fadeOut(500);
        $('#ak_modal_div').remove();
        $.dimScreenStop();
}

/**
 * Educational Videos load Function
 */
function loadFLV(flvPath)
{
    var player = getPlayer("flvPlayer");
    player.loadFLV(flvPath);
}

/**
 * Education Videos Get Player
 */
function getPlayer(gid)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[gid];
    }
    else
    {
        return document[gid];
    }
}

/*
 * POPUP Creator
 * This will be replacing the akmodal plugin.
 */
function realPopup(url, width, height)
{
    // These variables will position the popup window in the middle of the users screen
    var winT = (screen.height - height) / 2;
    var winL = (screen.width - width) / 2;

    window.open(url, 'Window2', "width=" +width+ ",height=" +height+ ",top=" +winT+ ",left=" +winL);
}