(function($){ 

    $.fn.extend({
		
		megaMenu: function() {
			
			mainCont = this.parent();
			
			totalWidth = (mainCont.children('.super-menu-child').children('.super-menu-child-column').length) * 195;
			
			mainCont.children('.super-menu-child').children('.super-menu-child-column:last').addClass('last');
			
			mainCont.children('.super-menu-child').css({ width: totalWidth+'px' });
			
			mainCont.hoverIntent(function() {
				
				mainCont.addClass('hovered');
				mainCont.children('.super-menu-child').stop(false, true).slideDown(400);	
				
			}, function() {
				
				mainCont.children('.super-menu-child').stop(false, true).slideUp(400, function() {
					
					mainCont.removeClass('hovered');
					
				});	
				
			});
			
		},
		
		tabbedContent: function() {
			
			//define vars
			var myTabs = this.children('ul:first');
			var myContent = this.children('ul:last');
			
			//let's create a number to be increased
			var i = 1;
			
			//Let's add classes to differ each one of them
			myTabs.children('li').each(function() {
				
				jQuery(this).addClass('tab_'+i);
				
				i++;
				
			});
			
			//let's create a number to be increased
			var x = 1;
			
			//Let's add classes to differ each one of them
			myContent.children('li').each(function() {
				
				jQuery(this).addClass('tab_'+x);
				
				x++;
				
			});
			
			//Let's add the current class to the firsts items
			myTabs.children('li:first').addClass('current');
			myContent.children('li:first').addClass('current');
			
			playingTab = 0;
			
			//Let's change tabs when click a button
			myTabs.children('li').click(function() {
					
			var myTabClass = jQuery(this).attr('class').split(' ');
			
			if(myTabClass[1] != 'current') {
				
				if(playingTab == 0) {
					
					playingTab = 1;
					
					var myTabId = myTabClass[0].split('_');
					
					myTabs.children('li.current').removeClass('current');
					jQuery(this).addClass('current');
					
					myContent.children('li.current').removeClass('current').addClass('playing').animate({ opacity: 0 }, 300, function() {
						
						jQuery(this).hide();
						
						myContent.children('li.tab_'+myTabId[1]).css({ display: 'block', opacity: 0 }).animate({ opacity: 1 }, 400, function() {
							
							jQuery(this).addClass('current');
							playingTab = 0;
							
						});
						
					});
					
				}
				
			}
				
			});
			
		}
		
	});
	
})(jQuery);

function openLogin() {
	
	if(jQuery('#login-register').css('display') == 'none') {
		
		jQuery('#login-register').stop(true, true).slideDown(400);
	
	} else {
		
		jQuery('#login-register').stop(true, true).slideUp(300);
		
	}
	
}

function closeLogin() {
	
	jQuery('#login-register').slideUp(300);
	
}

function dropDown() {
	
	jQuery('#right-menu ul li').children('ul').each(function() {
		
		jQuery(this).children('li:last-child').children('a').css({ border: 'none' });
		
	});
	
	jQuery("#right-menu ul li").hoverIntent(function() {
		jQuery(this).find('ul:first').stop(true, true).slideDown(400) //Slides down when hover the UL
		jQuery(this).addClass("hovered"); //Adds a hovered class, so you can see the menu path you are following
	}, function() {
		jQuery(this).find('ul:first').stop(true, true).slideUp(300); //Slides up on mouseleave
		jQuery(this).removeClass("hovered"); //removes the hovered class.
	});
	
}

function showHiddenSearch() {
	
	jQuery('#hidden-search > img').click(function() {
		
		if(jQuery('#hidden-search > form > p').css('display') == 'none') {
		
			jQuery('#hidden-search > form > p').stop(true, true).fadeIn(300);
			
		} else {
			
			jQuery('#hidden-search > form > p').stop(true, true).fadeOut(300);
			
		}
		
	});
	
}

function portfolioHover() {
	
	jQuery('.portfolio-image img:not(.under)').hover(function() {
		
		jQuery(this).stop().animate({ opacity: 0 }, 400);
		
	}, function() {
		
		jQuery(this).stop().animate({ opacity: 1 }, 200);
		
	});
	
}




function submitFooterForm() {  

	$('#contactFormFooter').submit(function() {

		// Disable the submit button
		$('#contactFormFooter input[type=submit]')
			.attr('value', 'Sending message')
			.attr('disabled', 'disabled');

		// AJAX POST request
		$.post(
			$(this).attr('action'),
			{
				name:$('#footerName').val(),
				email:$('#footerEmail').val(),
				message:$('#footerMessage').val(),
				email_to:$('.footeremail_to').val()
				
			},
			function(errors) {
				// No errors
				if (errors == null) {
					$('#contactFormFooter').hide().html('<h4>Thank you.</h4><p>Your message has been sent.</p>').show();
					Cufon.replace('h1, h2, h3, h4, h5, h6');
					
					$('.alert').removeClass('alert');
				}

				// Errors
				else {
					// Re-enable the submit button
					$('#contactFormFooter input[type=submit]')
						.removeAttr('disabled')
						.attr('value', 'Submit');

					// Technical server problem, the email could not be sent
					if (errors.server != null) {
						$('.alert').removeClass('alert');
						alert(errors.server);
						return false;
					}

					// Empty the errorbox and reset the error alerts
					$('.alert').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) {
							$('#' + field).addClass('alert');
						}
					}
				}
			},
			'json'
		);

		// Prevent non-AJAX form submission
		return false;
	});

}

function replacePageTitle() {
	
	var myPageTitle = jQuery('#page_title_rplc').text();
	
	jQuery('#page_title_rplc').remove();
	
	if(myPageTitle != '') {
	
		jQuery('#header .wrapper').append('<h1 class="page-title">'+myPageTitle+'</h1>');
	
	}
	
}


/**
* 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:100,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);
