var j = jQuery.noConflict();

/* Bubble ToolTip */
function bubbleToolTip() {
	
	j('ul.featured li').hoverIntent(function() {
		
		//adds a identifier class
		j(this).addClass('hovered');
		
		//fades all others
		j('ul.featured li:not(.hovered)').stop().animate({ opacity: .4 }, 250);
		
		//shows the balloon - 1) fixes the css - 2) animates it
		j(this).children('.bubble').css({ display: 'block', opacity: 0, bottom: '60px' }).stop().animate({ bottom: '75px', opacity: 1 }, 200);
		
	}, function() {
		
		//disappear balloon
		j(this).children('.bubble').fadeOut(150);
		
		//removes the identifier class
		j(this).removeClass('hovered');
		
		//makes all other lis solid again
		jQuery('ul.featured li').stop().animate({ opacity: 1 }, 200)
		
	});
	
}

/* Tabs */
function Tabs() {
	
	j("#nav-tabs .tab-content").hide();
	j("#nav-tabs ul.tabs li:first").addClass("active").show();
	j("#nav-tabs .tab-content:first").show();

	j("#nav-tabs ul.tabs li").click(function() {

		j("#nav-tabs ul.tabs li").removeClass("active");
		j(this).addClass("active");
		j("#nav-tabs .tab-content").hide();

		var activeTab = j(this).find("a").attr("href");
		j(activeTab).fadeIn();
		return false;
	});

	j("#post-tabs .tab-content").hide();
	j("#post-tabs ul.tabs li:first").addClass("active").show();
	j("#post-tabs .tab-content:first").show();

	j("#post-tabs ul.tabs li").click(function() {

		j("#post-tabs ul.tabs li").removeClass("active");
		j(this).addClass("active");
		j("#post-tabs .tab-content").hide();

		var activeTab = j(this).find("a").attr("href");
		j(activeTab).fadeIn();
		return false;
	});
	
}

/* Load Functions When DOM is Ready */
j(document).ready(function() {
	
	Tabs();
	bubbleToolTip();
	
	
	/* Contact Form  */
	j('#contactform').submit(function() {

	// Disable the submit button
	j('#contactform input[type=submit]')
		.attr('value', 'Sending message')
		.attr('disabled', 'disabled');

	// AJAX POST request
	j.post(
		j(this).attr('action'),
		{
			name:j('#name').val(),
			email:j('#email').val(),
			message:j('#message').val(),
			c_email:j('#c_email').val()
		},
		function(errors) {
			// No errors
			if (errors == null) {
				j('#contactform')
					.hide()
					.html('<h4>Thank you</h4><p>Your message has been successfuly sent.</p>')
					.show();
			}

			// Errors
			else {
				// Re-enable the submit button
				j('#contactform input[type=submit]')
					.removeAttr('disabled')
					.attr('value', 'Send');

				// Technical server problem, the email could not be sent
				if (errors.server != null) {
					alert(errors.server);
					return false;
				}

				// Empty the errorbox and reset the error alerts
				j('#contactform .errorbox').html('<ul></ul>').show();
				j('#contactform li').removeClass('alert');

				// Loop over the errors, mark the corresponding input fields,
				// and add the error messages to the errorbox.
				for (field in errors) {
					if (errors[field] != null) {
						j('#' + field).parent('li').addClass('alert');
						j('#contactform .errorbox ul').append('<li>' + errors[field] + '</li>');
					}
				}
			}
		},
		'json'
	);

	// Prevent non-AJAX form submission
	return false;
	
	});	
});




/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:30,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


