$(document).ready(function(){
	/* = menu
	*********************************/
	$('.menu-item a').hover(function(){
		$(this).stop().animate({marginLeft:0,paddingRight:"30px"},100);
	},function(){
		$(this).animate({marginLeft:"10px",paddingRight:"20px"},100);
	});
	
	$('.menu-item a').focus(function(){
		$(this).stop().animate({marginLeft:0,paddingRight:"30px"},100);
	}).blur(function(){
		$(this).animate({marginLeft:"10px",paddingRight:"20px"},100);
	});
	
	$('#banniere').cycle();
	var decale = 0;
	/* = navigation
	*********************************/
	//hash change
	$(window).hashchange( function(){
		
		var arg = window.location.hash.substring(3);
		var link = 'http://www.artplatv.com/'+arg;
		
		if(arg.substr(0,6)=='vimeo/'){
			var videoitem = arg.replace('vimeo/','');
			$.ajax({
				url:"/wp-admin/admin-ajax.php",
				type:'POST',
				data:'action=get_a_video&video=' + videoitem,
				success:function(data){
					if(decale==1){
						$('#bigvid').animate({marginLeft:"-350px"},1000);
						$('#mainmenu').animate({marginLeft:"-560px"},1000);
						$('#share_on').animate({marginLeft:"-525px"},1000);
						$('#fullinfo').animate({marginLeft:"1550px"},1000,function(){$(this).remove();});
						decale = false;
					}
					$('#bigvid').attr('rel',videoitem);
					var obj = $.parseJSON(data);
					document.title = obj.titreseo;
					$('link[rel=canonical]').attr('href','http://www.artplatv.com/#!/'+arg);
					$('#titre').fadeOut('200',function(){
						$(this).empty().text(obj.titre).fadeIn('200');
					});
					$('#description').fadeOut('200',function(){
						$(this).empty().html(obj.contenu).fadeIn('200');
					});
					//change player
					$('#the_vid').fadeOut('200',function(){
						var player = '<iframe src="http://player.vimeo.com/video/'+(obj.idPlayer)+'?api=1&title=false&portrait=false&byline=false&player_id=currentplayer" width="590" height="328" frameborder="0"></iframe>';
						$(this).empty().append($(player)).fadeIn('200');
					});
					_gaq.push(['_trackPageview','/'+arg]);
				}
			});
		}
		if((arg=='a-propos-dartplatv')||(arg=='contacter-artplatv')||(arg=='mentions-legales')){
			//do ajax call
			$.ajax({
				url:"/wp-admin/admin-ajax.php",
				type:'POST',
				data:'action=get_a_page&page=' + arg,
				success:function(data){ 
					if(decale==1){
						$('#fullinfo').animate({marginLeft:"1550px"},1000,function(){$(this).remove();});
						decale = false;
					}
					var obj = $.parseJSON(data);
					var contactform = (arg=='contacter-artplatv') ? '<form action="" id="contactform"><input type="text" name="contactnom" id="contactnom" value="nom" /><input type="email" name="contactemail" id="contactemail" value="adresse@email.com" /><input type="text" name="contactsujet" id="contactsujet" value="sujet du message" /><textarea name="contactmessage" id="contactmessage" cols="30" rows="5"></textarea><button id="contactsubmit">Valider le message</button></form>': '' ;
					var elem = '<div id="fullinfo"><h1>'+obj.titre+'</h1><div id="back">‹ retour</div><div id="textinfo">'+obj.contenu+contactform+'</div></div>';
					//animate
					document.title = obj.titreseo;
					$('link[rel=canonical]').attr('href','http://www.artplatv.com/#!/'+arg);
					$('#bigvid').animate({marginLeft:"-2000px"},1000);
					$('#mainmenu').animate({marginLeft:"-2210px"},1000);
					$('#share_on').animate({marginLeft:"-2210px"},1000);
					$(elem).insertAfter('header').animate({marginLeft:"-550px"},1000,function(){
						$('#the_vid').empty();
					});
					decale = 1;
					_gaq.push(['_trackPageview','/'+arg]);
				}
			});
		}
	});
	if(window.location.hash.substring(3)!=''){
		$(window).trigger( 'hashchange' );
	}
	//bouton retour
	$('#back').live('click',function(){
		var slug = $('#bigvid').attr('rel');
		window.location.hash = '!/vimeo/'+slug;
	});
	
	//boutons next et prev
	$('#prev,#next').live('click',function(){
		var href = $(this).parent().attr('href');
		href=href.replace('http://www.artplatv.com/#!/','');
		//first page ?
		if(href==''){
			var page = 1;
		}else{
			var page = href.replace('page/','');
		}
		$.ajax({
			url:"/wp-admin/admin-ajax.php",
			type:'POST',
			data:'action=get_a_listofvid&page=' + page,
			success:function(data){ 
				var obj = $.parseJSON(data);
				//les boutons
				$('#next,#prev').fadeOut(500,function(){
					$('#next,#prev').parent('a').remove();
					if(obj.prev!='none'){
						$('#allvids').prepend($(obj.prev));
					}
					if(obj.next!='none'){
						$('#allvids').prepend($(obj.next));
					}
				});
				//les miniatures
				var nb = obj.nb;
				$('.littlevid').each(function(index){
					$(this).fadeOut((index*100)+100,function(){
						$(this).remove();
					});
				});
				for (i=1; i<=nb; i++){ 
					$(obj.contenu[i]).appendTo('#allvids');
				}
			}
		});
	});
	
	//focus formulaire
	$('#contactnom,#contactemail,#contactsujet').live('focus',function(){
		if($(this).attr('value') == this.defaultValue) $(this).attr('value', '');
	});
	$('#contactnom').live('blur',function(){
		if($(this).attr('value') == '') $(this).attr('value', 'nom');
	});
	$('#contactemail').live('blur',function(){
		if($(this).attr('value') == '') $(this).attr('value', 'adresse@email.com');
	});
	$('#contactsujet').live('blur',function(){
		if($(this).attr('value') == '') $(this).attr('value', 'sujet du message');
	});
	// sendform
	$('#contactsubmit').live('click',function(e){
		e.preventDefault();
		var value = $('#contactform').serialize();
		// console.log(value);
		$.ajax({
			url:"/wp-admin/admin-ajax.php",
			type:'POST',
			data:'action=sendform&'+value,
			success : function(data){
				var obj = $.parseJSON(data);
				if(obj.send == 1){
					$('#contactform').replaceWith('<div id="messageenvoye">Votre message a été transmis à artplaTV</div>');
				}else{
					var errormessage = '';
					if(obj.erreur.type1 == 'retournnom'){
						var errormessage = errormessage+'Veuillez renseigner votre nom\r\n';
					}
					if(obj.erreur.type2 == 'retourmail'){
						var errormessage = errormessage+'Veuillez renseigner une adresse email correcte\r\n';
					}
					if(obj.erreur.type3 == 'retourmessage'){
						var errormessage = errormessage+'Veuillez renseigner votre message\r\n';
					}
					alert(errormessage);			
				}
			}			
		});			
	});
	$('#share_on').click(function(){
		var sharedlink = encodeURIComponent(window.location);
		window.open('http://www.facebook.com/sharer.php?u='+sharedlink,'Partager sur facebook',config='height=400, width=400');
	});
});

