/*
Created by Todd Dzur for opshop.org
Google Map Christmas lights. 2008

*/

//if (typeof(GIcon()) != "undefined")  {
	/*
  var icons = {"start":new GIcon(G_DEFAULT_ICON, 'http://maps.google.com/mapfiles/dd-start.png'),
    "end":new GIcon(G_DEFAULT_ICON, 'http://maps.google.com/mapfiles/dd-end.png'),
     */
//}
//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}
function sortLatLng(a, b){
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
  return  (b.getLatLng().lat() - a.getLatLng().lat());
}
//******************************

// Creates a marker whose info window displays the given number
function createMarker(point, number, sid)
{
var marker = new GMarker(point, {title: number});
// Show this markers index in the info window when it is clicked
var html = number;
GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html); storeDetails(sid);});
//storeDetails
return marker;
};


function createMarkerIcon(point, mtitle, name, icon) {
  //alert(icon);
  
  var GEARTH_ICON = new GIcon(); GEARTH_ICON.iconSize=new GSize(32,32); GEARTH_ICON.shadowSize=new GSize(56,32); GEARTH_ICON.iconAnchor=new GPoint(16,32); GEARTH_ICON.infoWindowAnchor=new GPoint(16,0);

   
  var icons = {"start":new GIcon(G_DEFAULT_ICON, 'http://maps.google.com/mapfiles/dd-start.png'),
    "end":new GIcon(G_DEFAULT_ICON, 'http://maps.google.com/mapfiles/dd-end.png'),
    "caution": new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/kml/pal3/icon41.png'),
    "other":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/ltblue-dot.png'),
    "check":new GIcon(GEARTH_ICON, 'http://opshop.org/gmap/question.gif'),
    "tree":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/kml/pal2/icon12.png'),
    "street":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/ylw-pushpin.png'),
    "topstreet":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/grn-pushpin.png'),  
    "topstreetpic":new GIcon(GEARTH_ICON, 'http://opshop.org/christmas/grn-pushpin-dot.png'),  
    "lowstreet":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/red-pushpin.png'),  
    "checkstreet":new GIcon(GEARTH_ICON, 'http://opshop.org/gmap/question.gif'),
    "house":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/yellow.png'),
    "tophouse":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/green.png'),
    "lowhouse":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/red.png'),
    "housepic":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/yellow-dot.png'),
    "tophousepic":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/green-dot.png'),
    "check":new GIcon(GEARTH_ICON, 'http://opshop.org/christmas/question.gif'),
    "lowcheck":new GIcon(GEARTH_ICON, 'http://opshop.org/christmas/question.gif'),
    "topcheck":new GIcon(GEARTH_ICON, 'http://maps.google.com/mapfiles/ms/micons/green.png')
    };
    
  if (icon) {
  	var marker = new GMarker(point, {
    icon: icons[icon],
    draggable: false,
    bouncy: false,
    title:mtitle
  });
  }else{
  	var marker = new GMarker(point);
  }
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(mtitle + '<br />' + name);
  });

  return marker;
}
// Creates a marker whose info window displays the given number
function createMarkers(point,stitle, mtitle, sid)
{
var marker = new GMarker(point, {title: stitle});
// Show this markers index in the info window when it is clicked
var html = mtitle;
GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html); storeDetails(sid);});
//storeDetails
return marker;
};

function vote(id,r) {	
   if (r < 1 || r > 5) {
	     alert ("Voting error");
   } else {  
      var url = "/phpsql_vote.php?id=" + id + "&rank=" + r + "&by=";
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 2) {
          marker.closeInfoWindow();
          document.getElementById("message").innerHTML = "Ranking added. Page will need to be reloaded to see any changes";
         
        } else {
        	document.getElementById("message").innerHTML = "<img src='http://maps.google.com/mapfiles/ms/micons/caution.png' /> Ranking error. " + data;
      	}
      });     
    }
}

function votelist(id,r) {	
   if (r < 1 || r > 5) {
	     alert ("Voting error");
   } else {
      var url = "/phpsql_vote.php?id=" + id + "&rank=" + r + "&by=";
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 2) {
          document.getElementById("message").innerHTML = "Ranking added. Page will need to be reloaded to see any changes";
         
        } else {
        	document.getElementById("message").innerHTML = "<img src='http://maps.google.com/mapfiles/ms/micons/caution.png' /> Ranking error. " + data;
      	}
      });
      
    }
}

function votelistc(id,r,com,chk) {
   if (r < 1 || r > 5) {
	     alert ("Voting error");
   } else {
      var url = "/phpsql_votecomment.php?id=" + id + "&rank=" + r + "&comment=" + com + "&chk=" + chk;
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 2) {
          document.getElementById("message").innerHTML = "Ranking added. Page will need to be reloaded to see any changes";
          if (marker) {marker.closeInfoWindow()};
        } else {
        	document.getElementById("message").innerHTML = data;
      	}
      });
      
    }
}


