
/* JQUERY EXTENSIONS
 -------------------------------------------------- */
 
jQuery.each({
	slideFadeIn: { opacity: 'show', height: 'show' },
	slideFadeOut: { opacity: 'hide', height: 'hide' }
}, function( name, props ) {
	jQuery.fn[ name ] = function( speed, easing, callback ) {
		return this.animate( props, speed, easing, callback );
	};
});


/* WRAPPER
 -------------------------------------------------- */
 
$(document).ready(function()
{	
	wizyGlobal();
	wizyNavigation();
	wizyShortcodes();
});


/* GLOBAL SCRIPTS
 -------------------------------------------------- */

function isIE8()
{
	return (navigator.appVersion.indexOf("MSIE 8.")==-1) ? false : true;	
}

function wizyGlobal()
{
	/* tooltips */
	$('.tooltip-n').tipsy({gravity: 'n'});
	$('.tooltip-nw').tipsy({gravity: 'nw'});
	$('.tooltip-ne').tipsy({gravity: 'ne'});
	$('.tooltip-w').tipsy({gravity: 'w'});
	$('.tooltip-e').tipsy({gravity: 'e'});
	$('.tooltip-sw').tipsy({gravity: 'sw'});
	$('.tooltip-s').tipsy({gravity: 's'});
	$('.tooltip-se').tipsy({gravity: 'se'});

	/* sidebar tag cloud */
	$('#sidebar').livequery(function() {
		
		$('.tagcloud a').each(function() {
			$(this).attr('style', 'font-size: auto;');
			$(this).contents().wrap('<span/>')
		});
		
		/* tabbed widgets */
		$("ul.tabs").tabs("div.panes > div");
		
		/* sidebar flickr */
		$('.sidebar_widgets_container li.widget_container .flickr_images_container a:nth-child(3n+3)')
		.css({ marginRight : 0 });
		
	});
	
	// back to top button
	$('.utility_button.top').click(function() {
		$('body,html').animate({scrollTop:0},800);
		return false;
	});
	
	// share this page tooltip
	$('.share_tip').tipsy({ gravity: 'n', html: true, delayOut: 500, trigger: 'manual' });
	$('.share_tip').each(function() {
		var	elem = $(this),
			tipsyTip = elem.tipsy('tip');
		
		elem.hover(function() {
			elem.tipsy('show');	
		}, function() {
			setTimeout( function(){ if ( ! tipsyTip.is(':hover') ) elem.tipsy('hide'); }, 500);
		});
		
		// checks if not button hover or tip hover
		// removes tip if not hover on neither
		setInterval( function() {
			if( ! elem.is(':hover') ) {
				tipsyTip.not(':hover').remove();
			}
		}, 1000 );
	});
	
	// content and sidebar heights
/*
	$('#content').each(function() {
		var content = $(this),
			sidebar = $('#sidebar'),
			contentH = ( content.outerHeight() ),
			sidebarH = sidebar.outerHeight();
			
		if( contentH > sidebarH )
		{
			sidebar.height( contentH - 10 );
		}
		else if( sidebarH > contentH )
		{
			content.height( sidebarH );	
		}
	});
*/

}


// Navigation and sub navigation
function wizyNavigation()
{
	$("nav ul > li").each(function()
	{
		$(this).has('ul').hoverIntent(
		{    
			over: function()
			{
				$('a:first', this).addClass('hover');
				$('ul:first', this).stop(true, true).slideFadeIn(100);	
			},
			interval: 0,
			timeout: 200,    
			out: function()
			{
				$('ul:first', this).stop(true, true).slideFadeOut(100, function()
				{
					var li = $(this).parent();
					$('a:first', li).removeClass('hover');
				});
			}    
		});
	});
}

// gets the highest z index on the page
function maxZIndex()
{
	Math.max.apply(null,$.map($('body *'), function(e,n) {
		if($(e).css('position') == 'absolute' || $(e).css('position') == 'relative')
			return parseInt($(e).css('z-index'))||1 ;
		})
	)	
}


/* SHORTCODES
 -------------------------------------------------- */

function wizyShortcodes()
{
	$('.wp-caption').each(function() {
		$(this).width( $('img', this).width() );
	});
	
	$('a.zoom').each(function() {
		var img = $('img', this);
		
		$(this).colorbox();
		
		$(this).hover(function() {
			img.stop(true, true).animate({opacity: 0.5}, 300);	
		}, function() {
			img.stop(true, true).animate({opacity: 1.0}, 200);	
		});
	});
	 
	/* SLIDER
	-------------------------------------- */
	$('.wizy_slider_inline_container .wizy_slider_nav_inline').each(function() {
		$(this).css({marginLeft: -($(this).innerWidth()/2)});
	});	
	
	/* TABBED CONTENT
	-------------------------------------- */
	$('.wizy_tabbed').each(function() {
		$(this).width($(this).innerWidth() - 30);
		
		$(this).tabs(".pane", { tabs: 'a.tab' });
	});
	
	/* COLLAPSIBL CONTENT
	-------------------------------------- */
	$('.wizy_collapsible').each(function() {
		if($('dt', this).hasClass('expanded')) {
			$('dd', this).show();
		} else {
			$('dd', this).hide();			
		}
		
		$('dt', this).live('click', function() {
			var dt = $(this),
				dl = dt.parent(),
				dd = $('dd', dl);
			
			if(dt.hasClass('expanded')) {
				dd.slideUp(300, function() {
					dt.removeClass('expanded');
				});
			} else {
				dt.addClass('expanded');
				dd.slideDown(300);
			}
			return false;
		});
	});
	
	/* MESSAGES
	-------------------------------------- */
	$('.success_notice, .error_notice, .info_notice, .warning_notice').live('click', function() {
		$(this).slideFadeOut(function(){
			$(this).remove();	
		});	
	});
	
	/* CONTACT FORM
	-------------------------------------- */
	$('#contactform').submit(function() {
		$('.required', this).removeClass('error')
		$('em.error').remove();
		
		var error = false,
			submittedForm = $(this);
			
		submiting = window.setInterval(function(){
			var button = $('#submit').val();
			if (button.length < 10){
				$('#submit').val(button + '.');				
			} else {
				$('#submit').val('Submit ');				
			}
		}, 300);
		
		$('.required', this).each(function() {	
			var fieldName = $(this).attr('name'),
				fieldLabel = $(this).prev().text();
				
			if( $.trim($(this).val()) == '' ) {
				$(this).addClass('error');
				error = true;
			} else if( $(this).hasClass('email') ) {	
				if( !$.trim( $(this).val() ).match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/) ) {
					$(this).addClass('error');
					error = true;
				}
			}
		});
		
		if( error )
		{
			window.clearInterval(submiting);	
		}			
		else if(!error)
		{
			var fieldsVal = $(this).serialize();
							
			$.ajax({
				type: "POST",
				url: ajaxurl,
				data: fieldsVal,
				success: function( result ){
					submittedForm.before(result);
					submittedForm.slideFadeOut(1000, function(){
						$(this).remove();
						window.clearInterval(submiting);	
					});
				}
			});
		}
		
		return false;
	});
}
