var sliding = false;
var top_li = 1;
var total_li = 0;
var marker_count;

function hide_overlay(){
	$('#brkmap_overlay').fadeOut(300);
}

function show_overlay(){
	$('#brkmap_overlay').fadeIn(300);
}
function overlay_txt(txt){
	$('#brkmap_overlay .action').text(txt);
}


function addListItem(i, city, dates, lat, long){
var listItem = '<div id="li'+i+'" lat="'+lat+'" long="'+long+'" class="list_item"><div class="inner"><div class="title">'+city+'</div>'+dates+'</div></div>	';
$('#list_wrapper').append(listItem);
}

function show_popup(txt){
	$(this).height('');
	$('#brkmap_info').html(txt);
	$('#brkmap_info').stop().slideDown(500, function(){
	$(this).height('');
	});
	$('#brkmap_info').bind('click', function(){
	hide_popup();									 
	});
}

function hide_popup(){
	$('#brkmap_info').stop().slideUp(500);
	$(this).height('');
}

function addMarker(point, popup){
	var marker = new GMarker(point);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function(){
		show_popup(popup);
		
	});
}

function loadMapPoints(sid){
	top_li = 1;
	$('#list_wrapper').html('');
	overlay_txt('Loading Map...');
	$.ajax({ url: 'http://purina.tampadigital.com/includes/show_json.php?show_id='+sid, dataType: "json", success: function(marks){
		if($.trim(marks) != ''){
		var mcount = marks.length;
		marker_count = mcount;
		var title = marks[0].title;
		var subtitle = marks[0].subtitle;
		var showtitle = title+' - '+subtitle;
		$('#map_title').text(showtitle);
		for(i=0;i<mcount;i++){
			var station = marks[i].s_name;
			var city = marks[i].city;
			var dates = marks[i].dates;
			var dl = dates.length;
			var date_str = '';
			var c_station = '';
			for(ii=0;ii<dl;ii++){
				if(c_station != dates[ii].s_name){
					c_station = dates[ii].s_name;
					date_str += '<div class="station"><b>'+dates[ii].s_name+'</b> - '+dates[ii].s_aff+'</div>';
				}
				date_str += '<div class="date">'+dates[ii].date+'</div>';
			}
			var popup = '<div class="point_pop" ><div class="title">'+city+'</div> <div class="air_dates">'+date_str+'</div></div>';
			var point = new GLatLng(marks[i].lat, marks[i].long);
			addListItem(i+1, city, date_str, marks[i].lat, marks[i].long);
			addMarker(point, popup);
			total_li = i+1;
			if(i == mcount-1){
				setListButtons();
				setTimeout ( "hide_overlay()", 2000 );
			}
		}	
		
		if($('#chg_zip').val() != ''){
		changeZip();
		}
		
}else{
setTimeout ( "hide_overlay()", 2000 );

}
	}});
}

//Slide List UP
function listSlideUp(){
	sliding = true;
	var max_slide = $('#slide_pane').height();
	var max_skip = Math.round(max_slide/63);
	var ti_pos = $('#li'+top_li).position().top;
	//find slide to
	for(i=0;i<marker_count;i++){
		var nxt_pos = $('#li'+(top_li+1)).position().top;
		if( nxt_pos <= (ti_pos+max_slide) ){
			top_li++;
		}
	}
	ti_pos = $('#li'+top_li).position().top;
	var lp = $('#left_slider').css('top').replace('px','');
	$('#left_slider').animate({top: -ti_pos+'px'}, 1000, setListButtons);
}
//Slide List DOWN
function listSlideDown(){
	sliding = true;
	var max_slide = $('#slide_pane').height();
	var max_skip = Math.round(max_slide/63);
	var ti_pos = $('#li'+top_li).position().top;
	var next_max = ti_pos-max_slide;
	if(next_max < 0){
		next_max = 0;
	}
	//find slide to
	for(i=0;i<marker_count;i++){
		if(top_li > 1){
			var nxt_pos = $('#li'+(top_li-1)).position().top;
			if( nxt_pos >= next_max){
				top_li--;
			}
		}
	}
	top_item = $('#li'+top_li);
	ti_pos = top_item.position().top;
	var lp = $('#left_slider').css('top').replace('px','');
	$('#left_slider').animate({top: -ti_pos+'px'}, 1000, setListButtons);
}

