var elem;
var dots;
var factor = 1;
var leftFactor = 0;
var topFactor = 0;

(function($) {

    $.fn.sort = function(sortAttr, sortDesc) {
        if (typeof (sortAttr) === "undefined") {
            return $(this);
        }
        if (sortAttr == "") {
            return $(this);
        }
        if (typeof (sortAttr) === "string") {

            var retObj = $(this).get().sort(function(a, b) {
                if (typeof ($(a).attr(sortAttr)) === "number") {
                    return $(a).attr(sortAttr) > $(b).attr(sortAttr) ? 1 : -1;
                }
                else {
                    return $(a).attr(sortAttr).toLowerCase() > $(b).attr(sortAttr).toLowerCase() ? 1 : -1;
                }
            });
            if (getSort(sortDesc)) {
                return $(retObj.reverse());
            }
            else {
                return $(retObj);
            }
        }
        if (typeof (sortAttr) === "object") {
            if ((sortAttr).length) {
                var retObj = $(this).get().sort(function(a, b) {
                    var i = 0;
                    var retval = 1;
                    while (i < sortAttr.length) {
                        var al = $(a).attr(sortAttr[i]).toLowerCase();
                        var bl = $(b).attr(sortAttr[i]).toLowerCase();

                        if (al > bl) { retval = 1; break; }
                        if (bl > al) { retval = -1; break; }
                        i++;
                    }
                    return retval;

                });
                if (getSort(sortDesc)) {
                    return $(retObj.reverse());
                }
                else {
                    return $(retObj);
                }
            }
            else {
                var retObj = $(this).get().sort(function(a, b) {
                    var attrLen = 0;
                    for (var v in sortAttr) {
                        var al = $(a).attr(v).toLowerCase();
                        var bl = $(b).attr(v).toLowerCase();
                        if (al > bl) { return (getSort(sortAttr[v])) ? -1 : 1; }
                        if (bl > al) { return (getSort(sortAttr[v])) ? 1 : -1; }
                    }

                });
                return $(retObj);

            }
        }
    }
    function getSort(sortDesc) {
        if (typeof sortDesc == "boolean") {
            return sortDesc;
        }
        else if (sortDesc.toLowerCase() == "desc") {
            return true;
        }
        else return false;
    }
})(jQuery);

function addlines() {
    elem = $('#lines').detach();
    $('#map').after(elem);
    $('#close').animate({ opacity: "1" }, 700);
}
function LoadDots(st) {
    var dt = new Date();
    $.ajax({
        type: "GET",
        url: "data/5bean_pod.xml?date=" + dt.getDate().toString() + dt.getTime().toString(),
        dataType: "xml",
        success: function(xml) {
            var left = 5;
            var dcount = 0;
            $(xml).find('locations').each(function() {
                var ID = $(this).attr('id');
                var img = ID + 'dot.png';
                $(this).find('location').filter('[state="' + st + '"]').sort("x", "desc").each(function() {
                    var xOffset = Number($(this).attr('x'));
                    var yOffset = Number($(this).attr('y'));

                    XX = (xOffset * factor) - leftFactor + left;
                    YY = -19 + (yOffset * factor) - ((375 * factor) - topFactor);

                    left -= 19;

                    var elemDot = $('<span ison="yes" id="divd' + dcount + '" data="' + $(this).find('person').text() + '" town="' + $(this).attr('city') + ', ' + st + '" style="left:' + XX + 'px;top:-' + YY + 'px" class="dot ' + ID + '"></span>').html('<img src="images/' + img + '" border="0"/>');
                    elemDot.appendTo('#dots');
                    wireDotMouseOver($(this), elemDot);
                    dcount++;
                });
                var elem = $('#btnDots' + ID).attr('src');
                if (elem.indexOf('off') == -1)
                    toggledots('#dots .'+ID,ID);

            });
        }
    });

}
function LoadAllDots() {
    var dt = new Date();
    $.ajax({
        type: "GET",
        url: "data/5bean_pod.xml?date=" + dt.getDate().toString() + dt.getTime().toString(),
        dataType: "xml",
        success: function(xml) {
            var left = 0;
            $(xml).find('locations').each(function() {
                var ID = $(this).attr('id');
                var img = ID + 'dot.png';
                $(this).find('location').each(function() {
                    var stateId = $(this).attr('state');
                    var xOffset = Number($(this).attr('zoom_x'));
                    var yOffset = Number($(this).attr('zoom_y'));

                    XX = (xOffset) + left;
                    YY = (yOffset);

                    left -= 19;

                    var elemDot = $('<span ison="yes" data="' + stateId + '" style="left:' + XX + 'px;top:-' + YY + 'px" class="allDots '+ID+'"></span>').html('<img src="images/' + img + '" border="0"/>');
                    elemDot.appendTo('#allDots');
                });
                wireAllDotClicks(ID);
            });
            //$(document).pngFix();
        }
    });

}

