var base_url = "/";
var current_state = "";

function getCities(state) {
	$("#locations").show();
	$("#stateHeader").html("<img src=\""+base_url+"media/images/buy/headers/states/"+state+".gif\" alt=\"\" />").fadeIn("slow");
	
	$.ajax({
		url: base_url+"ajax/cities/"+state, 
		dataType: 'json',
		type: 'POST',
		success: displayStates,
		error: function(XMLHttpRequest, txtStatus, errorThrown) 
		{
			alert("There was an error submitting the data: "+textStatus+", "+errorThrown);
		}
	});
	
}

function displayStates(data) {
	//console.log(data);
		var count;
		var num = data.length;
		
		if(num <= 0) {
			cityList = "<p>No locations available.</p>";
		} else {
			cityList = "<ul>";
				cityList += "<li style=\"margin: 0 0 25px 0;\"><a href=\"javascript:void(0);\" id=\"distributors\" onclick=\"showDistributors(this['id'])\">Our Distributor</a><span class=\"more_button_arrow_float\"></span></li>";
			$.each(data, function(x, val) {		
				cityList += "<li><a href=\"javascript:void(0);\" id=\"link"+x+"\" onclick=\"showPlaces('"+val+"', this['id'])\">"+val+"</a></li>";
			});
			cityList += "</ul>";
		}
	
		$('#cities').html(cityList);
		$('#cities').fadeIn();	
		$('#onlineHeader a').fadeIn();
}


function toggle(id) {
	$("#"+id).toggle();
}

function displayPlaces(data) {
	
	var count;
	var num = data.length;
	if(num <= 0) {
		placeList = "<p>No locations available.</p>";
	} else {		
		
		placeList = "<h3 id=\"liquor-stores\"><a href=\"javascript:void(0);\" onclick=\"toggle('stores')\">Liquor Stores</a></h3>";
		
		
		placeList += "<div id=\"stores\" class=\"placeList\">";
		numplaces=0;
		$.each(data, function(x, val) {
			if(val['type'] == 'Retail Store') {
				numplaces++;
				var shortwebsite = val['website'].split("/");
				var website = ''; if(val['website'] != '' && val['website'] != "N/A") { website = "<a href=\"http://"+val['website']+"\" target=\"_blank\">"+shortwebsite[0]+"</a><br />"; }
				var phone = ''; if(val['phone'] != '' && val['phone'] != "N/A") { phone = val['phone']+"<br />"; }
				placeList += "<div class=\"place\"><strong>"+val['name']+"</strong><br />"+val['address']+"<br />"+val['city']+", "+val['state']+"<br />"+phone+website+"<br /></div>";
			} 
		});
		if(numplaces==0) { placeList += "<div class=\"place\"><strong>No Stores Found.</strong></div>"; }
		placeList += "</div>";
		
		placeList += "<h3 id=\"restaurants-bars\"><a href=\"javascript:void(0);\" onclick=\"toggle('bars')\">Liquor Stores</a></h3>";
		placeList += "<div id=\"bars\" class=\"placeList\">";
		
		numplaces=0;
		$.each(data, function(x, val) {
			if(val['type'] == 'Restaurant/Bar') {
				numplaces++;
				var shortwebsite = val['website'].split("/");
				var website = ''; if(val['website'] != '' && val['website'] != "N/A") { website = "<a href=\"http://"+val['website']+"\" target=\"_blank\">"+shortwebsite[0]+"</a><br />"; }
				var phone = ''; if(val['phone'] != '' && val['phone'] != "N/A") { phone = val['phone']+"<br />"; }
				placeList += "<div class=\"place\"><strong>"+val['name']+"</strong><br />"+val['address']+"<br />"+val['city']+", "+val['state']+"<br />"+phone+website+"<br /></div>";
			} 
		});
		if(numplaces==0) { placeList += "<div class=\"place\"><strong>No Restaurants or Bars Found.</strong></div>"; }
		placeList += "</div>";
		
		var elem=$('#stores');
		var elem=$('#bars');
		
	}
	
	$('#places').html(placeList);
	$('#places').fadeIn();
	
}

