$(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(){
	$(".calcrollover").hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) {
				var newSrc = $(this).attr("src").replace(".png","_over.png#hover");
				$(this).attr("src",newSrc);
				loadCalc($(this).attr('id'));
			}
		},
		function(){
			if($(this).attr("src").indexOf("_over.png#hover") != -1) {
				var oldSrc = $(this).attr("src").replace("_over.png#hover",".png");
				$(this).attr("src",oldSrc);
			}
		}
	); 
	
	$(".calcrollover").click(
		function() {
			var oldSrc = $(this).attr("src").replace("_over.png#hover","_on.png#hover");
			$(this).attr("src",oldSrc);
		}
	);
});


function sendForm() {
	var name = $("#name").val();
	var email = $("#email").val();
	var message = $("#message").val();
	
	if (name=='') 		{ alert('Please enter you name'); return false; }
	if (validateEmail(email) == false) 		{ alert('Please enter valid email address'); return false; }
	if (message=='') 		{ alert('Please enter message'); return false; }
	
	input_box = confirm("Is all information correct?");	
	if (input_box == true)	{
		$.ajax({
		   type: "POST",
		   url: "/send_form.php",
		   data: "name="+name+"&email="+email+"&message="+message,
				 
		   // on success
		   error: function(){
				alert('ooops! - nasty error. Please try again.')
		   },
		   success: function(data){
			    if (data == 'OK') {
					$(".contact-form-container").fadeOut(200).html('Your message has been sent. <br />Thank you.<br /><br />We will reply as soon as possible').fadeIn(500);
				} else {
			   		alert('ooops! - nasty error. Please try again.');
		   		}
		   } 
		});
		return true;
	}
	else {	
		return false; 	
	}
}



function verifyForm() {
	d = document.frmRequest;
	var rf_fcbp2			= d.rf_fcbp2.checked;
	if (rf_fcbp2==false) 		{ alert('You must read and agree to our privacy policy'); return false; }
	
	input_box = confirm("Is all information correct?");	
	if (input_box == true)	
		return true;
	else	
		return false; 	
}

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: '#navbar 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;
};


function resetForm() {
	$("#name").val('');
	$("#email").val('');
	$("#message").val('');
}


function resetQuoteForm() {
	$("#username").val('');
	$("#company").val('');
	$("#email").val('');
	$("#phone").val('');
	$("#description").val('');
	$("#link1").val('');
	$("#link2").val('');
	$("._services").attr("checked","false");
}



$(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('codateam', 3);
});
	
function getStatues(username, maxItems)	{
	$.get("/getStatues.php",  function(xml){
		var i = 0;
		var delay = 2500;
		$('li', '#twitter').remove(); 
		$('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:     'fadeIn', 
			timeout: 4000, 
			//easing:  'backinout',
			next:   '#twitter'
			//pause:   0,
			//cleartype:  true,
    		//cleartypeNoBg:  true
		});
	});	 
} 


function sendQuoteForm() {
	$(".errBoxCannotSend").hide();
	$(".errBoxMissedData").hide();
	$(".msgBox").hide();
	
	var username = $("#username").val();
	var email = $("#email").val();
	var description = $("#description").val();
	
	if (username=='') 		{ alert('Please enter you name'); return false; }
	if (validateEmail(email) == false) 		{ alert('Please enter valid email address'); return false; }
	if (description=='') 		{ alert('Please enter description'); return false; }
	
	input_box = confirm("Is all information correct?");	
	if (input_box == true)	{
		var params = {};
			$("#frmQuote")
			.find("input[type='checkbox']:checked, input[type='text'], input[type='hidden'], input[type='password'], option[selected], textarea")
			.filter(":enabled")
			.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});
		
		//$("body").addClass("curWait");
		$.post("/send_quote.php", params, function(status){
			if (status == 1) {
				$("div.quoteFrmDiv").hide();
				$("div.msgBox").filter(":hidden").fadeIn("normal");
				
			} else if (status == 2) {
				// missing data
				$("div.errBoxMissedData").filter(":hidden").fadeIn("normal")
			} else {
				// cannot send
				$("div.errBoxCannotSend").filter(":hidden").fadeIn("normal")
			}
		});
		return false;
	}
	else {	 
		return false; 	
	}
}
