document.write('<style type="text/css">#contact form{top:-1000px;opacity:0;}</style>');

//for the image's original z-index;
var savedImageZindex = new Object();

//preload
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function(){
	//pretty photo
	if (typeof $("a[class*='prettyPhoto']").prettyPhoto == 'function') {
		$("a[class*='prettyPhoto']").prettyPhoto({
			padding: 50,
			opacity: 0.35,
			showTitle: true,
			counter_separator_label: '/',
			theme: 'dark_square',
			modal: false
		});
	}
	
	//preload images for hover
	jQuery.preLoadImages("/img/video-transparent-bg.png", "/img/marketing-transparent-bg.png", "/img/design-transparent-bg.png", "/img/web-transparent-bg.png", "/img/tooltip-lip.png", "/icon/close.png");
	
	// arget="_blank"
	$("a[rel='nofollow'], a[rel='external']").click( function() {
        window.open(this.href);
        return false;
    });
		
	// form validation
	$(".validate").blur(function() {
		$(this).validate.init(this);
	});
	
	$("#contact-status").hide();
	$("#contact form").css({top:'-1000px', opacity:0});
	
	// form submit
	$("#submit").click(function() {
		$("#contact-status").html('<img src="/icon/ajax-loader.gif" width="32" height="32" alt="loading" />').fadeIn("fast");
		$.ajax({
			type: "POST",
			url: "http://" + window.location.hostname + "/is/ajax_contact_submit/",
			data: $('#contact > form').serializeArray(),
			success: function(msg){
				$("#contact-status").html('<img src="/icon/check.png" width="32" height="32" alt="loading" />' + msg);
				window.setTimeout(function() {
 					$("#contact-status").fadeOut(500, animateContactUp);
				}, 2000);
   			},
   			error: function(XMLHttpRequest, textStatus, errorThrown) {
   				$("#contact-status").html('<img src="/icon/cancel.png" width="32" height="32" alt="loading" /><ul>' + XMLHttpRequest.responseText + '</ul>');
   				window.setTimeout(function() {
 					$("#contact-status").fadeOut(500);
				}, 3000);
   			}
 		});
 		return false;
	});
	
	var contactState = 'closed';
	var animationPosition = 0;
	var animationTimer;
	var animationArray = new Array();
		for (var i=0;i<12;i++) {
			animationArray[i] = new Array();
		}
		animationArray[0][0] = 0;
		animationArray[0][1] = 52;
		animationArray[1][0] = 52;
		animationArray[1][1] = 99;
		animationArray[2][0] = 151;
		animationArray[2][1] = 99;
		animationArray[3][0] = 250;
		animationArray[3][1] = 137;
		animationArray[4][0] = 387;
		animationArray[4][1] = 178;
		animationArray[5][0] = 565;
		animationArray[5][1] = 240;
		animationArray[6][0] = 805;
		animationArray[6][1] = 273;
		animationArray[7][0] = 1078;
		animationArray[7][1] = 382;
		animationArray[8][0] = 1460;
		animationArray[8][1] = 443;
		animationArray[9][0] = 1903;
		animationArray[9][1] = 475;
		animationArray[10][0] = 2378;
		animationArray[10][1] = 480;
		animationArray[11][0] = 2858;
		animationArray[11][1] = 485;
		animationArray[0][0] = 0;
		animationArray[0][1] = 52;
		animationArray[1][0] = 52;
		animationArray[1][1] = 99;
		animationArray[2][0] = 151;
		animationArray[2][1] = 99;
		animationArray[3][0] = 250;
		animationArray[3][1] = 137;
		animationArray[4][0] = 387;
		animationArray[4][1] = 178;
		animationArray[5][0] = 565;
		animationArray[5][1] = 240;
		animationArray[6][0] = 805;
		animationArray[6][1] = 273;
		animationArray[7][0] = 1078;
		animationArray[7][1] = 382;
		animationArray[8][0] = 1460;
		animationArray[8][1] = 443;
		animationArray[9][0] = 1903;
		animationArray[9][1] = 475;
		animationArray[10][0] = 2378;
		animationArray[10][1] = 480;
		animationArray[11][0] = 2858;
		animationArray[11][1] = 485;

	//open contact
	$('#contact span').click(function(){
		if (contactState == 'closed')
			animateContactDown();
		else
			animateContactUp();
	});
	$('#contact').css({'width': '538px', 'height': animationArray[0][1] + "px", 'backgroundPosition':  "0px " + "-" + animationArray[0][0] + "px"});
	$('#contact span').css({'position': 'absolute', 'right': '20px'});
		
	//contact
	function animateContactDown() {
		contactState = 'open';
		clearInterval(animationTimer);
		animationTimer = setInterval(animateDown, 75);
	}
	
	function animateContactUp() {
		contactState = 'closed';
		clearInterval(animationTimer);
		$('#contact form').animate({'opacity':0}, 400, 'linear', function(){
			animationTimer = setInterval(animateUp, 75);
		});
	}
	
	function animateUp() {
		$('#contact').css({'height': animationArray[animationPosition][1] + "px", 'backgroundPosition':  "0px " + "-" + animationArray[animationPosition][0] + "px"});
		if (animationPosition == 0) {
			clearInterval(animationTimer);
			$('#contact span').html('contact +');
			$('#contact form').css({top: '-1000px'});
		} else {
			animationPosition--;
		}
	}
	
	function animateDown() {
		$('#contact form').css({top: '40px'});
		$('#contact').css({'height': animationArray[animationPosition][1] + "px", 'backgroundPosition':  "0px " + "-" + animationArray[animationPosition][0] + "px"});
		if (animationPosition > 10) {
			clearInterval(animationTimer);
			jQuery.each(jQuery.browser, function(i, val) {
				if($.browser.msie && parseFloat(jQuery.browser.version.substr(0,3)) < 8)
					$('#submit').css({bottom: 0, right: 0});
			});
			$('#contact form').animate({opacity: 1}, 400);
			$('#contact span').html('contact &ndash;');
		} else {
			animationPosition++;
		}
	}
	
	//forms
	var checkedBoxImg = "/img/checkboxChecked.gif";
	var uncheckedBoxImg = "/img/checkboxUnchecked.gif";
	var imageRadioChecked = "/img/radiobuttonChecked.png";
	var imageRadioUnchecked = "/img/radiobuttonUnchecked.png";
	
	//checkboxes
	$("input:checkbox").css("display", "none");
	$("input:checkbox").each(function (i) {
		$(this).before('<a class="check-box"><img id="fakecheck' + i + '" src="' + uncheckedBoxImg + '" alt="' + $(this).next().html() + ' checkbox - unchecked" /></a>');
	});
	$("input:checkbox:checked").prev().children().attr("src", checkedBoxImg);
	$("input:checkbox").prev().click(function(){
		var thisCheckBox = $(this);
		if ($(this).next().is(":checked")) {
			$(this).children().attr("src", uncheckedBoxImg);
			$(this).next().removeAttr("checked");
			$(this).children().attr('alt', $(this).siblings('label').html() + ' checkbox - unchecked');
		} else {
			$(this).children().attr("src", checkedBoxImg);
			$(this).next().attr("checked","checked");
			$(this).children().attr('alt', $(this).siblings('label').html() + ' checkbox - checked');
		}
	});

	//radio
	$("input:radio").css({display: 'none'});
	$("input:radio").each(function (i) {
		$(this).before('<a class="radio-button"><img id="fakeradio' + i + '" src="' + imageRadioUnchecked + '" alt="' + $(this).next().html() + ' radio - unchecked" /></a>');
	});
	$("input:radio:checked").each(function (i) {
		var label = $(this).next();
		$(this).prev().children("img").attr({src: imageRadioChecked, alt: label.html() + ' radio - checked'});
		//$(this).before('<a class="radio-button"><img id="fakeradio' + i + '" src="' + imageRadioChecked + '" alt="' + $(this).next().html() + ' radio - unchecked" /></a>');
	});
	
	$("input:radio").prev().click(function(){
		var radio = $(this).next();
		var label = radio.next();
		var name = radio.attr("name");
		$("input:radio:checked[name='" + name + "']").removeAttr("checked").prev().children().attr({src: imageRadioUnchecked, alt: label.html() + ' radio - checked'});
		$(this).children().attr({src: imageRadioChecked, alt: label.html() + ' radio - checked'});
		radio.attr({checked: "checked"});
	});

	
	//clicks for service images
	//tooltip
	var savedTop = new Object();
	//tooltip
	
	$('.service-image').each(function () {
		var serviceImageId = $(this).attr('id');
		savedImageZindex[serviceImageId] = $(this).css('z-index');
		var serviceImage = $(this);
		jQuery.each(jQuery.browser, function(i, val) {
			if($.browser.msie)
				serviceImage.children('.service-statement').children('.close').hide();
		});
	});

	// Window/Keyboard events
	$(window).scroll(function(){ $scrollPos = _getScroll(); _centerOverlay(); _resizeOverlay(); });
	$(window).resize(function(){ _centerOverlay(); _resizeOverlay(); });
	
	$(".service-sprite, .service-image img").click(function(){
		if($('.si_overlay').size() == 0){
			_buildOverlay(); // If the overlay is not there, inject it!
		}
		var serviceImage = $(this).parent();
		
		serviceImage.css({zIndex: 9600});
		//	_centerOverlay(); // Center it
		// Fade the content in
		$('div.si_overlay').show().fadeTo(500, .7, function(){					
			// Show content
			serviceImage.children('.service-statement').children('.close').bind('click', _removeOverlay);
			serviceImage.children('.service-statement').fadeIn(500, function() {
				jQuery.each(jQuery.browser, function(i, val) {
					if($.browser.msie)
						serviceImage.children('.service-statement').children('.close').show();
				});
			});
		});
	});
});
	
