// global variables
var search_delay = '';
var map_select_opened = false;


// functions
function init_search()
{
	$("#keywords").keyup(function(e){
		if (e.keyCode == 13)
		{
			process_search();
		}
		else
		{		
			if (search_delay) 
			{
				clearTimeout(search_delay);
			}
			search_delay = setTimeout("process_search()",750);
		}
		
	});
	$("#searchform").submit(function(){
		return false;
	});	
	$("html").click(function(){
		$("#search_recommendations").fadeOut(function(){
			$("#search_recommendations").html('<center><img src="/scripts/colorbox/images/loading.gif" style="padding: 20px"></center>');		
		}); 
	});
	
	$("#keywords").focus(function(e){
		val = $("#keywords").val();
		if (val == '  Search')
		{
			$("#keywords").val('');
			$("#keywords").css('color','black');
		}
	});
	$("#keywords").blur(function(e){
		val = $("#keywords").val();
		if (val == '')
		{
			$("#keywords").val('  Search');
			$("#keywords").css('color','#cccccc');
		}
	});
	
	
	
	
}

function process_search()
{
	if ($("#keywords").val().length < 1 || $("#keywords").val() == '  Search') 
	{ 
		$("#search_recommendations").fadeOut(function(){
			$("#search_recommendations").html('<center><img src="/scripts/colorbox/images/loading.gif" style="padding: 20px"></center>');		
		}); 
	}
	else
	{
		if ($("#keywords").val().length > 2) 
		{
			$("#search_recommendations").fadeIn();
			//vars = "keywords=" + $("#keywords").val();
			vars = $("#searchform").serialize();
			//vars = "ACT=4&XID=4f89c5370343763aaf2feae99ad93a3aef72e1b0&RP=search%2Fresults&NRP=&RES=&status=&search_in=&site_id=1&" + vars;
			
			$.post("/index.php/search/recommendations/", vars, function(data) {
				$("#search_recommendations").html(data);
				$(".recommend_item").click(function(){
					link = $(this).attr('link');
					document.location.href = link;
				});
				
/*
				$('#recommend_table tr.recommend_current').focus();				
				$(document.documentElement).keyup(function (event) {
					if (event.keyCode == 37) {
						$('#recommend_table tr.recommend_current').parent().prev().click();	
					} 
					else if (event.keyCode == 39) 
					{
						$('#recommend_table tr.recommend_current').parent().next().click();
					}
				});
*/
				
				
				
			});
		}
		
	}
}

function set_site_select_button(page)
{
	$('#site_button_home').click(function(){
		$.fn.colorbox({
			width: "740px",
			height: "440px",
			href:'/index.php/site/site_select/',
			opacity: 0,
			onComplete: function() {

				$("#zip").keyup(function(e){
					if (e.keyCode == 13)
					{
						find_sites(page);
					}
					else
					{		
						if (search_delay) 
						{
							clearTimeout(search_delay);
						}
						search_delay = setTimeout("find_sites('" + page + "')",750);
					}
					
				});
				$("#site_select_form").submit(function(){
					return false;
				});
				$("#zip").focus();
				find_sites(page);
				set_map_tooltips();
			},
			onOpen: function() {
				map_select_opened = true;
			},
			onClosed: function() {
				map_select_opened = false;
			}
		});
	});
	
/*
	$('.current_obs_map area').click(function() {
		stid = $(this).attr('stid').toLowerCase();
		$.get('/index.php/site/current_box/' + stid, function(data) {
			$('#current_conditions').html(data);
			set_site_select_button();
		});
		if (map_select_opened) $.fn.colorbox.close();
	});
*/
}



function set_current_box()
{
	save_local_stid();
	timestamp = new Date().getTime();
	$.get('/index.php/site/current_box/' + stid + '/' + timestamp, function(data) {
		$('#current_conditions').html(data);
		set_site_select_button();
	});	
}

function set_local_page()
{
	save_local_stid();
	document.location.href = '/index.php/weather/local/' + stid;
}


function save_local_stid()
{
	$.cookie("stid", stid, { path: '/', expires: 30 });
}

function find_sites(page)
{
	if ($("#zip").val().length < 1) 
	{ 
			$.post("/index.php/site/site_select_results/", function(data) {
				$("#site_results").html(data);
				$(".stid_link").click(function(){
					stid = $(this).attr('stid').toLowerCase();
					
					if (page == 'local')
					{
						set_local_page();
					}
					else
					{
						set_current_box();	
					}
					
									
					$.fn.colorbox.close();
				});								
			});
	}
	else
	{
		if ($("#zip").val().length > 2) 
		{
			vars = $("#site_select_form").serialize();
			$.post("/index.php/site/site_select_results/", vars, function(data) {
				$("#site_results").html(data);
				$(".stid_link").click(function(){
					stid = $(this).attr('stid').toLowerCase();
					
					
					
					
					if (page == 'local')
					{
						set_local_page();
					}
					else
					{
						set_current_box();	
					}
					
					
									
					$.fn.colorbox.close();
				});								
			});
		}		
	}
}

function set_map_tooltips()
{
	$('.current_obs_map area').each(function()
	{
		$(this).qtip({
			content: $(this).attr('alt'), // Use the ALT attribute of the area map
			position: {
				corner: {
					target: 'top',
					tooltip: 'bottomLeft'
				},
				adjust: {
					x: 0,
					y: -5
				}
			},
			style: {
			name: 'blue', // Give it the preset dark style
			border: {
			   width: 0, 
			   radius: 4 
			}, 
			tip: true // Apply a tip at the default tooltip corner
			},
			hide: {
			when: 'mouseout',
			fixed: true
			}
		});
		$(this).qtip("focus");
	});

}


function image_refresh(image,obj)
{
	timestamp = new Date().getTime();
	$("." + obj).attr("src",image + "?" + timestamp);
}