function wireAllDotClicks(ID) {
    $('.'+ID).click(function() {
        switch ($(this).attr('data')) {
        	case 'AL':
        		Alabama();
        		break;
        	case 'AZ':
        		Arizona();
        		break;
        	case 'AR':
        		Arkansas();
        		break;	
        	case 'CA':
        		California();
        		break;	
        	case 'CO':
        		Colorado();
        		break;	
        	case 'CT':
        		Connecticut();
        		break;	
        	case 'DE':
        		Delaware();
        		break;	
        	case 'FL':
        		Florida();
        		break;	
        	case 'GA':
        		Georgia();
        		break;	
        	case 'ID':
        		Idaho();
        		break;	
        	case 'IL':
        		Illinois();
        		break;	
        	case 'IN':
        		Indiana();
        		break;	
        	case 'IA':
        		Iowa();
        		break;	
        	case 'KS':
        		Kansas();
        		break;	
        	case 'KY':
        		Kentucky();
        		break;	
        	case 'LA':
        		Louisiana();
        		break;	
        	case 'ME':
        		Maine();
        		break;	
        	case 'MD':
        		Maryland();
        		break;	
        	case 'MA':
        		Massachusetts();
        		break;	
        	case 'MI':
        		Michigan();
        		break;	
        	case 'MN':
        		Minnesota();
        		break;	
        	case 'MS':
        		Mississippi();
        		break;	
        	case 'MO':
        		Missouri();
        		break;	
        	case 'MT':
        		Montana();
        		break;	
        	case 'NE':
        		Nebraska();
        		break;	
        	case 'NV':
        		Nevada();
        		break;	
        	case 'NH':
        		NewHampshire();
        		break;	
        	case 'NJ':
        		NewJersey();
        		break;	
        	case 'NM':
        		NewMexico();
        		break;	
        	case 'NY':
        		NewYork();
        		break;	
        	case 'NC':
        		NorthCarolina();
        		break;	
        	case 'ND':
        		NorthDakota();
        		break;	
        	case 'OH':
        		Ohio();
        		break;	
        	case 'OK':
        		Oklahoma();
        		break;	
        	case 'OR':
        		Oregon();
        		break;	
        	case 'PA':
        		Pennsylvania();
        		break;	
        	case 'RI':
        		RhodeIsland();
        		break;	
        	case 'SC':
        		SouthCarolina();
        		break;	
        	case 'SD':
        		SouthDakota();
        		break;	
        	case 'TN':
        		Tennessee();
        		break;	
        	case 'TX':
        		Texas();
        		break;	
        	case 'UT':
        		Utah();
        		break;	
        	case 'VT':
        		Vermont();
        		break;	
        	case 'VA':
        		Virginia();
        		break;	
        	case 'WA':
        		Washington();
        		break;	
        	case 'WV':
        		WestVirginia();
        		break;	
        	case 'WI':
        		Wisconsin();
        		break;	
        	case 'WY':
        		Wyoming();
        		break;	
        }
    });
}

