﻿    // Global Javascript por Luis Merino (http://www.httpcomunicacio.com).
    
    // Globales y Constantes
    var MAX_FONT = 1.2;
    var MIN_FONT = 1;
    var LIMIT_FONT = MIN_FONT;
    
    // DOM listo, inicializamos valores
    /*Window.onDomReady(function(){
        //ulElement = $E('ul','idiomas');
        //toggleMenu(ulElement);
        textChange = new fx.Style('contenido_principal', 'font-size', {unit: 'em', duration: 500, transition: Fx.Transitions.sineInOut});
        rememberTextChange();
    });*/
    
    // Carga lista, iniciamos eventos y funciones
    window.addEvent('load', function(){
		
		textChange = new fx.Style('contenido_principal', 'font-size', {unit: 'em', duration: 500, transition: Fx.Transitions.sineInOut});
        rememberTextChange();									 
        // SWF Object
       /* var so = new SWFObject('../swf/menu.swf', 'menu', '755', '220', '7','#FFFFFF', true);
        so.addParam('movie', '../swf/menu.swf');
        so.addParam('menu', 'false');
        so.addParam('quality', 'high');
        so.addVariable('lang', lang);
        so.addVariable('menu', menu);
        so.addVariable('opcion', opcion);
        so.addVariable('cabecera', cabecera);
        so.addVariable('claim', claim);
        so.write('menu_flash');
        // Comprobar que los elementos existen en la página
        if ( $('peli1') && $('peli2') ) {
            // Peli 1
            so = new SWFObject('../swf/peli1.swf', 'peli1', '377', '110', '#FFFFFF');
            so.addParam('movie', '../swf/peli1.swf');
            so.addParam('menu', 'false');
            so.addParam('quality', 'high');
            so.write('peli1');
            // Peli 2
            so = new SWFObject('../swf/peli2.swf', 'peli1', '377', '110', '#FFFFFF');
            so.addParam('movie', '../swf/peli2.swf');
            so.addParam('menu', 'false');
            so.addParam('quality', 'high');
            so.write('peli2');
        }*/
        // Idiomas
        /*$E('a','idiomas').addEvent('click', function(){
            toggleMenu(ulElement);
        });*/
        
        // Text Resize
        $('sizeUp').addEvent('click', function(){
            if(LIMIT_FONT < MAX_FONT) {
                textChange.custom(MIN_FONT,MAX_FONT);
                LIMIT_FONT = MAX_FONT;
                Cookie.set("textChange", MAX_FONT);
            }
        });
        $('sizeDown').addEvent('click', function(){
            if(LIMIT_FONT > MIN_FONT)
                textChange.custom(MAX_FONT,MIN_FONT);
                LIMIT_FONT = MIN_FONT;
                Cookie.set("textChange", MIN_FONT);
        });
        
        // Popups
        $E('a','avisoLegal').onclick = function(){
            pop(this.href,'','500','500',true,'scrollbars=1');
            return false;
        };
        
        // Formulario contacto prensa, respuesta asincrónica
        $('prensaForm').onsubmit = function(){
            var cargandoAJAX = false;
            var div;
            new Ajax('envia-contacto-prensa.php', {
                postBody: this,
                update: 'mensajes',
                onStateChange: function(){
                    if (((this.readyState != 4) || (this.status == 200)) && !cargandoAJAX) {
                        newHTML = '<strong>' + Enviando + '... </strong> <img src="../img/ajax-loader.gif" alt="..." width="32" height="32" class="middle"  />';
                        div = new Element('div').addClass('center').setHTML(newHTML).injectInside('loader');
                        div.setOpacity(0);
                        new Fx.Opacity(div, {duration: 1000}).custom(0,1);
                        cargandoAJAX = true;
                    }
                },
                onComplete: function(){
                    if ($S('.errores').length > 0) {
                        (function(){div.remove();}).delay(200); //espera un medio segundo y bye bye
                    }
                    var skroll = new Fx.ScrollWindow({duration: 1000, wait: false, transition: Fx.Transitions.sineInOut});
                    skroll.scrollTo(0,300);
                    //document.location.hash = '#target';
                }
            }).request();
			return false;
        };
           // Formulario contacto prensa, respuesta asincrónica
        $('contactoForm').onsubmit = function(){
            var cargandoAJAX = false;
            var div;
            new Ajax('envia-contacto.php', {
                postBody: this,
                update: 'mensajes',
                onStateChange: function(){
                    if (((this.readyState != 4) || (this.status == 200)) && !cargandoAJAX) {
                        newHTML = '<strong>' + Enviando + '... </strong> <img src="../img/ajax-loader.gif" alt="..." width="32" height="32" class="middle"  />';
                        div = new Element('div').addClass('center').setHTML(newHTML).injectInside('loader');
                        div.setOpacity(0);
                        new Fx.Opacity(div, {duration: 1000}).custom(0,1);
                        cargandoAJAX = true;
                    }
                },
                onComplete: function(){
                    if ($S('.errores').length > 0) {
                        (function(){div.remove();}).delay(200); //espera un medio segundo y bye bye
                    }
					limpiaFormulario();
                    var skroll = new Fx.ScrollWindow({duration: 1000, wait: false, transition: Fx.Transitions.sineInOut});
                    skroll.scrollTo(0,300);
                    //document.location.hash = '#target';
                }
            }).request();
			return false;
        };
        // Contenido cargado con AJAX
        // [...]
    });
    
    // Clases
    Element.extend({
        fixPng: function() {    
            // IE7 je OK.
            if (!/MSIE (5.5|6.)/.test(navigator.userAgent)) return;          
            var f = 'DXImageTransform.Microsoft.AlphaImageLoader';
            var bgImg = this.getStyle("background-image");
            // neni co
            if (!bgImg || bgImg == 'none') return;
            if (bgImg.match(/^url[("']+(.*.png)[)"']+$/i)) {
                var s = RegExp.$1;
                // IE layout hack
                if (this.currentStyle.width == 'auto' && this.currentStyle.height == 'auto')
                    style.width = offsetWidth + 'px';
                this.style.backgroundImage = 'none';
                // IE link fix.
                for (var n = 0; n < this.childNodes.length; n++)
                    if (this.childNodes[n].style) this.childNodes[n].style.position = 'relative';        
                this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+s+"',sizingMethod='image')";       
    	    }
        }
    });
    
    Fx.ScrollWindow = Fx.Base.extend({
	initialize: function(options){
		this.setOptions(options);
		this.now = [];
		var b = document.body;
		var stop = this.clearTimer.bind(this);
		//if (!this.options.continue){
			if (b.addEventListener) b.addEventListener('DOMMouseScroll', stop, false);
			else b.onmousewheel = stop;
		//}
	},
	setNow: function(){
		[0,1].each(function(i){
			this.now[i] = this.compute(this.from[i], this.to[i]);
		}, this);
	},
	scrollTo: function(x, y){
		if (this.timer && this.options.wait) return;
		var left = Window.getScrollLeft();
		var top =  Window.getScrollTop();
		var width = Window.getWidth();
		var height = Window.getHeight();
		var fullWidth = Window.getScrollWidth();
		var fullHeight = Window.getScrollHeight();
		var maxScrollWidth = fullWidth - width;
		var maxScrollHeight = fullHeight - height;
		if (x > maxScrollWidth) x = maxScrollWidth;
		if (y > maxScrollHeight) y = maxScrollHeight;
		return this.custom([left, top], [x, y]);
	},
	toElement: function(el){
		return this.scrollTo($Element(el, 'getLeft'), $Element(el, 'getTop'));
	},
	increase: function(){
		window.scrollTo(this.now[0], this.now[1]);
	}
    });
    
	// Funciones generales        
    function toggleMenu(el) {
        var display = el.getStyle('display');

        if (display == "none") {
            display = "block";
        } else {
            display = "none";
        }
        
        el.setStyle('display', display);
    }
    
    function rememberTextChange() {
        var cookie = Cookie.get("textChange"); // Devuelve el tamaño del texto
        
        if (cookie) {
            textChange.set(cookie); // Comprueba cookie, asigna tamaño del texto
            LIMIT_FONT = cookie;    // Situa el nuevo limite de tamaño
        }
    }
    
    function pop(url, name, width, height, centered, features) {
		if(window.screen)if(centered)if(centered=="1") {
			var l = (screen.width-width)/2;
			var t = (screen.height-height)/2;
			features+=(features!='')?',':'';
			features+='left='+l+',top='+t;
		}
		var flop=window.open(url, name, features+((features!='')?',':'')+'width='+width+',height='+height);
		flop.focus();
	}
	
function writeFlash(movie) {
	document.write(movie);
}
