var oStatesTool = {

    sSelState:null,
    oTitles:{},
    aNEStr:['ne_','_zoom'],
    oStateData:{},
    oStateSet:{},
    sDfltEmailSbj:null,
    sImgPath:'images/state_tool',
    
    init:function(o,oNEInject,oNEZoomed) {
        var oSelf = this; //Scope reference

        //Insert NE Shape, remove, add logic
        $(o).append($($(oNEInject).children()));
        $(oNEInject).remove();

        $($(o).children()).each(function(i) { 
            var sTitle = $(this).attr('title');
            oSelf.oTitles[sTitle] = $(this).attr('title');

            $(this).bind('mouseover',function(){ oSelf.activate(oSelf.oTitles[sTitle],this,1); })
            $(this).bind('mouseout',function(){ oSelf.deactivate(oSelf.oTitles[sTitle],this,1); })
            $(this).bind('click',function(){ oSelf.set(oSelf.oTitles[sTitle],this,1); return false; });
        });

        //Remove default ne areas
        $('.ne').remove();

        //Set-up Zoomed
        $($(oNEZoomed).children()).each(function(i) { 
            var sTitle = $(this).attr('title');
            $(this).bind('mouseover',function(){ oSelf.activate(sTitle,this,2); })
            $(this).bind('mouseout',function(){ oSelf.deactivate(sTitle,this,2); })
            $(this).bind('click',function(){ oSelf.set(sTitle,this,2); return false; });
        })

        //Gather content from non-JS
        $('.statesContent_data > div').each(function() { oSelf.oStateData[this.className.split(' ')[0]] = $(this).html(); });

        //Initialize for message preview in modal
        this.oStateSet['stateYNFlag'] = 0;
    },

    activate:function(sState,o,iType) {
        var sActState = this.formatState(sState);
        $('.'+((iType==2)?this.aNEStr[0]:'')+'map_active').css({'background':"url('"+this.sImgPath+"/states_active/"+sActState+((iType==2)?this.aNEStr[1]:'')+".gif')"}).fadeIn('fast');
    },

    deactivate:function(sState,o,iType) {
        $('.'+((iType==2)?this.aNEStr[0]:'')+'map_active').css({'background':'none'}).hide();

        var sDeactState = this.formatState(sState);
        $('.'+((iType==2)?this.aNEStr[0]:'')+'map_active_back').css({'background':"url('"+this.sImgPath+"/states_active/"+sDeactState+((iType==2)?this.aNEStr[1]:'')+".gif')"}).show();
        $('.'+((iType==2)?this.aNEStr[0]:'')+'map_active_back').css({'background':"url('"+this.sImgPath+"/states_active/"+sDeactState+((iType==2)?this.aNEStr[1]:'')+".gif')"}).fadeOut('slow');
    },

    set:function(sState,o,iType) {

        //No need to set twice
        if(sState == this.oStateSet['state']) return false;

        this.sSelState = this.formatState(sState);

        //Set HTML
        $('.WrapperStateTool > .content').hide().html(this.oStateData[this.sSelState]).fadeIn();
        //Set Header BG + HTML
        $('.WrapperStateTool h4').hide().css({'background':'url('+this.sImgPath+'/statereq_headers/'+this.sSelState+'.gif) no-repeat','margin':'50px 0 0 0'}).html(sState+' results').fadeIn();
        /*$('.WrapperStateTool').css({'padding-top':'20px'});*/

        //Strore State/Set content
        this.getStateContent(sState);

        //Turn on Email
        $('.WrapResultContentTop').find('.Email').show();

        //Map UI
        $('.map_set').css({'background':"url('"+this.sImgPath+"/states_active/"+this.sSelState+".gif')"});
        $('.'+this.aNEStr[0]+'map_set').css({'background':"url('"+this.sImgPath+"/states_active/"+this.sSelState+this.aNEStr[1]+".gif')"});
    },

    //Format state name to naming convention for images, all lower case no space
    formatState:function(sState) { return this.oTitles[sState].toString().toLowerCase().replace(/[\.+\s+]+/g,''); },

    getStateContent:function(sState) {
        //Get & store data for STAF
        this.oStateSet['state'] = sState;
        this.oStateSet['stateParam'] = escape(sState);

        if(/no requirements\./g.test(this.oStateData[this.sSelState])) 
            this.oStateSet['stateYNFlag'] = 0;
        else
            this.oStateSet['stateYNFlag'] = 1;


    },

    preset:function() {
        var sSetState;
        var aSetState = location.search.replace(/\?/,'').split('&');
        for(var i=0;i<aSetState.length;i++) { if(aSetState[i].split('=')[0]=='setState') { sSetState = aSetState[i].split('=')[1].replace(/[^a-zA-Z]+/,' '); break; } }
        if(oStatesTool.oTitles[sSetState]) oStatesTool.set(sSetState);
    }
}


