var email = {
	validate: function(email) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var value = true;
		
		if (email != null && email != "") {
			if(reg.test(email) == false) {
				value = false;
			}
		}
		
		return value;
	}			
};

var util = {
	wrapper: function() {
		var html = '<div id="wrapper">';
			html += '<div id="popup">';
				html += '<div id="popup_container">';
						html += '<div id="popup_title">Status</div>';
						html += '<div id="popup_message"></div>';
						html += '<div class="buttonblock">';
							html += '<div class="buttonblockwrap"><input type="button" value="close" id="btnclose" /></div>';
						html += '</div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';			

		return html;	
	},
	
	show: function(id,message) {
		$("body").append(util.wrapper());		
		
		switch(id) {
			case 1:
				$("#email").val("enter you email address");
				$("#wrapper").fadeIn("fast");
				$("#popup_message").empty().append("<div class='m'>"+ message +"</div>");
				setTimeout(function(){
					$("#wrapper").fadeOut("fast",function() {
						$(this).remove();	
					});
				},5000);
				break;
			case 2:
				$("#wrapper").fadeIn("fast");
				$("#popup_message").empty().append("<div class='m'>"+ message +"</div>");
				setTimeout(function(){
					$("#wrapper").fadeOut("fast",function() {
						$(this).remove();	
					});
				},5000);
				break;		
		}
		
		$("#popup_container #btnclose").click(function() {
			util.hide("wrapper");
			return false;
		});
	},
	
	hide: function(id) {
		$("#"+ id).fadeOut("fast",function() {
			$(this).remove();	
		});	
	},
	
	weather: function() {
		var html = '<div id="wrapper_weather">';
			html += '<div id="wrapper_weather_container">';
				//html += '<div id="weather_forecast">'
					html += '<img style="visibility:hidden;width:0px;height:0px;" border=0 width=0 height=0 src="http://counters.gigya.com/wildfire/IMP/CXNID=2000002.0NXC/bT*xJmx*PTEyODY2MTg2NTcyNjUmcHQ9MTI4NjYxODY1OTk*OSZwPTEyMzQ*ODEmZD*mZz*xJm89YzUwNGExMGEzMDc3NDQ1N2E*/YWRiMTNhY2U4NjAwMTEmb2Y9MA==.gif" />';
					html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="twc-widget-wrapper-36hour" width="300" height="250" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
						html += '<param name="movie" value="http://s.imwx.com/v.20100719.135915/img/swf/twc-widget-wrapper.swf" />';
						html += '<param name="quality" value="high" />';
						html += '<param name="bgcolor" value="#ffffff" />';
						html += '<param name="wmode" value="transparent" />';
						html += '<param name="allowScriptAccess" value="always" />';
						html += '<embed src="http://s.imwx.com/v.20100719.135915/img/swf/twc-widget-wrapper.swf" quality="high" bgcolor="#ffffff" wmode="transparent\" width="300" height="250" name="twc-widget-wrapper-36hour" align="middle" play="true" loop="false" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" FlashVars="site=weather&moduleSrc=http://s.imwx.com/v.20100719.135915/img/swf/twc-widget-36hour.swf&preview=http://s.imwx.com/v.20100719.135915/img/common/twc-widget-node-36hour.png&origin=true&locationId=RPXX0007&tempUnits=f&country=RP">';
						html += '</embed>';
						html += '<param name="FlashVars" value="site=weather&moduleSrc=http://s.imwx.com/v.20100719.135915/img/swf/twc-widget-36hour.swf&preview=http://s.imwx.com/v.20100719.135915/img/common/twc-widget-node-36hour.png&origin=true&locationId=RPXX0007&tempUnits=f&country=RP" />';
					html += '</object>';
					html += '<div class="buttonblock">';
						html += '<div class="buttonblockwrap"><input type="button" value="close" id="btnclose" /></div>';
					html += '</div>';
				//html += '</div>';
			html += '</div>';			
		html += '</div>';			

		$("body").append(html);		
		$("#wrapper_weather").fadeIn("fast");
		$("#wrapper_weather_container #btnclose").click(function() {
			util.hide("wrapper_weather");
			return false;
		});
	},
	
	loader: function(value) {
		if (value) {
			var html = '<div id="wrapper_loader">';
				html += '<div id="wrapper_loader_container">';
					html += '<img src="images/ajax-loader.gif" />';
					html += '<div>Loading</div>';
				html += '</div>';
			html += '</div>';
			
			$("body").append(html);	
			$("#wrapper_loader").fadeIn("fast");
		} else {
			util.hide("wrapper_loader");	
		}
	},
	
	quote: function() {
		var html = '<div id="wrapper_quote">';
			html += '<div id="wrapper_quote_container">';
				html += '<div id="quote_loader">';
					html += '<img src="images/ajax-loader.gif" />';
					html += '<div>Loading</div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
		
		$("body").append(html);	
		$("#wrapper_quote").fadeIn("fast");					
				
		$.ajax({
			url: "actions/action_html.php",
			type: "POST",
			data: "t=freequote",
			cache: false,
			success: function(result) {
				$("#quote_loader").hide();
				
				if (result != "") {
					$("#wrapper_quote_container").append(result);
					$("#btncancel").click(function() {
						util.hide("wrapper_quote");					
					});				
					$("#btnok").click(function() {
						quote.send();
					});
					$("#qemail").blur(function() {		
						if (!email.validate($("#qemail").val())) {
							util.show(2,"Invalid Email Address.");				
						}
						
						return false;		
					});				
					$("#qnoperson").keyup(function() {
						if (!/^[0-9]+$/.test($(this).val())) {
							$(this).val("");
							$(this).focus();
						}
					});
				} else {
					util.hide("wrapper_quote");
					util.show(2, "Template Not Found.");
				}
			}	
		});		
	},
	
	details: function() {
		var counter = 0;
		var browser = "";
		var version = "";
		
		$.each($.browser, function(i, val) {
			if (counter == 0) {
				browser = i;
				counter++;
			} else {
				version = val;
			}		
		});
		
		var details = "&browser="+ browser;
		details += "&version="+ version;
		
		return details;
	},
	
	recommend: function() {
		var html = '<div id="wrapper_recommend">';
			html += '<div id="wrapper_recommend_container">';
				html += '<div id="recommend_loader">';
					html += '<img src="images/ajax-loader.gif" />';
					html += '<div>Loading</div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
		
		$("body").append(html);	
		$("#wrapper_recommend").fadeIn("fast");
		
		$.ajax({
			url: "actions/action_html.php",
			type: "POST",
			data: "t=recommend",
			cache: false,
			success: function(result) {
				$("#recommend_loader").hide();				
				
				if (result != "") {					
					$("#wrapper_recommend_container").append(result);
					$("#btncancel").click(function() {
						util.hide("wrapper_recommend");					
					});				
					$("#btnok").click(function() {
						recommend.send();
					});
					$("#remail,#rfemail").blur(function() {								
						if (!email.validate($("#"+ this.id).val())) {
							util.show(2,"Invalid Email Address.");
						}
						
						return false;
					});
				} else {
					util.hide("wrapper_recommend");
					util.show(2, "Template Not Found.");
				}
			}	
		});
	},
	
	language: function(id) {		
		var lang = "en";
		
		if (id == "lang_en") {
			lang = "en";			
		} else {
			lang = "jp";			
		}
		
		util.loader(true);
		
		$.ajax({
			url: "actions/action_utility.php",
			type: "POST",
			data: "action=1&lang="+ lang,
			success: function(result) {
				util.loader(false);
				location.reload();				
			}
		});
	},
	
	images: function(el) {
		var html = '<div id="wrapper_picture">';
			html += '<div id="wrapper_picture_container">';			
				html += '<div id="picturebox">';
					html += '<div id="picture_loader">';
						html += '<img src="images/ajax-loader.gif" />';
						html += '<div>Loading</div>';
					html += '</div>';
				html += '</div>';
				html += '<div class="buttonblock">';
					html += '<div class="buttonblockwrap"><input type="button" value="close" id="btnclose" /></div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
		
		$("body").append(html);	
		$("#wrapper_picture").fadeIn("fast");				
		setTimeout(function() {
			$("#picture_loader").hide();
			$("#wrapper_picture_container #picturebox").append(el);			
		},3000);
		$("#wrapper_picture_container #btnclose").click(function() {
			util.hide("wrapper_picture");
			return false;
		});
	},
	
	teaser: function() {
		var html = '<div id="wrapper_teaser">';
			html += '<div id="wrapper_teaser_container">';
				html += '<img src="images/announcements/announcement_20110525.jpg" />';
				html += '<div class="buttonblock">';
					html += '<div class="buttonblockwrap"><input type="button" value="close" id="btnclose" /></div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
		
		$("body").append(html);	
		$("#wrapper_teaser").fadeIn("fast");
		
		$("#btnclose").click(function() {
			util.hide("wrapper_teaser");					
		});
	}
};

