
var timeout    = 300;
var closetimer = 0;
var ddmenuitem = 0;

function quicklinks_open()
{  quicklinks_canceltimer();
   quicklinks_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function quicklinks_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function quicklinks_timer()
{  closetimer = window.setTimeout(quicklinks_close, timeout);}

function quicklinks_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#quicklinks > li').bind('mouseover', quicklinks_open)
   $('#quicklinks > li').bind('mouseout',  quicklinks_timer)});

document.onclick = quicklinks_close;

function catexpand(one) {
        
        divele = document.getElementById(one);
        
	if(divele.style.display == "block") {
	   divele.style.display = "none";
	} else {
	   divele.style.display = "block";
	}
	return false;
}
function toggleMapDisplay() {
   thediv = document.getElementById("map");
   thebutton = document.getElementById("mapbutton");
   if(thediv.style.display != 'none' ){
       thediv.style.display='none';
       thebutton.innerHTML = "View Map";
       
   }else{ 
      thediv.style.display='block';
      thebutton.innerHTML = "Hide Map";
   }
   
   return false;
}
function updateSearchCoord(response) {
	
   if (!response || response.Status.code != 200) {
    	//Use default cood
    	$("#maplat").val("43.625");
	$("#maplng").val("-116.4"); 
	alert("Address wasn't found by Google");
   } else {
	// Retrieve the object
	place = response.Placemark[0];
	// Retrieve the latitude and longitude & Update Form
	lat = place.Point.coordinates[1];
	lng = place.Point.coordinates[0];
	$("#maplat").val(lat);
	$("#maplng").val(lng);
   }
   
   $.post("search.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").html(data);}, "html");
	
}
function returnSearch(action) {

   if (action == "search" && $("#address").val() != "address, city, state or zip") {
   	$("#num").val(0);
	$("#action").val(action);
	geocoder = new GClientGeocoder();
	geocoder.getLocations($("#address").val(), updateSearchCoord);
   } else if (action == "fresh" ) { 
   	$("#num").val(0);
   	$("#action").val("filter");
   	$.post("search.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").html(data);}, "html");
   } else if (action == "more") {
   	$("#num").val(parseInt($("#num").val()) + 10);
   	$("#action").val(action);
	center = map.getCenter();
	$("#maplat").val(center.lat());
	$("#maplng").val(center.lng()); 
	$.post("search.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").append(data);}, "html");
   	
   } else {
   	$("#num").val(0);
	$("#action").val(action);
	center = map.getCenter();
	$("#maplat").val(center.lat());
	$("#maplng").val(center.lng()); 
	$.post("search.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").html(data);}, "html");
   }
   if (action != "more") $.scrollTo( '#searchResults', 800, {offset:-300});
   return false;
}

function returnMore(list) {

   num = $("#"+list+"more").attr('num');
   cat = $("#"+list+"more").attr('cat');
   if (cat == null) cat ="";
   //check for highest add slot
   ad=0;
   for(i=3;i<7;i++) {
   	if(!($('#google_ads_div_DealList'+i).length)) {
   		ad=i;
   		break;
   	}
   }

   
   $.post("more.php", {list: list,num: num, ad: ad, cat: cat}, function(data){
	       $("#"+list+"list").append(data);
	 }, "html");
   
   $("#"+list+"more").attr('num',(parseInt(num)+10));
   
   return false;
}


function updateBizSearchCoord(response) {
	
   if (!response || response.Status.code != 200) {
    	//Notify Address was bad
	alert("Address wasn't found by Google Maps");
   } else {
	// Retrieve the object
	place = response.Placemark[0];
	// Retrieve the latitude and longitude & Update Form
	lat = place.Point.coordinates[1];
	lng = place.Point.coordinates[0];
	$("#maplat").val(lat);
	$("#maplng").val(lng);
   }
   
   $.post("bizsearch.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").html(data);}, "html");
	
}
function returnBizSearch(action) {
   
   $("#action").val(action);
   
   if (action == "search" && $("#address").val() != "address, city, state or zip") {
   	$("#num").val(0);
   	geocoder = new GClientGeocoder();
   	geocoder.getLocations($("#address").val(), updateBizSearchCoord);
   } else if (action == "more") {
   	$("#num").val(parseInt($("#num").val()) + 10);
   	$.post("bizsearch.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").append(data);}, "html");
   } else {
   	$("#num").val(0);
   	$.post("bizsearch.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#searchResults").html(data);}, "html");
   }
   if (action != "more") $.scrollTo( '#searchResults', 800, {offset:-300});
   return false;
}
function mMarker(lat, lng, title, twit) {
	var posn   = new GLatLng(lat, lng);
	var marker = new GMarker(posn, {title: title,icon: icon});
	
	//marker.bindInfoWindowHtml(twit, {maxWidth: 50});
	GEvent.addListener(marker, 'click', function() {   
           marker.openInfoWindowHtml(twit, {maxWidth: 100});  
        });
	
	return marker;
}
function returnLike(dealid,list) {
  
	$.getJSON("ilike.php?dealid=" + dealid, function(json){
		if (typeof(json["error"]) != 'undefined') {
			alert("Error:  " + json["error"]);
		} else if (typeof(json["js"]) != 'undefined') {
			$("#jquery-js").html(json["js"]);
		} else {
			if (typeof(json["warn"]) != 'undefined') alert(json["warn"]);
			$("a.likes"+list+dealid).html(json["value"]);
		}
	});
	return false;
	
}
function submitComment(commentform) {
  
	$.post("icomment.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#commentlist").html(data);}, "html");
	return false;
}

function submitClaim(claimform) {
	$.post("iclaim.php?nocache=" + Math.random(), $("#search-form").serialize(), function(data){
	       $("#claim").html(data);}, "html");
	return false;
}

function bitlyStats(bitly, eleid) {

	$.getJSON("http://api.bit.ly/stats?version=2.0.1&shortUrl="+bitly+"&login=dealchirp&apiKey=R_b8ea6c7b9561b7b7d8133c091bd98199&callback=?", function(json){
		
		$("#"+eleid).html("deal viewed "+json["results"]["clicks"]+" times");
		
		
	});
	
	return false;
}

function submitFavorite(dealid,list) {
  
	$.getJSON("ifavorite.php?dealid=" + dealid, function(json){
			if (typeof(json["error"]) != 'undefined') {
				alert("Error:  " + json["error"]);
			} else if (typeof(json["warn"]) != 'undefined') {
				if (typeof(json["jquery-js"]) != 'undefined') {
					$("#jquery-js").html(json["jquery-js"]);
				}
				alert(json["warn"]);
				
			} else {
				$("#favorite"+list+dealid).html("<a href='/userstats.php'>my favorites</a>");
			}
			
			
		});
	return false;
}
