﻿

var sortHomeLat = "0";
var sortHomeLng = "0";
var sortHomeShow = "1";


var sortWorkLat = "0";
var sortWorkLng = "0";
var sortWorkShow = "1";

var sortRegionLat = "0";
var sortRegionLng = "0";
var sortCurrentHomeRegionName = "Select your "+sortRegionName; //city or neighborhood name seleted
var sortCurrentHomeRegionID = "0";

var sortCurrent = -1; //0=none 1=home 2=work 3=region

var cookieWarning = false;
var cookiesEnabled = false;
var cookiesLoaded = false;

var thisSortPoint;

function sortLinkClick(mSort){
//0=none 1=home 2=work 3=region
    var itemp = 0;
  itemp = parseInt(mSort);
   //alert(mSort);
    
        switch (itemp){
        
            case 0: //none
          // alert("0")
                document.getElementById("sortLink0").innerHTML = sortNoName
                document.getElementById("sortLink1").innerHTML = '<img src="images/map-Home_icon.gif"/>&nbsp;<a id="sortHomeLnk" href="#" onclick="sortLinkClick(1);;return false;" title="Close to my '+ sortHomeName + '">'+ sortHomeName + '</a>'
                document.getElementById("sortLink2").innerHTML = '<img src="images/map-Work_icon.gif"/>&nbsp;<a id="sortWorkLnk" href="#" onclick="sortLinkClick(2);;return false;" title="Close to my '+ sortWorkName + '">'+ sortWorkName + '</a>'
                document.getElementById("sortLink3").innerHTML = '<a id="sortRegionLnk" href="#" onclick="sortLinkClick(3);;return false;" title="'+sortCurrentHomeRegionName+'">'+ sortRegionName + '</a>'
                saveSortMode(itemp);
               // document.getElementById("sortTitle").innerHTML = sortNoTitle;
               
                break;
            case 1: //home
           // alert("1")
                document.getElementById("sortLink0").innerHTML = '<a id="sortNoneLnk" href="#" onclick="sortLinkClick(0);;return false;" title="Location is not important">'+ sortNoName + '</a>'
                document.getElementById("sortLink1").innerHTML =  '<img src="images/map-Home_icon.gif"/>&nbsp;'+sortHomeName 
                document.getElementById("sortLink2").innerHTML = '<img src="images/map-Work_icon.gif"/>&nbsp;<a id="sortWorkLnk" href="#" onclick="sortLinkClick(2);;return false;" title="Close to my '+ sortWorkName + '">'+ sortWorkName + '</a>'
                document.getElementById("sortLink3").innerHTML = '<a id="sortRegionLnk" href="#" onclick="sortLinkClick(3);;return false;" title="'+sortCurrentHomeRegionName+'">'+ sortRegionName + '</a>'
                if (sortHomeLat != "0" && sortHomeLng != "0"  ) {
                    saveSortMode(itemp);
                   // document.getElementById("sortTitle").innerHTML = sortHomeTitle;
                }
                else {//not set yet
                    window.open('mapIt.aspx?id='+wiynBookCode+'&sortID=1','WIYNMapIt','menubar=0, resizable=1,location=0, width=700, height=620');
                               
                }
                 break;
                 
            case 2: //work
           //alert("2")
                document.getElementById("sortLink0").innerHTML = '<a id="sortNoneLnk" href="#" onclick="sortLinkClick(0);;return false;" title="Location is not important">'+ sortNoName + '</a>'
                document.getElementById("sortLink1").innerHTML = '<img src="images/map-Home_icon.gif"/>&nbsp;<a id="sortHomeLnk" href="#" onclick="sortLinkClick(1);;return false;" title="Close to my '+ sortHomeName + '">'+ sortHomeName + '</a>'
                document.getElementById("sortLink2").innerHTML =  '<img src="images/map-Work_icon.gif"/>&nbsp;'+sortWorkName 
                 document.getElementById("sortLink3").innerHTML = '<a id="sortRegionLnk" href="#" onclick="sortLinkClick(3);;return false;" title="'+sortCurrentHomeRegionName+'">'+ sortRegionName + '</a>'
               if (sortWorkLat != "0" && sortWorkLng != "0"  ) {
                   saveSortMode(itemp);
                   // document.getElementById("sortTitle").innerHTML = sortWorkTitle;

                }
                else {//not set yet
                    window.open('mapIt.aspx?id='+wiynBookCode+'&sortID=2','WIYNMapIt','menubar=0, resizable=1,location=0, width=700, height=620');
                  
                }
                 break;
                
            case 3: //Region
            //alert("3")
                document.getElementById("sortLink0").innerHTML = '<a id="sortNoneLnk" href="#" onclick="sortLinkClick(0);;return false;" title="Location is not important">'+ sortNoName + '</a>'
                document.getElementById("sortLink1").innerHTML = '<img src="images/map-Home_icon.gif"/>&nbsp;<a id="sortHomeLnk" href="#" onclick="sortLinkClick(1);;return false;" title="Close to my '+ sortHomeName + '">'+ sortHomeName + '</a>'
                document.getElementById("sortLink2").innerHTML = '<img src="images/map-Work_icon.gif"/>&nbsp;<a id="sortWorkLnk" href="#" onclick="sortLinkClick(2);;return false;" title="Close to my '+ sortWorkName + '">'+ sortWorkName + '</a>'
                document.getElementById("sortLink3").innerHTML = sortRegionName 
                if (sortRegionLat != "0" && sortRegionLng != "0"  ) {
                   saveSortMode(itemp);
                  //  document.getElementById("sortTitle").innerHTML = sortRegionTitle;
                }
                else {//not set yet
                    window.open('mapIt.aspx?id='+wiynBookCode+'&sortID=3','WIYNMapIt','menubar=0, resizable=1,location=0, width=700, height=620');
                  
                }
                 break;
            case 99:
                 window.open('mapIt.aspx?id='+wiynBookCode+'&sortID=0','WIYNMapIt','menubar=0, resizable=1,location=0, width=700, height=620');
                 break;
        }
   
   
}

