//function to block an item
function block($id,$message,$width){
	if (typeof $message == 'undefined'){
		$message = "Loading...";
	}
	if (typeof $width == 'undefined'){
		$('#'+$id).block({
			message: '<b>'+$message+'</b> <img src="img/ajaxloader.gif">',  
			css: { 	border: '3px solid blue' }  
		});
	}else{
		$('#'+$id).block({
			message: '<b>'+$message+'</b> <img src="img/ajaxloader.gif">',  
			css: { 	border: '3px solid blue',
					width: $width
			}  
		});
	}
}

//function to block any selector
function blockall($id,$message,$width){
	if (typeof $message == 'undefined'){
		$message = "Loading...";
	}
	if (typeof $width == 'undefined'){
		$($id).block({
			message: '<b>'+$message+'</b> <img src="img/ajaxloader.gif">',  
			css: { 	border: '3px solid blue' }  
		});
	}else{
		$($id).block({
			message: '<b>'+$message+'</b> <img src="img/ajaxloader.gif">',  
			css: { 	border: '3px solid blue',
					width: $width
			}  
		});
	}
}

//standard unblock function
function unblock($id){
	$('#'+$id).unblock();
}

function unblockall($id){
	$($id).unblock();
}

//another unblock function for maps, used to retrieve map info from the session?


function unixtime(){
	return Math.round((new Date()).getTime() / 1000);
}

function emptyfields($array){
	for ($key in $array){
		$('#'+$array[$key]).val('');
	}
}

function sendemail($to,$subject,$message,$unblockid,$clearfields){
	//send email from form, if unblockid is set then will use jquery to unblock that ID. 
	//clearfields, if set, should be an array of field IDs to empty after successful send.
	//call email validator
	$.get('validate_email.php',{'email':$to},function(response){
		if (response == '1'){
			//call send email
			$.get('sendemail.php',{'to':$to,'subject':$subject,'message':$message},function(response){
				if (response == '1'){
					alert('Your message was sent successfully.');
					if (typeof $unblockid != 'undefined'){
						unblock($unblockid);
					}
					//clearfields is an array of IDs, clear each of them
					if (typeof $clearfields != 'undefined'){
						emptyfields($clearfields);
					}
				}else{
					alert('An error occurred while sending your message. Please try again.');
					if (typeof $unblockid != 'undefined'){
						unblock($unblockid);
					}
				}
			});
		}
	});
}

function validate_email($email){
	//can't really use this... because it is asynchronous it doesn't work as expected.
	$.get('validate_email.php',{'email':$email},function(response){
		if (response == '1'){
			return true;
		}else{
			return false;
		}
	});
}

//function to load map image into $id
function loadmap($id, $src, $postproduct){
	//unix timestamp..?
	var $ts = Math.round((new Date()).getTime() / 1000);
	//block image container
	block($id+'_container');
	//clear usemap attribute
	//$('#'+$id).attr('usemap','nothing');
	document.getElementById($id).useMap = '#nothing';
	//set unblock load event and img src
	$('#'+$id).load(function() {
		if (typeof $postproduct == 'undefined'){
			//empty the map product div
			$('#'+$id+'_product').html('');
			//unblock the container
			unblock($id+'_container');
		}else{
			//load mapproduct.php into $id+product which should be a div underneath the image.
			$('#'+$id+'_product').load('postmap.php',{'product':$postproduct,'tsflag':$ts},function(){
				//set id to usemap nothing then map_postproduct
				//$('#'+$id).attr('usemap','#map_'+$postproduct);
				document.getElementById($id).useMap = '#map_'+$postproduct+$ts;
				//unblock container
				unblock($id+'_container');
			});
		}
	}).attr('src', $src);
}

