$(document).ready(function(){
  
	$('.jsImgProducto').error(function() {
		HandleImgError(this);
	});

    
    
	// Llamadas a carro
	$('.borrarItemCarro').live('click', function(){
		var id = $(this).attr('rel');
		
		$.fn.borrarCarro(id);
	});
	
	$('.borrarItemCarroDetalle').live('click', function(){
		var id = $(this).attr('rel');
		
		$.fn.borrarCarroDetalle(id);
	});
	
	$('.agregarCarroAjax').live('click', function(){
		var id ;
		id = $(this).attr('rel');
		
		//alert('id '+id+' tamano '+tamano.length());
		$.fn.agregarCarro(id, 1);
	});
	

	$('.modificar_unidades').live('click', function(){
		var id=$(this).attr('rel');
		
		$('#dialog').html('<div style="padding-top:15px;">Ingrese la cantidad de unidades que desea:&nbsp; <input type="text" id="input_unidades"></div>');
		$('#input_unidades').numeric(null, false).focus();
		$('#dialog').dialog({
		autoOpen: true,
		width: 400,
		title:'Actualizar cantidad',
		modal: true,
		dialogClass: 'alert',
		stack:false,
		resizable:false,
		buttons: {
			"Aceptar": function() { 
				$.fn.agregarCarro(id, $('#input_unidades').val());
				
			}, 
			"Cancelar": function() { 
				$(this).dialog("close"); 
			} 
		}
		
		
		});
		


		
	});			
	$('#pagofacil').live('click', function(){

		$.fn.modal({
		cover:true,
		tipo:'ventana',
		mWidth:'700',
		claseCss:'widget_unidades',
		siCallback: function(){
			$.ajax({
					url: "/?mod=Ventas&ac=checkOut",
					cache: false,
					success: function(html){
						$.fn.modal({cover:true, tipo:'mensaje', contenido:html, claseCss:'modalLuka'});
					}
				
			});
		},
		noCallback: function(){},
		contenido:$('#confirma_checkout').html()
		
		});
		$('#input_billing').focus();
		$('attr[name="opcionEnvio"]').bind('click',function(){
			alert($(this).attr('precio'));
		});
		
		
	});
	
	$('.login_modal').live('click', function(){
			var redirect=$(this).attr('rel');
			$.ajax({
				url: "/?mod=Seguridad&login",
				cache: false,
				success: function(html){
					$('#dialog').html(html);
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 600,
					title:'<img src="/img/Profile.png" border="0" align="absmiddle"> Login: Ingrese sus datos personales.',
					modal: true,
					dialogClass: 'alert',
					stack:false,
					resizable:false,
					buttons: {
						"Acceder": function() { 
							submit();
							
							
						}, 
						"Cancelar": function() { 
							$(this).dialog("close"); 
						} 
					}
					
					
					});
					//$.fn.modal({cover:true, tipo:'mensaje', claseCss:'modalLuka', zIndex:9999, contenido:html, cerrarCallback:function(){window.location.href=redirect;}});
					
					//$('#cover').css({'z-index':9990});
					$("input[name='user']").focus();
					
				}
			
			});
		
		
	});	

	$('.login_refresh').live('click', function(){
			//var redirect=$(this).attr('rel');
			$.ajax({
				url: "/?mod=Seguridad&login",
				cache: false,
				success: function(html){
					$('#dialog').html(html);
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 600,
					title:'<img src="/img/Profile.png" border="0" align="absmiddle"> Login: Ingrese sus datos personales.',
					modal: true,
					dialogClass: 'alert',
					stack:false,
					resizable:false,
					buttons: {
						"Acceder": function() { 
							submitRefresh();
							
							
						}, 
						"Cancelar": function() { 
							$(this).dialog("close"); 
						} 
					}
					
					
					});
					//$.fn.modal({cover:true, tipo:'mensaje', claseCss:'modalLuka', zIndex:9999, contenido:html, cerrarCallback:function(){window.location.href=redirect;}});
					
					//$('#cover').css({'z-index':9990});
					$("input[name='user']").focus();
					
				}
			
			});
		
		
	});
	// # CARRO.JS
	(function($){
		$.fn.actualizarCarro= function(){
			//Carro soporte
			$.ajax({
				url: "/Carro/Actualizar/",
				cache: false,
				success: function(html){
					$('.itemsCarro').animate({opacity:'0.7', duration:'250'})
					$('.itemsCarro').html(html);
					$('.itemsCarro').animate({opacity:'1', duration:'250'})
				}
			
			});
			
			//Carro de detalle
			if( $('#itemsCarroDetalle').html() != null){
				$.ajax({
					url: "/Carro/ActualizarDetalle/",
					cache: false,
					success: function(html){
						$('#itemsCarroDetalle').animate({opacity:'0.7', duration:'250'})
						$('#itemsCarroDetalle').html(html);
						$('#itemsCarroDetalle').animate({opacity:'1', duration:'250'})
					}
				
				});
			
			
			}
		}
		
		,$.fn.borrarCarro= function(id){
			if(id==0){
					$('#dialog').html('<div class="p10"><h1>Confirme que desea eliminar todos los productos de su carro.</h1></div>');
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 400,
					title:'Vaciar Carro de Compras',
					modal: true,
					resizable:false,
					dialogClass: 'alert',
					buttons: {
						"Vaciar": function() { 
							
							$.ajax({
								url: "/Carro/Quitar/"+id+'/',
								cache: false,
								success: function(html){

									$.fn.actualizarCarro();
									$('#dialog').dialog("close");
									 
								}
							});
						}, 
						"Cancelar": function() { 
							$(this).dialog("close"); 
						} 
					}
					
					
					});
					
				/*
				$.fn.confirmar({
					cover:true,
					claseCss:'widget_unidades',
					siCallback: function(){
						$.ajax({
							url: "/Carro/Quitar/"+id+'/',
							cache: false,
							success: function(html){

								$.fn.actualizarCarro();
							}
						});
					},
					noCallback: function(){},
					contenido:'<div class="titulos"><div class="p10"><h1 class="di">Vaciar Carro:</h1></div></div><div style="padding:15px;"><h3>Confirme que desea eliminar todos los productos de su carro.</h3></div>'
				});
				*/
				
			}else{
				$.ajax({
					url: "/Carro/Quitar/"+id+'/',
					cache: false,
					success: function(html){
						
					$('#dialog').html('<div class="p10"><h1>'+html+'</h1></div>');
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 400,
					title:'Eliminar producto del Carro',
					modal: true,
					resizable:false,
					dialogClass: 'alert',
					buttons: {
						"Aceptar": function() { 
							$(this).dialog("close"); 
						} 
					}
					
					});
					
						//$.fn.modal({cover:false, timer:2800, zIndex:'1900', tipo:'mensaje', mHeight:'300', contenido:html, claseCss:'errormsg'});
						$.fn.actualizarCarro();
					}
				});
			
			
			}
		}
		,$.fn.borrarCarroDetalle= function(id){
				$.ajax({
					url: "/Carro/Quitar/"+id+'/',
					cache: false,
					success: function(html){
						
												
						$('#dialog').html('<div class="p10"><h1>'+html+'</h1></div>');
					
							$('#dialog').dialog({
							autoOpen: true,
							width: 400,
							title:'Eliminar producto del Carro',
							modal: true,
							resizable:false,
							dialogClass: 'alert',
							buttons: {
								"Aceptar": function() { 
									$(this).dialog("close"); 
								} 
							}
						});
						//$.fn.modal({cover:false, timer:2800, zIndex:'1900', tipo:'mensaje', mHeight:'300', contenido:html, claseCss:'errormsg'});
						$.fn.actualizarCarro();
					}
				});
		}
		,$.fn.agregarCarro = function (id,unidades){
			if(id < 1){
				
					$('#dialog').html('Ha ocurrido un error');
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 300,
					title:'Error',
					modal: true,
					stack:false,
					dialogClass: 'alert',
					resizable:false
					
					});
				//$.fn.modal({cover:true, timer:2800, zIndex:'1010', tipo:'mensaje', mHeight:'300', contenido:'El producto ya ha sido agregado al carro', claseCss:'errormsg'});
				return;
			}
			var agregarUnidades=unidades;
			if(unidades<1){
				agregarUnidades=1; 
			}
			$.ajax({
				url: "/Carro/Agregar/"+id+'/'+agregarUnidades,
				cache: false,
				success: function(html){
					
					if(unidades>0){
						$.fn.actualizarCarro();
					}
					$('#dialog').html(html);
					
					$('#dialog').dialog({
					autoOpen: true,
					width: 600,
					title:'Producto Agregado',
					modal: true,
					dialogClass: 'alert',
					resizable:false,
					buttons: {}
					
					});
					//$.fn.modal({cover:false, timer:2800, zIndex:'1900', tipo:'mensaje', mHeight:'300', contenido:html, claseCss:'infomsg'});
					//$.fn.modal({cover:true, zIndex:'1900', tipo:'mensaje', mHeight:'300', contenido:html, claseCss:'modalLuka'});
				}
			});
			
			$('#carro_id').val('');
		}
		,$.fn.actualizarFavs=function(){
			$.ajax({
				url: '/Favoritos/Actualizar',
				cache: false,
				success: function(html){
					$('.js_fav_list').html(html);
				}
			});
		}
	}) (jQuery);
	
	$('.j_titulo').live('mouseover',function(){
		$(this).find('.j_subtitulo').show();//animate({opacity:'0.85', duration:'250'});
	}).bind('mouseleave', function(){
		$(this).find('.j_subtitulo').hide();//animate({opacity:'0.00', duration:'250'});
	});

	
	$('#iconoCarro').bind('click',function(){
			$('#carro_de_compras_cont').animate({opacity: 'toggle',height:'toggle'}, 500);
	});
	$('#iconoLogin').bind('click',function(){
			$('.caja_login').animate({opacity: 'toggle',height:'toggle'}, 500,function(){$('#user').focus();});
	});
	
	$('.ayuda').bind('click',function(e){
	
		var ayuda=$(e.target).attr('rel');
		
		$('#dialog').html($('.'+ayuda).html());
		
		$('#dialog').dialog({
		autoOpen: true,
		width: 600,
		title:'<img src="/img/iconos/information.png"> Ayuda',
		modal: true,
		resizable:false,
		dialogClass: 'alert',
		buttons: { "Cerrar": function(){ $(this).dialog('close');}}
		
		});
		//$.fn.modal({cover:true, zIndex:'1999', tipo:'mensaje', contenido:$('.'+ayuda).html(), claseCss:'modalLuka'});
			
	});
	
	$('.ui-state-default').live('mouseover',function(){$(this).addClass('ui-state-hover');});
	$('.ui-state-default').live('mouseout',function(){$(this).removeClass('ui-state-hover');});
});
