// JavaScript Document
var pgBannerSlider = new Class({
	initialize: function(menu, options){
		this.options = $extend({
			duration: 300, //slide speed. lower for slower, bigger for faster
			delayHide: 500,
			direction: 'down',
			hidestyle: 'normal'
		}, options || {});
		this.menu = menu;
		this.timer = null;
		this.navWidth = 16;
		this.bannerWidth = 980;
		this.prevMvPos = null;
		this.fx = null;
		this.fxDesc = null;
		this.fxCust = null;
		this.bulletClickable = true;
		this.prevPos = null;
		this.bulletOver = null;
		//window.addEvent('load', this.start.bind(this));
		
		this.start();
		
	},
	start: function() {
		this.bannerList = $(this.menu).getChildren()[0];
		this.bannerNav = $(this.menu).getChildren()[1];
		this.bannerCust = $(this.menu).getChildren()[2];
		this.bannerNavButton = $(this.menu).getChildren()[3];
//		this.bannerDesc = $(this.menu).getChildren()[3];
		
		this.bannerList.setStyles({'display':'none'});
		this.bannerNav.setStyles({'display':'none'});
		this.bannerCust.setStyles({'display':'none'});
		this.bannerNavButton.setStyles({'display':'none'});
//		this.bannerDesc.setStyles({'display':'none'});
		
		var bannerItemList = $(this.bannerList).getChildren();
		var banner = bannerItemList[0];
		var aTag = banner.getChildren()[0];
		if (this.options.bannerLinksHide != 0) {
			var imgTag = aTag.getChildren()[0];
		} else {
			var imgTag = banner.getChildren()[0];
		}
		
//		this.setLoadingPanelItem();
		this.setLoadingPanel();
		
		var imageInfo = {img:imgTag};
		var imageOkInfo = IsImageOk(imageInfo, 0);
		imageOkInfo.onComplete = function(isLoaded, count) {
			if (isLoaded) {
				this.hideLoadingPanel();
				this.bannerList.setStyles({'display':'block'});
				if (!this.options.controlHide) this.bannerNav.setStyles({'display':'block'});
				this.bannerCust.setStyles({'display':'block'});
				this.bannerNavButton.setStyles({'display':'block'});
				this.doNav();
				this.doNavButton();
				
				//click
				this.doClick(0);
			}
		}.bind(this);
	},
	
	setLoadingPanelItem: function() {
		var bannerList = $(this.bannerList).getChildren();
		for (var i=1; i<bannerList.length; i++) {
			var banner = bannerList[i];
			var aTag = banner.getChildren()[0];
			if (this.options.bannerLinksHide != 0) {
				var imgTag = aTag.getChildren()[0];
			} else {
				var imgTag = banner.getChildren()[0];
			}
			var imageInfo = getImageSize(imgTag.src);
			var loadingDiv = new Element('div', {
				'id':'loading-panel-' + i,
				'class':'loading-item'
			});
			$(loadingDiv).setStyles({'width':imageInfo["width"], 'height':imageInfo["height"]});
			$(banner).adopt(loadingDiv);
			
			var imageInfo = {img:imgTag, loadingPanel:loadingDiv};
			var imageOkInfo = IsImageOk(imageInfo, i);
			imageOkInfo.onComplete = function(isLoaded, count) {
				if (isLoaded) {
					$('loading-panel-' + count).setStyles({'display':'none'});
				}
			}.bind(this);
		}
	},
	
	setLoadingPanel: function() {
		var loadingDiv = new Element('div', {
			'id':'loading-panel',
			'class':'loading-panel'
		});
		$(this.menu).adopt(loadingDiv);
	},
	
	hideLoadingPanel: function() {
		var loadingPanel = $(this.menu).getChildren()[4];
		$(loadingPanel).setStyles({'display':'none'});
	},
	
	doAnim: function(pos) {
		switch(this.options.hidestyle) {
			case "fade":
				this.doFade(pos);
				break;
			case "slide-train":
				this.slideTrainStyleArrage();
				this.doSlideTrain(pos);
				break;
			case "slide":
				$$(this.bannerList.getChildren()).setStyles({'left': this.bannerWidth});
				this.doSlide(pos);
				break;
			default:
				this.doFade();
		}
	},
	
	doFade: function(pos) {
		this.doFadeTransition(pos);
	},
	
	doFadeTransition: function(pos) {
		this.bulletClickable = false;
		var bannerList = $(this.bannerList);
		var banner = bannerList.getChildren()[pos];
		var lastID = bannerList.getChildren().length-1;
		var bannerCustBox = $(this.bannerCust).getChildren()[pos];
		var nextPos = 0;
		
		var aTag = $(banner).getChildren()[0];
		//impression($(aTag).rel);
		
		clearTimeout(this.timer);
		$$(bannerList.getChildren()).setStyles({'opacity':0});
		$$(this.bannerCust.getChildren()).setStyles({'opacity':0});
		
		if (this.fx) {
			this.fx.stop();
			this.fx = null;
		}
		if (this.fxCust) {
			this.fxCust.stop();
			this.fxCust = null;
		}
		this.fx = new Fx.Style(banner, 'opacity', {duration:this.options.scrollInTimer, 
			onComplete: function() {
				nextPos = pos + 1;
				if (nextPos > $(this.bannerList).getChildren().length-1) nextPos = 0
			}.bind(this)
		});
		this.fx.start(0, 1);
		
		//move custom banner
		this.fxCust = new Fx.Style(bannerCustBox, 'opacity', {duration:this.options.descScrollInTimer, 
			onComplete: function() {
				this.bulletClickable = true;
				this.fxCust.stop();
				this.fxCust = null;
				if (this.options.autoscroll) {
					this.timer = setTimeout(this.doClick.bind(this, nextPos), this.options.delayHide);			
				}
			}.bind(this)
		});
		this.fxCust.start(0, 1);
		this.prevPos = pos;
	},
	
	slideTrainStyleArrage : function() {
		var bannerTotalWidth = 0;
		var bannerList = $(this.bannerList).getChildren();
		for (var i=0; i<bannerList.length; i++) {
			var banner = bannerList[i];
			var aTag = banner.getChildren()[0];
			if (this.options.bannerLinksHide != 0) {
				var imgTag = aTag.getChildren()[0];
			} else {
				var imgTag = banner.getChildren()[0];
			}
			
			var imageInfo = getImageSize(imgTag.src);
			bannerTotalWidth += imageInfo["width"];
			$(banner).setStyles({ 'width':imageInfo["width"], 'height':imageInfo["height"] });
			
		}
		$(this.menu).getChildren()[0].setStyles({ 'width':bannerTotalWidth });
		$(this.bannerCust).setStyles({ 'width':bannerTotalWidth });
	},
	doSlideTrain : function(pos) {
		this.doSlideTrainTransition(pos);
	},
	
	doSlideTrainTransition: function(pos) {
		this.bulletClickable = false;
		var bannerList = $(this.bannerList);
		var marginLeft = -(pos*this.bannerWidth);
		var bannerCustBox = $(this.bannerCust).getChildren()[pos];
		var nextPos = 0;
		var fxPrev;
		
		clearTimeout(this.timer);
		
		//$(this.bannerCust).setStyles({'margin-left':this.bannerWidth});
		var banner = bannerList.getChildren()[pos];
		var aTag = $(banner).getChildren()[0];
		//impression($(aTag).rel);
//		clicks(banner);
				
		if (this.fx) {
			this.fx.stop();
			this.fx = null;
		}
		if (this.fxCust) {
			this.fxCust.stop();
			this.fxCust = null;
		}
		
		//move description
		this.fxCust = new Fx.Style(this.bannerCust, 'margin-left', {duration:this.options.descScrollInTimer, 
			onComplete: function() {
				this.fxCust.stop();
				this.fxCust = null;
				
			}.bind(this)
		});
		this.fxCust.start(this.prevMvPos, marginLeft);
		
		this.fx = new Fx.Style(this.bannerList, 'margin-left', {duration:this.options.scrollInTimer, 
			onComplete: function() {
				this.bulletClickable = true;
				this.prevMvPos = marginLeft;
				nextPos = pos + 1;
				if (nextPos > $(this.bannerList).getChildren().length-1) nextPos = 0
				if (this.options.autoscroll) {
					this.timer = setTimeout(this.doClick.bind(this, nextPos), this.options.delayHide);
				}
				
			}.bind(this)
		});
		this.fx.start(this.prevMvPos, marginLeft);
		
		this.prevPos = pos;
	},
	doSlide : function(pos) {
		this.doSlideTransition(pos);
	},
	doSlideTransition: function(pos) {
		var fx, fxPrev, fxDesc, fxCust, fxPrevDesc;
		var nextPos = pos + 1;
		if (nextPos > $(this.bannerList).getChildren().length-1) nextPos = 0
		
		this.bulletClickable = false;
		var bannerList = $(this.bannerList) 
		
		$$(this.bannerCust.getChildren()).setStyles({'left':this.bannerWidth});
		
		clearTimeout(this.timer);
		if (fx) {
			fx.stop();
			fx = null;
		}
		if (fxPrev) {
			fxPrev.stop();
			fxPrev = null;
		}
		
		var banner = bannerList.getChildren()[pos];
		var aTag = $(banner).getChildren()[0];
//		impression($(aTag).rel);
					
		if (this.prevMvPos != null) {
			if (this.prevMvPos == pos) this.prevMvPos = pos - 1;
			var prevBanner = bannerList.getChildren()[this.prevMvPos];
			
			prevBanner.setStyles({'left': 0});
			fxPrev = new Fx.Style(prevBanner, 'left', {duration:this.options.scrollOutTimer});
			fxPrev.start(0, -this.bannerWidth);
			
			//move description
			fxCust = new Fx.Style($(this.bannerCust).getChildren()[this.prevMvPos], 'left', {duration:this.options.descScrollOutTimer});
			fxCust.start(0, -this.bannerWidth);
	
			fx = new Fx.Style(banner, 'left', {duration:this.options.scrollInTimer});
			fx.start(this.bannerWidth, 0);
			
			//move custom banner
			fxCust = new Fx.Style($(this.bannerCust).getChildren()[pos], 'left', {duration:this.options.descScrollInTimer, 
				onComplete: function() {
					this.bulletClickable = true;
					if (this.options.autoscroll) {
						this.timer = setTimeout(this.doClick.bind(this, nextPos), this.options.delayHide);
					}
				}.bind(this)
			});
			fxCust.start(this.bannerWidth, 0);
		} else {
			banner.setStyles({'left': 0});
			//move custom banner
			fxCust = new Fx.Style($(this.bannerCust).getChildren()[pos], 'left', {duration:this.options.descScrollInTimer, 
				onComplete: function() {
					this.bulletClickable = true;
					fxCust.stop();
					fxCust = null;
					if (this.options.autoscroll) {
						this.timer = setTimeout(this.doClick.bind(this, nextPos), this.options.delayHide);
					}
				}.bind(this)
			});
			fxCust.start(this.bannerWidth, 0);
		}
		this.prevMvPos = pos
		this.prevPos = pos;
	},
	
	doClick: function(pos) {
		var bannerNav = $(this.bannerNav).getChildren()[0];
		var li = $(bannerNav).getChildren()[pos];
		var aTag = li.getChildren()[0];
		if (this.prevMvPos == pos) return;
		$(aTag).fireEvent('click');
	},
	
	doNav : function() {
		//click event
		var bkautoscroll = this.options.autoscroll;
		var bannerNav = $(this.bannerNav).getChildren()[0];
		var totalNavWidth = 0;
		var parent = this;
		$(bannerNav).getChildren().each (function(li, index) {
			totalNavWidth += this.navWidth;
			var li = $(bannerNav).getChildren()[index];
			var aTag = li.getChildren()[0];
			$(aTag).addEvent('mouseover', function(event) {
				if (event) {
					new Event(event).stop();
				}
				parent.bulletOver = true;
			});
			$(aTag).addEvent('mouseout', function(event) {
				if (event) {
					new Event(event).stop();
				}
				parent.bulletOver = false;
			});
			
			$(aTag).addEvent('click', function(event) {
				if (this.prevPos != index) {
					if (this.bulletClickable) {
						if (event) {
							new Event(event).stop();
						}
//						if (parent.bulletOver) parent.options.autoscroll = false;

						this.removeNavAllClass();
						$(aTag).addClass("active");
						$(aTag).set({'onclick':'return false;'});
						this.doAnim(index);
					}
				}
			}.bind(this));
		},this);
		$(this.bannerNav).setStyles({'width':totalNavWidth});
	},
	doNavButton : function() {
		var fx;
		var parent = this;
		//click event
		var bannerPrev = $(this.bannerNavButton).getChildren()[0];
		var bannerNext = $(this.bannerNavButton).getChildren()[1];
		$(bannerPrev).addEvent('mouseover', function(event) {
			$(bannerPrev).setStyles({'visibility':'visible'});
			fx = new Fx.Style(bannerPrev, 'opacity', {duration:200, 
				onComplete: function() {
					fx.stop();
				}.bind(this)
			});
			fx.start(0, 1);
		});
		$(bannerPrev).addEvent('mouseout', function(event) {
			fx = new Fx.Style(bannerPrev, 'opacity', {duration:200, 
				onComplete: function() {
					$(bannerPrev).setStyles({'visibility':'visible'});
					fx.stop();
				}.bind(this)
			});
			fx.start(1, 0);
		});
		$(bannerPrev).addEvent('click', function(event) {
			var pos = parent.prevPos - 1;
			if (pos < 0) {
				pos = $(parent.bannerList).getChildren().length-1;
			}
			parent.options.autoscroll = false;
			parent.doClick(pos);
		});
		$(bannerNext).addEvent('mouseover', function(event) {
			$(bannerNext).setStyles({'visibility':'visible'});
			fx = new Fx.Style(bannerNext, 'opacity', {duration:200, 
				onComplete: function() {
					fx.stop();
				}.bind(this)
			});
			fx.start(0, 1);
		});
		$(bannerNext).addEvent('mouseout', function(event) {
			fx = new Fx.Style(bannerNext, 'opacity', {duration:200, 
				onComplete: function() {
					$(bannerNext).setStyles({'visibility':'visible'});
					fx.stop();
				}.bind(this)
			});
			fx.start(1, 0);
		});
		$(bannerNext).addEvent('click', function(event) {
			var pos = parent.prevPos + 1;
			if (pos > $(parent.bannerList).getChildren().length-1) {
				pos = 0;
			}
			parent.options.autoscroll = false;
			parent.doClick(pos);
		});
		
	},
	
	removeNavAllClass: function() {
		var bannerNav = $(this.bannerNav).getChildren()[0];
		for (var i=0; i<$(bannerNav).getChildren().length; i++) {
			var prevnav = (bannerNav).getChildren()[i].getChildren()[0];
			prevnav.removeClass("active");
		}
	},
	
	doDesc: function(pos) {
		var banner = $(this.bannerList).getChildren()[pos];
		var aTag = banner.getChildren()[0];
		var bannerContainer = this.bannerDesc;
		var options = this.options;
		var fxDesc = this.fxDesc;
		var bannerHeight = this.bannerHeight;
		var bannerDescHeight = 100;
		
		if ($(bannerContainer).getStyle('height').toInt() > 0) {
			bannerDescHeight = $(bannerContainer).getStyle('height').toInt();
		}
		
		//$(bannerContainer).setStyles({'bottom':-bannerDescHeight});
		
		$(aTag).addEvent('mouseover', function(event) {
			var bannerDescBox = $(bannerContainer).getChildren()[pos];
			var direction = "";
			if ($(bannerContainer).getStyle('bottom').toInt() >= 0) {
				direction = "up";
			} else if ($(bannerContainer).getStyle('top').toInt() >= 0) {
				direction = "down";
			}
			
			$$(bannerContainer.getChildren()).setStyles({"display":"none"});
			$(bannerDescBox).setStyles({"display":"block"});
			
			var onMoveComplete = function() {
				fxDesc.stop();
				fxDesc = null;
			}
			if (direction == "up") {
				fxDesc = new Fx.Style($(bannerContainer), 'bottom', {duration:options.descScrollInTimer, onComplete:onMoveComplete});	
				fxDesc.start(-bannerDescHeight, 0);
			}
		});
		/*$(aTag).addEvent('mouseout', function(event) {
			new Event(event).stop();
			$(bannerContainer).setStyles({"bottom":-bannerDescHeight});
		});*/
	}
});