var quote = {
	send: function() {		
		var params = $("#form").serialize();
		
		var array = ["courses","fundive","malapascua","bohol","safari","destinations","boats","equipments","snorkelling","extras"];				
		var index = 1;
		var counter = 0;		
		var input = "";
		var flag = false;
		
		for(i=0;i<10;i++) {
			params += "&" + array[i] + "=";
			input = $("form .outer .inner li label input.f"+ index);			
			for(j=0;j<input.length;j++) {
				counter++;
				if ($(input[j]).is(":checked")) {
					if (flag) {
						params += "~";
					}
					params += $(input[j]).attr("t");
					if (input.length > 1 && counter != input.length) {
						flag = true;
					}					
				}				
			}
			flag = false;
			counter = 0;
			index++;
		}				
		
		params += util.details();		
		
		if ($("#qlastname").val() != "" && $("#qfirstname").val() != "" && ($("#qemail").val() != "" || $("#qcontact").val() != "")) {
			$("#free_quote_form").hide();
			$("#quote_loader").show();
			
			$.ajax({
				url: "actions/action_quote.php",			
				type: "POST",
				data: params,
				cache: false,
				success: function(result) {				
					$("#quote_loader").hide();
					$("#free_quote_form").show();
					
					if (result == 1) {						
						util.show(2,"Sending Successful.");					
					} else if (result == 0) {						
						util.show(2,"Please try again.");
					} else if (result == 2) {
						util.show(2,"We have received your request. We will get back to you as soon as possible. Thank you.");
					} else if (result == 3) {
						util.show(2,"Invalid Email Address.");
					}
				},
				error: function() {
					$("#quote_loader").hide();
					$("#free_quote_form").show();				
					util.show(2,"An Error Occured. Please email webmaster@funsundivetravel.com.");
				}
			});
		} else {
			util.show(2,"Please provide us with important details for us to quote.");
		}
	}
};

