var n_div = 0;
var consultor_online = 0;
var datos_online = new Array();
var respuestas_online = new Array();

function fn_component(obj){
	return ( obj.indexOf('?',0) < 0 )? '?tmpl=component2' : '&tmpl=component2';
}

function fn_consult1(){
	jQuery.post('/index.php/es/consultores/?tmpl=component3',{ 'process':'mensajes_consultor' },function(r){
		var new_d = jQuery("<div></div>").attr('id','n_div_'+n_div).addClass('respuesta').html('<hr>'+r).appendTo('#chat1');
		link_consultor(jQuery('a'));				
		fn_scroll('n_div_'+n_div);
		n_div++;
	});
}

function fn_scroll(n){
	var obj = jQuery('#'+n);
	var pos = obj.position();
	var topf = pos.top+obj.height();
	jQuery('#chat1').scrollTop(topf);
}

function fn_consultor_online(){
	var texto = jQuery.trim(jQuery('#info').val());
	jQuery('#info').val("");
	if( texto != '' && consultor_online >= 0 ){
		if( consultor_online  == 0 ){
			jQuery.post('/process.php',{ 'process':'mensajes_consultor' },function(r){
						eval(" datos_online = "+r+";");
						if( datos_online.length > 0 ){
							jQuery('#chat1').html("");
							respuestas_online[consultor_online] = texto;
							var new_resp = jQuery("<div></div>").attr('id','n_div_'+n_div).addClass('respuesta').html(texto).appendTo('#chat1');
							var new_div = jQuery("<div></div>").addClass('pregunta').html(datos_online[consultor_online]).appendTo('#chat1');
							consultor_online++;
							fn_scroll('n_div_'+n_div);
							n_div++;
						}
			});
		}
		else{
				var valido = true;
				if(consultor_online==3 && !validarEmail(texto)){
					valido = false;
					alert("¡ Email invalido !");
				}			
				if( valido ){
					respuestas_online[consultor_online] = texto;
					var new_resp = jQuery("<div></div>").addClass('respuesta').html(texto).appendTo('#chat1');
					var new_div = jQuery("<div></div>").addClass('pregunta').html(datos_online[consultor_online]).appendTo('#chat1');
					consultor_online++;
					if( consultor_online == datos_online.length ){
						jQuery.post('/process.php',{ 'process':'contact1','d':respuestas_online },function(r){
							consultor_online= -100;
							fn_consult1();
							//jQuery('#info_consultor').show();							
							//jQuery('#seccion1').show();
						});
					}
				}
		}
	}
}

function fn_dinamic(d){
  var i = 0; 
  var n = 2; /* Numero de divs activos */
	var divs_dinamicos = jQuery('div.'+d);
	var f = divs_dinamicos.length;
  jQuery('div.'+d+':gt(1)').hide();  
  setInterval(function(){
  	divs_dinamicos.slideUp('slow');		
  	for( v = 1; v <= n ; v++)
  		i = animate_divs(i,f,divs_dinamicos[i]);  	
  },5000);
}

function animate_divs(i,f,obj){
	jQuery(obj).slideDown('slow');
  return ( i == (f-1) )? 0 : ++i ;
}

function fn_open_principal(obj){
		var clon = jQuery(obj).clone();
		jQuery(obj).remove();
		jQuery('#sbox-overlay').css({ 'background':'#fff','zIndex': '65555', 'visibility': 'visible', 'opacity': '0.7', 'width': '1349px', 'height': '2459px'	}).show();
		clon.attr('id','clon_principal').css({ 'position':'absolute', 'top': '0' , 'left':'0' ,'zIndex': '66555', 'visibility': 'visible', 'width': '1349px', 'height': '2459px'	}).appendTo('BODY').show();	
		clon.find('a').each(function(){
			jQuery(this).click(function(){
				open_swf(this);
				fn_close_principal();
				return false;
			});
		});
}

function fn_close_principal(){
	jQuery('#clon_principal').remove();
	jQuery('#sbox-overlay').hide();
}