function setListButtons(){
	sliding = false;
	var max_slide = $('#slide_pane').height();
	var lp = $('#left_slider').css('top').replace('px','');
	var lh = $('#list_wrapper').height();
	var ls = Number(-lp)+Number(max_slide);
	
	if(top_li > 1){
		$('#slide_down_btn').removeClass('disabled');
	}else{
		$('#slide_down_btn').addClass('disabled');
	}
	
	if(top_li < total_li && ls < lh && lh > max_slide){
		$('#slide_up_btn').removeClass('disabled');
	}else{
		$('#slide_up_btn').addClass('disabled');
	}
}

//Load Maps
function mapsLoaded(){
  	map = new google.maps.Map2(document.getElementById("map"));
  	map.setCenter(new google.maps.LatLng(37.78808138412046, -96.470703125), 3);
	if(show_id == ''){
	loadMapPoints(6);
	}else{
 	loadMapPoints(show_id);
	}
}

function narrowList(){
	$('#left_slider').css('top', '0px');
	$('#no_results').remove();
	top_li = 1;
	var shown = 0;
	var bounds = map.getBounds();
	$('.list_item').each(function(){
		var lat = $(this).attr('lat');
		var long = $(this).attr('long');
		var point = new GLatLng(lat, long);
		if (!bounds.contains(point)) {
		$(this).hide();
		}else{
		$(this).show();
		shown++;
		}
	});
	if(shown == 0){
	var nr_div = '<div id="no_results">No Results</div>';
	$('#list_wrapper').append(nr_div);
	}
	setListButtons();
}

function changeZip(){
		var zip = $('#chg_zip').val();
		overlay_txt('Locating Zip...');
		show_overlay();
		$('#no_results').remove();
		$.ajax({ url: 'http://purina.tampadigital.com/includes/get_location.php?zip='+zip, success: function(data){
			var loc = data.split('|');
			var lat = loc[0];
			var long = loc[1];
			if(lat!='' && long!=''){
  			map.setCenter(new google.maps.LatLng(lat, long), 7);
			narrowList();
			}else{
			alert('Please enter a valid Zip Code.');			
			}
			overlay_txt('Showing Map...');
			setTimeout ( "hide_overlay()", 2000 );
		}});
		return false;
}

function resetZip(){
$('#no_results').remove();
overlay_txt('Resetting Map...');
show_overlay();
$('.list_item').show();
$('#left_slider').css('top', '0px');
$('#chg_zip').val('');
top_li = 1;
map.setCenter(new google.maps.LatLng(37.78808138412046, -96.470703125), 3);
setListButtons();
setTimeout ( "hide_overlay()", 2000 );
}

$(document).ready(function(){
	
	$('.change_show').bind('click', function(){
		var show_id = $(this).attr('show_id');
		if($('#map_error_bg').size() > 0){
			$('#map_error_bg').remove();
		}
		if(show_id == 9){
			if(show_id == 9){
				var error_msg = 'The Oklahoma City Summer Classic presented by Pro Plan will begin airing nationally on August 13th. Please check back soon for airdates.';
			}else{
			var error_msg = 'Error';
			}
			var mh = $('#map').height();
			var mw = $('#map').width();
			$('#map').append('<div id="map_error_bg" style="width:'+mw+'px;height:'+mh+'px" ><div class="inner">'+error_msg+'</div></div>');
			$('#map_error_bg').bind('click', function(){
				$('#map_error_bg').remove();
			});
		}else{
		if(!$(this).hasClass('active')){									 
		$('.change_show').removeClass('active');
		$(this).addClass('active');
		overlay_txt('Clearing Map...');
		show_overlay();
		hide_popup();
		map.clearOverlays();
		map.setCenter(new google.maps.LatLng(37.78808138412046, -96.470703125), 3);
		var lp = $('#left_slider').css('top', '0px')
		
		setTimeout ( "loadMapPoints("+show_id+")", 500 );
		}
		}
	});
	
	$('#reset_zip_btn').bind('click', function(){
		resetZip();
	});
	
	$('#slide_up_btn').bind('click', function(){
		if(!$(this).hasClass('disabled') && sliding == false){
			listSlideUp();
		}
	});
	
	$('#slide_down_btn').bind('click', function(){
		if(!$(this).hasClass('disabled') && sliding == false){
			listSlideDown();
		}
	});
	
	google.load("maps", "2", {"callback" : mapsLoaded});
	
});

