/*Javscript pedidos 2.0
By j0n4th4ntub3!
************************************************************
cambio_pestana(li){}
************************************************************
show_sort(a){}
************************************************************
new_pedido(){}
************************************************************
*/




//sleep function (php.js)
function sleep (seconds) {
    var start = new Date().getTime();
    while (new Date() < start + seconds*1000) {}
    return 0;
}
  
//trim function 1.0
function trim(myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}	
		

// cambio pestaņa!, efectos (animate, fadeTo)
function cambio_pestana(li){
var positionin = $(li).position();	
var hrefin = $(li).children().attr('href');

if($(li).is('.seleccionada')){
	
location.href=hrefin;	
	
}
else{
$('li').removeClass('seleccionada');

$(li).fadeTo(190, 0.25, function () {

$(li).animate({ top: 5,}, 100)
								   });



$(li).fadeTo(190, 1.25, function () {
$(li).animate({ top: 0,}, 100)								  
								  

$(li).addClass('seleccionada');



location.href=hrefin;
								  });


}
	
}


function show_sort(a){


if($('#sort_pedidos').css('display') == 'none')
{
$('#sort_pedidos').slideDown('fast');
$(a).parent().addClass('seleccionada');



}
else{
   $('#sort_pedidos').slideUp('fast');
$(li).parent().removeClass('seleccionada');	
	
}



					   

	




}

function new_pedido(){

var campo = $('#pedido_txt').val();
var img_campo = $('#pedido_attach').val();
var title = $('#tit_in').val();

if(trim(title) == ''){
$('#tit_in').focus();
return;
}
else if(trim(campo) == ''){
$('#pedido_txt').focus();

return;
}

$('#proceso_new_p').fadeIn('fast');
$('body').css('cursor','wait');
$('#info_pedido_request').hide();
$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=newpedido',
data: 'campo=' + encodeURIComponent(campo) + '&titulo=' + encodeURIComponent(title) + (trim(img_campo) != '' ?  '&img_attach=' + img_campo : '') + ($('#destacado').is(':checked') ? '&destacado=1' : '&destacado=0'),
success: function(h){

switch(trim(h).charAt(0)){
case '1': //  TODO OK!
$('.tip_ayuda').hide();
$('textarea').val('');
$(':input[type=text]').val('');

$('#proceso_new_p').fadeOut('fast');
$('body').css('cursor','');
$('#info_pedido_request').children().removeClass('statok').removeClass('staterror');
$('#info_pedido_request').children().addClass('statok');
$('#info_pedido_request').children().html(trim(h).substring(3));
$('#info_pedido_request').show('fast');

break;

case '0':
$('.tip_ayuda').hide();
$('#proceso_new_p').fadeOut('fast');
$('body').css('cursor','');
/*En que parte hay problema */
if(trim(h).substring(3).match('campo')){ // error en el campo del pedido!
$('#pedido_txt').focus();
$('#pedido_txt').parent().children().show();
$('#pedido_txt').parent().children().addClass('trouble');
$('#pedido_txt').parent().children().children().children().html(trim(h).substring(3).replace('(campo)',''));
}
else if(trim(h).substring(3).match('titulo')){
$('#tit_in').focus();
$('#tit_in').parent().children().show();
$('#tit_in').parent().children().addClass('trouble');
$('#tit_in').parent().children().children().children().html(trim(h).substring(3).replace('(titulo)',''));	
}
else if(trim(h).substring(3).match('esperar')){
$('#info_pedido_request').children().removeClass('statok').removeClass('staterror');
$('#info_pedido_request').children().addClass('staterror');
$('#info_pedido_request').children().html(trim(h).substring(3));
$('#info_pedido_request').show('fast');
	
	
}
else if(trim(h).substring(3).match('url')){
	
$('#pedido_attach').focus();
$('#pedido_attach').parent().children().show();
$('#pedido_attach').parent().children().addClass('trouble');
$('#pedido_attach').parent().children().children().children().html(trim(h).substring(3));	

}
else if(trim(h).substring(3).match('puntos')){

$('#destacado').parent().children().show();
$('#destacado').parent().children().addClass('trouble');
$('#destacado').parent().children().children().children().html(trim(h).substring(3));

	
	
}

break;
	
	

	
}
}
	   });
	   
	   


	
	
}

