$(document).ready(function(){

	$('body').addClass('has-js');
	
	// HOMEPAGES FEATURES
	/*$('#features a').click(function(e){
		e.preventDefault();
		$('#features-list li').stop();
		var $active = $('#features-list li:first-child');
		$active.animate({'left':'-940'}, 700, function(){
			$('#features-list li:first-child').remove().appendTo('#features-list');
			$(this).css('left',940);
		});
		$active.next().show().animate({'left':'0'}, 700);
	});*/
	
	pageName = $('body').attr('id');
	
	if(pageName == 'home'){
		$('#home #features-list').cycle({
			fx: 'scrollLeft',
			speed: 500,
			timeout: 0,
			next: '#features a.paginate'
		});
	}
	
	// HOMEPAGE WORK IMAGES
	if(pageName == 'home'){
		$('#home #screenshot-list').after('<ol class="slide-list"></ol>').cycle({
			fx: 'fade',
			speed: 500,
			timeout: 5000,
			pager: '#screenshot-list + ol',
			pagerAnchorBuilder: function(idx, img){
				return '<li><a href="#">' + (+idx+1) + '</a></li>';
			}
		});
	}
	
	// HOMEPAGE CLIENTS
	var size = $('#our-clients li').length;
	var logoW = 0;
	var firstLogo = 0;
	
	$('#our-clients ul li:lt(6)').each(function(){
		if(logoW == 0) firstLogo = $(this).outerWidth(true);
		logoW += $(this).outerWidth(true);
	});
	$('#our-clients').width(logoW);
	
	function slideFade(){
		$('#our-clients ul').animate({'left': -firstLogo},500,function(){
			$('> li:first', this).remove().appendTo(this);
			$(this).css('left', 0);
		});
		logoW = 0;
		$('#our-clients ul li:lt(7):gt(0)').each(function(i){
			if(logoW == 0) firstLogo = $(this).outerWidth(true);
			logoW += $(this).outerWidth(true);
			logoW += 2;
		});
		$('#our-clients ul').parent().animate({'width':logoW},250);
	}
	
	if(size > 6) {
		interval = setInterval(slideFade, 5000);
	}
	
	// LIGHTBOXES
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    return '<div class="lightbox-description"><h4>' + title + '</h4><p>' + alt + '</p></div>';
	}
	
	$('.image-list a, #work-list a.thumb').fancybox({
		cyclic: false,
		titleFormat: formatTitle
	});
	
	// BLOG OPTIONS
	$('#options-btn').click(function(e){
		e.preventDefault();
		$('#options').slideDown(300);
	});
	
	$('#options #close-btn').click(function(e){
		e.preventDefault();
		$('#options').slideUp(200);
	});
	
	// BLOG END BULLET
	/*$('#blog .article > *:last-child, #case-study #content > *:last-child').each(function(){
		element = $(this).get(0).tagName;
		if(element == "UL" || element == "ul" || element == "OL" || element == "ol"){
			$(this).children('li:last-child').append('<img class="bullet" src="/assets/images/bg-post.png" />');
		} else {
			$(this).append('<img class="bullet" src="/assets/images/bg-post.png" />');
		}
	});*/
	
	// BLOG COMMENT FORM
	$("#blog form").validate({
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted above.'
					: 'You missed ' + errors + ' fields.  They have been highlighted above.';
				$('em.error-msg').remove();
				$(this).append('<em class="error-msg"></em>');
				$("em.error-msg").hide().html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		}
	});
	
	// CONTACT FORM
	/*$.validator.addMethod("eeEmail",function (value) {
		return /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix.test(value);
	}, 'Please enter a valid email address');*/
	
	$('#contact #content').append('<strong id="busy" style="display: none;">Your message is sending...</strong>');
	$('#contact #content #busy').remove();
	
	var returnVal = $("#contact-form input[name='return']").val();
	$("#contact-form input[name='return']").val(returnVal + '/ajax');
	
	$("#contact-form").validate({
		rules: {
			email: {email: true}
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted above.'
					: 'You missed ' + errors + ' fields.  They have been highlighted above.';
				$('em.error-msg').remove();
				$(this).append('<em class="error-msg"></em>');
				$("em.error-msg").hide().html(message);
				$("em.error-msg").fadeIn('fast').css('display','block');
			} else {
				$("em.error-msg").hide();
			}
		}, submitHandler: function(form) {
			$('#content').append('<strong id="busy">Your message is sending...</strong>');
		
		   	$(form).ajaxSubmit({
				target: '#response span', 
				success: function() {
					$('#contact-form input, #contact-form textarea').val('');
					$('#busy').fadeOut();
					$.scrollTo('#response', 500);
				}
			});
		}
	});
	
	// ICON SWITCHING
	var currentTime = new Date();
	var hour = currentTime.getHours();
	if(hour >= 17) {
		$('.icon.day').remove();
	} else {
		$('.icon.night').remove();
	}
});
