// Funcions Pragma Backoffice
// Isaac Roca - iroca at pragmapublicitat dot cat

jQuery.noConflict();

var MyFCKObject = new MyFCKClass;

var showCarregant = function() {
		jQuery('#contingut').html(carregant); 
		jQuery('#contingut').show();
	}
	

var redirect = function(o) {
	//alert('RESPONSETEXT: '+o);
	arr=o.split("|");
	if (arr[1]==undefined) { 
		alert('En construccin'); 
	} else {
		if (arr[1]!='') { alert(arr[1]); } 
	} 
	if(parseInt(arr[0])) {
		var str=""; for (i=2;i<arr.length;i++) str+=arr[i]+"|";
		jQuery('#contingut').html(str.substr(0,str.length-1));
		ajaxiza();
		jsload(sec);
	}
}


var redirectLogin = function() {
	//alert('RESPONSETEXT: '+o);
	document.location='/'+canal;
}

/* funcions admin */
function editar(ident) {
	sec=$('sec').value;
	showCarregant();
	jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: 'canal='+canal+'&sec='+sec+'&id='+ident, success: redirect });
}
	
function logout() {
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: str+'&accio=logout&canal='+canal, success: redirectLogin });
		return false;
	}
	
function login() {
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: str+'&accio=login&canal='+canal, success: redirectLogin });
		return false;
	}
	

function guardar() {
		MyFCKObject.UpdateEditorFormValue();
		var str = $('formulari').serialize();
		//alert(str);
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: str+'&accio=guarda&canal='+canal, success: redirect });
		return false;
	}

function borra_foto(foto) {
		var str = $('formulari').serialize();
		// alert(str);
		//showCarregant();
		jQuery("#spanid"+foto+" img ").remove();
		jQuery("#codimgval"+foto).get()[0].value='';
		jQuery("#drop_id_"+foto).remove();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: str+'&accio=borra_foto&foto='+foto+'&canal='+canal, success: redirect });
		return false;
	}

function esborrar(mensaje) {
		if (mensaje) if (!confirm(mensaje)) return 0;
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: str+'&accio=esborra&canal='+canal, success: redirect });
	}
	
function valorsLlistaUsuari() {
	return { mostrar: $F('mostrar'), ordenarper: $F('ordenarper'), ordre: $F('ordre') };	
}
	
function llistaUsuaris() {
		var v=valorsLlistaUsuari();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: 'canal='+canal+'&sec=usuaris&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&id=-1', success: redirect });
}

function checkPass() {
		($F('password1').length>3)? jQuery('#password1').css("background-color","#74FF60") : jQuery('#password1').css("background-color","#FF7070");
		(($F('password1').length>3)&&($F('password1')==$F('password2')))? jQuery('#password2').css("background-color","#74FF60") : jQuery('#password2').css("background-color","#FF7070");
}

function editPass() { 
	jQuery('#chpass').html('<p>Nuevo password: <br /><input onkeyup="checkPass();" name="password1" id="password1" style="width:200px;" type="password" value=""  /></p><p>Repeticin: <br /><input onkeyup="checkPass();" name="password2" id="password2" style="width:200px;" type="password" value=""  /></p>');
	checkPass();
}

function editarUsuari(ident) {
		var v=valorsLlistaUsuari();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: 'canal='+canal+'&sec=usuaris&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&id='+ident, success: redirect });
}
	
function noticia(id) {
	showCarregant();
	jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', data: 'canal='+canal+'&sec=noticias&noticia='+id, success: redirect });
	return false;
}
	
function obreMenu(codi) {
	jQuery("#"+codi).each(function () {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).show("fast");
			} else {
				jQuery(this).hide("fast");
			}
	});
}


function preloadImages() {
	var prefix=new Array("aves","bovinos","caprinos","conejos","equinos","ovinos","porcinos");
	var suffix=new Array("on","off");
	var suffix2=new Array("over","out");
	for(i=0;i<prefix.length;i++) for(j=0;j<suffix.length;j++) for(k=0;k<suffix2.length;k++)
		MM_preloadImages('/img/'+prefix[i]+'_'+suffix[j]+'_'+suffix2[k]+'.png');
}
/* carrega secci */
function setupcalendar() {
		Calendar._C = null;
		window._dynarch_popupCalendar = null;
		//if (calendariIncluded) calendari.destroy();
		calendari=Calendar.setup({ 
				inputField:'dh',
				ifFormat:'%d/%m/%Y - %H:%M', 
				button:'lanzador' 
		});
}

