/****************************************
| ** Splash Pages **
| Class for page loading and scrolling
|
| Created by:	 James Hicks
| Date Created:	 2007-10-22
| Copyright 2007 Lightmaker Manchester
|
****************************************/

window.addEvent('domready', function() {
			
	var Pages = new Class({
			_self: this,
		
		    initialize: function(slideBox, pages, categoryId, categoryName, pageSize, searchTerms){
				this.slideBox    	= $(slideBox);
				this.pages       	= pages;
				this.categoryId 	= categoryId;
				this.categoryName 	= categoryName;
				this.pageSize 		= pageSize;
				this.searchTerms 	= searchTerms;
				this.controls    	= this.slideBox.getElement('.controls');
				this.scrollerDiv 	= this.slideBox.getElement('#scrollInner');
				this.frameDiv    	= this.slideBox.getElement('#scrollWrapper');
				this.page 		 	= splashConfiguration.currentPage;
				this.currentPage 	= splashConfiguration.currentPage;
				this.pageIndicators = $$("#pagnation .controls .page");
				this.pageIndicators = this.pageIndicators.reverse();
				this.numberOfPages 	= numberOfPages;
		
				this.scrollOut = new Fx.Styles(this.scrollerDiv, {
					wait: true,
					duraction: 1500,
					transition: Fx.Transitions.quadIn
				});	
				this.scrollIn = new Fx.Styles(this.scrollerDiv, {
					wait: true,
					duraction: 1500,
					transition: Fx.Transitions.quadOut
				});	
							
				// Set up page controls
				
				if($('manyPages')){
					// Many pages controls
					this.pageIndicator(1);
				} else {	
					// Simple controls
					
					pageIndicators = $$("#pagnation .controls .page");		
					pageIndicators = pageIndicators.reverse();
			
					pageIndicators.each(function(indicator,pageNum) {
						indicator.addEvent('click', function(event) {
							event = new Event(event).stop();
							//alert("click, pages.goToPage("+(pageNum+1)+")");
							splashUI.splashPages.loadPage(event.target.href, pageNum+1);
						});
					});
					
					this.pageIndicators = pageIndicators;			

					var next = $("nextPage");
					next.addEvent('click', function(event) {
						event = new Event(event).stop();
						if(splashUI.splashPages.page < splashUI.splashPages.numberOfPages){
							pageNum = splashUI.splashPages.page+1;
							href = splashUI.splashPages.pageIndicators[pageNum].href;
							splashUI.splashPages.loadPage(href, pageNum);
						}
					});
				
					var prev = $("previousPage");
					prev.addEvent('click', function(event) {
						event = new Event(event).stop();
						if(splashUI.splashPages.page > 1){
							pageNum = splashUI.splashPages.page-1;
							href = splashUI.splashPages.pageIndicators[pageNum].href;
							splashUI.splashPages.loadPage(href, pageNum);
						}
					});
				}
			},
			
			loadPage: function(address, pageNum) {
				document.loader(true);
				var myAjax = new Ajax(address + '?ajax=true', {onComplete: this.slideNewPage, method: 'get'}).request();
				this.page = pageNum;
			},
					
			pageIndicator: function(page){
				this.pageIndicators.each(function(indicator) {
						indicator.className = "page";
				});
			
				if(this.pageIndicators[page-1]) {
					this.pageIndicators[page-1].addClass("current");
				}
			},
			slideNewPage: function(slideSet){
				splashUI.splashPages.pageIndicator(splashUI.splashPages.page);
							
				var scrollerDiv = splashUI.splashPages.scrollerDiv;
				var scrollOut = splashUI.splashPages.scrollOut;
				var scrollIn  = splashUI.splashPages.scrollIn;
	
				//IE6 Fallback
				if(window.ie6){
					scrollerDiv.empty();
					var newSlider = slideSet.injectInside(scrollerDiv);
					document.loader(false);
					document.setUpdraggables();	
				} else {
					// Transition direction depends on page value
					// Forward
					offset = (this.page > this.currentPage) ? -1000 : 1000;
					scrollOut.start({
						'left': -1000
					}).chain(function() {
						scrollIn.set({
							'left': offset
						});
						scrollerDiv.empty();
						//var newSlider = slideSet.injectInside(scrollerDiv);
						scrollerDiv.innerHTML = slideSet;
						scrollIn.start({
							'left': 0
						}).chain(function() {
							splashUI.splashPopup.popupLinker.linkPopups();
						});
						
						document.loader(false);
					
						if(document.setUpdraggables) {
							document.setUpdraggables();
						}
					});
				}
				
				this.currentPage = this.page;
			}
	});
	

	if(typeof(window['scrollType']) != "undefined"){	
		switch(scrollType) {
			case "ringtones":
			case "videos":
			case "screensavers":
			case "wallpapers":
			case "games":
				splashUI.splashPages = new Pages("ringtoneScroller",numberOfPages,categoryId,categoryName,pageSize,searchTerms);		
			break;		
		}	
	}
	
	document.extend({
		loader: function(visible){
			if(visible){
				$('loader').className = "on";
			} else {
				$('loader').className = "off";			
			}
		}
	});
	
});