// JavaScript Document
var loading = document.createElement('div');
loading.setAttribute('id','loadingDiv');
//alert(loading);
loading.innerHTML = '<strong>Cargando...</strong>';
loading.style.top = '25px';
loading.style.left = '25px';
loading.style.height = '20px';
loading.style.width = '90px';
loading.style.position = 'absolute';
loading.style.background = '#FFFFFF';
document.getElementsByTagName('body').item(0).appendChild(loading);

var mycss = '<style type="text/css"><!--#wrapper,#footer { display:none;}--></style>';
document.write(mycss);
/*mycss = '<style type="text/css"><!--#mainParent {	z-index: -1;}--></style>';
document.write(mycss);*/

var w2dImages=new Array();

preloadimages("img/logo.png","img/w2d-background.jpg","img/buttons.png","img/fx-logo.gif","img/boxbg.gif","img/smallboxbg.png");



window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        }
        , fromJSON: function(s) {
                return s.evalJSON();
        }
});



/*
	FUNCIONES
*/


function preloadimages(){
	for (i=0;i<preloadimages.arguments.length;i++){
		w2dImages[i]=new Image();
		w2dImages[i].src=preloadimages.arguments[i];
		/*document.getElementsByTagName('body').item(0).appendChild(w2dImages[i]);*/
	}
}

function rssWindow(){
	
	var winrss = new Window("rsswin",{className: "alphacube", title: "RSS Google", width:184, height:160, parent: $('content'), resizable:false,minimizable:false,maximizable:false, recenterAuto:true});
	winrss.setContent("rssfeed");
    //win.setDestroyOnClose();
	winrss.show(false);
}

function tecnologia(){
	
	var wintec = new Window("tecwindow",{className: "alphacube", title: "Tecnologias w2d", width:184, height:174, parent: $('content'),resizable:false,minimizable:false, maximizable:false, closable:false, recenterAuto:true, showEffectOptions: {duration:1.5}});
	wintec.setContent("ajaxwin_tecnologias");
	wintec.show(false);
}

function main(){
	var parWindow = document.createElement('div');
	parWindow.setAttribute('id','mainParent');
	
	parWindow.style.top = '85px';
	parWindow.style.left = ((document.documentElement.clientWidth-760)/2+207)+'px';

	parWindow.style.height = '430px';
	parWindow.style.width = '548px';
	parWindow.style.position = 'absolute';

	document.getElementsByTagName('body').item(0).appendChild(parWindow);
	
	var main = new Window("mainw2d",{className: "alphacube", title: "w2d | Ventana principal", width:534, height:370, parent: $('mainParent'), resizable:false,minimizable:true,maximizable:true, closable:false, recenterAuto:true, showEffectOptions: {duration:1.5}});
	main.setContent("ajaxwin_contenedor");
	
	//main.minimize();
	
	//main.setConstraint(true);
	main.show(false);
}

function openrss(rssurl){	
	if(Windows.getWindow("rssnewswindow")){
		Windows.getWindow("rssnewswindow").setURL(rssurl);
		Windows.getWindow("rssnewswindow").setLocation(115, (document.documentElement.clientWidth-760)/2-2);
		Windows.getWindow("rssnewswindow").show(false);
	}else{
	var winrssbig = new Window("rssnewswindow",{className: "alphacube", title: "Google News Colombia RSS)", width:750, height:350, url: rssurl, resizable:true,minimizable:true,maximizable:true, recenterAuto:true});
	winrssbig.setDestroyOnClose();
	Windows.getWindow("rssnewswindow").setLocation(115, (document.documentElement.clientWidth-760)/2-2);
	winrssbig.show(false);
	}
	dhtmlHistory.add("news","none");
}

function openMain(urlfile){
	/*if(document.getElementById("ajaxwin_contenedor")){
		document.getElementById("mainw2d_content").removeChild(document.getElementById("ajaxwin_contenedor"));
	}*/
	//Windows.getWindow("mainw2d").setAjaxContent(urlfile, {method: 'get', parameters: {ajax: 'ok'}});
	if(!urlfile){
		urlfile = 'index-cont.php';
	}else{
		if(Windows.getWindow("rssnewswindow")){
			Windows.close("rssnewswindow");
		}
	}
	
	new Ajax.Request(urlfile, {
		method: 'get', parameters: {ajax: 'ok'},
		onSuccess: function(response) {
			Windows.getWindow("mainw2d").setHTMLContent(response.responseText);
			inicializarMainLinks();
			if( urlfile.match("googlemaps")){
					initializeMap();
			}
		},
		onFailure: function(response) {
			Windows.getWindow("mainw2d").setHTMLContent("<br>Error tratando de abrir la pagina<br><br>w2d le recomienda que verifique su conexion a internet e intente mas tarde");
		}
	});
	

}