var check_id = {

foco: function(patron){



this.show_tip(patron,'Ingresa una url o id', 'tip');
	
},


out_foco: function(patron){
if(trim($(patron).val()) == ''){
this.hide_tip(patron, true);
}
else{
var id_val = $(patron).val();
check_id.show_tip(patron,'Procesando...','wait');
$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=check_id&accion=checkear',
data: 'campo=' + encodeURIComponent(id_val),
success: function(h){

switch(trim(h).charAt(0)){
case '1':
check_id.show_tip(patron,trim(h).substring(3),'correct');
break;

case '0':
check_id.show_tip(patron,trim(h).substring(3),'trouble');
break;

}
}

	   });

	
	
	
}
	
},

show_tip: function(patron,mensaje,css){
$(patron).parent().children('div').removeClass('tip').removeClass('correct').removeClass('trouble').removeClass('wait').removeClass('warning');

$(patron).parent().children('div').show();
$(patron).parent().children('div').addClass(css);
$(patron).parent().children('div').children().children().html(mensaje);	


	
},

hide_tip: function (patron, remove){

$(patron).parent().children('div').hide();

if(remove){
$(patron).parent().children('div').removeClass('tip').removeClass('correct').removeClass('trouble').removeClass('wait');
$(patron).parent().children('div').children().children().html('');	
}
	
},

posteando: function(id){
var val_id = $('#inpt_val').val();

if(trim(val_id ) == ''){
$('#inpt_val').focus();
return;
		}
$('#inpt_val').attr('disabled','disabled');
$('#send_sol').attr('disabled','disabled');
$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=check_id&accion=postear',
data: 'campo=' + encodeURIComponent(val_id) + '&pedido=' + id,
success: function(h){
switch(trim(h).charAt(0)){
case'1':
check_id.hide_tip($('.relative').children('div'));

if($('#no_found')){
$('#no_found').hide().remove();	 
	 
	 }

$('#sugerencias').html('<div id="ajax_action" style="display:none; margin-bottom:-14px;">' + trim(h).substring(3) + '</div>' + $('#sugerencias').html());
$('#ajax_action').show('slow');
$('#ajax_action').removeAttr('id');
check_id.show_tip($('#info_sendp'),'Tu respuesta fue enviada.','correct');
$('#inpt_val').val('');
$('#send_sol').removeAttr('disabled');
$('#inpt_val').removeAttr('disabled');

break;

case '0':
check_id.show_tip($('#info_sendp'),trim(h).substring(3),'trouble');
$('#send_sol').removeAttr('disabled');
$('#inpt_val').removeAttr('disabled');
break;
	
	
}
}

	   });


	
}
	
	
}

function premiar_resp(div,id,idp,hero){
if (!confirm('\xbfEstas seguro?')) 
return false;	

$('.proceso').parent().css('position','relative');
$('.proceso').fadeIn();


$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=best_resp',
data: 'id=' + id + '&id_p=' + idp + '&hero=' + hero,
success: function(h){
switch(trim(h).charAt(0)){
case'1':
$(div).fadeTo(250, 0.90, function () {
$(this).addClass('selected_best');	
});
$('strong img').hide().remove();

$('.proceso').fadeOut();
check_id.show_tip($('.proceso').parent().parent().children().children('.tip_ayuda'),trim(h).substring(3),'correct');

break;

case'0':
$('.proceso').fadeOut();
check_id.show_tip($('.proceso').parent().parent().children().children('.tip_ayuda'),trim(h).substring(3),'trouble');

break;
}
	
}

	   });


	
	
}


function delete_resp(div,id,idp){
if (!confirm('\xbfEstas seguro?')) 
return false;

$('.proceso').parent().css('position','relative');
$('.proceso').fadeIn();
$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=delete_resp',
data: 'id=' + id + '&id_p=' + idp,
success: function(h){
switch(trim(h).charAt(0)){
case'1':
$(div).fadeTo(250, 0.60, function () {
$(this).addClass('deleted');	
});
$('.proceso').fadeOut();
check_id.show_tip($('.proceso').parent().parent().children().children('.tip_ayuda'),trim(h).substring(3),'correct');

break;

case'0':
$('.proceso').fadeOut();
check_id.show_tip($('.proceso').parent().parent().children().children('.tip_ayuda'),trim(h).substring(3),'trouble');

break;
	
	
}
	
}

	   });




	
	
}

fast_response = {
	
almaceno : new Array(),

foco: function(anuncios,id_p){

check_id.show_tip($(anuncios),'Ingresa una url o ID','tip');
	
	
},
	
blur: function(anuncios,id_p,inpt){
	
if(trim($(inpt).val()) == '' )
check_id.hide_tip($(anuncios));	
else{
	
this.chekeo(anuncios,id_p,inpt);	
	
	
	
}
	
	
},

chekeo: function(anuncios,id_p,inpt){
var id_val = $(inpt).val();



check_id.show_tip(anuncios,'Procesando...','wait');
$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=check_id&accion=checkear',
data: 'campo=' + encodeURIComponent(id_val),
success: function(h){

switch(trim(h).charAt(0)){
case '1':
check_id.show_tip(anuncios,trim(h).substring(3),'correct');
break;

case '0':
check_id.show_tip(anuncios,trim(h).substring(3),'trouble');
break;

}
}

	   });
	
	
},

