// Continious ScrollerII (14-December-2007)
// by: Vic Phillips http://www.vicsjavascripts.org.uk


// Application Notes
// The effect is initialised and controlled by event calls to function
// zxcScroller('h','tst2','start',1,100,200);
// where:
// parameter 0 = the mode, for vertical 'v', for horizontal 'h'.                     (string 'v' or 'h');
// parameter 1 = the unique id name of the scroll container.                         (string);
// parameter 2 = start or stop.                                                      (string 'start' or 'stop');
// parameter 3 = to scroll up/left = negative digit or down/right = positive digit.  (digit);
//               may also be used to control the scroll speed.
// parameter 4 = the scroll speed (milli seconds).                                   (digits);
// parameter 5 = the initial delay before scrolling (milli seconds).                 (digits);
//
// The first call will initialise the effect.
// Subsequent calls may be used control the effect by updating parameters 2, 3 and 4.
// If parameters 2 or 3 are not specified the parameter will be toggled.
// Examples
//<input type="button" name="" value="Toggle Direction" onclick="zxcScroller('h','tst2','start');"/>
//<input type="button" name="" value="Toggle Stop/Start" onclick="zxcScroller('h','tst2');"/>

// Functional Code size = 2.4k

function zxcScroller(zxcmde,zxcid,zxcrun,zxcud,zxcspd,zxcsrt){
 var zxcp=document.getElementById(zxcid);
 if (!zxcp[zxcmde+'scroll']) return zxcp[zxcmde+'scroll']= new zxcScrollerOOP(zxcmde,zxcp,zxcrun,zxcud,zxcspd,zxcsrt);
 var zxcoop=zxcp[zxcmde+'scroll']
 clearTimeout(zxcp.to);
 zxcoop.spd=zxcspd||zxcoop.spd;
 zxcoop.ud=zxcud||-zxcoop.ud;
 zxcp[zxcmde+'run']=(zxcrun=='stop'||zxcrun=='start')?zxcrun:(zxcp[zxcmde+'run']=='stop')?'start':'stop';
 if (zxcp[zxcmde+'run']=='start') zxcoop.scroll();
}

function zxcScrollerOOP(zxcmde,zxcp,zxcrun,zxcud,zxcspd,zxcsrt){
 this.p=zxcp;
 var zxcc=this.p.getElementsByTagName('DIV')[0]
 this.vh=(zxcmde.charAt(0).toLowerCase()=='v');
 this.mde=zxcmde;
 this.ary=[];
 this.ary[0]=[zxcc,0];
 var zxcmax=(((this.vh)?this.ary[0][0].offsetHeight:this.ary[0][0].offsetWidth))+((this.vh)?this.p.offsetHeight:this.p.offsetWidth);
 this.wh=(this.mde.charAt(0).toLowerCase()=='v')?this.ary[0][0].offsetHeight:this.ary[0][0].offsetWidth;
 var zxcpos=0;
 while (zxcpos<zxcmax){
  var zxc1=this.ary.length;
  this.ary[zxc1]=[this.ary[0][0].cloneNode(true),zxcpos+=this.wh];
  zxcES(this.ary[zxc1][0],{position:'absolute',left:((this.vh)?0:this.ary[zxc1][1])+'px',top:((this.vh)?this.ary[zxc1][1]:0)+'px'},this.p);
 }
 this.ud=zxcud||-1;
 this.spd=zxcspd||100;
 this.p.to=null;
 this.data=[zxcpos,-this.wh];
 this.p[this.mde+'run']=(zxcrun=='stop'||zxcrun=='start')?zxcrun:'start';
 if (this.p[this.mde+'run']) this.p.to=setTimeout(function(zxcoop){return function(){zxcoop.scroll();}}(this),zxcsrt||500);
}

zxcScrollerOOP.prototype.scroll=function(){
 if (this.p[this.mde+'run']=='start'){
  for (var zxc1=0;zxc1<this.ary.length;zxc1++){
   this.ary[zxc1][1]+=this.ud;
   zxcES(this.ary[zxc1][0],{position:'absolute',left:((this.vh)?0:this.ary[zxc1][1])+'px',top:((this.vh)?this.ary[zxc1][1]:0)+'px'});
   if ((this.ud<0&&this.ary[zxc1][1]<=this.data[1])||(this.ud>0&&this.ary[zxc1][1]>this.data[0])) this.ary[zxc1][1]=this.data[(this.ud<0)?0:1];
  }
 }
 this.p.to=setTimeout(function(zxcoop){return function(){zxcoop.scroll();}}(this),this.spd);
}

function zxcES(zxcele,zxcstyle,zxcp,zxctxt){
 if (typeof(zxcele)=='string'){ zxcele=document.createElement(zxcele); }
 for (key in zxcstyle){ zxcele.style[key]=zxcstyle[key]; }
 if (zxcp){ zxcp.appendChild(zxcele); }
 if (zxctxt){ zxcele.appendChild(document.createTextNode(zxctxt)); }
 return zxcele;
}

/**
 * amIAt
 **/
function amIAt(section) {
    return $$('body.'+section).length > 0;
}

