﻿var masterHtml = "";
var closeTimer = null;
var gotNums = 0;
var lastGeo = "";
var geoTitle = "";

var chartDir = "/dismal/graphs/geopopup/"
var browser=navigator.appName;
var version = parseFloat(navigator.appVersion);
var textObj="";
var loc=new Array();
var ypos=180;
var yMousePos=0;

$(document).ready( function(){ 
        //Add geopopup to body
        $("body").prepend( "<div id=\"popupwrapper\" onmouseout=\"timedHideGeoInfo()\" onmouseover=\"killTimer()\"><div id=\"geocontainer\" class=\"geocontainer\"><div id=\"innergeocontainer\"><div class=\"geoheader\">Pennsylvania</div><div class=\"innermostgeocontainer\"><div id=\"TabbedPanels1\" class=\"TabbedPanels\"><div class=\"geopanelscontentGroup\"><div id= \"GeoChart0\" class=\"geopanelscontent\" val=\"0\"></div><div id= \"GeoChart1\" class=\"geopanelscontent selectPanel\" val=\"1\"></div><div id= \"GeoChart2\" class=\"geopanelscontent\" val=\"2\"></div><div id= \"GeoChart3\" class=\"geopanelscontent\" val=\"3\"></div></div><div><ul class=\"TabbedPanelsTabGroup\"><li class=\"TabbedPanelsTab\" tabindex=\"0\" onclick=\"showGeoChart(0)\" >3 Months </li><li class=\"TabbedPanelsTab selectTab\" tabindex=\"1\" onclick=\"showGeoChart(1)\">1 Year</li><li class=\"TabbedPanelsTab\" tabindex=\"0\" onclick=\"showGeoChart(2)\">5 Years</li><li class=\"TabbedPanelsTab\" tabindex=\"0\" onclick=\"showGeoChart(3)\">10 Years</li></ul></div></div><br clear=\"all\"><div class=\"geoTableCon\"><table cellspacing=\"1\" cellpadding=\"0\" class=\"table_popup\"><tr><td width=\"141\"  align=\"left\"><img src=\"/dismal/images/geopopup/snapshot.gif\" alt=\"At a Glance\" class=\"snapshot\"/><span class=\"glncSpan\">At a Glance:</span></td><td width=\"48\" align=\"right\"><div id=\"geoColumn\" class=\"geopopuptitle\">PA</div></td><td width=\"48\" align=\"right\"><div class=\"geopopuptitle\">US</div></td><td width=\"34\" align=\"right\"><div class=\"geopopuptitle\">Date</div></td></tr><tr><td align=\"left\">Unemployment Rate</td><td id=\"lbrGeo\">-</td><td id=\"lbrUS\">-</td><td id=\"lbrDate\">4/08</td></tr><tr><td align=\"left\">Personal Income, % chg yr ago </td><td id=\"YTYPCTypqGeo\">-</td><td id=\"YTYPCTypqUS\">-</td><td id=\"YTYPCTypqDate\">-</td></tr><tr><td align=\"left\">Retail Sales, % chg yr ago </td><td id=\"FRTFSQGeo\">-</td><td id=\"FRTFSQUS\">-</td><td id=\"FRTFSQDate\">-</td></tr><tr><td align=\"left\">Median House Price</td><td id=\"rhx1medqGeo\">-</td><td id=\"rhx1medqUS\">-</td><td id=\"rhx1medqDate\">-</td></tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;% chg yr ago</td><td id=\"YTYPCTRHX1MEDQGeo\">-</td><td id=\"YTYPCTRHX1MEDQUS\">-</td><td id=\"YTYPCTRHX1MEDQDate\">-</td></tr></table></div><div id=\"researchLinkDiv\" class=\"researchLinkDiv\"></div></div></div></div></div>");
});

