var $j = jQuery.noConflict();

$j(document).ready(function(){
	/* INIT */
	
	$j("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showtitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: ' of ', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});

	
	//$j().ajaxStop($j.unblockUI);
	//$j("#emailHeader").corner("top");
	$j("#swapBanner").cycle({
		fx: 'fade',	   // transition type
		timeout: 7000  // time (ms) between beginning of each transition
	});
	$j(".swap").cycle({
		fx: 'fade',	   // transition type
		timeout: 7000  // time (ms) between beginning of each transition
	});
	$j("#rotate").cycle({
		fx: 'fade',	   // transition type
		timeout: 7000  // time (ms) between beginning of each transition
	});
	$j("#market").change(function(){
		if($j("#market").val() == "sale"){
			$j("#pbf").show();
			$j("#pbt").show();
			$j("#prf").hide();
			$j("#prt").hide();
		}else if($j("#market").val() == "rent"){
			$j("#pbf").hide();
			$j("#pbt").hide();
			$j("#prf").show();
			$j("#prt").show();
		}else if($j("#market").val() == "proj"){
			$j("#pbf").show();
			$j("#pbt").show();
			$j("#prf").hide();
			$j("#prt").hide();
		}
	})
	
	/* BANNER CONTACT */
	$j('#bannerEmailBox').dialog({
		autoOpen: false,
		width: 400,
		modal: true,
		zIndex: 3999,
		resizable: false
	});
	$j(".advertise").click(function(){
		$j('#bannerEmailBox').dialog("open");
	})
	$j("#cancel").click(function(){
		$j('#bannerEmailBox').dialog("close");
	})
	$j("#submit").click(function(){
		$j("#bannerEmailForm").validate({
			rules: {
				name: { required: true },
				email: { required: true, email: true },
				message: { required: true}
			},
			messages: {
				name: { required: "Name cannot be left empty." },
				email: { required: "Email address cannot be left empty.", email: "This is not a valid email address." },
				message: { required: "Contact number cannot be left empty." }
			},
			submitHandler: function() {
				$j("#bannerEmailForm").ajaxSubmit({
					url: "/template/miea/action.php?action=advertise",
					type: "post",
					timeout: 20000,
					clearForm: true, // clear all form fields after successful submit 
					resetForm: true, // reset the form after successful submit 
					beforeSubmit: function(){
						$j('#bannerEmailBox').dialog("close");
					}, 
					error: function(xhr, desc, e) {
						var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
						alert("Failed to save: " + msg);
					},
					success: function(msg){},
					complete: function(){
						$j("#bannerEmailBox").dialog("close");
					}
				})
			}
		})
	})
	/* START: SMARTSEARCH */
	$j("#threeinone").click(function(){
		var defaultVal = document.getElementById('threeinone').defaultValue;
		if ($j("#threeinone").val() == defaultVal) {
			$j("#threeinone").val("");
			$j("#threeinone").css({color: '#3E3E3E'});
		}
	})
	$j("#threeinone").focus(function(){
		var defaultVal = document.getElementById('threeinone').defaultValue;
		if ($j("#threeinone").val() == defaultVal) {
			$j("#threeinone").val("");
			$j("#threeinone").css({color: '#3E3E3E'});
		}
	})
	$j("#threeinone").blur(function(){
		if($j("#threeinone").val() == ''){ 
			$j("#threeinone").val('search');
			$j("#threeinone").css({color: '#C0C0C0'});
		}
	})
	/* END: SMARTSEARCH */
	
	$j("#accordion").hide();
	$j("#accordion").accordion({ // for FAQ use
		autoHeight: false,
		collapsible: true,
		active: false, 
		icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }
	});
	$j("#accordion").fadeIn("slow");
	
	$j("#contact").hide();
	
	$j(".banner").click(function(){
		var idAsLink = $j(this).attr("alt");
		if(idAsLink.substring(0, 7) == "http://"){
			window.location.replace(idAsLink);
		}
	})
	
	$j("#secretariat").click(function(){
		$j("#contact").slideDown();
		$j("#to").val($j("#secretariat").html());
		$j("#towhom").val($j("#secretariat").html());
		$j("#subject").val("Secretariat Matters");
	})
	$j("#membership").click(function(){
		$j("#contact").slideDown();
		$j("#to").val($j("#membership").html());
		$j("#towhom").val($j("#membership").html());
		$j("#subject").val("Members Related Matters");
	})
	$j("#crens").click(function(){
		$j("#contact").slideDown();
		$j("#to").val($j("#crens").html());
		$j("#towhom").val($j("#crens").html());
		$j("#subject").val("Matters Related to Certified Real Estate Negotiators");
	})
	$j("#complaints").click(function(){
		$j("#contact").slideDown();
		$j("#to").val($j("#complaints").html());
		$j("#towhom").val($j("#complaints").html());
		$j("#subject").val("For Filling Complaints");
	})
	$j("#enquiry").click(function(){
		$j("#contact").slideDown();
		$j("#to").val($j("#enquiry").html());
		$j("#towhom").val($j("#enquiry").html());
		$j("#subject").val("General Enquiries");
	})
	$j("#send").click(function(){
		var name = document.getElementById("name").value;
		var email = document.getElementById("email").value;
		var message = document.getElementById("message").value;
		if(name == "" || email == "" || message == ""){
			$j.blockUI({
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .5, 
					color: '#fff' 
				},
				message: "Please fill in all the fields before sending the email." 
			});
			$j('.blockOverlay').attr('title','').click($j.unblockUI); 
			return;
		}
	
		$j("#contactForm").ajaxSubmit({
			url: "/template/miea/action.php?action=email&subject=" + $j("#subject").val,
			type: "post",
			timeout: 20000,
			clearForm: true, // clear all form fields after successful submit 
			resetForm: true, // reset the form after successful submit 
			beforeSubmit: function(){
				$j.blockUI({
					css: { 
						border: 'none', 
						padding: '15px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '10px', 
						'-moz-border-radius': '10px', 
						opacity: .5, 
						color: '#fff' 
					}, 
					timeout: 3000,
					message: "Please wait while we are processing your email." 
				}); 			
			}, 
			error: function(xhr, desc, e) {
				var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
				alert("Failed to save: " + msg);
			},
			success: function(msg){
				if(msg == "true"){
					$j.blockUI({
						css: { 
							border: 'none', 
							padding: '15px', 
							backgroundColor: '#000', 
							'-webkit-border-radius': '10px', 
							'-moz-border-radius': '10px', 
							opacity: .5, 
							color: '#fff' 
						}, 
						timeout: 3000,
						message: "We have received your email. We will check and come back to you shortly."
					});
				}else if(msg == "true"){
					$j.blockUI({
						css: { 
							border: 'none', 
							padding: '15px', 
							backgroundColor: '#000', 
							'-webkit-border-radius': '10px', 
							'-moz-border-radius': '10px', 
							opacity: .5, 
							color: '#fff' 
						}, 
						timeout: 3000,
						message: "Email cannot be sent at the moment. Please try again later."
					});
				}
				else
					alert(msg);
			},
			complete: function(){
				$j("#contact").slideUp();
			}
		});
	})
	
	/* QUICK SEARCH */
	$j("#state").change(function(){
		$j().ajaxSubmit({
			url: "/template/miea/action.php?action=getSuburb&selectedState="+$j("#state").val(),
			type: "post",
			timeout: 15000,
			beforeSubmit: function(){
				$j("#suburb").hide();
				$j($j("#wait").html()).insertAfter("#suburb");
			},
			error: function(xhr, desc, e) {
				var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
				alert("Failed to save: " + msg);
				// alert(xhr.responseText);
			},
			success: function(msg){
				var msg_arr = msg.split("||");
				switch(msg_arr[0]){
					case "true":
						$j("#suburb").next().remove();
						$j("#suburb").show();
						$j("#suburb").html("");
						$j("#suburb").html(msg_arr[1]);
						break;
					case "false":
						alert(msg_arr[1]);
						break;
					default:
						alert(msg_arr[1]);
				}
			},
			complete: function(){}
		});
	})
	
	$j("#estate").change(function(){
		$j().ajaxSubmit({
			url: "/template/miea/action.php?action=getPropType&estate=" + $j("#estate").val(),
			type: "post",
			timeout: 15000,
			beforeSubmit: function(){
				$j("#cat").hide();
				$j($j("#wait").html()).insertAfter("#cat");
			},
			error: function(xhr, desc, e) {
				var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
				alert("Failed to save: " + msg);
				// alert(xhr.responseText);
			},
			success: function(msg){
				var msg_arr = msg.split("||");
				switch(msg_arr[0]){
					case "true":
						$j("#cat").next().remove();
						$j("#cat").show();
						$j("#cat").html("");
						$j("#cat").html(msg_arr[1]);
						break;
					case "false":
						alert(msg_arr[1]);
						break;
					default:
						alert(msg_arr[1]);
				}
			},
			complete: function(){}
		});
	})
	
	
	/* EMAIL ME */
	$j('#errDialog').dialog({
		autoOpen: false,
		width: '400',
		modal: true,
		zIndex: 3999,
		resizable: false,
		buttons: {
			"Ok": function() {
				$j(this).dialog("close"); 
			}
		}
	});
	$j('#sucDialog').dialog({
		autoOpen: false,
		width: '400',
		modal: true,
		zIndex: 3999,
		resizable: false,
		buttons: {
			"Ok": function() {
				$j(this).dialog("close"); 
			}
		}
	});
	$j("#emSubmit").click(function(){
		emailMeValidate();
	});
	emailMeValidate = function(){
		$j("#emailme").validate({
			rules: {
				emName: { required: true },
				emEmail: { required: true, maxlength: 100, email: true },
				emContact: { required: true, number: true }
			},
			messages: {
				emName: { required: "Name cannot be left empty." },
				emEmail: { required: "This is not valid email address." },
				emContact: { required: "Contact number cannot be left empty." }
			},
			submitHandler: function() { 
				$j("#emailme").ajaxSubmit({
					url: "/template/miea/action.php?action=emailme",
					type: "post",
					timeout: 20000,
					beforeSubmit: function(){
					}, 
					error: function(xhr, desc, e) {
						var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
						alert("Failed to save: " + msg);
						// alert(xhr.responseText);
					},
					success: function(msg){
						var msg_arr = msg.split("||");
						switch(msg_arr[0]){
							case "true":
								$j("#sucDialog").html(msg_arr[1]);
								$j("#sucDialog").dialog("open");
								break;
							case "false":
								$j("#sucDialog").html(msg_arr[1]);
								$j("#sucDialog").dialog("open");
								break;
							default:
								alert(msg);
						}
					},
					complete: function(){
						//$j('form').clearForm();// clear data
					}
				});
			}
		});
		return false;
	}
});var $j = jQuery.noConflict();