function startSponsors() {
    zxcScroller('h','footer','start', -1, 40);
    element = $('footer');
    element.addEvent('mouseover',(function(){
        this.hrun = 'stop';
    }).bind(element));
    element.addEvent('mouseout',(function() {
        this.hrun = 'start';
    }).bind(element));
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (
		horizontal ?
			(content.getScrollSize().x - content.getSize().x) :
			(content.getScrollSize().y - content.getSize().y));
	var slider = new Slider(scrollbar, handle, {
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if(!(ignoreMouse)){
		// Scroll the content element when the mousewheel is used within the
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){
			e = new Event(e);
			e.stop()
			var step = slider.step - e.wheel * 30;
			slider.set(step);
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

function startBiografy() {
	bio = new Fx.Morph('biografia', {duration: 1000, transition: Fx.Transitions.Sine.easeOut});
        clicked = false;
	$('linkBio').addEvent('click', function(e) {
		e = new Event(e);
		e.stop();
                if(clicked === false) {
                    $('biografia').set('style','display: block;');
                    bio.start({
                            'height': 398,
                            'width': 295,
                            'opacity': 1
                    }).addEvent('onComplete',function(){
                        //console.log($('bioContent'), $('scrollBar'), $('handle'));
                        makeScrollbar($('bioContent'), $('scrollBar'), $('handle'));
                    });
                    clicked = true;
                } else {
                    bio.start({
                            'height': 0,
                            'width': 0,
                            'opacity': 0
                    });
                    clicked = false;
                }
	});
}

function loadFlashIntro() {
    var pageSize, flashIntroDiv, flashContentDiv;
    if(amIAt('home')) {
        //Div Contiene toodo
        $('wrapper').addClass('blind');
        flashIntroDiv = new Element('div', {
            'id': 'flashIntro'
        }).inject($(document.body),'top');
        flashContentDiv = new Element('div', {
            'id' : 'flashContent'
        }).inject(flashIntroDiv);
        flashContentDiv.adopt(new Element('div', {
            'id' : 'flashPlayer2'
        }), new Element('div', {
            'id' : 'flashPlayer',
            'html': 'Reproductor'
        }), new Element ('p').adopt(new Element('a', {
            'id' : 'skipIntro',
            'html' : 'Saltear Intro',
            'href' : '#content',
            'events' : {
                'click': function(e) {
                    e = new Event(e);
                    e.stop();
                    $('flashPlayer2').destroy();
                    window.fxMorph.start({
                        'width' : 0,
                        'height' : 0
                    }).addEvent('complete',function (e) {
                        $('wrapper').removeClass('blind');
                        $('flashIntro').destroy();
                        startSponsors();
                    });
                }
            }
        })));
        swfobject.embedSWF("animaciones/intro.swf", "flashPlayer", "698", "393", "8.0.0");
        pageSize = $(window).getScrollSize();
        //console.log(pageSize);
        //Efectito
        window.fxMorph = new Fx.Morph('flashIntro', {
            duration: 500,
            transition: Fx.Transitions.Sine.easeOut
        }).start({
            'width': pageSize.x,
            'height': pageSize.y
        });

    }
}

/**
 * Resize function for the background
 **/
$(window).addEvent('resize', function() {
    if($chk($('flashIntro'))) {
        pageSize = $(window).getScrollSize();
        window.fxMorph.start({
            'width': pageSize.x,
            'height': pageSize.y
        });
    }
});

function startExpo() {
    $$('area').each(function(item) {
        item.addEvent('click', function(e) {
            var element, tabId;
            e = new Event(e);
            e.stop();
            element = e.target;
            tabId = element.href.split('#')[1];
                $$('area').each(function(item) {
                var divId = item.href.split('#')[1];
                $(divId).removeClass('active');
            });
            $(tabId).addClass('active');
           
        });
    });
}

/*
function loadElements() {

	$$('.load').each(function(item) {
		var ElementId = item.id, HTMLRequest,
		Url = ElementId+'.php';

		console.log(Url);
		console.log(ElementId);
		HTMLRequest = new Request.HTML({
			url : Url,
			evalScripts: false,
			update: $(ElementId),
			onSuccess: function(tree, elements, html, javascript) {
				console.log('Terminado 1');
			},
			onComplete: function () {

			}
		}).send('ajax=true');
	});
}


function showAnimation() {

}




function startScroller() {
	var contentId,aElement;

	$$('#menu li a').each(function(menuItem) {
		menuItem.addEvent('click',function(e) {
			e = new Event(e);
			e.stop();
			$$('#menu li a').each(function(item) {
				item.removeClass('selected');
			});
			aElement = e.target;
			contentId = aElement.get('href').replace('.html','');
			console.log(contentId);
			window._scroller.start()
			//window._scroller.toElement(contentId);
			aElement.addClass('selected');
		});
	});
}

function startEffects() {
	//Empecemos con lo importante de la página, el super scroll
	window._scroller = new Fx.Scroll($('content'), {
		wait: false,
		wheelStops: false,
		duration: 2000,
		offset: { 'x' : -260, 'y' : 0 },
		transition: Fx.Transitions.Sine.easeInOut
	});
	startScroller();
	//startBiografy();
	//Despues que sigue???
}

window.addEvent('domready', function(){
	loadElements();
	showAnimation();
	startEffects();
});

*/