sugiero: function(anuncios,id_p){
var val_id = $('#solucion_' + id_p).val();

if(trim($('#solucion_' + id_p).val()) == ''){
$('#solucion_' + id_p).focus();
return;
}




$('#proceso_new_p_' + id_p).show();
$('#solucion_' + id_p).attr('disabled','disabled');

$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=check_id&accion=postear&in=fast',
data: 'campo=' + encodeURIComponent(val_id) + '&pedido=' + id_p,
success: function(h){
switch(trim(h).charAt(0)){
case '1':
$('#proceso_new_p_' + id_p).hide();
check_id.show_tip(anuncios,trim(h).substring(3),'correct');
$('#solucion_' + id_p).val('');
$('#solucion_' + id_p).removeAttr('disabled');
break;

case '0':
$('#solucion_' + id_p).removeAttr('disabled');
$('#proceso_new_p_' + id_p).hide();
if(trim(h).match('Logueate'))
check_id.show_tip(anuncios,trim(h).substring(3),'warning');
else
check_id.show_tip(anuncios,trim(h).substring(3),'trouble');
break;
}}});}}




var live_search = {
data : new Array(),

help : function(q){
var arrow = $(q);	


check_id.show_tip(arrow, 'necesitas buscar algo?','tip');

arrow.blur(function(){

if(trim($(q).val()) == '')
check_id.hide_tip(arrow,false);
				   });
	
	
},

search : function(q){
// ayuda activada!
var arrow = $(q).parent().children('.tip_ayuda');
var patrones_b = $(q).val();

if(trim($(q).val()) == ''){
check_id.show_tip(arrow,'ingresa algo para la busqueda.','trouble');
return;
}



$('.proceso').fadeIn();
check_id.show_tip(arrow,'mostrando coincidencias . . .','wait');

$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=live_search',
data: 'q=' + encodeURIComponent(patrones_b),
success: function(h){
// on test --> alert(h);
switch(trim(h).charAt(0)){
case '1':
$('.tit_new_p').show();
$('#resultados').html(trim(h).substring(3));
$('.descript_pedido').fadeIn()
$('.proceso').fadeOut();
// cuantos resultados obtuvimos ?
var total_ = $('div.resultados_in').size();
//avisamos al usuario cuantos resultados hubo.
check_id.show_tip(arrow,'mostrando '+total_+' resultado'+ (total_ > 1 ? 's' : '') +'','correct');
break;

case'0':
check_id.show_tip(arrow,trim(h).substring(3),'trouble');
if(trim(h).match('encontro')){

$('div.resultados_in').fadeOut('fast',function(){ 
$('div.resultados_in').remove(); // removemos todos los resultados para mostrar nuevos.
											});
}
$('#resultados').html('');
$('#resultados').append('<div id="no_found" class="respuesta_p deleted" style="display:none;"><span><b></b></span></div>');
$('#no_found span b').html(trim(h).substring(3));
$('#no_found').show(function(){
$('.proceso').fadeOut();

								});
break;

}
}
		
 });
	
	
},



paginas : function(num,total){

var totales = total;
var termino = $('#search_btn').val();
var arrow = $('#search_btn').parent().children('.tip_ayuda');

$('.proceso').fadeIn();

check_id.hide_tip(arrow,true);

$.ajax({
type: 'post',
url: sources_directorio + '/pedidos_requests.php?do=live_search&launch=page',
data: 'q=' + encodeURIComponent(termino) + '&page=' + num,
success: function(h){	
switch(trim(h).charAt(0)){
case '1':
$('.tit_new_p').show();
$('#resultados').html(trim(h).substring(3));
$('.descript_pedido').fadeIn()
$('.proceso').fadeOut();



break;

case'0':
check_id.show_tip(arrow,trim(h).substring(3),'trouble');
if(trim(h).match('encontro')){
$('div.resultados').fadeOut('fast',function(){ 
$('div.resultados').remove(); // removemos todos los resultados para mostrar nuevos.
											});
}
$('.proceso').fadeOut();
break;


	
}
	
}
		
 });
	
	
	
	
}
	
	
	
	
	
	
	
	
	
	
	
	
}

