var active_button = '';

var message_propertyIsNotSelected = '';

var buttons = new Array('find', 'state', 'refresh',
        'zoomin', 'pan', 'zoomout', 'info',
        'help','measure', 'back', 'forward');

function pan(direction) {
    domouseclick('pan');
    mapViewer.pan(direction);
}

function showButtons() {
    for (var i = 0; i < buttons.length; i++) {
        document.getElementById(buttons[i]).style.display = 'inline';
    }
}

function domouseclick(button, param1) {

    if (disabled(button)) return;

    var last_active_button = active_button;
    active_button = '';
    for (var i = 0; i < buttons.length; i++)
        domouseout(buttons[i]);

    if (button == 'zoomfactor') {
        FUNCTION_POPUP(400, 200, "", ZOOM_FACTOR_ACTION +'?zoomsize=' + mapViewer.getZoomSize() + '&pansize=' + mapViewer.getPanSize());
        active(last_active_button);
    } else if (button == 'find') {
        FUNCTION_POPUP(590, 220, "", LOCALIZATOR_ACTION);
        domouseclick(last_active_button);
    } else if (button == 'state') {
        mapViewer.init();
        domouseclick(last_active_button);
    } else if (button == 'back') {
        mapViewer.back();
        domouseclick(last_active_button);
    } else if (button == 'forward') {
        mapViewer.forward();
        domouseclick(last_active_button);
    } else if (button == 'refresh') {
        mapViewer.refresh();
        domouseclick(last_active_button);
    } else if (button == 'clearSelect') {
        mapViewer.clearQuery();
        domouseclick('pan');
    } else if (button == 'printicon') {
        //Does not work. The window is printed before it is loaded.    
        //var out = window.open(document.main.src,"print");
        //out.print();        

        //Does not work in IE. In Firefox it does work.
        //var out=window.open('about:blank',"print");        
        //out.document.body.appendChild(document.getElementById('main').cloneNode(true));
        //var printScript=out.document.createElement("script");
        //printScript.text='window.print();';
        //out.document.body.appendChild(printScript);

        //Does not work
        //out.document.importNode(document.getElementById('main'),true);

        //var out=window.open('about:blank',"print");        
        //out.document.open();
        //out.document.write('<html><head><title></title></head><body>');
        //out.document.write('<img id="main" src="' + document.getElementById('main').src + '">');
        //out.document.writeln("<script>window.print()</"+"script>");
        //out.document.writeln("</body></html>");
        //out.document.close()
        //out.close();
        //window.open();
        FUNCTION_POPUP(600, 250, "", SHOW_PRINT_ACTION);

        active(last_active_button);
    } else if (button == 'help') {
        FUNCTION_POPUP(600, 525, "", param1);
        active(last_active_button);
    } else {
        active(button);
        mapViewer.changeMode(button);
    }
}

function checkDisabled() {
    for (var i = 0; i < buttons.length; i++) {
        if (disabled(buttons[i])) return;
        else enabled(buttons[i]);
    }
}

function enabled(button) {
    if (((button == 'back') && (mapViewer.cache.hasPrevious())) ||
        ((button == 'forward') && (mapViewer.cache.hasNext()))) {
        document.getElementById(button).setAttribute('src',
                IMAGES_PATH + button + '.gif');
        return true;
    } else return false;
}

function disabled(button) {
    if (((button == 'back') && (!mapViewer.cache.hasPrevious())) ||
        ((button == 'forward') && (!mapViewer.cache.hasNext()))) {
        document.getElementById(button).setAttribute('src',
                IMAGES_PATH + button + '_disabled.gif');
        return true;
    } else return false;
}

function active(button) {
    if (button == active_button) return;
    active_button = button;
    document.getElementById(button).setAttribute('src',
            IMAGES_PATH + button + '_over.gif');
}

function domouseover(button, text) {
    if (button == active_button) return;
    if (disabled(button)) return;
    document.getElementById(button).setAttribute('src',
            IMAGES_PATH + button + '_over.gif');
    printToolName(text);
}

function domouseout(button) {
    if (button == active_button) return;
    if (disabled(button)) return;
    document.getElementById(button).setAttribute('src',
            IMAGES_PATH + button + '.gif');
    printToolName('');
}

function printToolName(toolName){
    xInnerHtml('toolSelected', toolName);
}

function selectElement(form) {
    if (mapViewer.selectIds == '') alert(message_propertyIsNotSelected);
    else {
        var ids = mapViewer.selectIds.split('|');
        var lastId = ids[ids.length - 1];        
        form.id.value = lastId;
		form.submit();       
    }
}

//TODO: Borrar esta funcion, es de test
function selectElementTest(form, id) {
	mapViewer.selectIds = id;
	selectElement(form);
}

function viewerChange(change) {
    var widthTd = 76;    //90
    var heightTd = 93;   //108
    if (change == 'sizeChange') {
	    var widthMAP = parseInt(window.xGetElementById(MAP).style.width);
	    var heigthMAP = parseInt(window.xGetElementById(MAP).style.height);
        document.getElementById('bordermap').style.width = widthMAP + widthTd + "px";
        document.getElementById('bordermap').style.height = heigthMAP + heightTd + "px";
        
        if (widthMAP <= 250){
			document.getElementById('toolSelected').style.fontSize = '11px';        
        }else if (widthMAP <= 350){        
    		document.getElementById('toolSelected').style.fontSize = '13px';        
        }else{
			document.getElementById('toolSelected').style.fontSize = '15px';                
        }        
        (new AJAX()).getHttpRequest(CHANGE_SIZE_ACTION + '?size=' + document.getElementById('sizeSelect').value);

    } else if (change == 'cacheChange') {
        checkDisabled();
    }
}

function initSize() {
    document.getElementById('sizeSelect').value = MAP_WIDTH + ' ' + MAP_HEIGHT;
}

function sync() {
	if (mapViewer){
    	if (mapViewer.main) mapViewer.main.sync();
    	if (mapViewer.reference) mapViewer.reference.sync();
    	if (mapViewer.legend) mapViewer.legend.sync();
	}
}

addEvent(window, "load", initSize);
addEvent(window, "resize", sync);

FUNCTION_POPUP = openPopup;
VIEWER_CHANGE_HANDLER = viewerChange;


function showReferenceElement(){
	hide('showReference');
	show('hideReference');
	if (mapViewer) {
		mapViewer.addReference();
		mapViewer.adjustSize();			
	}
}

function hideReferenceElement(){
	hide('hideReference');
	show('showReference');
	if (mapViewer) {
		mapViewer.removeReference();
		mapViewer.adjustSize();	
	}
}

function showLegendElement(){	
	hide('showLegend');
	show('hideLegend');
	show(LEGEND);	
	if (mapViewer) {	
		mapViewer.addLegend();	
		mapViewer.adjustSize();		
	}
}

function hideLegendElement(){
	hide('hideLegend');
	show('showLegend');
	hide(LEGEND);
	if (mapViewer) {		
		mapViewer.removeLegend();	
		mapViewer.adjustSize();		
	}
}


if (REFERENCE_INIT_STATUS){
	addEvent(window, "load", showReferenceElement);	
} else {
	addEvent(window, "load", hideReferenceElement);
}

if (LEGEND_INIT_STATUS){
	addEvent(window, "load", showLegendElement);	
} else {
	addEvent(window, "load", hideLegendElement);
}

