// JavaScript Document
$(document).ready(function () {
    $("ul.sf-menu").superfish({ autoArrows: false, dropShadows: false });

    $(".hoverClass").hover(function () {
        $(this).attr("src", $(this).attr("src").split(".").join("-ovr."));
    }, function () {
        $(this).attr("src", $(this).attr("src").split("-ovr.").join("."));
    });

    if ($('#pledgeCounterContainer').css('display') == 'none') {
        $('#pledgeDownload').css('padding-top', '80px');
    }

    var thisURL = document.location.href;
    if (thisURL.indexOf('content') >= 1) {
        $('.content').show();
    }

    var padding = 0;
    var myWidth = 0;
    $('#mainNav ul li').each(function (i, val) {
        padding = padding + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
        myWidth = myWidth + parseInt($(this).width());
    });

    $('#mainNav ul').css('width', padding + myWidth);

    if ($.browser.webkit) {
        $('#mainNav ul li').css('padding-top', '5px');
        $('#mainNav ul li').css('padding-bottom', '7px');
        $('.sf-navbar li li').css('top', '4px');
    }

    $('#mainNav ul li').hover(function () {
        //alert('hello');
        $(this).children().css('color', '#6d8baa');
        //$(this).child().css('color','#6d8baa');
    }, function () {
        $(this).children().css('color', '#FFF');
        //$(this).child().css('color','#FFF');
    });

    $('#mainNav ul li').hover(function () {
        var liWidths = 0;
        $(this).children().children('li').each(function (i, val) {
            $(this).evenIfHidden(function (element) { liWidths += parseInt(element.width()); });
        });

        var myPos = $(this).position();

        if (myPos.left > 400) { //Every menu item on the right side of the screen will display its drop down on the right.
            var pos = $(this).children('ul').position();
            var widthDiff = $(this).children('ul').width() - liWidths;
            var myWidth = $(this).children('ul').width();

            if (myWidth + pos.left <= $('#mainNav').width()) {
                //alert('yep');
                $(this).children('ul').css('left', pos.left + widthDiff);
                if ($.browser.name == 'msie' && parseInt($.browser.version) < 7) {
                    //pos = $(this).children('ul').position();
                    //alert(pos.left);
                    $(this).children('ul').css('left', pos.left + widthDiff - 160);
                }
            }
        }
        //$(this).child().css('color','#6d8baa');
    }, function () {

    });

    var idToUse;
    $('.refDIV').hoverIntent(function () {
        idToUse = $(this).attr('rel');
        $(idToUse).effect("bounce", { times: 3, distance: 10 }, 300);
    }, function () { });
});

function openCBIFrame(myURL,myWidth,myHeight) {
    $.fn.colorbox({width:myWidth, height:myHeight, open:true, iframe:true, href:myURL});
}
function openContentEdit() {
    $.fn.colorbox({width:600, height:600, open:true, iframe:true, href:"/admin/addEditContent.aspx?pID="+$.query.get('pID')+""});
}
function openAddSection() {
    $.fn.colorbox({ width: 600, height: 600, open: true, iframe: true, href: "/admin/addEditSection.aspx?pID=" + $.query.get('pID') + "" });
}
function openReorderSection() {
    $.fn.colorbox({ width: 600, height: 600, open: true, iframe: true, href: "/admin/addEditContent.aspx?pID=" + $.query.get('id') + "" });
}
function openCB(myURL) {
    $.fn.colorbox({ width: "70%", height: "80%", open: true, iframe: true, href: myURL });
}






jQuery.fn.evenIfHidden = function( callback ) {

  return this.each( function() {
    var self = $(this);
    var styleBackups = [];
    
    var hiddenElements = self.parents().andSelf().filter(':hidden');
    
    if ( ! hiddenElements.length ) {
      callback( self );
      return true; //continue the loop
    }

    hiddenElements.each( function() {
      var style = $(this).attr('style');
      style = typeof style == 'undefined'? '': style;
      styleBackups.push( style );
      $(this).attr( 'style', style + ' display: block !important;' );
    });
    
    hiddenElements.eq(0).css( 'left', -10000 );

    callback(self);

    hiddenElements.each( function() {
      $(this).attr( 'style', styleBackups.shift() );
    });

  });
};

function closeAndThank(msg) {
    msg = '<div style=""font-size:11px;text-align:center;font-weight:bold;"">' + msg + '</div>';
    //setTimeout(function() { parent.$.fn.colorbox.close(); }, 6000);
    setTimeout(function () { $.fn.colorbox({ width: "250px", height: "190px", open: true, html: msg }); }, 700);
    setTimeout(function () { parent.$.fn.colorbox.close(); }, 6000);
}
