$(function(){
	
	// Logo fading on hover
	$('#header #logo a').clone().appendTo('#header #logo').addClass('hover');
	$('#header #logo').unbind().bind('mouseover', function(){
		$(this).find('a:not(.hover)').stop().animate({opacity: 0}, 200);
		$(this).find('a.hover').stop().animate({opacity: 1}, 200);
	}).bind('mouseleave', function(){
		$(this).find('a:not(.hover)').stop().animate({opacity: 1}, 300);
		$(this).find('a.hover').stop().animate({opacity: 0}, 300);
	});
	
	// Form labels
	$('form p input, form p textarea').unbind().bind('focus', function(){
		if($(this).val() == '') $(this).addClass('active');
	}).bind('click', function(){
		if($(this).val() == '') $(this).addClass('active');
	}).bind('blur', function(){
		if($(this).val() == '') $(this).removeClass('active');
	});
	
	// Social links new windows
	$('#social a.twitter,#social a.linkedin').unbind().bind('click', function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	// Homepage banners fading
	setTimeout(function(){
		$('#main.home div:not(.first) p,#main.home div:not(.first) a img.on').animate({opacity: 0}, 1250);
	}, 750);
	$('#main.home div:not(.first) p,#main.home div:not(.first) a').unbind().bind('mouseenter', function(){
		$(this).find('p,img.on').stop().animate({opacity: 1}, 300);
	}).bind('mouseleave', function(){
		$(this).find('p,img.on').stop().animate({opacity: 0}, 500);
	});
	
	// Portfolio page first website screenshot fade on load
	setTimeout(function(){
		$('#main.portfolio #portfolio1 img:not(.background)').fadeIn(2000);
	}, 500);
	
	// Portfolio automatically slide
	var portfolioSlide, portfolioDirection = 'next';
	function portfolioSlideSetup(){
		clearInterval(portfolioSlide);
		portfolioSlide = setInterval(function(){
			if(portfolioDirection == 'next'){
				$('#main.portfolio .arrows .next:not(.disabled)').size() == 1 ? $('#main.portfolio .arrows .next').click() : portfolioDirection = 'prev';
			}else{
				$('#main.portfolio .arrows .prev:not(.disabled)').size() == 1 ? $('#main.portfolio .arrows .prev').click() : portfolioDirection = 'next';
			};
		}, 6000);
	};
	portfolioSlideSetup();
	
	// New pages
	$('a.blank').unbind().bind('click', function(){
		window.open(this.href);
		return false;
	});
	
	// Ken burns effect on ecommerce page	
	if($('#main.ecommerce').size()){
		var directions = new Array(
			'bottom left',
			'bottom right',
			'top left',
			'top right'
		),
		theTime = 8;
		$('#main.ecommerce .transition div').crossSlide({
		  fade: 1
		}, [
		  {
			src:  'images/ecommerce/lizprice.jpg',
			from: 'top left',
			to:   'top right',
			time: theTime
		  }, {
			src:  'images/ecommerce/franchettibond1.jpg',
			from: directions[Math.floor(Math.random()*4)],
			to:   directions[Math.floor(Math.random()*4)] + ' 1.2x',
			time: theTime
		  }, {
			src:  'images/ecommerce/jamiejewellery.jpg',
			from: directions[Math.floor(Math.random()*4)],
			to:   directions[Math.floor(Math.random()*4)] + ' 1.2x',
			time: theTime
		  }, {
			src:  'images/ecommerce/franchettibond2.jpg',
			from: directions[Math.floor(Math.random()*4)],
			to:   directions[Math.floor(Math.random()*4)] + ' 1.2x',
			time: theTime
		  }
		]);
	};
	
	// Case studies page scrolling
	if(window.location.hash.length > 0){
		$('#main.casestudies .links a[href=' + window.location.hash + ']').addClass('current');
	}else{
		$('#main.casestudies .links a:first').addClass('current');
	};
	$('#main.casestudies .links a').unbind().bind('click', function(){
		if($(this).attr('class') == 'current') return false;
		$('#main.casestudies .links a.current').removeClass('current');
		$(this).addClass('current');
		var topPos = (426 * (parseFloat($(this).attr('href').replace('#casestudy', '')) - 1));
		$('#main.casestudies .content').stop().animate({scrollTop: topPos}, 1250, 'easeOutBounce');
		return false;
	});
	
	// Portfolio page slider
	var maxPortfolioSlides = $('#main.portfolio .pages .content').size();
	$('#main.portfolio .pages').width(maxPortfolioSlides * 800);
	$('#main.portfolio .arrows a').unbind().bind('click', function(){
		if($(this).attr('class').indexOf('disabled') != -1) return false;
		var theSlide = parseFloat($(this).attr('href').replace('#portfolio', ''));
		var amount = (theSlide * 800) * -1;
		$('#main.portfolio .pages #portfolio' + (theSlide + 1) + ' img:not(.background)').hide();
		$('#main.portfolio .pages').stop().animate({'marginLeft': amount}, 1000, function(){
			setTimeout(function(){
				$('img:not(.background,.left)').fadeIn(1200);
				$('img.left:not(.background)').fadeIn(1200);
			}, 100);
		});
		if($(this).attr('class').indexOf('prev') != -1){
			$(this).attr('href', '#portfolio' + (theSlide - 1));
			$(this).parent().find('.next').removeClass('disabled').attr('href', '#portfolio' + (theSlide + 1));
			if(theSlide == 0) $(this).addClass('disabled');
		}else{
			$(this).attr('href', '#portfolio' + (theSlide + 1));
			$(this).parent().find('.prev').removeClass('disabled').attr('href', '#portfolio' + (theSlide - 1));
			if(theSlide == (maxPortfolioSlides - 1)) $(this).addClass('disabled');
		};
		portfolioSlideSetup();
		return false;
	});
	
	// Validation
	$('#main.contactus form, #main.sendusyourbrief form').validate({
		rules: {
			email: {
				required: true,
				email: true
			},
			phone_number: {
				required: true,
				minlength: 3
			}
		}
	});
	
	// Scrollbar
	thehtml = $('#main .scrollable').html();
	$('#main .scrollable').html('<div class="scrollable-content">' + thehtml + '</div>');
	$('#main .scrollable').css({'overflow': 'hidden', 'width': '-=10px'}).prepend('<a href="#" class="arrowUp"></a><a href="#" class="arrowDown"></a>');
	$('#main .scrollable .arrowUp').unbind().bind('click', function(){
		what = $(this).parent().find('.scrollable-content');
		what.stop().animate({scrollTop: what.scrollTop() - 28}, 200);
		return false;
	});
	$('#main .scrollable .arrowDown').unbind().bind('click', function(){
		what = $(this).parent().find('.scrollable-content');
		what.stop().animate({scrollTop: what.scrollTop() + 28}, 200);
		return false;
	});

	// Send us your brief
	if($('#main.sendusyourbrief').size()){
		var btnUpload = $('#main.sendusyourbrief #file_upload_documentation');  
		var btnLabel = $('#main.sendusyourbrief .docs label');  
		var status = '';  
		var fileCount = 0;
		var results = $('#main.sendusyourbrief .docsResults .scrollable-content');
		new AjaxUpload(btnUpload, {  
			action: 'uploadbriefdocs.php',  
			name: 'upload_documentation',  
			onSubmit: function(file, ext){  
				if (!(ext && /^(jpg|jpeg|gif|png|psd|pdf|ai|eps|svf|doc|docx|txt|rtf|xls|xlsx)$/.test(ext))){  
					alert('Extension "' + ext + '" not permitted.'); 
					return false;  
				};
				classFile = file.split('.')[0].replace(/ /g, '_');
				fileCount = fileCount + 1;
				if(fileCount > 4) $('.arrowDown').removeClass('inactive');
				results.prepend('<span class="file_' + classFile + '">Uploading ' + file + '...</span>');
			},  
			onComplete: function(file, response){ 
				classFile = file.split('.')[0].replace(/ /g, '_');
				console.log(response);
				if(response.indexOf('Error: ') == -1){
					results.find('span.file_' + classFile + ':first').addClass('success').html(file + ' Uploaded');
					results.append('<input type="hidden" name="upload_documentation" value="' + file + '" />');
				}else{  
					results.find('span.file_' + classFile + ':first').addClass('fail').html(response.replace('Error: ', ''));
				}  
			}
		}, btnLabel);
	};
	
	// Twitter on homepage
	//if(document.location.pathname == '/') $.getScript('http://twitter.com/statuses/user_timeline/ansellandclarke.json?callback=twitterCallBackAC&count=1');
	
});

// Twitter on homepage callback
function twitterCallBackAC(tweet) {
	for (var i=0; i<tweet.length; i++){
		var status = tweet[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g);
	};
	$('#tweet p').append(status);									
};
