var ie = jQuery.browser.msie;
var ie6 = (jQuery.browser.msie && jQuery.browser.version == "6.0");
(function($){
    var base = window.base = {
        context: "",
        siteURL: "",
        cmsAlias: "",
        timeouts: [],
        init: function(url, app, alias) {
            base.context = "/"+ app;
            base.siteURL = url;
            base.cmsAlias = alias;
        },
        events: function(admin) {
            $("#nav2, #nav2 a, #nav2_dropdown").bind("click mouseover",function(e){
                if(this.id != "nav2_dropdown") {
                    e.preventDefault();
                }
                e.stopPropagation();
                if(e.type != "mouseover" && ie6) {
                    document.location = base.context +"/";
                }
                else if(!ie6) {
                    base.dropdown(1);
                }
            });
            if(!admin){
                $("#nav2 a").click(function(e){
                    document.location =  base.context +"/";
                    e.stopPropagation();
                });
            }
            $("body").mouseover(function(e){
                base.dropdown(2);
            });
            try{
                $("[rel=tooltip],[alt=tooltip]").tooltip({
                    track: true,
                    delay: false,
                    showURL: false,
                    showBody: " - ",
                    opacity: 0.85
                });
            }catch(e){}
            $("img[name=report]").click(function(){
                var cc = ($(this).siblings('input')).get(0).value.split(",");
                var w = window.open('about:blank','Report','width=750,height=630,scrollbars,resizable');
                w.document.location =  base.context +"/a/search/viewprofile?talentId="+cc[0]+"&src="+cc[3];
                w.focus();
            });
            $("div[name=report] a").click(function(e){
                e.preventDefault();
                var w = window.open('about:blank','Report','width=750,height=630,scrollbars,resizable');
                w.document.location = this.href;
                w.focus();
            });
            $("#struts_error").each(function(){
                if($(this.innerHTML).hasClass("success")){
                    popup({
                        html: this.innerHTML,
                        height: "110px"
                    });
                }else{
                    popup({
                        title: "Whoops!",
                        html: this.innerHTML,
                        error: true
                    });
                }
            });
            $("div.box input:first").focus();

            //multiple select box handling
            $("._sel_none").click(function(e){
                e.preventDefault();
                $(this).parents("p").find("select").val("");
            });
            $("._sel_all").click(function(e){
                e.preventDefault();
                $(this).parents("p").find("select option").attr("selected",true);
            });
            $("a[rel*=help]").click(function(e){
                e.preventDefault();
                e.stopPropagation();
                if(ie6){
                    $("#help_panel").css({
                        height: $(document.body).height(),
                        top: document.documentElement.scrollTop
                    });
                }
                $("#help_panel").animate({
                    right: "0px"
                });
                $("#help_content").load(this.href);
            });
            $("#help_panel").click(function(e){
                e.stopPropagation();
            });
            $("body,#help_panel div.close").click(function(){
                $("#help_panel").animate({
                    right: "-355px"
                });
            });
        },
        dropdownstate: 1, //up=1; down=2;
        dropdown: function(dir){
            if(!dir) {
                dir = base.dropdownstate;
            }
            if(dir == 1){
                if($.browser.safari || $.browser.mozilla){
                    $("#nav2_dropdown").stop().animate({
                        height: "150px",
                        borderBottom: "5px solid #0B2E50"
                    });
                } else if($.browser.msie) {
                    $("#nav2_dropdown").show();
                } else {
                    $("#nav2_dropdown").css({
                        height: "150px"
                    }).css("border-bottom","5px solid #0B2E50");
                }
                base.dropdownstate = 2;
            }
            else {
                if($.browser.safari || $.browser.mozilla){
                    $("#nav2_dropdown").stop().animate({
                        height: "12px",
                        borderBottom: "2px solid #FFF"
                    },200);
                } else if($.browser.msie) {
                    $("#nav2_dropdown").hide();
                } else {
                    $("#nav2_dropdown").css({
                        height: "12px"
                    }).css("border-bottom","2px solid #FFF");
                }
                base.dropdownstate = 1;
            }
        },
        raphael: function(el){
            function michaelangelo(el){
                if(!$.browser.msie) return;
                var x = $(el).outerWidth();
                var y = $(el).outerHeight();
                if(el.raph){
                    $(el).children("div.raphael").remove();
                }
                if($(el).parents().is("div.tab_nav, div.tab_nav_under")){}
                else if(!$(el).parents().is(":hidden")){
                    var generate = true;
                    var opaque = el.nodeName.match(/button/i);
                    if(el.nodeName.match(/button/i)) {
                        y -= 3;
                    }
                    if(ie6) {
                        if(!el.nodeName.match(/button/i)) {
                            x -= 5;
                        }
                        generate = !(el.className.match(/inputBox/g));
                    }
                    var bgcolor = "#F3F5F7";
                    if(el.className.match(/bggreen/g)) {
                        bgcolor = "#83AC58";
                    }
                    if(el.className.match(/bgblue/g)) {
                        bgcolor = "#274B6E";
                    }
                    if(el.className.match(/bggray/g)) {
                        bgcolor = "#ddd";
                    }

                    if(generate){
                        try {
                            var raphael = $("<div class='raphael'></div>").prependTo(el).get(0);
                            var R = Raphael(raphael,x+10,y+10);
                            el.raph = R.rect(0,0,x,y,10).attr({
                                fill: bgcolor,
                                "fill-opacity": opaque ? 1 : 0.8,
                                stroke: "none"
                            });
                        } catch(e){}
                    }
                }
            }
            if(el) {
                michaelangelo(el);
            }
            else {
                $("div.box, div.inputBox, div.box button").not("#nav2, #nav2_dropdown").each(function(){
                    michaelangelo(this);
                });
            }
        },
        reraph: function(el){
            var par = $(el).parents("div.inputBox").get(0);
            base.raphael(par);
            par = $(el).parents("div.box").get(0);
            base.raphael(par);
        }
    };
    // allow <a> default functionality when wrapping buttons in msie 6-7
    $(function(){
        if($.browser.msie) {
            $('button').click(function(e){
                var a = $(this).parent().get(0);
                if(a && a.href) {
                    e.preventDefault();
                    document.location = a.href;
                }
            });
            base.raphael();
            $(".req").append('<span style="color: red;">*</span>');
        }
    });

})(jQuery);

//prevent console errors in browser that do not support it
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i){
        window.console[names[i]] = function() {};
    }
}