//Reset the charts to hide all charts then show the first chart.
function resetCharts( )
{
    //This will hide all the charts
    $(".geopanelscontent").each( function() {
        var $$ = $(this);
        $$.css({
			"opacity": 0,
			"display": "none"
		});
        $$.removeClass( "selectPanel" );
    });
    //This will remove the select class from all the select options
    $(".TabbedPanelsTab").each( function() {
        var $$ = $(this);
        $$.removeClass( "selectTab" );
    });
        
    //This will force the first chart to be shown.
    $("#GeoChart1").css({
		"opacity": 1,
		"display": "inline"
	});
    //This will set the selecting text to 1 year
    $(".TabbedPanelsTab:eq(1)").addClass( "selectTab" );
    
    
}

//Pull the geos data from the xml and display it in the proper areas.
//This is the function that will be called externally
function getXMLGeoNums( geo){
    if( (gotNums == 0) || (lastGeo != geo)){
        $.ajax({
            url: "/dismal/backend/overlay_backend.ashx",
            type: "post",
            async: false,
            data: { geo: geo, cmd: 3 },
            dataType: "json",
            success: function( o ){
                //Set the values for the geos
                $("#lbrGeo").attr("innerHTML", o[0]);
                $("#YTYPCTypqGeo").attr("innerHTML", o[2]);
                $("#FRTFSQGeo").attr("innerHTML", o[4]);
                $("#rhx1medqGeo").attr("innerHTML", o[6]);
                $("#YTYPCTRHX1MEDQGeo").attr("innerHTML", o[8]);
                
                //Set the values for the us
                $("#lbrUS").attr("innerHTML", o[10]);
                $("#YTYPCTypqUS").attr("innerHTML", o[11]);
                $("#FRTFSQUS").attr("innerHTML", o[12]);
                $("#rhx1medqUS").attr("innerHTML", o[13]);
                $("#YTYPCTRHX1MEDQUS").attr("innerHTML", o[14]);
                
                //Set the values for the date                
                $("#lbrDate").attr("innerHTML", o[1]);
                $("#YTYPCTypqDate").attr("innerHTML", o[3]);
                $("#FRTFSQDate").attr("innerHTML", o[5]);
                $("#rhx1medqDate").attr("innerHTML", o[7]);
                $("#YTYPCTRHX1MEDQDate").attr("innerHTML", o[9]);
  
            }
        });    
        gotNums=1;
        lastGeo = geo;
    }else{
    }
}

