
var slideBox = new Class({

	Implements: [Events, Options],

	options: {
		className:'slideBox',
		prevArrow:'-previous',
		nextArrow:'-next',
		removeArrows:true,
		fadeArrows:false,
		startOpacity:0.5,
		endOpacity:1,
		mouseoverBox:true,
		startClass:'normal',
		endClass:'over',
		speed:5,
		transition:Fx.Transitions.Quart.easeOut
	},

	initialize: function(element,options){
		element = $(element);
		this.setOptions(options);		
		this.active = false;

		this.up = element.getElements('div[class=' + this.options.className + '-previous]');
		this.up = this.up[0].getElements('a');
		this.up = this.up[0];
		
		this.down = element.getElements('div[class=' + this.options.className + '-next]');
		this.down = this.down[0].getElements('a');
		this.down = this.down[0];
		
		this.wrapper = element.getElements('div[class=' + this.options.className + '-wrapper]');
		this.wrapperH = this.wrapper[0].getStyle('height').toInt();
		
		this.slider = this.wrapper[0].getElements('div[class=' + this.options.className + '-slider]');
		this.slider = this.slider[0];
		
		
		if(this.options.removeArrows) this.removeArrows();
		if(this.options.fadeArrows) this.fadeArrows();
		if(this.options.mouseoverBox) this.mouseoverBox();
		
		this.clickEvent(element);
	},
	
	removeArrows: function() {
		this.start = this.slider.getStyle('top').toInt();			
		if(this.start==0) this.up.getParent().setStyle('display','none');
	},
	
	fadeArrows: function(){
		this.up.setStyle('opacity',this.options.startOpacity);
		this.down.setStyle('opacity',this.options.startOpacity);
		
		this.up.addEvent('mouseenter', this.up.fade.bind(this.up,[this.options.endOpacity]));
		this.down.addEvent('mouseenter', this.down.fade.bind(this.down,[this.options.endOpacity]));
		
		this.up.addEvent('mouseleave', this.up.fade.bind(this.up,[this.options.startOpacity]));
		this.down.addEvent('mouseleave', this.down.fade.bind(this.down,[this.options.startOpacity]));
	},
	
	mouseoverBox: function(){
		$$('.' + this.options.className + '-slider UL LI').each(function(element,index){
			element.addClass(this.options.startClass);
			element.addEvent('mouseenter',function(){
				element.addClass(this.options.endClass);
				element.removeClass(this.options.startClass);
			}.bind(this));
			element.addEvent('mouseleave',function(){
				element.addClass(this.options.startClass);
				element.removeClass(this.options.endClass);
			}.bind(this));
		}.bind(this));
	},
	
	setArrows: function() {
		this.current = this.slider.getStyle('top').toInt();
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);
		if(this.current==0) { 
			this.up.getParent().setStyle('display','none');
			this.down.getParent().setStyle('display','block');
		} else if(this.current > this.last) {
			this.up.getParent().setStyle('display','block');
			this.down.getParent().setStyle('display','block');
		} else {
			this.up.getParent().setStyle('display','block');
			this.down.getParent().setStyle('display','none');
		}
	},
	
	clickEvent: function(element){		
		this.height = this.slider.getSize().y;		
		this.slideFx = new Fx.Tween(this.slider,{
			duration:(this.options.speed*100),
			transition:this.options.transition,
			wait:false,
			onComplete:function(){
				this.active = false;
				if(this.options.removeArrows) this.setArrows();
			}.bind(this)
		});
		
		this.up.addEvent('click',function(e){
			var e = new Event(e).stop();
			if(this.active==false) {
				this.scrollUp();
			}
		}.bind(this));
		
		this.down.addEvent('click',function(e){
			var e = new Event(e).stop();
			if(this.active==false) {
				this.scrollDown();
			}
		}.bind(this));
	},
	
	scrollDown: function(){		
		this.now = this.slider.getStyle('top').toInt();		
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);
		if(this.now > this.last) {
			this.active = true;
			this.slideFx.start('top',(this.slider.getStyle('top').toInt()-this.wrapperH)+'px');
		}
	},
	
	scrollUp: function() {
		this.now = this.slider.getStyle('top').toInt();
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);		
		if(this.now < 0) {
			this.active = true;
			this.slideFx.start('top',(this.now+this.wrapperH)+'px');
		}
	}

});
var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}



window.addEvent('domready', function() {
	// You can skip the following two lines of code. We need them to make sure demos
	// are runnable on MooTools demos web page.
		if (!window.demo_path) window.demo_path = '';
			var demo_path = window.demo_path;
			// --
			
			var about_req = new Request.HTML({url:demo_path+'about.php', 
				onSuccess: function(html) {
					//Clear the text currently inside the results div.
					$('about_holder').set('text', '');
					//Inject the new DOM elements into the results div.
					$('about_holder').adopt(html);
				},
				
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				onFailure: function() {
					$('about_holder').set('text', 'The request failed.');
				}
			});
			
		
			var contact_req = new Request.HTML({url:demo_path+'contact.html', 
				onSuccess: function(html) {
					//Clear the text currently inside the results div.
					$('contact_holder').set('text', '');
					//Inject the new DOM elements into the results div.
					$('contact_holder').adopt(html);
				},
				//Our request will most likely succeed, but just in case, we'll add an			
				//onFailure method which will let the user know what happened.
				onFailure: function() {
					$('contact_holder').set('text', 'The request failed.');
				}
			});		
		
			var scroll = new Fx.Scroll('container', {
				wait: false,
				duration: 2000,
				offset: {'x': -200, 'y': -50},
				transition: Fx.Transitions.Back.easeInOut
			});

			$('home').addEvent('click', function(event) {				
				event = new Event(event).stop();
                                $('home_text').setStyle('display', '');
   			        scroll.toElement('home_text').chain(function() {
                                  $('about_page').setStyle('display', 'none');
                                  $('work_page').setStyle('display', 'none');
                                  $('contact_page').setStyle('display', 'none');
                                });			
			});

			
			$('about').addEvent('click', function(event) {		
				event = new Event(event).stop();
                                $('about_page').setStyle('display', '');
				about_req.send();	
				scroll.toElement('about_page').chain(function() {
                                  $('home_text').setStyle('display', 'none');
                                  $('work_page').setStyle('display', 'none');
                                  $('contact_page').setStyle('display', 'none');
                                });
			});
			 
			$('work').addEvent('click', function(event) {			
				event = new Event(event).stop();
                                $('work_page').setStyle('display', '');
				scroll.toElement('work_page').chain(function() {
                                  $('home_text').setStyle('display', 'none');
                                  $('about_page').setStyle('display', 'none');
                                  $('contact_page').setStyle('display', 'none');
                                });
			});
			$('contact').addEvent('click', function(event) {				
				event = new Event(event).stop();
                                $('contact_page').setStyle('display', '');
				contact_req.send();
				scroll.toElement('contact_page').chain(function() {
                                  $('home_text').setStyle('display', 'none');
                                  $('about_page').setStyle('display', 'none');
                                  $('work_page').setStyle('display', 'none');
                                });
			});
});