var cp = {};

cp.jQuery = jQuery.noConflict();

(function($) {
	$().ready(function() {
		
		$('#mc-embedded-subscribe-form').colourProcessForm();
		
		$('#twitter .module-header a').click(function() {
			window.open($(this).attr('href'));
			return false;			
		});
		
		/* hover nav */
		
		var hovertimeout;
		var currentMenu = null;
		
		$("#navigation ol>li").hover(function() {
			if(currentMenu && currentMenu !== this) {
				$(currentMenu).find("ul").hide();
			}

			currentMenu = this;
			$(this).find("ul").show();
			clearTimeout(hovertimeout);
		}, function() {
			var that = this,
			    removeMenu = function() {
				$(that).find("ul").hide();				
			};
			hovertimeout = setTimeout(removeMenu, 200);
			
		});
		
		

		$("#navigation ol li li").hover(function() {
			$(this).addClass("hover");		
		}, function() {
			$(this).removeClass("hover");	
		});
		
		

		(function($) {
			var url = "http://twitter.com/status/user_timeline/colourprocess.json?count=3&callback=?"; 
			       $.getJSON(url, 
			        function(data){ 
					var outputHTML = '<ol>';
			            $.each(data, function(i, item) {			
							outputHTML += '<li><a href="http://twitter.com/colourprocess/status/' + item.id + '">' + item.text + '</a></li>';
			            }); 
						outputHTML += '</ol>';
						$('#twitter').append(outputHTML);	
						
			        }); 	
			})(cp.jQuery);		
		
		  (function() {
			    var myLatlng = new google.maps.LatLng(-33.887074400583614, 151.2074249982834);
			    
			    var myOptions = {
			      zoom: 17,
			      center: myLatlng,
			      mapTypeId: google.maps.MapTypeId.SATELLITE,
			      disableDefaultUI: true
			    }
			    
			    
			    var map = new google.maps.Map(document.getElementById("map-location"), myOptions);
			    var marker = new google.maps.Marker({
			        position: myLatlng, 
			        map: map
			    });
			    
			    var infowindow = new google.maps.InfoWindow({
			        content: '<p>35 Buckingham St Surry Hills NSW 2010</p>'
			    });

			    google.maps.event.addListener(marker, 'click', function() {
			      infowindow.open(map,marker);
			    });

			    
			  })();		
		
	});
}(cp.jQuery));


/*
 * form processor
 * 
 */

(function ($) {
	jQuery.fn.colourProcessForm = function (options) {
	   var settings = jQuery.extend(jQuery.fn.colourProcessForm.defaults, options),
	       validate,
	       validform = true;
	   
	  validate = function(obj, msg, regex) {
		   var re = new RegExp(regex),
		       val = obj.val();
		   
		   if(re.test(val) === false) {
			   obj.after('<span class="error-message">' + msg + '</span>');
			   validform = false;
		   }
	  };
	  
	  return this.each(function () {
		  $(this).submit(function () {
			  validform = true;
			  $(this).find('.error-message').remove();
			  // email
			  validate($(this).find('input.#mce-EMAIL'),'Valid email address required',/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/);
			  // first name
			  validate($(this).find('input.#mce-FNAME'),'First name required',/[a-zA-Z]/g);			  
			  // last name
			  validate($(this).find('input.#mce-LNAME'),'Last name required',/[a-zA-Z]/g);			  
			  // company
			  validate($(this).find('input.#mce-MMERGE3'), 'Company name required',/[a-zA-Z]/g);	
			  
			  return validform;
		  });
	  });
	  
	  jQuery.fn.colourProcessForm.defaults = {};
	  
	};

})(cp.jQuery);