/**
 * @author imac
 * Document Jquery
 */

$(document).ready(function(){
	function showChoices(){
		
		//alert($(this).attr('type'));
/*
		
		if ($(this).parent().find('.show-color-name span').hasClass(textChoice)) {
			$('.show-color-name span.' + textChoice).remove();
		}
		
		else {
			$('.show-color-name').append('<span class="' + textChoice + '">' + textChoice + '</span>')
		}
*/
		if($(this).attr('type') == 'checkbox'){
			textChoice = $(this).attr('name');
			if ($(this).parent().parent().parent().find('.show-color-name span').hasClass(textChoice)) {
				$(this).parent().parent().parent().find('.show-color-name span.' + textChoice).remove();
			}
			
			else {
				$(this).parent().parent().parent().find('.show-color-name').append('<span class="' + textChoice + '">' + textChoice + '</span>')
			}
		}

		else{
			textChoice = $(this).parent().find('.color-name').text();
			$(this).parent().parent().parent().find('.show-color-name').empty().append('<span class="' + textChoice + '">' + textChoice + '</span>')
		}		


	};
	
	$('.couleurs input[type=checkbox]').click(showChoices);

	$('.couleurs input[type=radio]').click(showChoices);
	
	
	function emptySearchForm(){
		valueSearch = $(this).attr('value');
		
		if(valueSearch == 'search'){
			$(this).attr('value','');
		}
	}
	
	$('#header form input[type=text]').focus(emptySearchForm);
	
	
	function fillSearchForm(){
		valueSearch = $(this).attr('value');
		
		if(valueSearch == ''){
			$(this).attr('value','search');
		}
	}
	
	$('#header form input[type=text]').blur(fillSearchForm);
	
	
	function copyInfoInscription(){
		
		if($(this).attr('checked') == false){
			$('#inscription .livraison input[type=text]').each(function(){
				$(this).attr('value','')
			})
	
			$('#inscription .livraison option.none').attr("selected", true)
		}
		
		else{
			$('#inscription .facturation input[type=text]').each(function(){
				inputId = $(this).attr('id');
				
				inputVal = $(this).attr('value');
				
				$('#inscription .livraison').find('input[type=text][id='+inputId+'-delivery]').attr('value',inputVal);
			})
	
			$('#inscription .facturation select option:selected').each(function(){
				inputClass = $(this).attr('class');
				
				$('#inscription .livraison option[class='+inputClass+']').attr("selected", true)
				
			})
		}
	}
	
	function clearInfoInscription(){
		
		$('#inscription .livraison input[type=text]').each(function(){
			$(this).attr('value','')
		})

		$('#inscription .livraison option.none').attr("selected", true)
	}
	
	$('#inscription input[type=checkbox].same-before').click(copyInfoInscription);
	
	$('#inscription input[type=checkbox].same-before:checked').click(clearInfoInscription);
	
	function deleteEmptyCase(){
		$('.module-checkout .relais .case').each(function(){
			if($(this).text() == ''){
				$(this).remove()
			}
		})	
	}
	
	deleteEmptyCase()
	
	function checkValueTaille(){
		$('#erreur-taille').animate({
			opacity:'0'
		},250, function(){
			$(this).css({
				visibility:'hidden'
			})	
		});
	}
	
	function transfertValueCouleur(){
		
		valueRadio = $(this).attr('value');
		
		$('#id_couleur').attr('value',valueRadio);
		
		$('#erreur-couleur').animate({
			opacity:'0'
		},250, function(){
			$(this).css({
				visibility:'hidden'
			})	
		});
		
		//alert($('#id_couleur').attr('value'));
	}

	$('#module-produit .couleurs input[type=radio]').click(transfertValueCouleur);
	
	$('#sku').change(checkValueTaille);

	function checkFormProduit(){
		
		error=false;
		
		if($('#id_couleur').attr('value') == ''){
			$('#erreur-couleur').css({
				visibility:'visible'
			}).animate({
				opacity:'1'
			},250)
			error=true;
		};
		
		
	
		if($('#sku option:selected').attr('value') == '' || $('#sku option:selected').attr('value') == false){
			$('#erreur-taille').css({
				visibility:'visible'
			}).animate({
				opacity:'1'
			},250)
			error=true;
		}
		
		if(error==true){
			return false;
		}
		else{
			$('#send-to-panier').submit();
		}

	}
	
	function showLogForm(){
		$('#connect').css({
				display:'block',
				visibility:'visible'
			}).animate({opacity:'1'},250)
	}
	
	function hideLogForm(){
		$('#connect').css({
				display:'none',
				visibility:'visible'
			}).animate({opacity:'0'},250)
	}
	
	$('#ajouter-panier').click(checkFormProduit);
	
	$('#btnlog').click(showLogForm);
	$('#btncancel').click(hideLogForm);
	
	
	function popupInscription(){
		
		$('.bg-popup-inscription .exit-left').css({
			width: '50%'
		})
		
		$('.bg-popup-inscription .exit-right').css({
			width: '50%'
		})
		
		$('.bg-popup-inscription').fadeIn(250);
		
		
	}
	
	$('.message-get-logged').click(popupInscription);
	
	function closePopupInscription(){
		$('.bg-popup-inscription').fadeOut(250);
	}
	
	$('.bg-popup-inscription .exit-left').click(closePopupInscription);
	$('.bg-popup-inscription .exit-right').click(closePopupInscription);
	
});

function changeVideo(value){
	element=document.getElementById('playervideo');
	newSrc	=	"http://player.vimeo.com/video/"+value+"?title=0&byline=0&portrait=0&color=ffffff&autoplay=1";
	element.src=newSrc;
}

