var base_url = 'http://www.top-troc.com'

$(document).ready(function(){
	$('#slideshow').cycle({ 
    	fx:    'fade', 
    	speed:  2000,
    	timeout: 1500  
 	});

	///////////////////////////////////////////////////////////////////////////////////////
	// AJAX FORM
	///////////////////////////////////////////////////////////////////////////////////////
	
	$('#optionRegion').change(function(){
		window.location.href =  $(this).val();
	});
	
	$('#select_recherche_01').change(function(){
		$.post("http://www.top-troc.com/fr/catalogue/get_new_select_categorie",{
	 		id: $('#select_recherche_01').val()
		}, function(data) {
			$('#select_recherche_02').html(data);
		});
	});
	
	$('#recherche_go').click(function(){
		window.location.href = "http://www.top-troc.com/fr/catalogue/meubles/" + $('#select_recherche_01').val() + '/' + $('#select_recherche_02').val();
	});
	
	$('#send_to_friend').ajaxForm(function(){
		$('#content_overlay').fadeOut();	
		$('#send_to_friend_container').fadeOut();
		$('#send_to_friend .clearme').val('');
	});
	
	$('#show_form_send_to_friend').click(function(){
		$('#content_overlay').css({opacity: 0.5});
		$('#content_overlay').fadeIn();
		$('#send_to_friend_container').fadeIn();
		return false;
	});
	
	$('#content_overlay').click(function(){
		$(this).fadeOut();
		$('#send_to_friend_container').fadeOut();
	});
	
	$('#send_to_friend_close').click(function(){
		$('#content_overlay').fadeOut();
		$('#send_to_friend_container').fadeOut();
	});
	
	
	
	//////////////////////////////////////////////////
	// SCROLL
	$('a[href^=#]').click(function(){
		cible=$(this).attr('href');
		if($(cible).length>=1){
			hauteur=$(cible).offset().top;
		}
		else{
			hauteur=$("a[name="+cible.substr(1,cible.length-1)+"]").offset().top;
		}
		$('html,body').animate({scrollTop:hauteur},1000,'easeOutQuint');
		return false;
	});
	
	/////////////////////////////////////////////////
	// UPLOAD INPUT STYLE
	
	var button = $('#upload_button'), interval;
	var count = 0;
	
	new Ajax_upload(button,{
		action: 'vendeur/add_pic',
		name: 'userfile',
		autoSubmit: true,
		onSubmit : function(file, ext){	
			button.text('Uploading');
			this.disable();
			interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {
					button.text('Uploading');				
				}
			}, 200);
		},
		onComplete: function(file, response){
			if (count >= 2) {
				button.text('Merci');
				window.clearInterval(interval);
			}else {
				button.text('parcourir');		
				window.clearInterval(interval);
				this.enable();	
			}	
			count = count + 1;		
			$('<li></li>').appendTo('.files').html(response + ' <input type="hidden" name="file_' + count + '" value="' + response + '" />');
		}
	});
	
	///////////////////////////////////////////////////
	// FORM
	var optionGlobal = { 
	    beforeSubmit:        validate, 
	    success:    function() { 
	        $('#form_vendeur').resetForm();
	        alert ('Merci !');
	        location.reload();
	    } 
	};

	$('form').ajaxForm(optionGlobal);
	
	function validate(formData, jqForm, options) { 
	    var form = jqForm[0]; 
	    if (!form.nom.value || !form.prenom.value || !form.ville.value || !form.email.value || !form.tel.value || !form.body.value ) { 
	        alert("Remplir obligatoirement les champs munis d'une etoile (*)"); 
	        return false; 
	    } 
	   		//alert('Both fields contain values.'); 
	}

	
});

$(window).bind("load", function() { 
    $("#gallery_img").slideView({ 
    	easeFunc: "easeInOutCubic", 
   	 	easeTime: 600 
	}); 
});  
