var witchIsVisible;

var ToggleCat = Class.create({
    initialize: function(elmt){
        this.parent = $(elmt);
        this.toggle = $(elmt.childElements()[0]);
        this.visible = false;
        this.child = null;
        
        if (elmt.childElements()[1] != null) {
            this.child = elmt.childElements()[1];
            this.toggle.observe('click', this.toggleState.bind(this));
        }
    },
    Up: function(){
        new Effect.BlindUp(this.child, {
            duration: '0.35'
        }, {
            queue: {
                position: 'end',
                scope: 'sidebarscope',
                limit: 10
            }
        });
        
        $(this.child).style.height = '';
        this.toggle.removeClassName('open');
        
        return false;
    },
    Down: function(){
        $(this.child).style.height = '';
        
        new Effect.BlindDown(this.child, {
            duration: '0.35'
        }, {
            queue: {
                position: 'end',
                scope: 'sidebarscope',
                limit: 10
            }
        });
        
        $(this.child).style.height = '';
        this.toggle.addClassName('open');
        
        return false;
    },
    toggleState: function(event){
        if (this.visible == false) {
            if (witchIsVisible != null) {
                witchIsVisible.toggleState();
            }
            this.visible = true;
            witchIsVisible = this;
            this.Down();
        }
        else {
            this.visible = false;
            witchIsVisible = null;
            this.Up();
        }
        
        return false;
    }
});

var jsButton = Class.create({
    initialize: function(elmt){
        this.btn = $(elmt);
        this.btn.observe('mouseover', this.over.bind(this));
        this.btn.observe('mouseout', this.out.bind(this));
    },
    over: function(){
        this.btn.addClassName('hover');
        return false;
    },
    out: function(){
        this.btn.removeClassName('hover');
        return false;
    }
});

function sideBar(){
    $$(".category .sub-category-container").invoke('hide');
    witchIsVisible = null;
    
    $$(".category").each(function(elmt){
        this.toggle = new ToggleCat(elmt);
    });
};

var YouTubeBtn = Class.create({
    initialize: function(elmt){
        this.elmt = elmt;
        this.data = elmt.readAttribute("data");
        this.type = 'object[type="application/x-shockwave-flash"]';
    },
    
    getObj: function(){
        var obj = '<object data="' + this.data + '" type="application/x-shockwave-flash" width="640" height="480">' +
        '<param name="src" value="' +
        this.data +
        '">' +
        '</object>';
        
        return obj;
    },
    
    toHTML: function(){
        var str = '<div><a href="#" class="video_btn" ref="' + this.data + '">' +
        '> Guarda Video' +
        '</a></div>';
        this.elmt.replace(str);
    }
});

var videoBox = Class.create({
    initialize: function(){
        var objBody = $$('body')[0];
        
        objBody.appendChild(Builder.node('div', {
            id: 'videoOverlay'
        }));
        
        objBody.appendChild(Builder.node('div', {
            id: 'videoBox'
        }, [Builder.node('div', {
            id: 'videoContainer'
        }, '')]));
        
        this.overlayDuration = 0.2;
        
        this.overlay = $('videoOverlay');
        this.box = $('videoBox');
        this.container = $('videoContainer');
        
        this.overlay.hide().observe('click', (function(){
            this.end();
        }).bind(this));
		
		this.container.hide();
		
        /*
         * $('close').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
         */
        var video = $$('.text object[type="application/x-shockwave-flash"]');
        this.nVideo = new Array;
        for (var i = 0; i < video.length; i++) {
            this.nVideo[i] = new YouTubeBtn(video[i]);
            this.nVideo[i].toHTML();
        }
        
        this.vBtn = $$('.video_btn');
        
        for (var i = 0; i < this.vBtn.length; i++) {
            this.vBtn[i].observe('click', this.start.bind(this, i));
        }
    },
    start: function(idx){
		// calculate top and left offset for the lightbox		
		var arrayPageScroll = document.viewport.getScrollOffsets();
        var Top = (arrayPageScroll[1] + document.viewport.getHeight())/2 - 360;
        var Left = (arrayPageScroll[0] + document.viewport.getWidth())/2 - 240;
        this.container.update(this.nVideo[idx].getObj());
		this.container.setStyle({ 'background-color': '#fff', top: Top + 'px', left: Left + 'px', width: '640px', height: '480px', padding: '0px' });
        	
		var arrayPageSize = this.getPageSize();
        this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
        
		new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: 0.8 });
		this.container.show();
    },
    end: function(){
		this.container.update('');
		new Effect.Fade(this.container, { duration: this.overlayDuration/2 });
		new Effect.Fade(this.overlay, { duration: this.overlayDuration });
        this.overlay.hide();
    },
	getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
});

function buttonsInit(){
    $$('.button').each(function(elmt){
        this.btn = new jsButton(elmt);
    });
};

document.observe('dom:loaded', function(){
    sideBar();
    buttonsInit();
    new videoBox();
});



