/**
 * phpjs.org f()s
 */

function array_search (needle, haystack, argStrict) {
    // Searches the array for a given value and returns the corresponding key if successful  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/array_search
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
    // *     returns 1: 'surname'

    var strict = !!argStrict;
    var key = '';

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            return key;
        }
    }

    return false;
}

function in_array (needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false

    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}

/** end phpjs.org f()s */


function extractStyleInfo(element) {

	var elementID = "#" + element;


//	var interestingProperties = ["font-size", "color", "margin-top", "padding-left", "text-transform"];

	var cssText =  elementID +" {<br />";


//	for (var i=0; i < interestingProperties.length; i++) {
//		cssText = cssText + interestingProperties[i] + ": " + $(element).css(interestingProperties[i]) + ";<br />";
//	}

	var elem1 = document.getElementById("h1stub");
	var style = window.getComputedStyle(elem1, null);
	var cssRulesString = style.cssText;
	var cssJumble = cssRulesString.split(";");
	var cleanedString = "Element Styles: <br />";	
	for (var i=0; i < cssJumble.length; i++) {
		var compareMe = cssJumble[i];
		if (!(compareMe.substr(1,1) == "-")) {
		cleanedString += cssJumble[i] + "<br />";
		}
	}

	$(elementID).after('<code>' + cleanedString + '</code>');	

}

// unused!
function get_floorplan(floorplan_id, floorplan_size) {
		$.ajax({
			type: "POST", 
			url: "ajax/get_floor_plan", 
			context: document.body,
			data: {
				floorplan_id: floorplan_id,
				floorplan_size: floorplan_size
			},
			success: function(response){
		        $(".floorplan_container").prepend(response);
				armFloorplans();
		      }
		});
	}

// Commenting original f() - going to try to arm by providing an id
// function armFloorplans (){
// 	var objectID;
// 	$("img.floorplan-image").click(function(){
// 		// grab the floorplan's id
// 		objectID = $(this).attr("objectID");
// 		
// 		// $("img.floorplan-image[rel]").overlay({
// 		$("img#floorplan-image-"+objectID+"[rel]").overlay({
// 			fixed: false,
// 			mask: {
// 				color: '#fff',
// 				opacity: '0.8'
// 			},
// 			top:'5',
// 			onBeforeLoad: function() {
// 				alert("onBeforeLoad: with objectID="+objectID);
// 				$.ajax({
// 					type: "POST", 
// 					url: "ajax/get_floor_plan_overlay", 
// 					context: document.body,
// 					data: {
// 						objectID: objectID,
// 						overlay_type: "floorplan"
// 					},
// 					success: function(response){
// 				        $(".simple_overlay").html(response).append(objectID);
// 				      }
// 				});
// 			}
// 		});
// 
// 
// 	});
// 	
// 
// 	Cufon.refresh();
// }

function armFloorplans(providedID){
		
	$("img#floorplan-image-"+providedID+"[rel]").overlay({
		fixed: false,
		mask: {
			color: '#fff',
			opacity: '0.8'
		},
		top:'5',
		onBeforeLoad: function() {
			$.ajax({
				type: "POST", 
				url: "ajax/get_floor_plan_overlay", 
				context: document.body,
				data: {
					objectID: providedID,
					overlay_type: "floorplan"
				},
				success: function(response){
			        $("div.simple_floorplan_overlay div#floorplan-overlay-content").html(response);
			      }
			});
		}
	});

	Cufon.refresh();
}

function armGalleryOverlay(providedID) {
	$("img#gallery-"+providedID+"[rel]").overlay({
		fixed: false,
		mask: {
			color: '#fff',
			opacity: '0.8'
		},
		top:'5',
		onBeforeLoad: function() {
			$.ajax({
				type: "POST",
				url: "ajax/get_gallery_image_overlay", 
				context: document.body,
				data: {
					objectID: providedID,
					overlay_type: "floorplan"
				},
				success: function(response){
			        $("div.simple_gallery_overlay div#gallery-overlay-content").html(response);
			      }
			});
		}
	});
}

function image_replacement(targetEl, imgID) {
	$.get('ajax/get_image', {image_id: imgID}, function(data, textStatus, xhr) {
		$(targetEl).html(data);
	});
}

//function goToFirstTitle(){
//	$.scrollTo($('div.modulebox:eq(0) h4'), {
//		duration:'4',
//		offset: {left: 0, top:-200 }
//	});				
//}





$(document).ready(function() {
	// Stuff to do as soon as the DOM is ready;
		
	// ensure required fields aren't empty
	$(".required").focusout(function(){
		var value = $(this).val();
		if(!value) {
			markFieldAsError(this);
		}
		else {
			markFieldOK(this);
		}
	});
	
	// ensure valid email address
	$("#contact-form input#email").focusout(function(){
		// extract and check email
		var email = $("#contact-form input#email").val();
		if (!checkEmail(email))
		{
			markFieldAsError(this);
			return;
		}
		else 
		{
			markFieldOK(this);
		}
		
	});
	
	
	$("#contact-submit").click(function(){
		//$('form#contact-form').submit(function() { return false; });

		var email = $("#contact-form input#email").val();
		if (!checkEmail(email)) 
		{
			markFieldAsError($("#contact-form input#email"));
			alert("Please enter a valid Email Address");
			return false;
		}
		else 
		{
			markFieldOK($("#contact-form input#email"));
		}
		
		var firstname = $("#contact-form input#name").val();
		if(!firstname) {
			markFieldAsError($("#contact-form input#name"));
			alert("Please enter your Name");
			return false;
		}
		else
		{
			markFieldOK($("contact-form input#firstname"));
		}
		
		var phone = $("#contact-form input#phone").val();
		if(!phone) {
			markFieldAsError($("#contact-form input#phone"));
			alert("Please enter your Phone Number");
			return false;
		}
		else
		{
			markFieldOK($("contact-form input#phone"));
		}
		
		var comment = $("#contact-form textarea#questions").val();
		if(!comment) {
			markFieldAsError($("#contact-form textarea#questions"));
			alert("Please enter your Comment");
			return false;
		}
		else
		{
			markFieldOK($("contact-form textarea#questions"));
		}
				
		var serialize = $("#contact-form").serialize();
		
		
		$.ajax({
			url: "submit_comment",
			type: "POST",
			data: serialize,
			success: function(response){
				console.log(response);
				$("#contact-form").fadeOut();
				$("#contact-submit").fadeOut(function(){
					$(".tool").html("<div style='margin-top:35px;'>Thank you for your submission.</div>").fadeIn('fast');
				});
			}
		});

		return false;
	});
});

function checkEmail(email)
{	
	var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return pattern.test(email);
}
function markFieldAsError(theField) {
	$(theField).addClass("input-error");
}
function markFieldOK(theField) {
	$(theField).removeClass("input-error");
}