var recommend = {
	send: function() {
		var params = $("#form").serialize();
		
		if ($("#rlastname").val() != "" && $("#rfirstname").val() != "" && $("#remail").val() != "" && $("#rflastname").val() != "" && $("#rffirstname").val() != "" && $("#rfemail").val() != "") {
			$("#recommend_container").hide();
			$("#recommend_loader").show();
			
			$.ajax({
				url: "actions/action_recommend.php",			
				type: "POST",
				data: params,
				cache: false,
				success: function(result) {				
					$("#recommend_loader").hide();
					$("#recommend_container").show();
					
					if (result == 1) {						
						util.show(2,"Sending Successful.");
					} else if (result == 0) {						
						util.show(2,"Please try again.");					
					} else if (result == 3) {
						util.show(2,"Invalid Email Address.");
					}
				},
				error: function() {
					$("#recommend_loader").hide();
					$("#recommend_container").show();
					util.show(2,"An Error Occured. Please email webmaster@funsundivetravel.com.");
				}
			});
		} else {
			util.show(2,"Please fill up all the fields.");
		}
	}
};

$(document).ready(function() {		
	$("#weatherupdates_image img").click(function() {
		util.weather();
		return false;	
	});
	
	$("#recommendtoafriend_image img").click(function() {
		util.recommend();
		return false;
	});
	
	$("#freequote_image img").click(function() {
		util.quote();		
		return false;
	});

	if (document.URL.indexOf("diving") > -1 || document.URL.indexOf("islandhopping") > -1) {
		$("img").lazyload({ 
			threshold: 200,
			effect: "fadeIn"
		});
	}				
});