function wireDotMouseOver(xmldata, dotMO) {
    $(dotMO).hover(function() {
        $("#defaultCopy").fadeOut(400, function() {
            $('#images').children().remove();
            $('#copy').children().slice(1).remove();
            $(xmldata).each(
            function() {
            var tip = "<b>" + $(this).find("person").text() + "</b><br/><em>" + $(this).attr('city') + ', ' + $(this).attr('state') + "</em>";
            var content = '<b>Name:</b><br/>' + $(this).find("person").text() + '<br/>';
            content += '<br/><b>Town:</b><br/>' + $(this).attr('city') + ', ' + $(this).attr('state') + '<br/>';
            //                    content += '<br/><b>Team 5-Bean Member Since:</b><br/>' + $(this).find("Team").text() + '<br/>';
            //                    content += '<br/><b>Number of 5-Bean Pods found:</b><br/>' + $(this).find("Pods").text() + '<br/>';
            //                    content += '<br/><b>Asgrow<sup>&reg;</sup> Genuity<sup>&reg;</sup> Roundup Ready 2 Yield<sup>&reg;</sup> varieties grown:</b><br/>' + $(this).find("Varieties").text() + '<br/>';
            //                    content += '<br/><b>Dealer:</b><br/>' + $(this).find("Dealer").text() + '<br/>';
            $('<p style="display:none;"></p>').html(content).appendTo('#copy').fadeIn(300);

            $(this).find("photo").each(function() {
                $('<img align="center" style="display:none;" src="images/photos/' + $(this).text() + '" />').appendTo('#images').fadeIn(300);
            });

            $(dotMO).append(
             '<div class="toolTipWrapper">'
                + '<div class="toolTipTop"></div>'
                + '<div class="toolTipMid" style="background-image: url(images/beanPod.png); ">'
                  + tip
                + '</div>'
                + '<div class="toolTipBtm"></div>'
              + '</div>'
            );

            $(dotMO).find(".toolTipWrapper").css({ top: -63, left: 25 })
            $(dotMO).find(".toolTipWrapper").fadeIn(0);

            //$('.toolTipMid').pngFix();


            });
        });
        },
        function() {
            $(".toolTipWrapper").remove();
        }
    );

}
function adddots(st) {
    LoadDots(st);
    $('#dots').show(1000);
    $('.dot').show(1000);
}

function Alabama() {
	addlines();
	adddots("AL");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-850px",left: "-805px", opacity: "1" }, 700);
}

function Arizona() {
	addlines();
	adddots("AZ");
    factor = 1700.0 / 650.0;
    leftFactor = 205;
    topFactor = 800;
    elem.animate({ width: "1700px", top: "-800px",left: "-205px", opacity: "1" }, 700);
}

function Arkansas() {
	addlines();
	adddots("AR");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-800px",left: "-705px", opacity: "1" }, 700);
}

function California() {
	addlines();
	adddots("CA");
    factor = 1600.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1600px", top: "-640px",left: "-95px", opacity: "1" }, 700);
}

function Colorado() {
	addlines();
	adddots("C0");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-640px",left: "-305px", opacity: "1" }, 700);
}

function Connecticut() {
	addlines();
	adddots("CT");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-540px",left: "-1055px", opacity: "1" }, 700);
}

function Delaware() {
	addlines();
	adddots("DE");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-540px",left: "-1055px", opacity: "1" }, 700);
}

function Florida() {
	addlines();
	adddots("FL");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-970px",left: "-1005px", opacity: "1" }, 700);
}

function Georgia() {
	addlines();
	adddots("GA");
    factor = 1700.0 / 650.0;
    leftFactor = 975;
    topFactor = 850;
    elem.animate({ width: "1700px", top: "-850px",left: "-975px", opacity: "1" }, 700);
}

function Idaho() {
	addlines();
	adddots("ID");
    factor = 1700.0 / 650.0;
    leftFactor = 145;
    topFactor = 420;
    elem.animate({ width: "1700px", top: "-420px",left: "-145px", opacity: "1" }, 700);
}