//javascript date/timer
function gmtClock(){ 
	//refresh clock every 60 seconds
	var timer = 60000;
	//
	var zone=new Date().getTimezoneOffset();
	if(zone==-60) {zone=0;}
	var gmtTime = new Date(new Date().getTime()+(zone*60000));
	var clock, month, day, year, hour, minute, second;
	//date
	month = gmtTime.getMonth() + 1;
	day = gmtTime.getDate();
	year = gmtTime.getFullYear();
	//time
	hour = ((gmtTime.getHours() < 10) ? "0" : "") + gmtTime.getHours();
	minute = ((gmtTime.getMinutes() < 10) ? "0" : "") + gmtTime.getMinutes();
	second = ((gmtTime.getSeconds() < 10) ? "0" : "") + gmtTime.getSeconds();
	var secondms = second * 1000;
	//syncs update with system clock
	var newdelay = timer-secondms;
	clock = month + '/' + day + '/' + year + ' ' + hour + ':' + minute + ' UTC';
   $("#gmtclock").html(clock);
   //alert(newdelay);
	setTimeout('gmtClock()',newdelay);
}

function selectTab($id){
	//first, remove the activetab class from all tabs, then set the tabs class on all tabs.
	//for $id, remove tabs and set activetab
	//load content from tabs.php into tabscontent
	$('#tabscontent').load('lib/index/tabs.php',{'t':$id},function(){
		//success
	});
	//adjust css classes for tabs
	$('[id^="tab_"]').removeClass('activetab');
	$('[id^="tab_"]').addClass('tabs');
	$('#' + $id).removeClass('tabs');
	$('#' + $id).addClass('activetab');
	/* //NOT NEEDED ANYMORE
	/*
	$('#tabtitle').load('lib/index/tabtitles.php',{'t':$id}, function(){
			//successful loading of tab content and title
			
		});
	//change bg image of tabimg cells
	$('#tabimg1').css('backgroundImage','url("img/carousel/' + $id + '_1_small.gif")');
	$('#tabimg2').css('backgroundImage','url("img/carousel/' + $id + '_2_small.gif")');
	//change hidden a hrefs in tablinks to point to new files
	$('#tablink1').attr('href','img/carousel/' + $id + '_1.gif');
	$('#tablink2').attr('href','img/carousel/' + $id + '_2.gif');
	*/
}
function loadproduct($id,$type){
	//consider a cancel loading function here
	
	//check type
	if (typeof $type == 'undefined'){
		$fname = 'product.php';
	}else if ($type == 'tropical'){
		$fname = 'tropproduct.php';
	}
	
	$('#productcell').load($fname,{'p':$id},function(){
		//success
	});
	//adjust css classes for tabs
	$('[id^="producttab_"]').removeClass('activetab');
	$('[id^="producttab_"]').addClass('tabs');
	$('#producttab_' + $id).removeClass('tabs');
	$('#producttab_' + $id).addClass('activetab');
}
//
//
function login(){
	//get username and passwords
	var username = $('#username').val();
	var password = $('#password').val();
	//alert(username + ',' + password);
	//md5 them
	//var hashuser = md5(username);
	var hashpass = md5(password);
	var hashuser = username; //not md5'ing the username
	//var hashpass = password;
	//alert(hashuser + ',' + hashpass);
	//submit md5'd to php script that will check their values
	$.get('validate.php', {'u':hashuser, 'p':hashpass}, function(returnval){
		if (returnval == '1'){
			//reload home 
			loadcontent('home','index');
			//reload loginarea to show account details
			$('#loginarea').hide().load('lib/index/loginbox.php', function() {
				$('#loginarea').fadeIn(500);
			});
			//change sign up link to my account
			
		}else{
			alert(returnval);
		}
	});
	//prevent submit redirection
	return false;
}
//
function loadcontent($r, $f){
	block('maincontent');
	//load new content into the content cell in the middle of the page
	$('#maincontent').load('content.php',{'r':$r, 'f':$f}, function() {
		$('#maincontent').fadeIn(500);
		unblock('maincontent');
	});
}

function loadtropical($r, $f, $p){
	//r is the storm code or basin name, f is tropbasin or tropstorm, p is a specific product to view
	if (typeof $p == 'undefined'){
		$p = '0';
	}
	//load tropical basin content
	$('#maincontent').hide().load('content.php',{'r':$r, 'f':$f, 'p':$p}, function() {
		$('#maincontent').fadeIn(500);
	});
}

//
function afterloaded(){
	//fancybox for all pages
	$("a.fgroup").fancybox( {
		autoScale: false
	});
}
//
//dynamically call fancybox
function callfancybox(my_href, w, h) {
      $.fancybox(my_href,{
			'height': h,
			'width': w,
			'type':'iframe',
			'autoScale':'false'
		});
}
