$(document).ready(function(){

	var defaultText = "Example: birchhosting";
	
	// Set-up domain search input
	$('#domain').bind("focus", function(e) {
		if(this.value == defaultText) this.value = "";
	});
	
	$('#domain').bind("blur", function(e) {
		if(this.value == "") this.value = defaultText;
	});
	
	var defaultEnterDomainText = "eg:- birchhosting";
	$('#enterDomain1').bind("focus", function(e) {
		if(this.value == defaultEnterDomainText) this.value = "";
	});
	
	$('#enterDomain1').bind("blur", function(e) {
		if(this.value == "") this.value = defaultEnterDomainText;
	});
	
	var defaultEnterDomainText2 = "eg:- birchhosting.co.uk";
	
	$('#enterDomain2').bind("focus", function(e) {
		if(this.value == defaultEnterDomainText2) this.value = "";
	});
	
	$('#enterDomain2').bind("blur", function(e) {
		if(this.value == "") this.value = defaultEnterDomainText2;
	});
	
	resizeTableRows("row1");
	resizeTableRows("row2");

	function resizeTableRows(rowClass) {
		var rows = $('.' + rowClass);
		
		var defaultHeight = 33;

		var rowArray = new Array();
		
		for(var i = 0; i < rows.length; i++) {
			var row = $(rows[i]);
			var cells = row.children("div");
			
			// Create vars to store max height of row and vertical padding
			var maxHeight = 0;
			var verticalPadding = 0;
			
			// Loop through each row cell and get max height
			for(var j = 0; j < cells.length; j++) {
				var cell = $(cells[j]);
				
				// Set vertical padding
				if(j == 0) {
					var paddingTop = Number(cell.css("padding-top").substring(0, cell.css("padding-top").indexOf("px")));
					verticalPadding = paddingTop * 2;				
				}
				
				// Get height of tallest content span
				var contentSpans = cell.children(".content");
				if(contentSpans.length > 0) {
					var span = $(contentSpans.get(0));					
					if(span != undefined && span.height() > maxHeight) maxHeight = span.height();
				}
			}
			
			// If no max-height has been set or max height is same as default height,
			// continue to next row
			if(maxHeight == 0 || maxHeight + 5 <= defaultHeight) {
				continue;
			}
			
			// Calulate new height of row
			var newHeight = maxHeight + verticalPadding;
			
			// Set the new calculated height of the div
			row.css("height", newHeight + "px");
			
			// Set dynamic image as bg (even though we haven't attempted to create it yet) as it
			// may be already in existence and so will prevent a delay in displaying
			row.css("background-image", "url(/images/dynamic/table_" + rowClass + "_bg_528_" + newHeight + ".jpg)");
			
			// If the browser is not MSIE 7
			if(false == (jQuery.browser.msie && jQuery.browser.version.indexOf("7") == 0)) {
				// Add bottom padding to the row
				row.css("padding-bottom", (verticalPadding / 2) + "px");
			}
			
			if(rowArray[rowClass + "_" + newHeight] == undefined) {
				rowArray[rowClass + "_" + newHeight] = new Array();
			}
			rowArray[rowClass + "_" + newHeight].push(row);
		}
		
		var imageArray = new Array();		
		
		for(var key in rowArray) {		
			rowClass = key.substring(0, key.indexOf("_"));			
			newHeight = key.substring(key.indexOf("_") + 1, key.length);

			// Make AJAX call to PHP that will create image of correct size
			$.ajax({
				type:"GET",
				url: "/make-image/make-image.php",
				data: "width=" + 528 + "&height=" + newHeight + "&type=" + rowClass,
				success: function(msg) {				
					var height = msg.substring(0, msg.indexOf(":"));
					var image = msg.substring(msg.indexOf(":") + 1, msg.length);					
					var hoverImage = image.substring(0, image.length - 4) + "_hover." + image.substring(image.length - 3);
					
					var imageImg = new Image();
					imageImg.src = image;
					imageArray.push(imageImg);
					
					var hoverImg = new Image();
					hoverImg.src = hoverImage;
					imageArray.push(hoverImg);
					
					for(var x = 0; x < rowArray[rowClass + "_" + height].length; x++) {					
						var thisRow = rowArray[rowClass + "_" + height][x];						
						thisRow.css("background-image", "url("+ image +")");
						thisRow.hover(
							function() { $(this).css("background-image", "url(" + hoverImage + ")") }, 
							function() { $(this).css("background-image", "url("+ image + ")") }
						);			
					}					
				}
			});	
		}
	}
});

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

function showContactForm() {
	animatedcollapse.toggle('contact-content');
	$("#subject").val("Custom web design enquiry");
}

function showContactForm1() {
	animatedcollapse.toggle('contact-content1');
	$("#subject").val("Sales Enquiry");
}