$(document).ready(function(){

	var map = getFlashMovie("map");

    $("body").click(function(event){

        if($(event.target).is("#expand"))
		{
			if($("#short_list")[0].canshow)
			{
				$("#short_list").animate({height: $("#short_list")[0].realheight}, 800, function(){
					$("#short_list").css({overflow: "visible", height: "auto"});
					var curtext = $("#expand").text();
					$("#expand").text(curtext.replace("More", "Less"));
					$("#short_list")[0].canshow = false;
				});
			}
			else
			{
				$("#short_list").animate({height: $("#short_list")[0].shortheight}, 800, function(){
					$("#short_list").css("overflow", "hidden");
					var curtext = $("#expand").text();
					$("#expand").text(curtext.replace("Less", "More"));
					$("#short_list")[0].canshow = true;
				});
			}

		}
		else if($(event.target).is(".ajax_rating"))
		{
			var href = $(event.target).attr("href");
			$.get(href, function(data){
				var li = $(event.target).parents("ul").children(":first").clone();
				$(li).css("width", data + "%");
				$(event.target).parents("ul").html($(li));
			});

			return false;
		}
		else if($(event.target).parents().hasClass("buisnessinfo"))
		{
			var id = $(event.target).siblings(".buisid").text();
			map.businessclick(id);
		}
	});

	$("body").mouseover(function(event){
		if($(event.target).parents().hasClass("buisnessinfo"))
		{
			var id = $(event.target).siblings(".buisid").text();
			map.businessmouseover(id);
		}
	});

	$("body").mouseout(function(event){
		if($(event.target).parents().hasClass("buisnessinfo"))
		{
			var id = $(event.target).siblings(".buisid").text();
			map.businessmouseout(id);
		}
	});



	$(".combo").change(function(){
		window.location = $(this).find("option:selected").val();
	});



	if($("#short_list").length && $("#short_list").find("#expand").length)
	{
		$("#short_list")[0].canshow = true;
		$("#short_list")[0].realheight = $("#short_list").height();

		$("#short_list").css("overflow", "hidden");
		var shortheight = Number($("#short_list").attr("attr"));

		if($.browser.msie)
			shortheight += 12;

		$("#short_list")[0].shortheight = shortheight;

		$("#short_list").height(shortheight + "px");
	}



	//set cookie for default city
	$("#setuserlocation").click(function(){
		$.get($(this).attr("href"), function(data){
			if(data == "1")
			{
				$("#setuserlocation").parent().html("This is your hometown");
			}
		});
		var stop = '';
		return false;
	});


	if($(".hideme").length > 1)
	{
		$(".hideme").each(function(i){
			$(this).hide();

			if(!(i % 2))
				$(this).parents("tr").css("background-color", "lightgrey");


			//add a +/- to expand
			var tmpdiv = $("<div>+</div>");
			tmpdiv[0].canshow = true;
			tmpdiv.css("float", "right");
			tmpdiv.click(function(){
				if(tmpdiv[0].canshow)
				{
					tmpdiv.text("-");
					tmpdiv.siblings("table").show();
					tmpdiv[0].canshow = false;
				}
				else
				{
					tmpdiv.text("+");
					tmpdiv.siblings("table").hide();
					tmpdiv[0].canshow = true;
				}
			});

			$(this).before(tmpdiv);

		});
	}

	if(!$.browser.msie)
	{
		$("#openlogin").toggle(function(){
			$("#toplogin").show();

		},function(){
			$("#toplogin").hide();
		});
	}


	//this si for city select
	$("#stateselect").change(function(){
		var href = "/forum/bajax.php?state=" + $("#stateselect option:selected").val();
		$("#cityselect").empty();

		$("<option value='0'>Please Choose a City</option>").appendTo("#cityselect");

		$.getJSON(href, function(data){
			$.each(data, function(i, val){
				$("<option value='" + val['id'] + "'>" + val['name'] + "</option>").appendTo("#cityselect");
			});
		});
	});
	//end of city select


	//this is for sub cat lookup
	$("#categoryselect").change(function(){
		var href = "/forum/bajax.php?cat=" + $("#categoryselect option:selected").val();

		$("#subcatlist").empty();

		$.getJSON(href, function(data){
			if(data.length == 0)
			{
				$("<tr><td>There are no subcategories for category at this time</td></tr>").appendTo("#subcatlist");
				return false;
			}

			$.each(data, function(i, val){
				$("<tr><td><input type='checkbox' name='subcatlist[]' value='" + val['id'] + "' />: " + val['keyword'] +"</td></tr>").appendTo("#subcatlist");
			});
		});
	});
	//end of subcat lookup

	//this is for sub cat lookup for mulitple category choices
	$(".categorychoose").change(function(){

		var id = $(this).attr("attr");
		var href = "/forum/bajax.php?cat=" + $(this, "option:selected").val();

		$("#subcatlist_" + id).empty();

		$.getJSON(href, function(data){
			if(data.length == 0)
			{
				$("<tr><td>There are no subcategories for category at this time</td></tr>").appendTo("#subcatlist_" + id);
				return false;
			}

			$.each(data, function(i, val){
				$("<tr><td><input type='checkbox' name='subcategory[]' value='" + val['id'] + "' />: " + val['keyword'] +"</td></tr>").appendTo("#subcatlist_" + id);
			});
		});
	});
	//end of subcat lookup

	//this is for questions to remove the text
	$("#questiontitle").focus(function(){
		if($("#questiontitle").data("clicked") == null)
		{
			$("#questiontitle").val("");
			$("#questiontitle").data("clicked", true);
		}
	});

	$("#questionmessage").focus(function(){
		if($("#questionmessage").data("clicked") == null)
		{
			$("#questionmessage").empty();
			$("#questionmessage").data("clicked", true);
		}
	});
	//end of remove text for question



});

