﻿//Parameter Class Names used to grab JQuery
var datesFilter = "da_ipt"; //Date awarded parameter = da
var goalsFilter = "goals_ipt"; //Goals parameter = goals
var topicsFilter = "topics_ipt"; //Topics parameter = topics
var techFilter = "tech_ipt"; //Technology parameter = techs
var continentsFilter = "continent_ipt"; //Continents parameter = continents
var continentMapId;
var continentMap;

var filterArray = new Array(datesFilter, goalsFilter, topicsFilter, techFilter, continentsFilter);

var plotMapBase = "/SiteCollectionDocuments/mapPlot.aspx";
var mapResultsBase = "/SiteCollectionDocuments/mapResults.aspx";
var plotMapZoom = "/SiteCollectionDocuments/mapPlot_zoom.aspx";

//Onload
$(document).ready(function() {
    //Grab all input box sections and hide
    var filterDivs = $('#filterContainer > div > div');
    filterDivs.hide();

    //Grab Headings and expando parts & apply expand/collapse functionality
    var filterAnchors = $('#filterContainer > div > a').click(function() {
        var styleElm = $(this).find("span");
        (styleElm.html() == '+') ? styleElm.html('-') : styleElm.html('+');
        $(this).next('div').slideToggle('medium').siblings('div:visible').slideUp('fast');

    }); ;

    //Check sub checkbox is parent is checked
    $('#goalsFilters > ul > li > input').click(function() {
        var ste = $(this).attr('checked');
        var checkboxes = $(this).next().find('input');

        if (checkboxes.length > 0) {
            checkboxes.each(function(i) {
                if (ste)
                    $(this).attr('checked', 'checked');
                else
                    $(this).removeAttr('checked');
            });
        }
    });

    //Set Clear Filter Button
    $('#clearFilter').click(function() { window.location = location.pathname });

    //Apply the querystring for the filter button
    $('#applyFilter').click(function() {
        var qs = "";
        for (i = 0; i < filterArray.length; i++) {
            var selectedInputs = $("." + filterArray[i] + ":checked");
            if (selectedInputs.length > 0) {
                qs += filterArray[i].substring(0, filterArray[i].indexOf('_ipt')) + '=-';
                qs += getQueryStringParams(selectedInputs);
                if (filterArray[i] != filterArray[filterArray.length - 1])
                    qs += "&";
            }
        }

        if (qs.length > 0) {
            qs = (qs.lastIndexOf('&') == (qs.length - 1)) ? qs.substring(0, qs.length - 1) : qs;
            //this is such a bullshit hack
            if (qs.indexOf("topics") > -1) {
                //alert("topic");
                if (qs.indexOf("goals") > -1) {
                    qs = qs.replace('goals', 'goal');
                }
            }
            if (qs.indexOf("topics") == -1) {
                if (qs.indexOf("goals") > -1) {
                    qs = qs + "&topics=-20-";
                }
            }
            //for new Goal Contra 5/4/10
            if (qs.indexOf("goal") > -1) {
                var expGoal;
                var expTop = qs.substring(qs.indexOf("&topics") + 8);
                //goals-find qs variables
                if (qs.indexOf("goals=") > -1) {
                    expGoal = qs.substring(qs.indexOf("goals=") + 6);
                    if (expGoal.indexOf("&") > -1) {
                        expGoal = expGoal.substring(0, expGoal.indexOf("&"));
                    }
                }
                //goal-find qs variables
                else {
                    expGoal = qs.substring(qs.indexOf("goal=") + 5)
                    if (expGoal.indexOf("&") > -1) {
                        expGoal = expGoal.substring(0, expGoal.indexOf("&"));
                    }
                }
                //add in Topic 23
                if (expGoal.indexOf("-10-") > -1) {
                    //find value of topics
                    if (expTop.indexOf("&") > -1) {
                        expTop = expTop.substring(0, expTop.indexOf("&"));
                    }
                    expTop.replace("-20-", "");
                    var expTopNew = expTop + "-23-";
                    qs = qs.replace(expTop, expTopNew);
                    //alert(qs);
                    qs = qs.replace("-20--23", "-23-");
                    qs = qs.replace("--", "-");
                }
            }

            //alert(qs);
            //end bullshit hack
            var zoom_cont_src = document.getElementById("mapPlotFrame").src;

            if (qs.toLowerCase().indexOf("continent=") > -1) {
                var contMapStart = qs.toLowerCase().indexOf("continent=-");
                continentMapId = qs.substring(parseInt(contMapStart) + 11, parseInt(contMapStart) + 12);
                convertContinentId(continentMapId);

                qs = qs + "&cont=" + continentMap;
                $("#mapPlotFrame").attr("src", plotMapZoom + "?" + qs);
                $("#resultsFrame").attr("src", mapResultsBase + "?" + qs);
            }
            else if (zoom_cont_src.toLowerCase().indexOf("cont=") > -1) {
                var zoom_cont_start = zoom_cont_src.toLowerCase().indexOf("cont=") + 5;
                var zoom_cont = zoom_cont_src.substring(zoom_cont_start);
                $("#mapPlotFrame").attr("src", plotMapZoom + "?" + qs + "&cont=" + zoom_cont);

                //NEW: so when you zoom the filters stay on the same continent
                var resultsQS = zoom_cont;
                //alert(resultsQS);
                resultsQS = resultsQS.replace("africa", "&continent=-1-");
                resultsQS = resultsQS.replace("asia", "&continent=-2-");
                resultsQS = resultsQS.replace("europe", "&continent=-3-");
                resultsQS = resultsQS.replace("oceania", "&continent=-4-");
                resultsQS = resultsQS.replace("north america", "&continent=-5-");
                resultsQS = resultsQS.replace("south america", "&continent=-6-");

                $("#resultsFrame").attr("src", mapResultsBase + "?" + qs + resultsQS);
            }
            else {
                $("#mapPlotFrame").attr("src", plotMapBase + "?" + qs);
                $("#resultsFrame").attr("src", mapResultsBase + "?" + qs);
            }
        }
        else
        { alert('You must specify search criteria.'); }
    });
    if (location.search.length > 1)
        setCheckBoxes();
});

