
/***************************************************************************************************************
* Function			:	crossPromotionObj.init(containerId, autoTime, userTime)
* Description		:	Enable banner framework. Load full size banners when thumbnail banner is clicked.
* Parameter Usage	:	containerId	:	container id
*						autoTime	:	(Optional) time interval in seconds between each banner is shown, default is 5 seconds
*						userTime	:	(Optional) time interval in seconds after user triggered click received, default is 10 seconds
*						lifepsan	:	(Optional) time interval in minutes to remove and create full size banner, default is 120 minutes
* Location			:	/content/hongkongpws/hk_home/js/hk_home.js
* Author			:	Mitch Leung
* Creation Date		:	13 January 2009
* Side effect		:	N/A
* Amendment History	:
* Date		By					Description
* ---------	------------	--------------------------------------------------------------------------------
*  3 Mar 09	Mitch Leung	Ranomize initial banner appearance
* 24 Apr 09	Mitch Leung	Lazy loading fullsize banner image and creating div containers
***************************************************************************************************************/
var crossPromotionObj = {
	interval:5000,
	intervalAuto:5000,
	intervalAfterClick:10000,
	bUserClicked:0,
	placardId:'',
	controlId:'',
	timer:0,
	lifespan:120,	// MINUTE(S) for full size banner images before it requests the server again
	placard:null,
	controls:null,
	init:function(containerId, autoTime, userTime, lifespan){
		if (autoTime!=null && autoTime > 0){
			this.intervalAuto = autoTime * 1000;
			this.interval = autoTime * 1000;
		}
		if (userTime!=null && userTime > 0){
			this.intervalAfterClick = userTime * 1000;
		}
		if (lifespan!=null && lifespan > 0){
			this.lifespan = parseInt(lifespan);
		}
		this.placardId = containerId;
		var container = document.getElementById(this.placardId);
		var i;
		var divs = container.getElementsByTagName('div');
		if (divs.length >= 2){
			this.placard = divs[0];
			this.controls = divs[1];
			var containers = this.controls.getElementsByTagName('div');
			var startId = parseInt(Math.random() * containers.length);
			var startEl = null;
			for (i=0; i<containers.length; i++){
				var ahrefs = containers[i].getElementsByTagName('a');
				if (ahrefs.length == 1){
					var img = ahrefs[0].getElementsByTagName('img');
					if (img.length == 1){
						ahrefs[0].onclick = function(){
							crossPromotionObj.userClick(this);
							return false;
						};
						ahrefs[0].setAttribute('rel','poster'+i);
					}
					img = null;
				}
				if (i == startId){
					startEl = ahrefs[0];
				}
				ahrefs = null;
			}
			if (startEl != null){
				this.thumbClick(startEl);
			}else{
				this.timer=setTimeout('crossPromotionObj.autoPilot()',this.interval);
			}
			containers = startId = startEl = null;
		}
		container = i = divs = null;
	},
	createBanner:function(id,href,src,alt,tar,js){
		var div = document.getElementById(id);
		if (div){
			div.parentNode.removeChild(div);
		}
		var newEl = document.createElement('div');
		newEl.setAttribute('id',id);
		this.placard.appendChild(newEl);
		div = document.getElementById(id);
		this.setExpiry(div);
		if (tar!=null && tar!=''){
			tar = ' target="'+tar+'"';
		}else{
			tar = '';
		}
		div.innerHTML = '<a href="'+href+'"'+tar+'"><img src="'+src+'" alt="'+alt+'" /></a>';
		newEl = div = null;
	},
	setExpiry:function(div){
		var today = new Date();
		var lifespan = parseInt(today.valueOf()/1000/60)+this.lifespan;
		div.setAttribute('expiry',lifespan);
		today = lifespan = null;
	},
	expiry:function(div){
		var today = new Date();
		if (div.getAttribute('expiry') == null || parseInt(div.getAttribute('expiry')) <= parseInt(today.valueOf()/1000/60)){
			today = null;
			return true;
		}
		today = null;
		return false;
	},
	userClick:function(a){
		clearTimeout(this.timer);
		this.interval = this.intervalAfterClick;
		this.bUserClicked = 1;
		crossPromotionObj.rotateBanner(a);
		this.timer=setTimeout('crossPromotionObj.autoPilot()',this.interval);
	},
	thumbClick:function(a){
		clearTimeout(this.timer);
		if (this.bUserClicked==1){
			this.interval = this.intervalAuto;
			this.bUserClicked = 0;
		}
		crossPromotionObj.rotateBanner(a);
		this.timer=setTimeout('crossPromotionObj.autoPilot()',this.interval);
	},
	rotateBanner:function(a){
		var img = a.getElementsByTagName('img')[0];
		if (!img){
			return;
		}
		var id = a.getAttribute('rel');
		var href = a.getAttribute('href');
		var src = img.getAttribute('longdesc');
		var alt = (img.getAttribute('alt')!=null) ? img.getAttribute('alt') : '';
		var div = document.getElementById(id);
		var tar = a.getAttribute('target');
		if ((div && this.expiry(div)) || (id && href && src && !div)){
			this.createBanner(id,href,src,alt,tar);
		}
		var banners = this.controls.getElementsByTagName('div');
		for (i = 0; i < banners.length; i++){
			banners[i].className = '';
		}
		a.parentNode.className='active';
		var i, posters, poster;
		posters = this.placard.getElementsByTagName('div');
		for (i=0;i<posters.length;i++){
			posters[i].style.display = 'none';
			posters[i].style.visibility = 'hidden';
		}
		poster = document.getElementById(a.getAttribute('rel'));
		if (poster){
			poster.style.display = 'block';
			poster.style.visibility = 'visible';
		}
		img = id = href = src = alt = div = tar = js = banners = i = posters = poster = null;
	},
	autoPilot:function(){
		clearTimeout(this.timer);
		var ahrefs = this.controls.getElementsByTagName('a');
		var i, a, next;
		for (i=0; i<ahrefs.length; i++){
			a = ahrefs[i];
			if (a.parentNode.className == 'active'){
				next = (i==ahrefs.length-1) ? 0 : i+1;
				this.thumbClick(ahrefs[next]);
				break;
			}
		}
		i = a = next = ahrefs = null;
	}
};
crossPromotionObj.init("crosspromotions",2.5,10,60);