var effects = Class.create({

    init: function(){
        
    },

    transparentDiv: function(id) {
        var mainDiv = $("#"+id);
        if (!mainDiv) return false;

        //create the new Div
        var className = mainDiv.attr("class");
        var classNameArray = new Array();
        classNameArray = className.split(" ");
		
        //$('body').createAppend('div', { id: id+"Transparent", className: classNameArray[0]});
        mainDiv.createAppend('div', { id: id+"Transparent", className: "transparentChildren "+classNameArray[0]});
        mainDiv.createAppend('div', { id: id+"Body", className: "transparentChildren "+classNameArray[0]});
		

        //change the styles of the elements
        var transparentDiv = $("#"+id+"Transparent");
		var mainDivZIndex = mainDiv.css("z-index");
		if (mainDivZIndex) {
			transparentDiv.css("z-index",parseInt(mainDiv.css("z-index"))-10);
		}
        transparentDiv.css("background-color","white");
		

        var bodyDiv = $("#"+id+"Body");
		if (mainDivZIndex) {
			bodyDiv.css("z-index",parseInt(mainDiv.css("z-index")));
		}
        bodyDiv.css("background-color","transparent");
		
        //make the background semi-transparent
        transparentDiv.fadeTo(0,0.8);
		
        
        //make the background color of the mail toolBar transparent
        mainDiv.css("background-color","transparent");
    },

    createLoading: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        div.empty();

        div.createAppend('div',{id: id+"Background"});
        //body
        div.createAppend('div',{id: id+"Body"});
        div.css("background-color","transparent");

        var divBack = $("#"+id+"Background");
        divBack.css("background-color","gray");
        divBack.css("position","absolute");
        divBack.fadeTo(0,0.85);

        var divBody = $("#"+id+"Body");


        divBody.createAppend('h3',{'style':{'background-color':'#022241','color':'white','font-weight':'bold','padding':'5px'}},locStrings["_title"]);
        divBody.css("position","absolute");
        divBody.css("background-color","#C3C3C3");
        divBody.css("border","10px solid white");

        divBody.createAppend('div',{'id':id+"BodyDiv",'style':{'margin':'15px'}});
        var divBodyDiv = $("#"+id+"BodyDiv");
        
        
        divBodyDiv.html('<br><center><img src="img/logo.gif" title="Tremp"><br><br><span>'+locStrings["_loading_text_1"]+'</span></center>');

        div.css("z-index","1900");

    },

    hideLoading: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        div.css("display","none");
        UI.restoreOverflow();
    },

    createAccordion: function(id) {
        this.accord = $("#"+id);
        if (!this.accord) return false;

        this.accordionId = id;

        this.accord.accordion();
    },

    createFileTree: function(id,fileType) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        div.fileTree({ root: '', script: 'php/fileTree.php?fileType='+this.fileType }, function(file) {
            //alert(file);
        });

        $('#linkCloseDivList').click(function() {
            effects.hideModalDiv("divPrint");
        });
    },

    createCodaSlider: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        jQuery("#slider1").codaSlider();

    },

    createObsoleteBrowser: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        var domDiv = document.getElementById(divId);

        while(domDiv.firstChild) domDiv.removeChild(domDiv.firstChild);

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(locStrings["_obsolete_browser"]));

        var divObs = document.createElement('div');
        divObs.className = "divGPXClass";

        divObs.appendChild(document.createTextNode(locStrings["_obsolete_text_1"]));
        divObs.appendChild(document.createElement('br'));
        divObs.appendChild(document.createElement('br'));
        divObs.appendChild(document.createTextNode(locStrings["_obsolete_text_2"]));
        divObs.appendChild(document.createElement('br'));
        divObs.appendChild(document.createElement('br'));

        var table = document.createElement('table');
        var tbody = document.createElement('tbody');
        var tr = document.createElement('tr');
        var td1 = document.createElement('td');
        var a1 = document.createElement('a');
        var img1 = document.createElement('img');
        var td2 = document.createElement('td');
        var a2 = document.createElement('a');
        var img2 = document.createElement('img');
        var td3 = document.createElement('td');
        var a3 = document.createElement('a');
        var img3 = document.createElement('img');

        table.className = 'obsoleteTable';

        a1.href = 'http://es-es.www.mozilla.com/es-ES/';
        a1.target = '_blank';
        a1.title = 'Firefox 3.5';
        img1.src = 'img/browser/firefox.png';
        a1.appendChild(img1);
        td1.width = "1%";
        td1.appendChild(a1);

        a2.href = 'http://www.google.com/chrome/index.html?brand=CHNY&utm_campaign=en&utm_source=en-et-youtube&utm_medium=et';
        a2.target = '_blank';
        a2.title = 'Google Chrome';
        img2.src = 'img/browser/google.png';
        a2.appendChild(img2);
        td2.width = "1%";
        td2.appendChild(a2);

        a3.href = 'http://www.microsoft.com/windows/internet-explorer/default.aspx';
        a3.target = '_blank';
        a3.title = 'Internet Explorer 8';
        img3.src = 'img/browser/iexplorer.png';
        a3.appendChild(img3);
        td3.width = "1%";
        td3.appendChild(a3);

        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);

        tbody.appendChild(tr);
        table.appendChild(tbody);

        divObs.appendChild(table);

        var closeButton = document.createElement('input');
        closeButton.type = 'submit';
        closeButton.id = "linkCloseGPXModalWindow";
        closeButton.value = locStrings["_close_button"];
        divObs.appendChild(closeButton);

        OpenLayers.Event.observe(closeButton, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        domDiv.appendChild(header);
        domDiv.appendChild(document.createElement('br'));
        domDiv.appendChild(divObs);

    },

    createFileLoader: function(id) {

        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        var divError = $("#divGPXError");
        divError.css("display","none");

        div.fileUpload({
            'uploader': 'uploadify/uploader.swf',
            'cancelImg': 'img/cancel.png',
            'script': 'php/gpx.php',
            'multi': false,
            'folder': 'files',
            'auto':true,
            'fileExt':'*.gpx',
            'fileDesc':'Archivos GPX (.gpx)',
            'buttonImg': 'img/gpxLoader.png',
            'width': 150,
            'height': 66,
            //'rollover': true,
            'onSelectOnce': function() {
                effects.clearErrorGPX("divGPXError");
            },
            'onComplete': function(event,queueID,fileObj,response,data) {
                var toolBar = eGV.getMap().getControl("toolbar");
                var error = toolBar.getControl("gpxControl").processResults(response);
                if (error == "0") {
                    effects.hideModalDiv("divPrint");
                    effects.showFloatingDiv("divProfile",true);
                } else {
                    effects.showErrorGPX("divGPXError",error);
                }
            }
        });
    },

    clearErrorGPX: function(id) {
        var divId = id;
        var div = $("#"+divId);
        if (!div) return false;

        div.empty();
        div.css("display","none");

    },

    showErrorGPX: function(id,error) {
        var divId = id;
        var div = $("#"+divId);
        if (!div) return false;

        div.empty();
        div.css("display","block");
        div.createAppend('span',{},"Error: "+locStrings["_gpx_error_"+(error*(-1))]);

        
        
    },

    createSlidersContent: function(id,sliders) {
        this.container = $("#"+id);
        if (!this.container) return false;
        this.containerId = id;

        var i = 0;
        var len = sliders.length;

        var contDom = document.getElementById(id);

        var table = document.createElement('table');
        table.width = "100%";
        var tbody = document.createElement('tbody');
        var tr = document.createElement('tr');
        var td = document.createElement('td');
        var divTd = document.createElement('div');

        for (i = 0; i < len; i++) {
            tr = document.createElement('tr');
            td = document.createElement('td');
            td.className = "mapTransparencyTitle";
            td.appendChild(document.createTextNode(sliders[i].title));
            tr.appendChild(td);

            divTd = document.createElement('div');
            divTd.id = sliders[i].id;
            divTd.className = "mapTransparencySlider"
            td = document.createElement('td');
            td.className = "mapTransparencySlider";
            td.appendChild(divTd);
            tr.appendChild(td);

            tbody.appendChild(tr);

            //this.container.createAppend('label',{className:"mapTransparencyTitle"},sliders[i].title);
            //this.container.createAppend('div',{id:sliders[i].id,className:"mapTransparencySlider"});
            //this.container.createAppend('br',{id:sliders[i].id,className:"mapTransparencySlider"});
            
        }

        table.appendChild(tbody);
        contDom.appendChild(table);
    },

    createSlider: function(id, options) {
        this.slider = $("#"+id);
        if (!this.slider) return false;
        this.sliderId = id;

        

        options.slide = function(event, ui) {
            var newOpacity = ui.value/100;
            eGV.getControl(options.control).changeOpacities(newOpacity);
        }
        this.slider.slider(options);
    },

    createModalDiv: function(id) {
        var div = $("#"+id);
        if (!div) return false;

        //create the parts of the div
        //title
        div.createAppend('div',{id: id+"Background",className:"cursorClick"});

        //body
        div.createAppend('div',{id: id+"Body"});
        
        //close
        //div.createAppend('div',{id: id+"Close",className:"divClose cursorClick"});

    },

    showModalDiv: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        var body = $("#"+divId+"Body");

        var background = $("#"+divId+"Background");
        
        div.css("display","inline");

        //make the div height = 100% (IE bug)
        var wHeight = $(document).height();
        var yOffset = $(window).attr("pageYOffset");
        if (!yOffset) yOffset = 0;
        background.height(wHeight);

        //make the div body center in the middle of the page (IE bug)
        body.css("top",((wHeight+yOffset)*0.2));
        body.height((wHeight+yOffset)*0.6);
        background.fadeTo(0,0.7);
        background.css("display","inline");
        body.css("display","inline");

    },

    hideModalDiv: function(id) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var div = $("#"+divId);
        if (!div) return false;

        var body = $("#"+divId+"Body");
        var container = $("#"+divId+"Container");

        body.css("display","none");

        div.css("display","none");

        container.css("display","none");

    },



    toggleMenus: function() {

        var map = eGV.getMap();
        if (!map) return false;
        
        //get center of the map
        var center = map.getCenter();
        var zoom = map.getZoom();

        //toggle visibilty of the menus
        var divMenus = $("#divMenus");
        if (!divMenus) return false;

        var divSeparator = $("#divVSeparator");

        if (divMenus.css("display") == "inline" || divMenus.css("display") == "block") {
            divMenus.css("display","none");
            divSeparator.draggable('disable');
            divSeparator.attr("class","divSeparatorExpanded");

        } else {
            divMenus.css("display","inline");
            divSeparator.draggable('enable');
            divSeparator.attr("class","divSeparatorCollapsed");
        }

        //resize elems of the page
        UI.resizeMenuMap();

        //set center
        map.setCenter(center,zoom);
        //map.panTo(center);
    },

    floatingDiv: function(id,options) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        //create the parts of the div
        //title
        divFloating.createAppend('div',{id: id+"Title",className:"divTitle cursorMove"},options.title);

        var divFloatingTitle = $("#"+id+"Title");
        
        //body
        divFloating.createAppend('div',{id: id+"Body"});
        var divFloatingBody = $("#"+id+"Body");
        divFloatingBody.css("overflow","auto");
        divFloatingBody.css("padding-left","5px");
        divFloatingBody.css("margin-right","5px");
        divFloatingBody.css("margin-bottom","5px");
        //divFloatingBody.height(divFloatingBody.height()-5);


        //close
        divFloatingTitle.createAppend('div',{id: id+"Close",className:"divClose cursorClick"});
        var divFloatingClose = $('#'+id+"Close");
        divFloatingClose.click(function() {
            effects.hideFloatingDiv(id);
            if (options.embedded) {
                UI.resizeMapHeight();
            }
        });

        //minimize
        if (options.minimize) {
            divFloatingTitle.createAppend('div',{id: id+"Minimize",className:"divMinimize cursorClick"});
            var divFloatingMin = $('#'+id+"Minimize");
            divFloatingMin.click(function() {
                effects.toggleFloatingDiv(id);
            });
        }

        var container = "";
        if (options.containment == null) {
            container = "document";
        } else {
            container = "#"+options.containment;
        }
        if (options.draggable) {
            divFloating.draggable({
                containment: container,
                handle:'#'+id+"Title"
            });
        }

        if (options.resizable) {
            divFloating.resizable({
                containment: container,
                handles:"se",
                minHeight:200,
                minWidth:200,
                resize:function(event) {
                    divFloatingBody.height(divFloating.height()-parseInt(divFloatingTitle.attr("offsetHeight"))-parseInt(divFloatingBody.css("margin-bottom")));
                }
            });
        }

        if (options.visible) {
            divFloating.css("display","inline");
        } else {
           divFloating.css("display","none");
        }


    },

    showFloatingDiv: function(id,embedded) {
        var divId = id;
        if (this.divId) divId = this.divId;
        var infoDiv = $("#"+divId);
        if (!infoDiv) return false;

        infoDiv.css("display","inline");

        

        var emb = embedded;
        if (this.embedded) { emb = this.embedded; }
        if (emb == true) {
            UI.resizeMapHeight();
        }

        
    },

    hideFloatingDiv: function(id) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        divFloating.css("display","none");
    },

    toggleFloatingDiv: function(id) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        var divFloatingBody = $("#"+id+"Body");
        if (!divFloatingBody) return false;

        var divFloatingTitle = $("#"+id+"Title");
        if (!divFloatingBody) return false;

        divFloatingBody.toggle();
        
        if (divFloatingBody.css("display") == "none") {
            divFloating.height(parseInt(divFloatingTitle.attr("offsetHeight")));
            $(divFloating.selector+" .ui-resizable-handle").css("display","none");
        } else {
            divFloating.height(parseInt(divFloatingTitle.attr("offsetHeight"))+divFloatingBody.height());
            $(divFloating.selector+" .ui-resizable-handle").css("display","block");
        }
    },

    fillFloatingDiv: function(id,html) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        var divFloatingBody = $("#"+id+"Body");
        if (!divFloatingBody) return false;

        divFloatingBody.empty();
        divFloatingBody.html(html);
    },

    fillFloatingDivBody: function(id,url) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        var divFloatingBody = $("#"+id+"Body");
        if (!divFloatingBody) return false;

        divFloatingBody.empty();
        //divFloatingBody.load(url);
        $.ajax({
            type: "GET",
            url: url,
            contentType: "text/html;charset=ISO-8859-1",
            dataType: "text",
            charSet: "ISO-8859-1",
            processData: false,
            success: function(html){
                divFloatingBody.append(html);
            }

        });
    },

    fillFloatingDivBodyFrame: function(id,url) {
        var divFloating = $("#"+id);
        if (!divFloating) return false;

        var divFloatingBody = $("#"+id+"Body");
        if (!divFloatingBody) return false;

        divFloatingBody.empty();
        divFloatingBody.createAppend('iframe', { id: id+"BodyFrame", src:url, width:"99%",height:"99%" });
    }



});