var calendariIncluded=false;
function jsload(sec) {
	switch(sec) {
		case 'admNoticias':
			if (!calendariIncluded) {
				csslink('/lib/calendari/calendar-green.css');
				include('/lib/calendari/calendar_stripped.js');
				include('/lib/calendari/lang/calendar-es.js');
				include('/lib/calendari/calendar-setup_stripped.js');
				calendariIncluded=true;
			} else {
				setupcalendar();
			}
			
		break;
		case 'planosituacion':
				//<![CDATA[
				//function load() {
				  	if (GBrowserIsCompatible()) {        
						var lloc = new GClientGeocoder();
						lloc.getLatLng('salou, calle valencia 12',function (punt){
							var map = new GMap2(document.getElementById("map"));  
							map.addControl(new GLargeMapControl());        
							map.addControl(new GMapTypeControl());
							map.setCenter(punt,16);
							var marker = new GMarker(punt);
							map.addOverlay(marker);
							marker.openInfoWindowHtml('Finques <strong>SOLANO SALOU</strong><br>Calle Valencia, 12<br>43840 Salou<br><br>Tel. 977 381 099 Fax. 977 350 052');
							jQuery("a.gmnoprint").attr("target","_blank");     
						});
						var lloc2 = new GClientGeocoder();
						lloc2.getLatLng('reus, gaudi 12',function (punt){
							var map2 = new GMap2(document.getElementById("map2"));  
							map2.addControl(new GLargeMapControl());        
							map2.addControl(new GMapTypeControl());
							map2.setCenter(punt,16);
							var marker2 = new GMarker(punt);
							map2.addOverlay(marker2);
							marker2.openInfoWindowHtml('Finques <strong>SOLANO REUS</strong><br>Calle Gaudí, 12<br>43202 Reus<br><br>Tel. 977 333 449 Fax. 977 320 866');
							jQuery("a.gmnoprint").attr("target","_blank");     
						});  
						var lloc3 = new GClientGeocoder();
						lloc3.getLatLng('tarragona 43001, calle cervantes 23',function (punt){
							var map3 = new GMap2(document.getElementById("map3"));  
							map3.addControl(new GLargeMapControl());        
							map3.addControl(new GMapTypeControl());
							map3.setCenter(punt,16);
							var marker3 = new GMarker(punt);
							map3.addOverlay(marker3);
							marker3.openInfoWindowHtml('Finques <strong>TAYRE</strong><br>Calle Cervantes, 23<br>43001 Tarragona<br><br>Tel. 977 221 105 Fax. 977 229 571');
							jQuery("a.gmnoprint").attr("target","_blank");     
						});  
					}
				//}
			
				//]]>
		break;
	}
}
	
function getAjax(obj) {
	sec=obj.getAttribute("rel");
	showCarregant();
	jQuery.ajax({ type: "POST", url: wwwroot+'ajax.php', dataType:'html', data: 'canal='+canal+'&sec='+sec, success: redirect });
	return false;
}

function ajaxiza() {
	jQuery("a[@rel]").each(function () {
		this.href='#';
		this.onclick=function () { getAjax(this); return false; }
	});
}
/* si els links tenen rel, es converteixen en ajax cridar secci */
function init() {
	jsload(sec);
	ajaxiza();
	preloadImages();
} 

//Funcions Ivn
/*Botonera del Men*/

function estiloOn(which, estilo){
//alert (estilo);
if (document.all||document.getElementById){
which.className= estilo;
}
}

function estiloOff(which, estilo){
if (document.all||document.getElementById){
which.className= estilo;
}
}

function enviarformcsinmo(archivo)
{
	if(archivo == 'contactar' || archivo == 'alquilarpiso' || archivo == 'venderpiso' || archivo == 'contactarreus' || archivo == 'contactartarragona' || archivo == 'contactarsalou')
	{
			if (document.formulario.Nombre.value==''){alert("Falta el Nombre");return false;}
			if (document.formulario.Provincia.value==''){alert("Falta la Provincia");return false;}
	}
	document.formulario.action = "/php/"+archivo+".php";
	document.formulario.submit();

}

function MyFCKClass(){
    this.UpdateEditorFormValue = function(){
	for ( i = 0; i < parent.frames.length; ++i )
		if ( parent.frames[i].FCK )parent.frames[i].FCK.UpdateLinkedField();
	}
}
