//Google Maps
var map = null;
var geocoder = null;

function load(name, address) {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		showAddress(address, name);
	}
}

function showAddress(address, name){
	geocoder.getLatLng(address, function(point){
		if(!point){
			alert(address + " not found");
		}else{
			map.setCenter(point, 14);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml("<div align='left' style='color:#333; font-size:14px'><b>" + name + "</b><br />" + address + "</div>");
		}
	});
}




//Trim
function trim(strText) { 
    //Leading spaces 
    while(strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    //Trailing spaces 
    while(strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 




//Menu
var curMenuItem=0;
function menu_switcher(id){
	if(id != curMenuItem){
		$("#menu"+curMenuItem).slideUp("fast");
		$("#m"+curMenuItem).removeClass("browser_maincat_hover");
		
		$("#menu"+id).slideDown("fast");
		$("#m"+id).addClass("browser_maincat_hover");
		curMenuItem = id;
	}
	else{
		if($("#menu"+id).css("display") == "none"){
			$("#menu"+id).slideDown("fast");
			$("#m"+id).addClass("browser_maincat_hover");
		}else{
			$("#menu"+id).slideUp("fast");
			$("#m"+id).removeClass("browser_maincat_hover");
		}
	}
}

function shLocMenu(){
	if($("#loc_menu").css("display") == "none"){
		$("#loc_txt").fadeOut("fast", function(){ $("#loc_menu").fadeIn("fast"); });
		menutimer = setTimeout('shLocMenu()', 4500);
	}else{
		$("#loc_menu").fadeOut("fast", function(){ $("#loc_txt").fadeIn("fast"); });
		$('#hidebar').hide();
		clearTimeout(menutimer);
	}
}

function location_changer(sid){
	$.ajax({
		url: "ajax/locations.php?sid="+sid,
		type: "GET",
		success: function(html){
			$("#location").empty();
			d = html.split(";");

			$("#location").append("<option value=' '>Select Location</option>");

			for(i=0; i < d.length; i+=2){
				$("<option value=\""+ d[i] +"\">"+ d[i+1] +"</option>").appendTo("#location");
			}
		},
		beforeSend: function(){
			$("#ajax").show();	
		},
		complete: function(){
			$("#ajax").hide();
		}
	});
}

function Delete_Cookie( name, path, domain ) {
	document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}




//Category Browser
function shBrowser(){
	$('#bubble').hide();
	if($('#browser').css("display") == "none"){
		$("#search_linkdiv").hide();
		$("#browser").slideDown("fast");
		
		//AJAX it
		if($("#browser").html() == ""){
			$("#browser").append("<img src=\"img/ajax.gif\" height=\"12\" width=\"12\" style=\"display:none\" id=\"ajax2\" />");

			$.ajax({
				url: "ajax/browser.php",
				success: function(html){
					$("#browser").hide();
					d = html.split(";");

					var iCat=1;
					for(i=0; i<d.length; i++){
						$("#browser").append("<div class=\"browser_maincat\" onclick=\"menu_switcher(" + iCat + ")\" id=\"m" + iCat + "\">" + d[i] + "</div>");
						$("#browser").append("<div align=\"left\" class=\"browser_subcatdiv\" id=\"menu" + iCat + "\"></div>");
						i++;
						loopCnt = d[i];
						i++;

						//Print subcat items
						$("#menu"+iCat).append("<div style=\"height:5px\"> </div>");
						for(j=0; j<loopCnt; j++){
							$("#menu"+iCat).append("<div class=\"browser_subcat\" onclick=\"location.href='listings.php?c=" + d[i] + "'\">" + d[i+1] + "</div>");
							i+=2;
						}
						
						$("#menu"+iCat).append("<div style=\"height:5px\"> </div>");
						iCat++;
					}
				},
				beforeSend: function(){
					$("#ajax2").show();	
				},
				complete: function(){
					$("#ajax2").hide();
					$("#browser").slideDown("fast");
				}
			});
		}
	}else{
		$("#browser").slideUp("fast", function(){ $("#browser").css("display","none"); });
		$("#menu"+curMenuItem).slideUp("fast");
		$("#m"+curMenuItem).removeClass("browser_maincat_hover");
	}
}



function shBrowser2(){
	if($('#browser').css("display") == "none"){
		$("#search_linkdiv").hide();
		$("#browser").slideDown("fast");
		$('#browserhead').attr('class','left_browser_open');
		
		//AJAX it
		if($("#browser").html() == ""){
			$("#browser").append("<img src=\"img/ajax.gif\" height=\"12\" width=\"12\" style=\"display:none\" id=\"ajax3\" />");

			$.ajax({
				url: "ajax/browser.php",
				success: function(html){
					$("#browser").hide();
					d = html.split(";");

					var iCat=1;
					for(i=0; i<d.length; i++){
						$("#browser").append("<div class=\"browser_maincat2\" onclick=\"menu_switcher(" + iCat + ")\" id=\"m" + iCat + "\">&nbsp;" + d[i] + "</div>");
						$("#browser").append("<div align=\"left\" class=\"browser_subcatdiv2\" id=\"menu" + iCat + "\"></div>");
						i++;
						loopCnt = d[i];
						i++;

						//Print subcat items
						$("#menu"+iCat).append("<div style=\"height:5px\"></div>");
						for(j=0; j<loopCnt; j++){
							$("#menu"+iCat).append("<div class=\"browser_subcat\" onclick=\"location.href='listings.php?c=" + d[i] + "'\">" + d[i+1] + "</div>");
							i+=2;
						}
						
						$("#menu"+iCat).append("<div style=\"height:5px\"></div>");
						iCat++;
					}
				},
				beforeSend: function(){
					$("#ajax3").show();	
				},
				complete: function(){
					$("#ajax3").hide();
					$("#browser").slideDown("fast");
				}
			});
		}
	}else{
		$("#browser").slideUp("fast", function(){ $("#browser").css("display","none"); });
		$("#menu"+curMenuItem).slideUp("fast");
		$("#m"+curMenuItem).removeClass("browser_maincat_hover");
		$('#browserhead').attr('class','left_browser');
	}
}




//Listing
var curListing = 1;
function listSwitcher(id, name, address){
	if($("#listing"+id).css("height") == "111px"){
		if($("#listing"+curListing).css("height") != "111px"){
			$("#listing"+curListing).css("height","111px");
			$("#listing"+curListing).css("border-bottom","none");
			GUnload();
			$("#listingmap"+curListing).empty();
			$("#listingswitch"+curListing).html("View Map &raquo;");
		}
		$("<div id='map' style='width:650px; height:300px; border:3px solid #333; background-color:#fff'></div>").appendTo("#listingmap"+id);
		$("#listing"+id).animate({height:"455px"}, 200, "linear", function(){
															  $("#listing"+id).css("border-bottom","1px solid #2067ad");
															  $("#listingswitch"+id).html("Hide QuickMap");
															  $("#listingmap"+id).fadeIn("fast");
															  load(name, address);
															  map.setMapType(G_HYBRID_MAP);
															  });
		curListing = id;
		
	}else{
		$("#listingmap"+id).fadeOut("fast",function(){
										    $("#listing"+id).animate({height:"111px"}, 200);
											$("#listing"+id).css("border-bottom","none");
										    $("#listingswitch"+id).html("View QuickMap");
										    GUnload();
										    $("#listingmap"+id).empty();
										    document.getElementById("listing"+id).className = "listingdiv";
										    });
	}
}


//Login/Search box
function searchAdv(){
	if($("#search_box").css("height") != "60px"){
		$("#login_bar").toggle();
		$("#search_box").animate({height:'60px'}, 200, 'linear', function(){$('#adv_searchbox').show();});
	}else{
		$("#search_box").animate({height:'30px'}, 200, "linear", function(){ $('#adv_searchbox').hide(); $("#login_bar").toggle(); });
	}
}



function loginBox(){
	if($("#search_box").css("height") != "60px"){
		$("#email").val("");
		$("#pass").val("");
		$("#search_bar").toggle();
		$("#search_box").animate({height:'60px'}, 200, "linear", function(){ $("#login_box").fadeIn("fast"); $("#email").focus(); });
		$("#login_login").css("color","#ff4e00");
		$("#login_login").attr("title","Minimize");
		$("#login_spam").hide();
		$("#login_form").show();
	}else{
		if($("#login_form_recover").css("display") != "none")
			loginRecover();
		$("#login_box").fadeOut("fast", function(){$("#search_box").animate({height:'30px'}, 200, "linear", function(){ $("#search_bar").fadeIn("fast"); }); });
		$("#login_login").css("color","#fff");
		$("#login_login").attr("title","Log in to your account");
	}
	$("#redx").hide();
}



function loginChecker(){
	if($("#email").val() != "" && $("#pass").val() != ""){
		var dstr = "email=" + $("#email").val() + "&pass=" + $("#pass").val();
		$.ajax({
			type: "POST",
			url: "ajax/user.php?m=0",
			data: dstr,
			success: function(d){
				if(d == "1"){
					$("#form_login").submit();
				}else if(d == "0"){ //Error
					$("#redx").show();
				}else{ //Spam
					$("#login_form").hide();
					$("#login_spam").show();
				}
			},
			beforeSend: function(){
				$("#redx").hide();
				$("#ajax1").show();	
			},
			complete: function(){
				$("#ajax1").hide();
			}
		});
	}else
		$("#redx").show();
}



function loginRecover(){
	if($("#login_form_recover").css("display") == "none"){
		$("#login_form").fadeOut("fast", function(){
										  $("#login_form").hide();
										  $("#email").val("");
										  $("#pass").val("");
										  $("#login_spam").hide();
										  $("#recover_box").html("Log In");
										  $("#login_form_recover").fadeIn("fast");
										  });
	}else{
		$("#login_form_recover").fadeOut("fast", function(){
												$("#recover_box").html("Lost password?");
												$("#login_form").fadeIn("fast");
												});
	}
	$("#redx").hide();
}



function loginRecoverChecker(){
	if($("#remail").val() != ""){
		var dstr = "email=" + $("#remail").val();
		$.ajax({
			type: "POST",
			url: "ajax/user.php?m=2",
			data: dstr,
			success: function(d){
				if(d == "1")
					$("#form_passrecovery").submit();
				else if(d == "0") //Error
					$("#redx2").show();
			},
			beforeSend: function(){
				$("#redx2").hide();
				$("#ajax2").show();	
			},
			complete: function(){
				$("#ajax2").hide();
			}
		});
	}else
		$("#redx").show();
}



//Tagging venues
tagWorking = false;
function tagger(id,vid){
	//Check to see if we're currently doing another request
	if(!tagWorking){
		//Determine which action to take
		if($("#tag"+id).attr('class') == 'listing_tag_untagged')
			a = 'add';
		else
			a = 'del';
		
		//Ajax it
		$.ajax({
			type: "GET",
			url: "ajax/tag.php?id="+vid+"&a="+a,
			success: function(d){
				if(d == 1){
					if($("#tag"+id).attr('class') == 'listing_tag_untagged'){
						$("#tag"+id).attr('class','listing_tag_tagged');
						$("#tag"+id).attr('title','Untag Location');
					}else{
						$("#tag"+id).attr('class','listing_tag_untagged');
						$("#tag"+id).attr('title','Tag Location');
					}
				}else if(d == 0){
					alert('Maximum tagged entries (50). You cannot add any more. Please untag others first.');
				}
			},
			beforeSend: function(){
				tagWorking = true;
				$("#tag"+id).hide();
				$("#tajax"+id).show();
			},
			complete: function(){
				tagWorking = false;
				$("#tajax"+id).hide();
				$("#tag"+id).show();
			}
		});
	}
}




//Hours
function hrsMgr(id,hrs,action){
	if(action){
		if(hrs == "(x)"){
			$('#hours'+id).text('Closed');
		}else{
			$('#hours'+id).text(hrs);
		}
	}else{
		$('#hours'+id).text('');
	}
}
