var xhr;

$(document).ready(function(){
	$('.template-studio h2, .template-names h2, .template-clients h2').each(function(){
		var ct=$(this).html();
		$(this).html('<span>'+ct+'</span>');
	});
	$('input:text,textarea').addClass('text');
	$('input:submit').addClass('submit');
	$('.template-index .canvas:nth-child(2),.template-archives .canvas:nth-child(2)').addClass('second');
	$('.template-index #content,.template-archives #content').masonry();
	//$('.template-index #content .canvas,.template-archives #content .canvas').mouseover(function(){
		//$('img',this).stop(true,false).animate({'opacity':0.9});
	//}).mouseout(function(){
		//$('img',this).stop(true,false).animate({'opacity':1});
	//});
	$('#content .image').click(function(){
		 $('body').animate({scrollTop:0});
	});
	$('.bibliotheque').click(function(){
		openBibliotheque($(this).attr('href'));
		return false;
	});
	$('.blank,a[href^="http:"]:not([href*="'+window.location.host+'"])').each(function(){
		$(this).attr('target','_blank');
	});
	$('#flash').each(function(){
		var str=$('#flash').html();
		$(this).html(str+' /// '+str+' /// '+str+' /// '+str+' /// '+str.charAt(0))
		.mouseenter(function(){
			clearInterval(fi);
		}).mouseleave(function(){
			fi=setInterval(Flash,500);
		});
		var fi=setInterval(Flash,500);
	});
	$('input.focus').each(function(){
		if($(this).val()=='') {
			$(this).val($(this).attr('title')).addClass('empty');
		}
	}).focus(function(){
		if($(this).val()==$(this).attr('title')) {
			$(this).val('').removeClass('empty');
		}
	}).focusout(function(){
		if($(this).val()=='') {
			$(this).val($(this).attr('title')).addClass('empty');
		}
	});
	$('#mobile #regular').click(function(){
		$('#mobile').fadeOut(function(){$('#mobile').remove();});
		return false;
	});
	//Subscribe form
	$('#subForm').submit(function(e){
		e.preventDefault();
		$.getJSON(
		this.action+'?callback=?',
		$(this).serialize(),
		function (data) {
			if(data.Status===400){
				alert('Error...');
			}else{
				alert('Thanks for joining us!');
			}
		});
	});
}).keydown(function(e){
	if(e.keyCode==27){
		$('#bibliotheque-close').trigger('click');
		return false;
	}
});

$(window).scroll(function(){
	var st=$(window).scrollTop();
	$('.canvas h2,.canvas .note,.fade').each(function(){
		var t=$(this);
		var o=$(t).offset().top-st;
		if(o<200){
			$(t).css('opacity',(o-100)/180);
		}else{
			$(t).css('opacity',1);
		}
	});
});

function Flash(){
	var str=$('#flash').html();
	if (str.charAt(1)=='\u0020'){
		$('#flash').html(str.substring(1,str.length)+str.charAt(1));
		str=$('#flash').html();
	}
	$('#flash').html(str.substring(1,str.length)+str.charAt(1));
}

function closeBibliotheque(){
	if(xhr){
		xhr.abort();
	}
	$('#bibliotheque-data').html('');
	$('#bibliotheque,#bibliotheque-background').hide();
}

function initBibliotheque(){
	$('#bibliotheque,#bibliotheque-close,#bibliotheque-background').unbind().click(function(){
		closeBibliotheque();
	});
	$('#bibliotheque-data').each(function(){
	});
	$('#bibliotheque').jScrollPane({autoReinitialise:true,showArrows:true});
}

function openBibliotheque(href){
	if(xhr){
		xhr.abort();
	}
	$('#bibliotheque,#bibliotheque-background').show();
	xhr=$.ajax({
		type:'POST',
		url:href,
		error:function(msg){
			initBibliotheque();
		},
		complete:function(msg){
			initBibliotheque();
		},
		success:function(msg){	
			$('#bibliotheque-data').hide().html(msg).fadeIn();
		}
	});
}
