﻿// Get base url
url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
var historyContentUp;
var historyContentDown;
var ajax_get_error = false;

// pre-load map images
var map=new Array(17)
for (i = 0;i < 17;i++) {
	map[i] = new Image();
	map[i].src = "./images/provincia/"+i+".gif";
}

function ajax_do (url) {
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
		url = base_url + url;
	}

	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;

	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);

	return true;
}

function startSettings () {
	ajax_do ('./footer/footer.php');
	ajax_do ('./menu/menu.php?mid=' + 1);
	//divUpdates(1,0,0);
	ajax_do ('./content/presentacion/nuevoinicio.php');
}

function divUpdates (mid,sid,did) {
	ajax_do ('./footer/footer.php');
	document.getElementById('bgColor').bgColor='#e2ebf0';
	document.getElementById('footerImage').style.backgroundImage="url('./images/footer_1.jpg')";
	if (mid == 1) {  //presentacion
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/presentacion/images.php');
		ajax_do ('./content/presentacion/text.php');
		
	} else if (mid == 2) {
		ajax_do ('./menu/menu.php?mid=' + mid + '&sid=' + sid + '&did=' + did);
		
		if (sid == 1) {
			if(ajax_do ('./content/promociones/group_lister.php?state=' + did)) { //Por Estado = state
			ajax_do ('./content/promociones/flat_lister.php?state=' + did);
			}
			}
		else if (sid == 2) {
			if(ajax_do ('./content/promociones/group_lister.php?flattype=' + did)) { //Por Tipo = flattype
			ajax_do ('./content/promociones/flat_lister.php?flattype=' + did);
			}
			}
		else if (sid == 3) {
			if(ajax_do ('./content/promociones/group_lister.php?promotion=' + did)) { //Por Promocion = promotion
			ajax_do ('./content/promociones/flat_lister.php?promotion=' + did);
			}
			} 
		else if (sid == 4) {
			if(ajax_do ('./content/promociones/group_lister.php?province=' + did)) { //Por Province = province
			ajax_do ('./content/promociones/flat_lister.php?province=' + did)}
			}		
		else {
			if(ajax_do ('./content/promociones/group_lister.php')) { //Todos
			ajax_do ('./content/promociones/flat_lister.php');
			}
		}	
		//div = document.getElementById('content_down');
		//div.innerHTML = "<div style='height: 180px;'>&nbsp;</div>";
		//historyContentUp = document.getElementById('content_up').innerHTML;
			
	} else if (mid == 3) {  //Quienes Somos
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/quienessomos/quienessomos_up.php');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (mid == 4) {  //localizacion
		//document.getElementById('bgColor').bgColor='#003d6c';
		document.getElementById('footerImage').style.backgroundImage="url('./images/footer_2.jpg')";
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/localizacion/localizacion_up.php');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (mid == 5) {  //Calidad
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/calidad/calidad_up.php');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (mid == 6) {  //contacto
		//document.getElementById('bgColor').bgColor='#003d6c';
		document.getElementById('footerImage').style.backgroundImage="url('./images/footer_2.jpg')";
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/contacto/contacto_up.php');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	}else if (mid == 7) {  //inicio
		//document.getElementById('bgColor').bgColor='#003d6c';
		document.getElementById('footerImage').style.backgroundImage="url('./images/footer_2.jpg')";
		ajax_do ('./menu/menu.php?mid=' + mid);
		ajax_do ('./content/presentacion/nuevoinicio.php');
		div = document.getElementById('content_up');
		div.innerHTML = "<div style='height: 100%'></div>";
	}


}
function updateFooter (id) {
	document.getElementById('bgColor').bgColor='#003d6c';
	document.getElementById('footerImage').style.backgroundImage="url('./images/footer_2.jpg')";
	if (id == 1) {  //Quienes Somos
		ajax_do ('./menu/menu.php');
		ajax_do ('./content/quienessomos/up.php');
		ajax_do ('./footer/footer.php?id=1');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (id == 2) {  //Mapa Web
		ajax_do ('./menu/menu.php');
		ajax_do ('./content/mapaweb/up.php');
		ajax_do ('./footer/footer.php?id=2');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (id == 3) {  //localization
		ajax_do ('./menu/menu.php');
		ajax_do ('./content/localizacion/up.php');
		ajax_do ('./footer/footer.php?id=3');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%'></div>";
	} else if (id == 4) {  //contacto
		ajax_do ('./menu/menu.php');
		ajax_do ('./content/contacto/up.php');
		ajax_do ('./footer/footer.php?id=4');
		div = document.getElementById('content_down');
		div.innerHTML = "<div style='height: 100%;'></div>";
	}
}	
function updateFlatLister (province,state,flattype,promotion,sort,sortdir) {
	ajax_do ('./content/promociones/flat_lister.php?province='+province+'&state='+state+'&flattype='+flattype+'&promotion='+promotion+'&sort='+sort+'&sortdir='+sortdir);
	//historyContentDown = document.getElementById('content_down').innerHTML;
	}
function backLister () {
	document.getElementById('content_up').innerHTML = historyContentUp;
	document.getElementById('content_down').innerHTML = historyContentDown;
}
function updateDetailLister (province,state,flattype,promotion,sort,sortdir,flatid) {
	div = document.getElementById('content_down');
	div.innerHTML = "<div style='height: 100%'></div>";
	ajax_do ('./content/promociones/detail_lister.php?province='+province+'&state='+state+'&flattype='+flattype+'&promotion='+promotion+'&sort='+sort+'&sortdir='+sortdir+'&flatid='+flatid);
}
function aldaiPopup (type,url) {
	
	if(type == 'pdf') {
		var popUp = window.open(url,'aldaiPopupWindow','height=600,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,status=no');
		popUp.focus();
	} else if(type == '3d') {
		var popUp = window.open(url,'aldaiPopupWindow','height=600,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,status=no');
		popUp.focus();
	} else if(type == 'hires') {
		var popUp = window.open(url,'aldaiPopupWindow','height=600,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,status=no');
		popUp.focus();
	} else if(type == 'confidental') {
		var popUp = window.open(url,'aldaiPopupWindow','height=600,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,status=no');
		popUp.focus();
	}

}
function changePreview300x200 (amount,value,id) {
	var image_src=document.images.preview_300x200.src;

	if (value < 1) {
		for (var i=1;i<=amount;i++) {
			if (image_src.indexOf(id+"_"+i+".jpg") != -1) {
				
				if (i==1) {
					document.images.preview_300x200.src="./library/preview_300x200/"+id+"_"+amount+".jpg";
					div = document.getElementById('changePreview300x200Text');
					div.innerHTML = "<nobr>"+amount+"/"+amount+"</nobr>";
				} else {
					document.images.preview_300x200.src="./library/preview_300x200/"+id+"_"+(i-1)+".jpg";
					div = document.getElementById('changePreview300x200Text');
					div.innerHTML = "<nobr>"+(i-1)+"/"+amount+"</nobr>";
				}
				break;
			}
		}
	
	} else {
		for (var i=1;i<=amount;i++) {
			if (image_src.indexOf(id+"_"+i+".jpg") != -1) {
				
				if (i==amount) {
					document.images.preview_300x200.src="./library/preview_300x200/"+id+"_1.jpg";
					div = document.getElementById('changePreview300x200Text');
					div.innerHTML =  "<nobr>"+"1/"+amount+"</nobr>";
				} else {
					document.images.preview_300x200.src="./library/preview_300x200/"+id+"_"+(i+1)+".jpg";
					div = document.getElementById('changePreview300x200Text');
					div.innerHTML =  "<nobr>"+(i+1)+"/"+amount+"</nobr>";
				}
				break;
			}
		}
	
	}

}
function changeMap(id,name) {
	document.images.map.src = map[id].src;
	div = document.getElementById('provincename');
	if (id > 0) {
		div.innerHTML = name;
	} else {
			div.innerHTML = "";
	}
	
}
function changeIconText(name) {
	div = document.getElementById('iconText');
	div.innerHTML = name;	
}

