//var DEBUG;(typeof(window.console) != "undefined")?DEBUG=1:DEBUG=0;//alert(DEBUG);

var zAutopage = new Class({
	Implements: Options,
	options: {
		currentid: 0
	},
	initialize: function(options){
		
		this.setOptions(options);
		options = this.options;
			
		currentid = options.currentid;
		option2 = options.option2;
		
		// global vars
		curid = 0;
		nextid = 0;
		previd = 0;
		
		start = this.start;
		askIds = this.askIds;
		loadContentNext = this.loadContentNext;
		animate = this.animate;
		removeOldCurrent = this.removeOldCurrent;
		renameNext = this.renameNext;
		createNewNext = this.createNewNext;
		
		//remove all the active classes and add active to current
		$$('.image').removeClass('active');
		$$('.'+currentid).addClass('active');
		
		//start the autopage
		PagePeriodical = start.periodical(6500);

	},
	start: function(){
		askIds();
	},
	askIds: function(){
		//ask current & next id
		var callids = new Request({
			url: '/includes/contentCall.asp',
			method: 'get',
			onComplete: function(response){	
				var parsed = response.split('|');
				previd = parsed[0];
				curid = parsed[1];
				nextid = parsed[2];
				
				loadContentNext(nextid);
			}
		});
		if (nextid!==0){
			callids.send('script=gIds&page_id='+nextid);
		}
		else {
			callids.send('script=gIds&page_id='+currentid);
		}

	},
	loadContentNext: function(nextid){
		//load next content in next div
		var call = new Request.HTML({
			url: '/includes/contentCall.asp',
			method: 'get',
			update: 'pagecontent-next',
			onComplete: function(){	
					animate.delay(1000); //1sec delay so the object is preloaded before animation.
			}
		});
		
		call.send('script=gContent&getid='+nextid);

	},
	animate: function(){
		//animation
		$('pagescroller').set('tween', {
							  duration: 700, onComplete: function(){
									removeOldCurrent();
									$$('.image').removeClass('active');
									$$('.'+nextid).addClass('active');
								  }
							  });
		$('pagescroller').tween('margin-left',-900,-1800);
	},
	removeOldCurrent: function(){
		$('pagecontent-current').dispose(); //remove pagecontent-current
		$('pagescroller').setStyle('margin-left',-900); //reset the margin-left
		renameNext();
	},
	renameNext: function(){
		//rename pagecontent-next to pagecontent-current (because now its the current object)
		$('pagecontent-next').set('id','pagecontent-current');
		createNewNext();
		
	},
	createNewNext: function(){
		//create a new pagecontent-next div
		var nextdiv = new Element('div', {id: 'pagecontent-next'});
		nextdiv.set('class', 'content');
		nextdiv.inject($('pagescroller'));
	}
});//end zAutopage class