function Illinois() {
    addlines();
    adddots("IL");
    factor = 2000.0 / 650.0;
    leftFactor = 570;
    topFactor = (850);
    elem.animate({ width: "2000px", top: "-700px", left: "-900px", opacity: "1" }, 700);
}

function Indiana() {
    addlines();
    adddots("IN");
    factor = 1900.0 / 650.0;
    leftFactor = 900;
    topFactor = (670);
    elem.animate({ width: "1900px", top: "-670px", left: "-900px", opacity: "1" }, 700);
}

function Iowa() {
    addlines();
    adddots("IA");
    factor = 1700.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1700px", top: "-550px", left: "-600px", opacity: "1" }, 700);
}

function Kansas() {
	addlines();
	adddots("KS");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-670px",left: "-455px", opacity: "1" }, 700);
}

function Kentucky() {
    addlines();
    adddots("KY");
    factor = 1800.0 / 650.0;
    leftFactor = 830;
    topFactor = (920);
    elem.animate({ width: "1800px", top: "-700px", left: "-930px", opacity: "1" }, 700);
}

function Louisiana() {
	addlines();
	adddots("LA");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-850px",left: "-705px", opacity: "1" }, 700);
}

function Maine() {
	addlines();
	adddots("ME");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-380px",left: "-1055px", opacity: "1" }, 700);
}

function Maryland() {
    addlines();
    adddots("MD");
    factor = 2000.0 / 650;
    leftFactor = 1350;
    topFactor = (600);
    elem.animate({ width: "2000px", top: "-600px", left: "-1350px", opacity: "1" }, 700);
}

function Massachusetts() {
	addlines();
	adddots("MA");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-540px",left: "-1055px", opacity: "1" }, 700);
}

function Michigan() {
    addlines();
    adddots("MI");
    factor = 2000.0 / 650.0;
    leftFactor = 570;
    topFactor = (850);
    elem.animate({ width: "2000px", top: "-520px", left: "-950px", opacity: "1" }, 700);
}

function Minnesota() {
    addlines();
    adddots("MN");
    factor = 1700.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1700px", top: "-400px", left: "-550px", opacity: "1" }, 700);
}

function Mississippi() {
	addlines();
	adddots("MS");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-850px",left: "-815px", opacity: "1" }, 700);
}

function Missouri() {
    addlines();
    adddots("MO");
    factor = 1700.0 / 650.0;
    leftFactor = 360;
    topFactor = (860);
    elem.animate({ width: "1700px", top: "-660px", left: "-580px", opacity: "1" }, 700);
}

function Montana() {
    addlines();
    adddots("MT");
    factor = 1800.0 / 650.0;
    leftFactor = 255;
    topFactor = (400);
    elem.animate({ width: "1800px", top: "-400px", left: "-255px", opacity: "1" }, 700);
}

function Nebraska() {
    addlines();
    adddots("NE");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 650;
    elem.animate({ width: "2300px", top: "-700px",left: "-750px", opacity: "1" }, 700);
}

function Nevada() {
    addlines();
    adddots("NV");
    factor = 1800.0 / 650;
    leftFactor = 60;
    topFactor = (680);
    elem.animate({ width: "1800px", top: "-680px", left: "-60px", opacity: "1" }, 700);
}

function NewHampshire() {
	addlines();
	adddots("NH");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-380px",left: "-1055px", opacity: "1" }, 700);
}

function NewJersey() {
    addlines();
    adddots("NJ");
    factor = 2000.0 / 650;
    leftFactor = 1350;
    topFactor = (600);
    elem.animate({ width: "2000px", top: "-600px", left: "-1350px", opacity: "1" }, 700);
}

function NewMexico() {
    addlines();
    adddots("NM");
    factor = 1800.0 / 650.0;
    leftFactor = 255;
    topFactor = (400);
    elem.animate({ width: "1800px", top: "-890px", left: "-255px", opacity: "1" }, 700);
}