function posicionarVentanas(){
	document.getElementById('mainParent').style.left = ((document.documentElement.clientWidth-760)/2+207)+'px';
	//document.getElementById('mainParent').style.height = (document.documentElement.clientHeight-60)+'px';
	Windows.getWindow("rsswin").setLocation(305, (document.documentElement.clientWidth-760)/2+5);
	Windows.getWindow("tecwindow").setLocation(95, (document.documentElement.clientWidth-760)/2+5);
	if(!Windows.getWindow("mainw2d").isMaximized())
		Windows.getWindow("mainw2d").setLocation(10, 0/*(document.documentElement.clientWidth-760)/2+207*/);
}

function inicializarLinks(){
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');
	
	for (var i=0; i<anchors.length; i++){
		var currentanchor = anchors[i];
		
		var relAttribute = String(currentanchor.getAttribute('rel'));
		
		if (currentanchor.getAttribute('href') && (relAttribute.toLowerCase().match('w2dnormal'))){
			//alert(currentanchor.getAttribute('href'));
			currentanchor.onclick = function (evt) {
				var srcElement;
				if(window.event) {
					srcElement = window.event.srcElement;
				}else {
					srcElement = Event.element(evt);
				}
				dhtmlHistory.add(srcElement.getAttribute('href'),srcElement.getAttribute('href'));
				openMain(srcElement.getAttribute('href'));
				return false;
			}
		}else if (currentanchor.getAttribute('href') && (relAttribute.toLowerCase().match('w2dmenu'))){
			currentanchor.onclick = function (evt) {
				var srcElement;
				if(window.event) {
					srcElement = window.event.srcElement;
				}else {
					srcElement = Event.element(evt);
				}
				noCurrent();
				srcElement.parentNode.className="current";
				dhtmlHistory.add(srcElement.getAttribute('href'),srcElement.getAttribute('href'));
				openMain(srcElement.getAttribute('href'));
				return false;
			}
		}
	}
}

function inicializarMainLinks(){
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++){
		var currentanchor = anchors[i];
		
		var relAttribute = String(currentanchor.getAttribute('rel'));
		
		if (currentanchor.getAttribute('href') && (relAttribute.toLowerCase().match('w2dnormal'))){

			if (!currentanchor.onclick){
				currentanchor.onclick = function (evt) {
					var srcElement;
					if(window.event) {
						srcElement = window.event.srcElement;
					}else {
						srcElement = Event.element(evt);
					}
					dhtmlHistory.add(srcElement.getAttribute('href'),srcElement.getAttribute('href'));
					openMain(srcElement.getAttribute('href'));
					return false;
				}
			}
		}
	}
}

function noCurrent(){
	if (!document.getElementsByTagName){ return; }
	var lis = document.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++){
		var currentli = lis[i];
		var classAtt = String(currentli.className);
		if (classAtt.toLowerCase().match('current')){
			currentli.className = "";
		}
	}
}



function historyChange(newLocation, historyData) {
	//alert("A history change has occurred: " + "newLocation="+newLocation+ ", historyData="+historyData);
	if(newLocation == historyData){
		openMain(historyData);
	}else{
		if(!historyData){
			openMain('index-cont.php');
		}
		if(Windows.getWindow("rssnewswindow")){
			Windows.close("rssnewswindow");
		}
	}
}

/*window.dhtmlHistory.create({
	debugMode: true//set this to false, or just don't pass in an options bundle, to see real-world, non-debug conditions 
});*/




Event.observe(window, 'load', function() {
	
	main();
	tecnologia();
	rssWindow();
	inicializarLinks();
	posicionarVentanas();
	document.getElementById('wrapper').style.display = 'block';
	document.getElementById('footer').style.display = 'block';
	ReadRSS('rss_proxy.php','rssBodyTemplate');
	
	document.getElementsByTagName('body').item(0).removeChild(loading); // Remover "Cargando..."
	
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
	
},false);

Event.observe(window, 'resize', function() {
	posicionarVentanas();
},false);

/*
Event.observe(document.getElementsByTagName('body'), 'mousemove', function(){
	Windows.getWindow("mainw2d").minimize();
	alert("good move");
},false);

Event.observe(window, 'mouseover', function(){
	Windows.getWindow("mainw2d").minimize();
	Event.observe(window, 'mouseover', function(){},false);
	//alert("good over");
},false);

/*document.getElementById("content").onmouseover = function(){
	Windows.getWindow("mainw2d").minimize();
	
};
*/



 