var checkfortags = false;



function loaddata(data, container, url, col, showmore)
{
	$(container).empty();
	var innerhtml = new Array();
	innerhtml.push("<div class='list_" + col + "col'><ul>");
	var y = 1;
	var percolumn = (data.length % col == 0) && data.length > 9 ? Math.ceil((data.length + 1)/ col) : Math.ceil(data.length / col);
	var columnnumber = 1
	for(var i = 0; i < data.length; i++)
	{
		if(y == 10 && columnnumber == col)
		{
			innerhtml.push("<li><div id='expand'>Show Less</div></li>");
		}

		innerhtml.push("<li><a class='c2' href='" + url + data[i]['url'] + "/'>" + data[i]['name'] + "</a></li>");

		if(y == percolumn)
		{
			innerhtml.push("</ul></div><div class='list_" + col + "col'><ul>");
			y = 0;
			columnnumber++;
		}

		y++;
	}

	innerhtml.push("</ul></div>");
        innerhtml = innerhtml.join('');

	$(container).append($(innerhtml));

    $("#short_list")[0].realheight = $("#short_list").height();

	var shortheight = Number($("#short_list").attr("attr"));

	if($.browser.msie)
		shortheight += 12;

	$("#short_list")[0].shortheight = shortheight;
}

function getFlashMovie(movieName)
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


function mapready()
{

	plotbuisness($(".buisnessinfo"));

//	alert("ready");

}

function plotbuisness(buisness)
{
	var map = getFlashMovie("map");
	map.clearmap();

	$(buisness).each(function(){

		if($(this).hasClass("separator"))
			return;

		var name = $(".title", this).text();
		if(name.length == 0)
			name = $(".title2", this).text();

		var link = $(".title", this).attr("href");
		if(link == null)
			link = window.location;

		var address = $(".address", this).text();
		var id = $(".buisid", this).text();

		//alert(name + "   " + link + "  " + address + "  " + id);

		if(id != "")
			map.addBusiness(id, address, name, link);
	});
}


function ismouseout(obj, e)
{
	var pos = $(obj).offset();
	var x = e.pageX;
	var y = e.pageY;

	if(y < pos.top || (y > pos.top + $(obj).height()) || x < pos.left || (x > pos.left + $(obj).width()))
		return true;
	else
		return false;
}

function GetCurrentURL()
{
	var str = window.location.href;
	str = str.replace(/#([\w_]+)/i, '');
	if('/' != str.substr(str.length - 1))
		str += '/';

	return str;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
