//Desplegable

function handleResponse(action, status, msg)
{
    if( status == 'SUCCESS' ){
    	var title = '';
    	if(action == 'RECOMENDACION_AMIGA')
    	{
    		title = 'Enviáselo a una amiga';
    	}
    	else if(action == 'RECOMENDACION_A_MI')
    	{
    		title = 'Enviátelo a tu mail';
    	}
    	else if(action == 'COMENTARIO')
    	{
    		title = 'Dejá tu opinión';
    	}
    	else if(action == 'VALORACION')
    	{
    		title = 'Valoración de productos';
    	}
    	else if(action == 'NEWSSLETTER')
    	{
    		title = 'Suscripción al Newssletter';
    	}
    	
    	$('#popupMsg div p#popMsg_titulo').html(title);
        $('#popupMsg div p#popMsg_txt').html(msg);
        
    	showBubbleCenter('popupMsg');
    	
    }else if( status == 'ERROR' ){
        // status ERROR
        if(action == 'RECOMENDACION_AMIGA')
        {
        	showBubbleCenter('popup3');
        }
        else if(action == 'RECOMENDACION_A_MI')
        {
        	showBubbleCenter('popup2');
        }
        else if(action == 'COMENTARIO')
        {
        	showBubbleCenter('popup');
        }
        else if(action == 'NEWSSLETTER')
        {
        	$('#popupMsg div p#popMsg_titulo').html('Suscripción al Newssletter');
            $('#popupMsg div p#popMsg_txt').html(msg);
        	showBubbleCenter('popupMsg');
        }
        else if(action == 'VALORACION')
        {
        	$('#popupMsg div p#popMsg_titulo').html('Votalo');
            $('#popupMsg div p#popMsg_txt').html(msg);
        	showBubbleCenter('popupMsg');
        }

    }
}

function callServiceJSON(p_name, p_data, fx, async, c)
{
	
    var v_async = false;
    if(async)
    {
        v_async = async;
    }

    if(c == undefined)
    {
        c = 10;
    }
    
    if (c > 0)
    {
        $.ajax(
        {
            type: "POST",
            dataType: "json",
            data: p_data,
            url: p_name,
            async: v_async,
            success: function (json)
            {
                fx(json);
                
            },
            cache: false,
            error: function (request, status, error) 
            {
                //reintentando...
                callServiceJSON (p_name, p_data, fx, async, c - 1);
            }
        });
    }
    else
    {
        // ocultar div plubicar error hoy con alert    
        alert("Error en " + p_name);
    }
}



function cb_suscribirNews(json)
{
	handleResponse('NEWSSLETTER', json.status, json.msg);
}

function handleSearch(event)
{
	if(event=='focus'){
		if($('#input_search').val()=='Buscar')
		{
			$('#input_search').val('');
		}
	}else if(event=='blur'){
		if($('#input_search').val()=='')
		{
			$('#input_search').val('Buscar');
		}
	}else if(event=='execute'){
		if($('#input_search').val()!='' && $('#input_search').val()!='Buscar')
		{
			return true;
		}else{
			return false;
		}
	}
}


function handleNewss(event, url)
{
	if(event=='focus'){
		if($('#suscripto_newsletter_email').val()=='Ingrese su email')
		{
			$('#suscripto_newsletter_email').val('');
			$('#popupMsg').fadeOut('slow');
		}
	}else if(event=='blur'){
		if($('#suscripto_newsletter_email').val()=='')
		{
			$('#suscripto_newsletter_email').val('Ingrese su email');
		}
	}else if(event=='execute'){
		var email = $('#suscripto_newsletter_email').val();
		if(email == 'Ingrese su email' || email == '')
		{
			handleResponse('NEWSSLETTER', 'ERROR', 'Debe ingresar su email');
		}
		else
		{
			callServiceJSON(url, 'suscripto_newsletter_email='+email, cb_suscribirNews);
		}
	}
}


jQuery(document).ready(function($) {
	
   // Muestra y oculta los menús 
   $('ul.menu li:has(ul)').hover( 
      function(e) 
      { 
         $(this).find('ul').css({display: "block"}); 
      }, 
      function(e) 
      { 
         $(this).find('ul').css({display: "none"}); 
      } 
   ); 
	
	$("#cont").click(function(e){
	    $("#newsletter").fadeIn("slow");
	});
	$("#closeCon").click(function(e){
	    $("#newsletter").fadeOut("slow");
	    $('#suscripto_newsletter_email').val("Ingrese su email");
	    $('#popupMsg').fadeOut('slow');
	});
});

//activos

 setActive=function(d){
 	$("."+d).addClass("activo");
 };