function changeDone(sortMode){

    loadSortVariables();
    if (sortMode==0){sortMode = sortCurrent;}
    setSort();
    
    //document.getElementById("results").innerHTML = "<div class='loading'></div>";
    //document.getElementById("pagesBottom").innerHTML = ""
    
    setTimeout("sortLinkClick("+sortMode+")",500);

}

function saveSortMode(sortMode){
    setSortCenter(sortMode);
    saveSortVariables();

}


function setMapSearchCenter(lng,lat,grid,title){

// *fix* STILL NEED TO MAKE TITLE CHANGE

    mapSearchCenter = lng+"|"+lat+"|"+grid+"|"+title;
    document.getElementById("sortTitle").innerHTML = title;
    //alert(mapSearchCenter);
    if (sortCurrent >=1 ){
        thisSortPoint = new GLatLng(lat,lng);
    }
    if(resultCount >= 1){
        getResults();
    }
    else{
        map.setCenter(thisSortPoint);
    }
    
}

function setSortCenter(newSort){

    sortCurrent = parseInt(newSort);
    //alert(sortCurrent);
    switch (sortCurrent){
    
        case 0: //none
            setMapSearchCenter("0","0","0",sortNoTitle)
            break;
        case 1: //home
            setMapSearchCenter(sortHomeLng,sortHomeLat,mapGrid,sortHomeTitle)
            break;
        case 2: //work
            setMapSearchCenter(sortWorkLng,sortWorkLat,mapGrid,sortWorkTitle)
            break;
        case 3: //Region
            setMapSearchCenter(sortRegionLng,sortRegionLat,mapGrid,sortRegionTitle)
            break;
    }

}