function convertContinentId(mapId) {
	if (mapId==1) {
		continentMap = "Africa";
		}
	else if (mapId==2) {
		continentMap = "Asia";
		}
	else if (mapId==3) {
		continentMap = "Europe";
		}
	else if (mapId==4) {
		continentMap = "Oceania";
		}
	else if (mapId==5) {
		continentMap = "North America";
		}
	else if (mapId==6) {
		continentMap = "South America";
		}
	else {
		alert("Where's my map ID?");
		}
	return continentMap;
}

function getQueryStringParams(selectedInputs){
    var rtn = "";
	selectedInputs.each(function(i) {
		rtn += $(this).attr('value') + "-";
	});
			
	return rtn;
}

function setCheckBoxes()
{
	//Set checkboxes from querystring params	
	try
	{
		var params = location.search.substring(1).split('&');
		for(i=0;i<params.length;i++)
		{
			var keyVals = params[i].split('=');		
			var boxValues = keyVals[1].substring(1, keyVals[1].length-1).split('-');
			
			for(j=0;j<boxValues.length;j++)
			{
				$("."+keyVals[0]+"_ipt[value="+boxValues[j]+"]").attr('checked', 'true');
			}
			
			if(keyVals[0] == "topics")
				$("#goalsFilters").show();										
			else	
				$("#"+keyVals[0]+"Filters").show();										
		}
	}catch(err){}
}




// For Plotting
function mouseot() {
	document.getElementById('fullmap').src='/SiteCollectionImages/GrantMapImages/unzoomed_map.gif'
}
function mouseovr(continent) {
	var mapname = "/SiteCollectionImages/GrantMapImages/roll_" + continent + ".gif"
	document.getElementById('fullmap').src=mapname;
}
function zoomin(loc) {	
    //Creating querystring to change the url of the map iframe.
	var qs = (location.search.length>0)? location.search + "&cont=" + loc: "?cont=" + loc;		
	
	var buildUrl = plotMapZoom + qs;
	window.parent.handleResponse(buildUrl);
	
	//Creating querystring to change the url of the results iframe. Since the querystring contains map info, we need to convert this to continents in the results iframe
	var resultsQS = qs;
	resultsQS = resultsQS.replace("cont=africa","continent=-1-");
	resultsQS = resultsQS.replace("cont=asia","continent=-2-");
	resultsQS = resultsQS.replace("cont=europe","continent=-3-");
	resultsQS = resultsQS.replace("cont=oceania","continent=-4-");
	resultsQS = resultsQS.replace("cont=north america","continent=-5-");
	resultsQS = resultsQS.replace("cont=south america","continent=-6-");
	
	var resultsUrl = mapResultsBase + resultsQS;
	parent.resultsFrame.location.replace(resultsUrl);
}

function handleResponse(buildUrl, zoom) {
	if(zoom == 'zoomout')
		$("#mapPlotFrame").attr("src", plotMapBase + buildUrl);
	else
		$("#mapPlotFrame").attr("src", buildUrl);
	    //alert(buildUrl);
}	

function filterResultById(expId) {
	//alert(expId);
	$("#resultsFrame").attr("src", mapResultsBase + "?expId=" + expId);	
}