function _buildOverlay(){
	toInject = "";
		
	// Build the background overlay div
	toInject += "<div class='si_overlay'></div>";
	
	$('body').append(toInject);	
	
	// So it fades nicely
	$('div.si_overlay').css('opacity',0);
	
	jQuery.each(jQuery.browser, function(i, val) {
		if(!($.browser.msie && parseFloat(jQuery.browser.version.substr(0,3)) < 8))
			$('div.si_overlay').css('height',$(document).height()).hide().bind('click', _removeOverlay);
		else {
			$('div.si_overlay').css('height', 0).hide().bind('click', _removeOverlay);
			$('body').bind('click', _removeOverlay);
		}
	});
}

function _centerOverlay(){
}
	
function _getScroll(){
	
}

function _resizeOverlay() {
	$('div.pp_overlay').css({
		'height':$(document).height(),
		'width':$(window).width()
	});
}

function _removeOverlay() {
	$('.service-statement').fadeOut(500);
	jQuery.each(jQuery.browser, function(i, val) {
		if($.browser.msie) {
			$('div.close').hide();
		}
	});
	$('div.si_overlay').fadeOut(500, function(){
		$('div.si_overlay').remove();
		$('.service-image').each(function () {
			var serviceImage = $(this);
			jQuery.each(jQuery.browser, function(i, val) {					
				if(!($.browser.msie && parseFloat(jQuery.browser.version.substr(0,3)) < 8)) {
					var serviceImageId = serviceImage.attr('id');
					serviceImage.css({zIndex: savedImageZindex[serviceImageId]});
				}
			});
		});
	});
}

new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
		init: function(o) {
			if(o.name == 'name') { this.theirname(o) };
			if(o.name == 'company') { this.company(o) };
			if(o.name == 'phone') { this.phone(o) };
			if(o.name == 'email') { this.email(o) };
		},
		//can't be blank
		theirname: function(o) {
			if (!o.value == "") {
				doSuccess(o);
			} else {
				doError(o);
			};
		},
		company: function(o) {
			if (!o.value == "") {
				doSuccess(o);
			} else {
				doError(o);
			};
		},
		//proper phone number
		phone: function(o) {
		var phone = /^\(?\d{3}\)?\s?-?\d{3}-?\s?\d{4}$/;
			if (o.value.match(phone) && !o.value == "") {
				doSuccess(o);
			} else {
				doError(o);
			};
		},
		//proper email
		email: function(o) {
			var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (o.value.match(email)) {
				doSuccess(o);
			} else {
				doError(o);
			};
		}
	};
}
	
	function doSuccess(o) {
		$('#' + o.id).removeClass("error");
		$('#' + o.id).addClass("success");
	}
	
	function doError(o) {
		$('#' + o.id).addClass("error");
		$('#' + o.id).removeClass("success");
	}