function NewYork() {
    addlines();
    adddots("NY");
    factor = 1700.0 / 650.0;
    leftFactor = 360;
    topFactor = (860);
    elem.animate({ width: "1700px", top: "-460px", left: "-980px", opacity: "1" }, 700);
}

function NorthCarolina() {
    addlines();
    adddots("NC");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-800px", left: "-1025px", opacity: "1" }, 700);
}

function NorthDakota() {
    addlines();
    adddots("ND");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-400px", left: "-500px", opacity: "1" }, 700);
}

function Ohio() {
    addlines();
    adddots("OH");
    factor = 2000.0 / 650.0;
    leftFactor = 1070;
    topFactor = (650);
    elem.animate({ width: "2000px", top: "-650px", left: "-1070px", opacity: "1" }, 700);
}

function Oklahoma() {
	addlines();
	adddots("OK");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-760px",left: "-475px", opacity: "1" }, 700);
}

function Oregon() {
    addlines();
    adddots("OR");
    factor = 1800.0 / 650;
    leftFactor = 30;
    topFactor = (500);
    elem.animate({ width: "1800px", top: "-500px", left: "-30px", opacity: "1" }, 700);
}

function Pennsylvania() {
    addlines();
    adddots("PA");
    factor = 2000.0 / 650;
    leftFactor = 1350;
    topFactor = (600);
    elem.animate({ width: "2000px", top: "-600px", left: "-1350px", opacity: "1" }, 700);
}

function RhodeIsland() {
	addlines();
	adddots("RI");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-540px",left: "-1055px", opacity: "1" }, 700);
}

function SouthCarolina() {
    addlines();
    adddots("SC");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-800px", left: "-1025px", opacity: "1" }, 700);
}

function SouthDakota() {
    addlines();
    adddots("SD");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-500px", left: "-500px", opacity: "1" }, 700);
}

function Tennessee() {
    addlines();
    adddots("TN");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-800px", left: "-880px", opacity: "1" }, 700);
}

function Texas() {
    addlines();
    adddots("TX");
    factor = 1600.0 / 650.0;
    leftFactor = 255;
    topFactor = (400);
    elem.animate({ width: "1600px", top: "-905px", left: "-455px", opacity: "1" }, 700);
}

function Utah() {
    addlines();
    adddots("UT");
    factor = 1800.0 / 650;
    leftFactor = 105;
    topFactor = (680);
    elem.animate({ width: "1800px", top: "-680px", left: "-105px", opacity: "1" }, 700);
}

function Vermont() {
	addlines();
	adddots("VT");
    factor = 1700.0 / 650.0;
    leftFactor = 100;
    topFactor = 450;
    elem.animate({ width: "1700px", top: "-380px",left: "-1055px", opacity: "1" }, 700);
}

function Virginia() {
    addlines();
    adddots("VA");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-700px", left: "-1025px", opacity: "1" }, 700);
}

function Washington() {
    addlines();
    adddots("WA");
    factor = 1800.0 / 650;
    leftFactor = 30;
    topFactor = (400);
    elem.animate({ width: "1800px", top: "-400px", left: "-30px", opacity: "1" }, 700);
}

function WestVirginia() {
    addlines();
    adddots("WV");
    factor = 1800.0 / 650.0;
    leftFactor = 300;
    topFactor = (650);
    elem.animate({ width: "1800px", top: "-700px", left: "-1025px", opacity: "1" }, 700);
}

function Wisconsin() {
    addlines();
    adddots("WI");
    factor = 2000.0 / 650.0;
    leftFactor = 570;
    topFactor = (850);
    elem.animate({ width: "2000px", top: "-520px", left: "-900px", opacity: "1" }, 700);
}

function Wyoming() {
    addlines();
    adddots("WY");
    factor = 1800.0 / 650.0;
    leftFactor = 255;
    topFactor = (400);
    elem.animate({ width: "1800px", top: "-550px", left: "-255px", opacity: "1" }, 700);
}

