$(document).ready(function() {			
	if($('#contentCarro').attr('id')=="contentCarro") cargaCarro();				
	
	if($('#tablaDataCarro').attr('id')=="tablaDataCarro"){
		$("#formCotizacion").validationEngine({
			inlineValidation: false,
			success :  false,
			failure : function() { callFailFunction()  }
		});
		cargaCarroTipo();
		$('#enviarForm').click(function(event){
			event.preventDefault();
			if($("#formCotizacion").validationEngine({returnIsValid:true})) {				
				WYSIWYG.display('all'); 
				$("#formCotizacion").submit();
			}
		});
	}
	if($('#formAction').attr('id')=="formAction"){
		$('.setAction').click(function(event){
			event.preventDefault();
			$('#categoria').attr('value', $(this).attr('id').split('_')[1]);  
			$("#formAction").attr('action', 'productos.html');
			$("#formAction").submit();						
		});
	}
	if($('#ListaProductos').attr('id')=="ListaProductos"){	
		$('.addCarro').click(function(event){
			event.preventDefault();
			var producto = $(this).attr('id').split('_')[1];
			var idioma = $('#idioma').attr('value');
			$.ajax({
				type: "POST",
				url: "/lib/add-to-carro.php",
				data: "idioma="+ idioma + "&productoId=" + producto,
				success: function(msg){
					cargaCarro();
				}
			});			
		});
		$('.verMas').click(function(event){
			event.preventDefault();
			$('#productoId').attr('value', $(this).parent().attr('id').split('_')[1]);  
			$("#formAction").attr('action', 'ver-producto.html');
			$("#formAction").submit();	
		});
		$('.verPagina').click(function(event){
			event.preventDefault();
			$('#paginaId').attr('value', $(this).attr('id').split('_')[1]);  
			$("#formAction").attr('action', $(this).attr('href'));
			$("#formAction").submit();	
		});
	}	

	//activate the lightbox
	//jQuery('a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg]').prettyPhoto({theme: "light_square"});

	if($('#slider-content').attr('id')=='slider-content') {	
		jQuery('a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg]').prettyPhoto({theme: "light_square"});
		// here you can see the slide options I used in the demo page. depending on the id of the slider a different setup gets activated
		$('#frontpage-slider').aviaSlider({	blockSize: {height: 80, width:80},
			transition: 'slide',
			display: 'all',
			transitionOrder: ['diagonaltop', 'diagonalbottom','topleft', 'bottomright', 'random']
		});

		$('#diagonal-blocks').aviaSlider({	blockSize: {height: 80, width:80},
			transition: 'slide',
			display: 'diagonaltop',
			switchMovement: true
		});	

		$('#winding-blocks').aviaSlider({	blockSize: {height: 80, width:80},
			transition: 'slide',
			display: 'topleft',
			switchMovement: true
		});								

		$('#randomized-blocks').aviaSlider({	blockSize: {height: 80, width:80},
			transition: 'slide',
			display: 'random'
		});


		$('#fading_curtain').aviaSlider({	blockSize: {height: 'full', width:40},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:150,
			animationSpeed: 600,
			switchMovement: true
		});

		$('#fading-top-curtain').aviaSlider({	
			blockSize: {height: 40, width:'full'},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:150,
			animationSpeed: 600,
			switchMovement: true
		});	
							
		$('#fullwidth-fade-slider').aviaSlider();

		$('#direction-fade-slider').aviaSlider({
			blockSize: {height: 3, width:'full'},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:10,
			animationSpeed: 400,
			switchMovement: true
		});

		$('#droping-curtain').aviaSlider({
			blockSize: {height: 'full', width:40},
			display: 'topleft',
			transition: 'drop',
			betweenBlockDelay:80,
			animationSpeed: 800,
			switchMovement: true,
			slideControlls: 'items',
			appendControlls: '.aviaslider'
		});
	}
});