//The main show, displays the showgeoinfo
function showGeoInfo(e, geo, that, title){
    //Reset the charts to hide all divs and then reshow the current div only
    if( (gotNums == 0) || (lastGeo != geo)){
		resetCharts();
    }
    //See if the geopopup is visible, if it isn't do fun stuff
    if( $(".popupwrapper:visible").is("*") != true ){
        if (document.layers) {
            // Firefox / opera /safari / chrome
            xMousePos = e.pageX;
            yMousePos = e.pageY;
            yMousePosMax = window.innerHeight+window.pageYOffset;
        } else if (document.all) {
            //IE because it can't play well with others.
            yMousePosMax = document.documentElement.clientHeight+document.documentElement.scrollTop;
            yMousePos = window.event.clientY+document.documentElement.scrollTop;
            xMousePos = window.event.clientX+document.documentElement.scrollLeft;
            
        } else if (document.getElementById) {
            // Netscape 6 - .... we'll call that legacy
            xMousePos = e.pageX;
            yMousePos = e.pageY;
            yMousePosMax = window.innerHeight+window.pageYOffset;
        }
        //Find the location of the element you moused over
        loc = findPos(that);
        
        //Set the object instance
        textObj = that;

        //Calculate the difference between the 
        //mouse position and the object position
        //Note: This is a mostly a fix for IE
		var yIEpos = Math.abs(eval(loc[1] - yMousePos));
		var xIEpos = Math.abs(eval(loc[0] - xMousePos));        

		//If the Y difference is 
        if ( yIEpos >= 20){loc[1] = eval(loc[1]+20);}
        
        //If the X difference is 
        if ( xIEpos >= 400){loc[0] = eval(loc[0]-490);}   
        
        //
        if ( loc[1] + 370 >= yMousePosMax ){
            $("#popupwrapper").css('top', loc[1] - 373);
        }else{       
            $("#popupwrapper").css('top', loc[1] + 20 );
        }
            
        //
        $("#popupwrapper").css('left', (loc[0]) - 15);              
        
        //Figure out the bottom of the window
        winBot= yMousePosMax;
              
        //Figure out the top of the window.
        winTop = eval(yMousePosMax - document.documentElement.clientHeight);
        
        //Check the bottom of the window v.s. 
        //the location of the moused over element
        spacBot = winBot - loc[1];
        //The top space is calcuated by the positon of the elment (Y of course)
        // minus the location of the top of the window
        spacTop = loc[1] - winTop;
        //NOTE: Both of the above already factor in scroll positon
        //      you do not need to calcuate this in advance
        
        //
        if (spacTop < 350){$("#popupwrapper").css('top', loc[1] + 20 );}
        
        //Set the title
        $(".geoheader").attr("innerHTML", title);
        
        //Set the four charts
        $("#GeoChart0").attr("innerHTML", "<img src=\"" + chartDir + "EMP3MO"+ geo + ".png\">");
        $("#GeoChart1").attr("innerHTML", "<img src=\"" + chartDir + "EMP1yr" + geo + ".PNG\">");
        $("#GeoChart2").attr("innerHTML", "<img src=\"" + chartDir + "EMP5yr" + geo + ".PNG\">");
        $("#GeoChart3").attr("innerHTML", "<img src=\"" + chartDir + "EMP10yr" + geo + ".PNG\">");
        
        //Set the data table's column header for the geo
        $("#geoColumn").attr("innerHTML", geo);

        //Get the geo values from the xml
        getXMLGeoNums(geo);
        
        //Show the geopopup
        $("#popupwrapper").show();
        $("#popupwrapper").css("display", "inline");
        
        //Fill in the buy the research section, 
        $("#researchLinkDiv").attr("innerHTML", "<img src=\"/dismal/images/geopopup/cart.gif\" width=\"18\" height=\"19\" alt=\"Get Full Research\" style=\"margin-bottom:-5px\"/> ");
        $("#researchLinkDiv").append("<a href=\"http://www.economy.com/home/products/snapshot/us/state.aspx?g=" + geo + "&src=geopopup\" target=\"blank\">Get Full Report </a>");

        //If the browser is IE and the popup would overrun the side bar then hide all the selects on the page.
        if ((document.all) && ((285 + loc[0]) >= 823)) { selectHide(); }
    }else{
        //If the popup is visible a new overlay needs to be created
        //Hide the previous popup
        hideGeoInfo();
        //Show the new popup
        showGeoInfo(geo);
    }
}

//Hide the geopopup
function hideGeoInfo(){
    //Hide the geopopup if the timer fires 
    //or if you mouse over a new popup
    $("#popupwrapper").hide();
    //IF ie reshow the selects
    if (document.all) {
        selectShow();
    }
}

//Set the close out timer
function timedHideGeoInfo(){
    closeTimer = setTimeout( hideGeoInfo, 500 );
}

//Kill the close timer for the popup
function killTimer(){
    clearTimeout( closeTimer );
}

//Show # num chart
function showGeoChart(num){
    clearGeoChart();
    toc=$(".geopanelscontent")[num];
    tic=$(".TabbedPanelsTab")[num];
    $(toc).addClass( "selectPanel" );
    $(toc).show().css("opacity", "1");
    $(tic).addClass( "selectTab" );
}

//clear out the class from all the charts
function clearGeoChart(){
    for( var i = 0; i < 4; i++ ){
        toc=$(".geopanelscontent")[i];
        tic=$(".TabbedPanelsTab")[i];
        $(toc).hide().css("opacity", "0");
        $(toc).removeClass( "selectPanel" );
        $(tic).removeClass( "selectTab" );
    }
}

//Find the position of the term and put the popup over it.
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

