
$(document).ready(function(){
	$(".rollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
})


$(document).ready(function(){
	$(".rollover-gif").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".gif","_over.gif#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.gif#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.gif#hover",".gif");
				$(this).attr("src",oldSrc);
			}
		}
	);
});

$(document).ready(function(){
	$(".rollover-jpg").hover(
		function(){
			if($(this).attr("src").indexOf("_active") == -1) {
				var newSrc = $(this).attr("src").replace(".jpg","_active.jpg#hover");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("_active.jpg#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_active.jpg#hover",".jpg");
				$(this).attr("src",oldSrc);
			}
		}
	);
});


function makeContact() {
	var firstname = $("#firstname").val();
	var lastname = $("#lastname").val();
	var company = $("#company").val();
	var email = $("#email").val();
	var enquiry = $("#enquiry").val();
	var website = $("#website").val();
	
	if (!firstname || firstname == '') {
		alert('Please enter your firstname');
		return false;
	}
	if (!lastname || lastname == '') {
		alert('Please enter your lastname');
		return false;
	}
	
	if (!company || company == '') {
		alert('Please enter company');
		return false;
	}
	

	if (validateEmail(email) == false) {
		alert('Please enter valid email address');
		return false;
	}
	
	if (!enquiry || enquiry == '') {
		alert('Please enter your question');
		return false;
	}
	
	
	if (enquiry && firstname && lastname && company) {
		input_box = confirm("Is all information correct?");	
		if (input_box == true)	{
			//$("#wrap").css('height','20px');
			$.ajax({
			   type: "POST",
			   url: "/sendform.php",
			   data: "website="+website+"&email="+email+"&enquiry="+enquiry+"&firstname="+firstname+"&lastname="+lastname+"&company="+company,
			   error: function(){
					alert('Sorry, we cannot complete your request.\nPlease try again.');
			   },
			   success: function(data){
				   if (data == "OK") {
						$("#firstname").val('');
						$("#email").val('');
						$("#lastname").val('');
						$("#enquiry").val('');
						$("#company").val('');
						
						alert('Thank you for your request, someone will respond in due course');
				   } else if (data == 'ERROR') {
						alert('Sorry, we cannot complete your request.\nPlease try again.');
				   } else {
						alert(data);
				   }
			   }
			});	
			//$("#wrap").css('height','0px');
			return true;
		} else {
			return false; 	
		}
	} 
	
	
}



function applyForAudit() {
	var firstname = $("#firstname").val();
	var lastname = $("#lastname").val();
	var company = $("#company").val();
	var position = $("#position").val();
	var company_type = $("#company_type").val();
	var company_type_other = $("#company_type_other").val();
	var email = $("#email").val();
	var phone = $("#phone").val();
	var website = $("#website").val();
	
	var cem_managed_in_house = $("#cem_managed_in_house").attr('checked');
	var cem_managed_by_agency = $("#cem_managed_by_agency").attr('checked');
	var cem_no_existing_campaign = $("#cem_no_existing_campaign").attr('checked');
	
	if (cem_managed_in_house == true) {
		cb_1 = 1; 	
	} else {
		cb_1 = 0;	
	}
	
	if (cem_managed_by_agency == true) {
		cb_2 = 1; 	
	} else {
		cb_2 = 0;	
	}
	
	if (cem_no_existing_campaign == true) {
		cb_3 = 1; 	
	} else {
		cb_3 = 0;	
	}
	
	
	
	if (!firstname || firstname == '') {
		alert('Please enter your firstname');
		return false;
	}
	if (!lastname || lastname == '') {
		alert('Please enter your lastname');
		return false;
	}
	
	if (!company || company == '') {
		alert('Please enter company');
		return false;
	}
	
	if (!position || position == '') {
		alert('Please enter your position');
		return false;
	}
	
	if (!company_type || company_type == '0') {
		alert('Please select company type');
		return false;
	}
	
	if (company_type == 'other' && (company_type_other == '' || !company_type_other)) {
		alert('Please state other company type');
		return false;
	}
	
	if (cb_1 == 0 && cb_2 == 0 && cb_3 == 0) {
		alert('Select current email marketing');
		return false;
	}

	if (validateEmail(email) == false) {
		alert('Please enter valid email address');
		return false;
	}
		
	if (!phone || phone == '') {
		alert('Please enter phone number');
		return false;
	}
	
	
	
	if (phone && firstname && lastname && company && email && position) {
		input_box = confirm("Is all information correct?");	
		if (input_box == true)	{
			$.ajax({
			   type: "POST",
			   url: "/applyforaudit.php",
			   data: "website="+website+"&email="+email+"&phone="+phone+"&firstname="+firstname+"&lastname="+lastname+"&company="+company+"&company_type="+company_type+"&company_type_other="+company_type_other+"&position="+position+"&cb_1="+cb_1+"&cb_2="+cb_2+"&cb_3="+cb_3,
			   error: function(){
					alert('Sorry, we cannot complete your request.\nPlease try again.');
			   },
			   success: function(data){
				  // alert(data);
				   if (data == "OK") {
						$("#firstname").val('');
						$("#email").val('');
						$("#lastname").val('');
						$("#phone").val('');
						$("#position").val('');
						$("#company_type").val(0);
						$("#company_type_other").val('');
						$("#company_type_other").attr('disabled',true);
						
						$("#cem_managed_in_house").attr('checked',false);
						$("#cem_managed_by_agency").attr('checked',false);
						$("#cem_no_existing_campaign").attr('checked',false);
						
						$("#company").val('');
						
						$("#wrap").html("<p style='color:green'>Thank you for your request, someone will respond in due course</p>").fadeOut(5000);
				   } else if (data == 'ERROR') {
						$("#wrap").html("<p style='color:red'>Sorry, we cannot complete your request.\nPlease try again.</p>").fadeOut(4000);
				   } else if (data == 'EMAIL_ERRORERROR') {
						alert('This email address is invalid');
				   }
			   }
			});	
			return true;
		} else {
			return false; 	
		}
	} 
	
	
}