function cargaCarro(){
	$.ajax({
		type: "POST",
		url: "/lib/carga-carro.php",
		data: "idioma=" + $('#pageLang').attr('value'),
		success: function(data){
			$('#contentCarro').html(data);
			$('#cotizarProductos').click(function(event){
				window.location.href="cotizar-productos.html";
			});
			$('#borrarProductos').click(function(event){
				$.ajax({
					type: "POST",
					url: "/lib/delete-carro.php",
					data: $("#setCarro").serialize(),
					success: function(dataInfo){
						cargaCarro();
						if($('#tablaDataCarro')) cargaCarroTipo();
					}
				});
			});			
		}
	});	
}

function cargaCarroTipo(){
	$.ajax({
		type: "POST",
		url: "/lib/carga-carro.php",
		data: "tipoCarga=tabla&idioma=" + $('#idioma').attr('value'),
		success: function(dataInfo){
			$('#tablaDataCarro').html(dataInfo);
			$('.cantidad').change(function(event){
				var producto = $(this).attr('id').split('_')[1];  
				var cantidad = $(this).attr('value');
				$.ajax({
					type: "POST",
					url: "/lib/add-to-carro.php",
					data: "cantidad="+cantidad+"&productoId=" + producto,
					success: function(msg){
						cargaCarroTipo();
						cargaCarro();
					}
				});			
			});
			$('.deleteItem').click(function(event){
				event.preventDefault();
				if(confirm("¿Esta seguro de eliminar del carro el producto seleccionado?")){
					var producto = $(this).attr('id').split('_')[1];  			
					$.ajax({
						type: "POST",
						url: "/lib/delete-producto-carro.php",
						data: "productoId=" + producto,
						success: function(dataInfo){
							cargaCarro();
							if($('#tablaDataCarro')) cargaCarroTipo();
						}
					});
				}
			});	
		}
	});	
}
function activaUploader(){
	uploadVar = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
		verbose: true,
		url: '../class/accion.php',
		data: 'id_album='+$('idAlbum').get('value'),   //+'&album='+$('nombreEsp').value,
		path: '../fancyUpload/source/Swiff.Uploader.swf',
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
		},
		target: 'demo-browse',
		onLoad: function() {
			$('demo-status').removeClass('hide'); // we show the actual UI
			$('demo-fallback').destroy(); // ... and hide the plain form
			this.target.addEvents({
				click: function() {
					return false;
				},
				mouseenter: function() {
					this.addClass('hover');
				},
				mouseleave: function() {
					this.removeClass('hover');
					this.blur();
				},
				mousedown: function() {
					this.focus();
				}
			});
			$('demo-clear').addEvent('click', function() {
				uploadVar.remove(); // remove all files
				return false;
			});
 
			$('demo-upload').addEvent('click', function() {
				uploadVar.start(); // start upload
				return false;
			});
		},					
		onSelectFail: function(files) {
			files.each(function(file) {
				new Element('li', {
					'class': 'validation-error',
					html: file.validationErrorMessage || file.validationError,
					title: MooTools.lang.get('FancyUpload', 'removeTitle'),
					events: {
						click: function() {
							this.destroy();
						}
					}
				}).inject(this.list, 'bottom');
			}, this);
		},				
		onFileSuccess: function(file, response) {
			var json = new Hash(JSON.decode(response, true) || {});
			if (json.get('status') == '1') {
				file.element.addClass('file-success');
				file.info.set('html', '<strong>Image was uploaded:</strong> ' + json.get('width') + ' x ' + json.get('height') + 'px, <em>' + json.get('mime') + '</em>)');
			} else {
				file.element.addClass('file-failed');
				file.info.set('html', '<strong>An error occured:</strong> ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
			}
			$('cargoFotos').set('value', '0');
		},		 
		onFail: function(error) {
			switch (error) {
				case 'hidden': // works after enabling the movie and clicking refresh
					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
					break;
				case 'blocked': // This no *full* fail, it works after the user clicks the button
					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
					break;
				case 'empty': // Oh oh, wrong path
					alert('A required file was not found, please be patient and we fix this.');
					break;
				case 'flash': // no flash 9+ :(
					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
			}
		}
	});
}