function login(e) {
	  document.getElementById("message").innerHTML = "Trying to Login";
	  var url = "/login.php?email=" + e;
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 2) {
          document.getElementById("message").innerHTML = "Email Registred OK.";
          //document.getElementById("login").innerHTML = "";
          if (marker) {marker.closeInfoWindow()};
         
        } else {
        	document.getElementById("message").innerHTML = "<img src='http://maps.google.com/mapfiles/ms/micons/caution.png' /> Login Error. " + data;
      	}
      }); 
}

function showlogin() {
	document.getElementById("login").innerHTML = "<form onsubmit=\"login2(document.getElementById('email').value); return false\">Email:<input type='text' id='email' /> <input type=submit value='Login' /></form><br />";
}


function login2(e) {
	  document.getElementById("message").innerHTML = "Trying to Login";
      var url = "/login3.php?email=" + e;
	  $('#message').load(url); 
      
}

function login2a(e) {
	  document.getElementById("message").innerHTML = "Trying to Login";
	  var url = "/login2.php?email=" + e;
      GDownloadUrl(url, function(data, responseCode) {
		
        if (responseCode == 200 && Left(data,4) == "user") {
	        
	      document.getElementById("message").innerHTML = "";
          document.getElementById("userdetails").innerHTML = "<b>" + Mid(data,5,50) + "</b>";
          document.getElementById("login").innerHTML = "";
          document.getElementById('mainpage').style.display = 'block'; 
          if (typeof(marker) != "undefined") {marker.closeInfoWindow()};
         
        } else {
        	document.getElementById("message").innerHTML = "<img src='http://maps.google.com/mapfiles/ms/micons/caution.png' /> Login Error. " + data;
      	}
      });
      
}

function login_faceb() {
	  document.getElementById("message").innerHTML = "Trying to Login";
	  var url = "/connectAccount.php";
      GDownloadUrl(url, function(data, responseCode) {
		
        if (responseCode == 200 && Left(data,4) == "user") {
	        
	      document.getElementById("message").innerHTML = "";
          document.getElementById("userdetails").innerHTML = "<b>" + Mid(data,5,50) + "</b>";
          document.getElementById("login").innerHTML = "";
          document.getElementById('mainpage').style.display = 'block'; 
          if (typeof(marker) != "undefined") {marker.closeInfoWindow()};
         
        } else {
        	document.getElementById("message").innerHTML = "<img src='http://maps.google.com/mapfiles/ms/micons/caution.png' /> Login Error. " + data;
      	}
      });
      
}




function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=400,height=200,left = 440,top = 412');");
}

function loadDetail(id) {

    $('#ID'+id).toggleClass('hide');
	if (document.getElementById('ID'+id).innerHTML == "" || document.getElementById('ID'+id).innerHTML == "&nbsp;") {
	   $('#ID'+id).empty().html('<img src="/images/loader.gif" />');
	   $('#ID'+id).load('/phpsql_storedetailsNew.php?id='+id);
	 }
     
}
function loadDetailH(id) {

   $('#ID'+id).toggleClass('hide');
	if (document.getElementById('ID'+id).innerHTML == "") {
	   $('#ID'+id).empty().html('<img src="/images/loader.gif" />');
	   $('#ID'+id).load('/phpsql_storedetailsNew.php?h=true&id='+id);
	 }
      
}


function votelistajax(id,r,com,chk) {
	
   if (r < 1 || r > 5) {
	     alert ("Voting error");
   } else {
   	  //$('#debug').text('debug');
   	  $('#message').empty().html('<div class="pad1"><img src="/images/loader.gif" /></div>');
      var url = "/phpsql_votecommentajax.php?id=" + id + "&rank=" + r + "&comment=" + escape(com) + "&chk=" + chk;
      //alert(url);
      //$('#debug').text(url);
      $('#message').load(url);
      
      
    }
}

//Added 11/10/11
  			function updateStars(r) {
  			 	for (var i=1;i<=5;i++){
  			 		if (i<=r) {
  			 			$("#star"+i).attr("src","/star_on.gif");
  			 		}else{
  			 			$("#star"+i).attr("src","/star_off.gif");
  			 		}
  			 	}
  			}

function hidebanner1() {
$("#banner1").fadeOut('slow', function(){ 

	$("#banner1").css('visibility', 'hidden');
	
});
$.post("http://opshop.org/cookie.php", { cookie: "banner1", value: "hide" } );

}

function storeDetails2(id, map) {
	if (id < 1) {
	     alert ("Store id error");
   } else {
      
      var url = "/phpsql_storedetails.php?id=" + id + "&showmap=" + map;
      GDownloadUrl(url, function(data, responseCode) {
        if (responseCode == 200) {
          document.getElementById("rightlist").innerHTML = data;
        } else {
        	document.getElementById("message").innerHTML = "Error retriving store details" & data & "<br />";
      	}
      });
      
    }
}
function storeDetails(id) {
	
	storeDetails2(id, "false");

}

   function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }
        
               function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }
        
               function Left(str, n)
        /***
                IN: str - the string we are LEFTing
                    n - the number of characters we want to return

                RETVAL: n characters from the left side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }
