//SET NOCONFLICT TO WORK WITH OTHER LIBRARIES
jQuery.noConflict();

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){

	
	//Clear input fields	
	jQuery('input, textarea').clearInput();

	//Styles
	jQuery('#freshPosts div.content ul li:last-child').addClass('last');
	jQuery('ul#portfolioList li:last').addClass('last');	
	jQuery('#blog div.blogPost:last').addClass('last');
	jQuery('#comments ol.commentEntry li:even').addClass('even');
	jQuery('#blogSidebar ul li:last-child').addClass('last');
	jQuery('ul#postGallery li:first-child').addClass('first');
	jQuery('#sidebar div.widget ul li:last-child').addClass('last');
	jQuery('ul#homePagePortfolioList li:first-child').addClass('first');
	jQuery('ul#homePagePortfolioList li:last-child').addClass('last');

	//Check if current browser is IE6
	if(typeof document.body.style.maxHeight === "undefined") {
	
		//Current browser IS IE6
	
	} else {
	
		//Current browser ISNOT IE6

		//Fade backgrounds for sidebar links
		jQuery('#nav ul li a, #freshPosts div.content ul li a, #sidebar div.widget ul li a').fadeBackgroundPosition();
		
		//Fade in active nav item
		jQuery('#nav ul li.here a').stop().animate({backgroundPosition: '(0 -138px)'}, {duration:1600});
	
		//Sortable widgets
		jQuery('#sidebar').sortable({
			
			items: 'div.sortable',
			handle: 'h3',
			containment: '#sidebar',
			axis: 'y',
			placeholder: 'helper',
			forcePlaceholderSize: 'true',
			opacity: '0.7',
			zIndex: '99',
			distance: '15',
			
			//onUpdate function to serialize list
			update: function(event, ui) {
						
			}
		
		});	
	
	}


	//Show all portfolio items function
	jQuery('a#showall').toggle(function(){
		
		jQuery(this).html('Hide Portfolio');
		
		jQuery('#sectionNavigationList').slideDown('fast');
		
	}, function(){
		
		jQuery(this).html('Show Portfolio');
		
		jQuery('#sectionNavigationList').slideUp('fast');

	});


	//ChildNav show & hide nav functions
	var childNavConfig = {    
	     sensitivity: 3, //sensitivity threshold (must be 1 or higher)    
	     interval: 200, //milliseconds for onMouseOver polling interval    
	     over: showChildNav,  
	     timeout: 800, //milliseconds delay before onMouseOut    
	     out: hideChildNav 
	};
	
	//Enable childnav expand/collapse
	//jQuery('#nav ul li').hoverIntent(childNavConfig);
	

	//Show loader
	jQuery('#twitter span.loader').fadeIn('slow');

	//Add twitter feed
    jQuery('#twitter').tweet({
		//username: "orangesprocket",
        query: "orangesprocket",
        join_text: "auto",
        avatar_size: 32,
        count: 4,
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: ""
    });	


	//Flickr Feed
	jQuery.ajax({
	
		url: "http://api.flickr.com/services/feeds/photos_public.gne?id=26163786@N03&lang=en-us&format=json&jsoncallback=?",
		cache: false,
		dataType: "json",
		success: function(data){
			
			//Loop through the returned data
			jQuery.each(data.items, function(i,item){
				
				//Stop at the 9th item
				if ( i == 9 ) return false;
				
				jQuery("<img/>").attr("src", item.media.m).attr('width','54px').appendTo("#flickr ul").wrap("<li><a href='" + item.media.m + "' title='" + item.title + "' class='lightview' rel='gallery[flickr]'>","</a></li>");
				
			});			
		
		}
	
	});


	//SET CONTACTOPTIONS
	var contactOptions = { 
		dataType:		'json',
		beforeSubmit:  	showContactRequest, 
		success:       	showContactResponse
	}; 

	//SUBMIT CONTACTFORM
	jQuery('form#contact_form').ajaxForm(contactOptions);


	//SET COMMENTOPTIONS
	var commentOptions = { 
		dataType:		'post',
		beforeSubmit:  	showCommentRequest, 
		success:       	showCommentResponse
	}; 

	//SUBMIT COMMENTFORM
	jQuery('form#comment_form').ajaxForm(commentOptions);


});


//Homepage Showcase Gallery
jQuery.fn.homeShowcase = function(){
	
	//Get text for first image
	var homeShowcaseText = "";
	var homeShowcaseText = jQuery('ul#controls li:first-child a').attr('title');
	
	//Set text for first image
	jQuery('p#description').html(homeShowcaseText);
	
	//Add here class to associated control
	jQuery('ul#controls li:first-child').addClass('here');


	//Loop through array of images
	var showcaseItems = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], i = 0;
	
	//Loop function
	var showcaseLoop = setInterval(function(){
		
		// Get currentId
		var currentId = showcaseItems[++i] || showcaseItems[i=0];
		
		//Remove here class from all controls
		jQuery('ul#controls li').removeClass('here');
		
		//Add here class to associated control
		jQuery('ul#controls li a#' + currentId).parent().addClass('here');
		
		//Get text for associated image
		var homeShowcaseText = "";
		var homeShowcaseText = jQuery('ul#controls li a#' + currentId).attr('title');
										
		//Set text for associated image
		jQuery('p#description').html(homeShowcaseText);
				
		//Fade out previous photo
		jQuery('#homeShowcasePhoto div.showcaseItem').fadeOut('fast');
		
		//Fade in new photo
		jQuery('#homeShowcasePhoto #item_' + currentId).fadeIn('slow');

	
	},5000);	
	
	
	//Showcase controls
	jQuery('ul#controls li a').click(function() {
		
		//Remove here class from all controls
		jQuery('ul#controls li').removeClass('here');
		
		//Add here class to associated control
		jQuery(this).parent().addClass('here');
		
		//Stop loop
		clearInterval(showcaseLoop);
		
		//Get text for associated control
		var homeShowcaseText = "";
		var homeShowcaseText = jQuery(this).attr('title');
		
		//Set text for associated control
		jQuery('p#description').html(homeShowcaseText);
		
		//Set currentId
		var homeShowcasePhoto = "";
		var homeShowcasePhoto = jQuery(this).attr('id');
		
		//Fade out previous photo
		jQuery('#homeShowcasePhoto div.showcaseItem').fadeOut('fast');
		
		//Fade in new photo
		jQuery('#homeShowcasePhoto #item_' + homeShowcasePhoto).fadeIn('slow');
		
	},function(){});
}


//FadeBackgroundPosition function
jQuery.fn.fadeBackgroundPosition = function(){
	
	jQuery(this).css({backgroundPosition: '0 138px'});
	
	//Hover over
	jQuery(this).hover(function() {

		//Check if link is the current page
		if (jQuery(this).parent().hasClass('here') || jQuery(this).parent().hasClass('expanded')) {
		
			//Do nothing
		
		}
		else {		
			
			jQuery(this).stop().animate({backgroundPosition: '(0 -138px)'}, {duration:500});
		
		}
		
	//Hover out
	},function(){

		//Check if link is the current page
		if (jQuery(this).parent().hasClass('here') || jQuery(this).parent().hasClass('expanded')) {
		
			//Do nothing
		
		}
		else {
			
			jQuery(this).stop().animate({backgroundPosition: '(0 138)'}, {duration:200});
		
		}

	});	

}


//Clear input fields function
jQuery.fn.clearInput = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};


//SUMBITCONTACTFORM
function showContactRequest(formData, jqForm, contactOptions) { 

	var form = jqForm[0];
	var emailAddress = form.email.value;
	
	//CHECK FULLNAME
    if (form.name.value == 'What is your name?') {
    	
    	//Set message class to error and focus
    	jQuery('input#name').addClass('error');
    	jQuery('input#name').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#contactFormStatus').addClass('error');
		jQuery('label#contactFormStatus').html('Enter your full name!');
		jQuery('label#contactFormStatus').fadeIn('slow'); 
        
        return false;
        
    } 
	
	//CHECK EMAILADDRESS
    else if (form.email.value == 'Email please :)') {
    	
    	//Remove previous error
    	jQuery('input#name').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('input#email').addClass('error');
    	jQuery('input#email').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#contactFormStatus').addClass('error');
		jQuery('label#contactFormStatus').html('Enter your email address!');
		jQuery('label#contactFormStatus').fadeIn('slow'); 
        
        return false;
        
    }

	//CHECK EMAILADDRESS IS VALID
    else if (emailAddress.search(/^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/) == -1) {
		
		//Remove previous error		    	
    	jQuery('input#name').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('input#email').addClass('error');
    	jQuery('input#email').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#contactFormStatus').addClass('error');
		jQuery('label#contactFormStatus').html('Enter a valid email address!');
		jQuery('label#contactFormStatus').fadeIn('slow'); 
        
        return false;
        
    }
    
    //CHECK MESSAGE
    else if (form.comment.value == 'Message:') {
    	
    	//Remove previous errors
    	jQuery('input#name').removeClass('error');
    	jQuery('input#email').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('textarea#comment').addClass('error');
    	jQuery('textarea#comment').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#contactFormStatus').addClass('error');
		jQuery('label#contactFormStatus').html('Enter your message!');
		jQuery('label#contactFormStatus').fadeIn('slow'); 
        
        return false;
        
    }
	
	
	//SUBMITFORM
	else {
		
		if (form.url.value == 'http://') {
			
			//Set the url to blank if something isn't typed in.
			jQuery('input#url').val('');
		
		}
		
		//Remove error classes
		jQuery('label#contactFormStatus').removeClass('error');
		jQuery('input#name').removeClass('error');
		jQuery('input#email').removeClass('error');
		jQuery('textarea#comment').removeClass('error');
		jQuery('button').attr('disabled', 'disabled'); 
		
		//Change status text and fade in			
		jQuery('label#contactFormStatus').html('Sending Email...');
		jQuery('label#contactFormStatus').fadeIn('slow');	
	
	}

}


//CONTACTFORMRESPONSE
function showContactResponse(data) {
	
	jQuery('label#contactFormStatus').animate({opacity: 1.0}, 1000, function(){
		
		//Show success message	
		jQuery(this).animate({opacity: 1.0}, 1000, function(){
			
			//Remove error classes
			jQuery('input#name').removeClass('error');
			jQuery('input#email').removeClass('error');
			jQuery('textarea#comment').removeClass('error');
			jQuery('label#contactFormStatus').removeClass('error');
			
			jQuery('label#contactFormStatus').addClass('success');
			
			//Fade out form list
			jQuery('#contact_form fieldset.form ol').fadeOut('slow', function(){
				
				jQuery('#message').addClass('success');
				jQuery('#message').fadeIn();
				
				//Reset form to default values
				jQuery('input#name').val('What is your name?');
				jQuery('input#email').val('Email please :)');
				jQuery('textarea#comment').val('Message:');				
				jQuery('button').attr('disabled', '');
				
			});			
			
			jQuery('#message p').html('Email Sent Successfully!, Thanks ' + data.name + ' :)');
			
			//Wait 1 second before fading in form
			jQuery('#contact_form fieldset.form ol').animate({opacity: 1.0}, 4000, function(){
				
				jQuery('label#contactFormStatus').hide();
				jQuery('label#contactFormStatus').removeClass('success');
				
				jQuery('#message').fadeOut('fast', function(){
				
					jQuery('#contact_form fieldset.form ol').fadeIn();
				
				});
			
			});
			
		});

	});

}



//SUMBITCOMMENTFORM
function showCommentRequest(formData, jqForm, commentOptions) { 

	var form = jqForm[0];
	var emailAddress = form.email.value;
		
	//CHECK FULLNAME
    if (form.name.value == 'What is your name?') {
    	
    	//Set message class to error and focus
    	jQuery('input#name').addClass('error');
    	jQuery('input#name').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#commentFormStatus').addClass('error');
		jQuery('label#commentFormStatus').html('Enter your full name!');
		jQuery('label#commentFormStatus').fadeIn('slow'); 
        
        return false;
        
    } 
	
	//CHECK EMAILADDRESS
    else if (form.email.value == 'Email please :)') {
    	
    	//Remove previous error
    	jQuery('input#name').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('input#email').addClass('error');
    	jQuery('input#email').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#commentFormStatus').addClass('error');
		jQuery('label#commentFormStatus').html('Enter your email address!');
		jQuery('label#commentFormStatus').fadeIn('slow'); 
        
        return false;
        
    }

	//CHECK EMAILADDRESS IS VALID
    else if (emailAddress.search(/^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/) == -1) {
    	
    	//Remove previous error
    	jQuery('input#name').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('input#email').addClass('error');
    	jQuery('input#email').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#commentFormStatus').addClass('error');
		jQuery('label#commentFormStatus').html('Enter a valid email address!');
		jQuery('label#commentFormStatus').fadeIn('slow'); 
        
        return false;
        
    }
    
    //CHECK COMMENT
    else if (form.comment.value == 'Message:') {
    	
    	//Remove previous errors
    	jQuery('input#name').removeClass('error');
    	jQuery('input#email').removeClass('error');
    	
    	//Set message class to error and focus
    	jQuery('textarea#comment').addClass('error');
    	jQuery('textarea#comment').focus();
    	
    	//Change status class, text and fade in
    	jQuery('label#commentFormStatus').addClass('error');
		jQuery('label#commentFormStatus').html('Enter your message!');
		jQuery('label#commentFormStatus').fadeIn('slow'); 
        
        return false;

    }
	
	
	//SUBMITFORM
	else {
		
		//Remove error classes
		jQuery('label#commentFormStatus').removeClass('error');
		jQuery('input#name').removeClass('error');
		jQuery('input#email').removeClass('error');
		jQuery('textarea#comment').removeClass('error');
		
		//Change status text and fade in			
		jQuery('label#commentFormStatus').html('Sending Comment...');
		jQuery('label#commentFormStatus').fadeIn('slow');	
	
	}

}


//COMMENTFORMRESPONSE
function showCommentResponse(data) {
	
	//Set variables to use in fake comment post
	var name = jQuery('input#name').val();
	var emailAddress = jQuery('input#email').val();
	var comment = jQuery('textarea#comment').val();	
	
	
	jQuery('label#commentFormStatus').animate({opacity: 1.0}, 1000, function(){
		
		//Remove error classes
		jQuery('input#name').removeClass('error');
		jQuery('input#email').removeClass('error');
		jQuery('textarea#comment').removeClass('error');			
		jQuery('label#commentFormStatus').removeClass('error');
		
		//Add success class
		jQuery('label#commentFormStatus').addClass('success');		
		jQuery('label#commentFormStatus').html('Comment Successfully Added!');
		
		//Wait 1 second before fading out form
		jQuery('label#commentFormStatus').animate({opacity: 1.0}, 1000, function(){
							
			jQuery('label#commentFormStatus').fadeOut('fast');
			
			//Check if commented is the first to post
			if (jQuery('#comments p strong').html() == 'No one has commented on this post yet. Be the first!'){
			
				jQuery('#comments p strong').html('Congratulations, you are the first person to comment on this post!');
			
			}
			
			//Fake add new post to comment list
			jQuery('ol.commentEntry').append("<li><img src='http://orangesprocket.com/resource/images/avatar.gif' alt='+ name +' class='avatar' /><div class='comment'><p>"+ comment +"</p></div><p class='title'><strong><a href=''>"+ name +"</a></strong>'s comment is awaiting moderation.</small></p></li>").hide().fadeIn('slow');
			
			jQuery('#message p').html('Comment Posted Successfully!, Thanks ' + name + ' :)');

			//Fade out form list
			jQuery('#comment_form').fadeOut('slow', function(){
				
				jQuery('#message').addClass('success');
				jQuery('#message').fadeIn();
				
				//Reset form to default values
				jQuery('input#name').val('What is your name?');
				jQuery('input#email').val('Email please :)');
				jQuery('textarea#comment').val('Message:');				
			
			});			
			
			jQuery('label#commentFormStatus').removeClass('success');
			
			jQuery('#comments ol.commentEntry li:even').addClass('even');
			
		});

	});

}


//Show child navigation function
function showChildNav(){
		
	//Check if hovered item contains a second level
	if (jQuery(this).children('ul').length > 0){
		
		//Check if second level is visible before hiding others
		if(jQuery(this).children('ul').is(':visible')){
		
			//Do nothing
		
		}
		else {
			
			jQuery(this).addClass('expanded');
			
			//Slide up all other visible child navs
			jQuery('#nav ul li ul').slideUp('slow');
			
			//Slide down current child nav
			jQuery(this).children('ul').slideDown('slow');
		
		}
		
	}

}


//Hide child navigation function
function hideChildNav(){
	
	//Check if hovered item is currently active
	if(jQuery(this).hasClass('here')){
	
		//Do nothing
	
	}
	else {
			
		//Slide up current child nav
		jQuery(this).children('ul').slideUp('slow', function(){
						
			jQuery(this).siblings('a').animate({backgroundPosition: '(0 138)'}, {duration:400});
			jQuery(this).parent('li').removeClass('expanded');		
		
		});
		
	}
	
}