var oToolTip = function(oTipEl) {
    var oTitles = {};//Store titles
    var iTimeOut;//Tip timeout storgate
    this.oTipEl =  oTipEl;//Tip ref on intstantiation

    this.init = function(a) {
        var oSelf = this;

        //Store title in object, bind CTA
        a.each(function(i) {
            oTitles[i] = $(this).attr('title');
            $(this).bind("mouseover",function(e){ oSelf.show(e,i,this); }).bind("mouseout",function(){ oSelf.hide(this,i); })
        });
    }

    this.hide = function(o,i) {
        clearTimeout(iTimeOut);
        this.oTipEl.hide();
        $(o).attr({'title':oTitles[i]});
    }

    this.show = function(e,i,oA) {
        var oSelf = this;
        //Clear prev tip
        clearTimeout(iTimeOut);
        //Clear title
        oA.setAttribute('title','');
        //Set Content
        $('.content',this.oTipEl).html(oTitles[i]);
        //Delay Tip
        iTimeOut = setTimeout(function() { oSelf.setPos(oA); },500);
    }

    this.setPos = function(oA) {
        this.oTipEl.css({top:parseInt(oA.coords.split(',')[1])-35,left:parseInt(oA.coords.split(',')[0])-15}).show();
    }
}

var oNEZoom = {
    iTimer:null, 

    init:function() {    
        var oSelf = this;
        $('.bindNE').bind('mouseover',function() {oSelf.show();});
    },

    show:function() {
        var oSelf = this;
        clearTimeout(this.iTimer);

        $('.ne_map_wrapper').fadeIn('fast',function(e) { 
            $('.ne_map_faux').show(); 
            $('.ne_map_set').show(); 
            $('.ne_map_wrapper').bind("mouseout",function(e){ oSelf.trytohide(e); }); 
            if($.browser.msie) $('.ne_map_wrapper').bind("mouseover",function(e){ oSelf.trytohide(e,1); }); 
            oSelf.iTimer = setTimeout(function() {oSelf.hide()}, 500);
        });
    },

    //Check
    trytohide:function(e,b) {
        clearTimeout(this.iTimer);
        //Cross-platform
        var relTarg = ($.browser.msie) ? (b) ? e.fromElement : e.toElement : e.relatedTarget;

        var oWrapper = $('.ne_map_wrapper')[0];
        var b = false;
        $('.ne_map_wrapper').find('*').each(function() { if (this == relTarg || relTarg == oWrapper) { b=true; return; } });
        if (b) return;

        //Default
        this.hide();
    },

    //Hide tip
    hide:function() { 
        $('.ne_map_faux').hide();
        $('.ne_map_set').hide(); 
        $('.ne_map_wrapper').hide('fast',function() { $('.ne_map_wrapper').unbind(); });
    }
}


$(document).ready(function() {

    if(bAccessibleSet) return false;

    //Instantiate tips
    var oTip = new oToolTip($('#tooltip'));
    oTip.init($('.bindTip'));

    var oTip_ne = new oToolTip($('#tooltip_ne'));
    oTip_ne.init($('.bindTip_ne'));

    //Instantiate tips
    oStatesTool.init('#state_map','#ne_map_inject','#ne_map');

    //Init Zoom NE
    oNEZoom.init();

    //States Data hide
    $('.statesContent_data').hide();

    //Check for preset
    oStatesTool.preset();
});