function switchCTO() {
	var company_type = $("#company_type").val();
	if (company_type == 'other') {
		$("#company_type_other").val('');
		$("#company_type_other").attr('disabled',false);
	} else {
		$("#company_type_other").val('');
		$("#company_type_other").attr('disabled',true);
	}
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else {
	   return true;
   }
}



$(document).ready(function() {

	$('a').click(function(){
		this.blur();
	});
	
	// smooth hover effects by DragonInteractive
	var hover = hoverEffects();
	hover.init();

});


/**
 * All credit here goes to DragonInteractive and Yuri Vishnevsky
 */
var hoverEffects = function() {
	var me = this;
	var args = arguments;
	var self = {
		c: {
			navItems: '#main-menu li, #portfolio-container li',
			navSpeed: ($.browser.safari ? 600: 350),
			snOpeningSpeed: ($.browser.safari ? 400: 250),
			snOpeningTimeout: 150,
			snClosingSpeed: function() {
				if (self.subnavHovered()) return 123450;
				return 150
			},
			snClosingTimeout: 700 
		},
		init: function() {
			$('.bg', this.c.navItems).css({
				'opacity': 0
			});
			this.initHoverFades()
		},
		subnavHovered: function() {
			var hovered = false;
			$(self.c.navItems).each(function() {
				if (this.hovered) hovered = true
			});
			return hovered
		},
		initHoverFades: function() {
			$('#navigation .bg').css('opacity', 0);
			$(self.c.navItems).hover(function() {
				self.fadeNavIn.apply(this)
			},
			function() {
				var el = this;
				setTimeout(function() {
					if (!el.open) self.fadeNavOut.apply(el)
				},
				10)
			})
		},
		fadeNavIn: function() {
			$('.bg', this).stop().animate({
				'opacity': 1
			},
			self.c.navSpeed)
		},
		fadeNavOut: function() {
			$('.bg', this).stop().animate({
				'opacity': 0
			},
			self.c.navSpeed)
		},
		initSubmenus: function() {
			$(this.c.navItems).hover(function() {
				$(self.c.navItems).not(this).each(function() {
					self.fadeNavOut.apply(this);
				});
				this.hovered = true;
				var el = this;
				self.fadeNavIn.apply(el);
			},
			function() {
				this.hovered = false;
				var el = this;
				if (!el.open) self.fadeNavOut.apply(el);
			})
		}
	};
	
	return self;
};


$(document).ready(function(){
	$('a.ea').each(function(){
		e = this.rel.replace('$$','@');
		this.href = 'mailto:' + e;
		$(this).text(e);
	});
	$('a.ea2').each(function(){
		e = this.rel.replace('$$','@');
		this.href = 'mailto:' + e;
	});	
	getStatues('getshaking', 3);
});
	
function getStatues(username, maxItems)	{
	$.get("/getStatues.php",  function(xml){
		var i = 0;
		var delay = 2500;
		$('item',xml).each(function(i){
			if (i < maxItems) {
				var title = $(this).find("title").text();
				title = title.substring(0, 100)+'...';
				title = title.replace(username+': ','');
				var pubDate = $(this).find("pubDate").text();
				var url = $(this).find("link").text();
				$("#twitter").append("<li><a href=\""+url+"\" target=_blank>"+title+"</a><span>"+pubDate+"</span></li>");
				i++;
			}
		});
		$('#twitter').cycle({ 
			fx:     'scrollRight', 
			timeout: 4000, 
			easing:  'backinout',
			next:   '#twitter',
			pause:   0,
			cleartype:  true,
    		cleartypeNoBg:  true
		});
	});	 
}
