function is_email(emailAddress){
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

$(document).ready(function(){
	/*
	$("#q").keyup(function(){
		var q_input = $(this).val(), query = 'q='+q_input;
		if(q_input.length > 3){
			$.ajax({
				type: "GET",
				url: "/search",
				data: query,
				beforeSend: function(){
					$('input#q').addClass('loading');
				},
				success: function(server_response){
					//If successfull ,output server response in div
					$('#searchresultdata').html(server_response).show();
					//Show the result on the page
					$('span#faq_category_title').html(faq_search_input);

					if ($('input#faq_search_input').hasClass("loading")) {
 						$("input#faq_search_input").removeClass("loading");
 					} //And finally remove the animated loading image to make it static .
 					
				}
			});
		}
		return false;
	});
	*/
	
	$('ul#breadcrumbs').append('<li class="print"><a href="#print"><img src="/images/print-icon.gif" alt="Print this page" /> Print this page</a></li>');
	$('ul#breadcrumbs li.print a').click(function() {
		window.print();
		return false;
	});
	
	
	$(".gallery_item .image a img").each(function(index){
		if (this.height > 234) {
			$(this).css({ marginTop: (234 - this.height) / 2 });
		}
	});
	
	$("#msg").hide();
	var msie6 = $.browser == 'msie' && $.browser.version < 7;
	if (!msie6) {
		$("#msg").offset().top - parseFloat($('#msg').css('margin-top').replace(/auto/, 0));
		$(window).scroll(function(event){
			var y = $(this).scrollTop();
			if (y >= top) 
				$('#msg').addClass('fixed');
			else 
				$('#msg').removeClass('fixed');
		});
	}
	$(".field").focus(function(){
		if($(this).val()==$(this).attr("title")) $(this).val("");
		
		if($(this).parents("#sidebar").size()) $(this).parent().addClass("field-box-focus");
	}).blur(function(){
		if($(this).val()=="") $(this).val($(this).attr("title"));
		
		if($(this).parents("#sidebar").size()) $(this).parent().removeClass("field-box-focus");
	});
	
	$('#newsletterSubmit').click(function(){
		$('#newsletterForm').submit();
		return false;
	});
	
	$('#collectionSubmit').click(function(){
		$('#collectionForm').submit();
		return false;
	});
	
	$('.price-request-link').click(function() {
		var url = this.href, err = false, dialog = $('<div style="display:hidden"></div>').appendTo('body');
		$("#message").hide(0);
		dialog.load(url, {}, function (responseText, textStatus, XMLHttpRequest) { 
			dialog.dialog({
				width: 500,
				/*height: 320,*/
				modal: true,
				buttons: {
					"Request Price": function(el){
						if ($("#price-request-form input[name=name]").val() == "") {
							$("#price-request-form input[name=name]").css({
								"background-color": "#FFEFEF"
							});
							err = true;;
						}
						
						if (is_email($("#price-request-form input[name=email]").val())) {
							err = false;
						} else {
							$("#price-request-form input[name=email]").css({
								"background-color": "#FFEFEF"
							});
							err = true;
						}
							
						if (err != true){
							$.ajax({
								type: 'POST',
								url: "/price-request.php", 
								dataType: 'JSON',
								data: $("#price-request-form").serialize(), 
								success: function(data){
									if (data.error === true) 
										$('#message').removeClass().addClass('error').text(data.msg).show(500);
									
									else {
										$("#price-request-wrapper").hide(0);
										$('#message').removeClass().text(data.msg).show(500);
									}
								}
							});
							$("div.ui-dialog-buttonset:nth-child(1)").hide(0);
							// $("div.ui-dialog-buttonset button.ui-button span.ui-button-text").text("OK");
							$(".ui-widget-overlay").fadeOut(1000);
							$(this).dialog('close');
						}
						
					},
					Cancel: function(){
						$(this).dialog('close');
					}
				}
			});
		});
		
		return false;
	});
	
	$('.viewing-room-add').click(function() {
		var url = this.href;
		//alert(url);
		$.ajax({
			url: url,
			dataType: 'JSON',
			success: function(data){
				$("#msg").text(data.msg);
				$("#msg").slideDown().effect("highlight", {}, 500).delay(3000).fadeOut();
			}
		});
		
		return false;
	});
});