function getImageSize(src) {

	var image = new Image()
	image.src = src;
	
	var imageObj = {};
	imageObj["width"] = image.width;
	imageObj["height"] = image.height;
	
	return imageObj;
}

function IsImageOk(imgInfo, count) {
	var info = {};
	info.imgInfo = imgInfo;
	var date = new Date();
	var d = "?" + date.getTime();
	
	var onload = function() {
		info.onComplete(true, count);
	}
	
	var imgObj = new Image();
	imgObj.src = imgInfo.img.src + d;
	
	if (document.all) {
		var ieLoad = function() {
			if(imgObj.complete != null && imgObj.complete == true){ 
				onload();
				return;
			}
			setTimeout(ieLoad, 1000);
		}
		setTimeout(ieLoad, 1000);
	} else {
		imgObj.onload = function() {
			onload();
		}
		imgObj.onerror = function() {
			info.onComplete(false, count);
		}
	}
	
	
/*	
	var myAjax = new Ajax(imgInfo.img.src + d, {
		method: 'get', 
		onComplete:function() {
			alert("complete");
			info.onComplete(true, count);
		},
		onFailure: function() {
			info.onComplete(true, count);
		}
	}).request();
*/	
	/*
	imgInfo.img.onload = function() {
		info.onComplete(true, count);
	}
	imgInfo.img.onerror = function() {
		info.onComplete(false, count);
	}*/
	return info;
}


function impression(id) {
	
	var url = "modules/mod_ds_bannerslider/impclick.php";
	var myAjax = new Ajax(url, {
		method: 'get', 
		data:"bid=" + id + "&action=i", onComplete:function() {
		}
	}).request();
}

function clicks(banner) {
	var aTag = $(banner).getChildren()[0];
	var url = $(aTag).href;
	var id = $(aTag).rel;
	var target = $(aTag).target;
	var impclickurl = "modules/mod_ds_bannerslider/impclick.php";
		
	aTag.addEvent("click", function(event) {
		var e = new Event(event);
		e.stopPropagation();
		var myAjax = new Ajax(impclickurl, {
			method: 'get', 
			data:"bid=" + id + "&action=c", onComplete:function() {
				/*if (target == "_blank") {
					window.open(url, "_blank");
				} else {
					document.location = url;	
				}*/
			}
		}).request();
	});
	
	
}