function displayDistributors(data) {

	var count;
	var num = data.length;
	if(num <= 0) {
		distList = "<p>No distributors available.</p>";
	} else {
		distList = "<div id=\"distributors\">";
		$.each(data, function(x, val) {
			var shortwebsite = val['website'].split("/");
			var website = ''; if(val['website'] != '' && val['website'] != "N/A") { website = "<a href=\"http://"+val['website']+"\" target=\"_blank\">"+shortwebsite[0]+"</a><br />"; }
			var phone = ''; if(val['phone'] != '' && val['phone'] != "N/A") { phone = val['phone']+"<br />"; }
			distList += "<div class=\"place\"><strong>"+val['name']+"</strong><br />"+val['address']+"<br />"+val['city']+", "+val['state']+"<br />"+phone+website+"<br /></div>";
		});
		distList += "</div>";
	}
	
	$('#distributorList').html(distList);
	$('#distributorList').fadeIn();
	
}


function showPlaces(city, i) {
	$("ul a").css("font-weight", "normal").css("color", "#9e9790");
	//Bold the Link on the Left
	$("#"+i).css("font-weight", "bold").css("color", "#fff");
	
	$('#distributorList').hide();
	$('#places').hide();
	
	var data = {};
	data['state'] = current_state;
	data['city'] = city;
	
	$.ajax({
		url: base_url+"ajax/places/", 
		data: data,
		dataType: 'json',
		type: 'POST',
		success: displayPlaces,
		error: function(XMLHttpRequest, txtStatus, errorThrown) 
		{
			alert("There was an error submitting the data: "+textStatus+", "+errorThrown);
		}
	});
}

function showDistributors(i) {
	$("ul a").css("font-weight", "normal").css("color", "#9e9790");
	$("#"+i).css("font-weight", "bold").css("color", "#fff");
	var data = {};
	data['state'] = current_state;
	
	// Hide Distributor
	$('#distributorList').hide();
	$('#places').hide();
	
	$.ajax({
		url: base_url+"ajax/distributors/", 
		data: data,
		dataType: 'json',
		type: 'POST',
		success: displayDistributors,
		error: function(XMLHttpRequest, txtStatus, errorThrown) {
			alert("There was an error submitting the data: "+textStatus+", "+errorThrown);
		}
	});
}

function showOnline() {
	hideEverything();
	$("ul a").css("font-weight", "normal").css("color", "#9e9790");
	$('#websites').toggle('fade');
}

function hideOnline() {
	$('#websites').hide();
}

function hideEverything() {
	$(".selected").fadeOut();
	$("#stateHeader").hide();
	$('#locations').hide();
	$('#cities').hide();
	$('#places').hide();
	$('#distributorList').hide();
}


var stateHighlight = function(){ this.init.apply(this, arguments); }
    stateHighlight.prototype = {
        last_state : '',
        init : function()
        {
            var self = this;
            $('area').mouseover(function(){
                var state = this.alt;
                    self.highlight_state(state);
            });

			$('area').click(function() {
				
				hideEverything();
				hideOnline();
				var state = this.alt;
				current_state = this.alt;
				//alert(state);
				$("#"+state+"Default").fadeIn("slow");
				
				
				getCities(state);
			});
        },
        highlight_state : function(state) 
        {
            var self = this;
			
       			if(self.last_state != state) {
					$("#"+state+"Hilite").fadeIn(); 
					$("#"+self.last_state+"Hilite").fadeOut(); 
	
					$('#USAMap').css({'z-index':'1000'});
					$("#"+state+"Hilite").css({'z-index':'10'});
					
				}

                self.last_state = state;
        }
    }
    
$(document).ready(function(){
	var map_highlight = new stateHighlight();
})