jQuery(function() {
	var width_windows = jQuery(window).width();
	var height_windows = jQuery(window).height();
  fn_dinamic('class_div_dinamico');
	fn_dinamic('class_div_dinamico1');	
	jQuery.post('/process.php',{ 'process':'print_session' },function(r){
		if( jQuery.trim(r) != '' ){
			var dt;
			eval(" dt = "+r+";");
			if( jQuery.trim(dt.pais) != '' ){
				jQuery('.copyright').hide();
				jQuery('#copy_'+jQuery.trim(dt.pais)).show();
			}
			//alert(dt.contenido);
			jQuery('#header_pais').html('<h2>'+dt.contenido+'</h2>');
			if( jQuery('#capa1').length > 0 )
				jQuery('#capa1').html(new_swf(dt.url));	
		}
		else{
			jQuery('div.principal').each(function(){ fn_open_principal(this); });
		}
	});
	
	jQuery('a').each(function(){
		var dir = jQuery(this).attr('href');
		if( jQuery(this).hasClass('modal') ){
			dir+=( jQuery(this).hasClass('sinpanel') )?'?tmpl=component2':'';
			//dir+=( dir.indexOf('?',0) < 0 )? '?tmpl=component' : '&tmpl=component';

			jQuery(this).attr({ 'href':dir, 'rel':"{handler:'iframe', size:{x:"+(width_windows-100)+",y:"+(height_windows-50)+"}}" }).click(function(){
				return false;
			});
		}
		else{
			if( dir && dir.indexOf('javascript:',0) < 0  && dir.indexOf('#',0) < 0 && !jQuery(this).hasClass('sinajax') ){
				/*
				dir+=fn_component(dir);
				jQuery(this).attr({ 'href':dir }).click(function(){
					jQuery('#component').load(jQuery(this).attr('href'));
					return false;
				});
				*/
			}
		}
	});
	
	jQuery('.banderas a , .menu_idioma a').each(function(){
		jQuery(this).click(function(){
			open_swf(this);
			fn_close_principal();
			return false;
		});
	});
	
	link_consultor(jQuery('a.consultor'));
});

function link_consultor(obj){
	obj.each(function(){
		if(!jQuery(this).hasClass('paso')){
			jQuery(this).addClass('paso');
			jQuery(this).attr('title','1').click(function(event){
				event.preventDefault();	
				var url = jQuery(this).attr('href')+'?tmpl=component3';
				jQuery.post(url,{},function(r){
					var new_div = jQuery('<div></div>').attr('id','n_div_'+n_div).html('<hr>'+r).appendTo('#chat1');
					link_consultor(jQuery('a'));
					fn_scroll('n_div_'+n_div);
					n_div++;
				});			
			});
		}
	});
}

function new_swf(n){
	var obj_swf="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'";
	obj_swf+="codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' ";
	obj_swf+="width='800' height='170'>";
	obj_swf+="<param name='quality' value='high' />";
	obj_swf+="<param name='movie' value='"+n+".swf' />";
	obj_swf+="<param value='transparent' name='wmode' />";
	obj_swf+="<embed pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high' wmode='transparent' ";
	obj_swf+="src='"+n+".swf' type='application/x-shockwave-flash'"; 
	obj_swf+="width='800' height='170'>";
	obj_swf+="</embed>";
	obj_swf+="</object>";
	return obj_swf;
}

function open_swf(obj){
	var content=jQuery(obj).html().split('<img');
	var pais = jQuery(obj).attr('href');
	var array_pais = pais.split('/');
	var contenido = jQuery(obj).html();
	//alert( contenido )
	jQuery('#header_pais').html('<h2>'+contenido+'</h2>');
	jQuery('#capa1').html(new_swf(pais));	
	if( jQuery.trim(array_pais[4]) != '' ){
		jQuery('.copyright').hide();
		jQuery('#copy_'+jQuery.trim(array_pais[4])).show();
	}
	var obj_seleccion = new function(){
		this.url = pais;
		this.contenido = contenido;
		this.pais = array_pais[4];
	};	
	jQuery.post('/process.php',{ 'process':'open_session','d':obj_seleccion },function(r){});
}

function active_class(clase,id_active){
	jQuery('.'+clase).hide();
	//jQuery('#'+id_active).slideDown('slow');
	jQuery('#'+id_active).show();
}

function fn_idioma(i){
	location.href='http://www.psigmacorp.com/index.php/'+i.toLowerCase();
}

function fn_contactenos(){
	var valido = true;
	jQuery('.validar').each(function(){
			jQuery(this).removeClass('error');
			if( this.value == ""){
				jQuery(this).addClass('error');
				valido = false;
			}
	});	
	
	if( !valido){
		alert("Los campos senalados(*) son requeridos");
	}
	else{
		jQuery('.email').each(function(){
				jQuery(this).removeClass('error');
				if(!validarEmail(this.value)){
					jQuery(this).addClass('error');
					valido = false;
				}
		});	
		if( !valido){
			alert("¡ Email invalido !");
		}
		else{
			jQuery.post('/process.php',{ 'process':'contact', 'd':jQuery('#datos').serializeArray() },function(r){
				if( r!='')
					alert("¡ Su informacion ha sido enviada correctamente ! ");
					jQuery('#BORRAR').trigger('click');
					location.reload();
			});
		}
	}
}

function validarEmail(valor) {
	valor = jQuery.trim(valor);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true);
	} else {
		return (false);
	}
}
