if ( deffect.browser().msie && parseFloat(deffect.browser().version) <= 7 ) {
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent)
		window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			var src = img.src;
			var div = document.createElement("DIV");
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			img.replaceNode(div);
		}
	}
}
function promotionSlider(element, config) {
	var el = deffect.getElementReference(element);
	if ( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 ) {
		config.position = "absolute";
		el.style.setExpression("top", "eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0))");
	} else
		config.position = "fixed";
	var mainBodyOverlay = new overlay(el, config);
	var wholeOverlay = new overlay(el, config);
	var topOverlay = new overlay(el, config);
	var dur = 0.5;
	var POPUP_STATE = "popupState";
	var BROWSER_POP_STATE = "browserPopState";
	var slider = {
		state: {
			EXPAND: 1,
			COLLAPSE: 0,
			CLOSE: -1
		}
	};
	var currState = slider.state.CLOSE;
	var arrowImg = null;
	var arrowImgPath = {EXPAND: "",	COLLAPSE: ""};
	var topHeight = 0;
	var mainBodyHeight = 0;
	
	if ( config.duration )
		dur = config.duration;
		
	if ( config.arrowImage && config.arrowImage.length == 3 ) {
		arrowImg = deffect.getElementReference(config.arrowImage[0]);
		arrowImgPath.EXPAND = config.arrowImage[1];
		arrowImgPath.COLLAPSE = config.arrowImage[2];
	}
	
	if ( config.topHeight != null && config.mainBodyHeight != null ) {
		topHeight = deffect.css.getNumeric(config.topHeight);
		mainBodyHeight = deffect.css.getNumeric(config.mainBodyHeight);
		var slideAttr, backAttr;
		
		// Initialize sliding effect to the overlays
		if ( el.style.top != "" ) {
			slideAttr = {top:{from:(deffect.css.performArithmetic(el.style.top, "+", topHeight)), to:(deffect.css.performArithmetic(el.style.top, "+", topHeight+mainBodyHeight))}};
			backAttr = {top:{to:(deffect.css.performArithmetic(el.style.top, "+", topHeight))}};
		} else if ( el.style.bottom != "" ) {
			slideAttr = {bottom:{from:(deffect.css.performArithmetic(el.style.bottom, "+", topHeight)), to:(deffect.css.performArithmetic(el.style.bottom, "+", topHeight+mainBodyHeight))}};
			backAttr = {bottom:{to:(deffect.css.performArithmetic(el.style.bottom, "+", topHeight))}};
		}
		var slide = {attributes: slideAttr, duration: dur, callback: updateArrowImg};
		var back = {attributes: backAttr, duration: dur, callback: updateArrowImg};
		var customEffect = new deffect.overlay.effect(el, slide, back);
		if (!( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 ))
			mainBodyOverlay.applyEffect(customEffect);
		
		if ( el.style.top != "" ) {
			slideAttr = {top:{from:el.style.top, to:(deffect.css.performArithmetic(el.style.top, "+", topHeight+mainBodyHeight))}};
			backAttr = {top:{to:el.style.top}};
		} else if ( el.style.bottom != "" ) {
			slideAttr = {bottom:{from:el.style.bottom, to:(deffect.css.performArithmetic(el.style.bottom, "+", topHeight+mainBodyHeight))}};
			backAttr = {bottom:{to:el.style.bottom}};
		}
		slide = {attributes: slideAttr, duration: dur, callback: updateArrowImg};
		back = {attributes: backAttr, duration: dur, callback: updateArrowImg};
		customEffect = new deffect.overlay.effect(el, slide, back);
		if (!( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 ))
			wholeOverlay.applyEffect(customEffect);
		
		if ( el.style.top != "" ) {
			slideAttr = {top:{from:el.style.top, to:(deffect.css.performArithmetic(el.style.top, "+", topHeight))}};
			backAttr = {top:{to:el.style.top}};
		} else if ( el.style.bottom != "" ) {
			slideAttr = {bottom:{from:el.style.bottom, to:(deffect.css.performArithmetic(el.style.bottom, "+", topHeight))}};
			backAttr = {bottom:{to:el.style.bottom}};
		}
		slide = {attributes: slideAttr, duration: dur, callback: updateArrowImg};
		back = {attributes: backAttr, duration: dur, callback: updateArrowImg};
		customEffect = new deffect.overlay.effect(el, slide, back);
		if (!( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 ))
			topOverlay.applyEffect(customEffect);
		
		// Read cookie to determine for showing which effect of overlay
		var state = deffect.cookie.readCookie(POPUP_STATE);
		var browserPopState = deffect.cookie.readCookie(BROWSER_POP_STATE);
		
		if ( state == null ) {
			// As cookie is absent, create new one for popup state of slider
			deffect.cookie.createCookie(POPUP_STATE, slider.state.EXPAND, 36500, ".hktdc.com");
			
			// Mark expand state into this control
			currState = slider.state.EXPAND;
			
			// Show promotion slider in expand state
			wholeOverlay.show();
		} else {
			// Change state back to number, then mark it into this control
			currState = +state;
			
			if(browserPopState != null && browserPopState != "") {
				currState = +browserPopState;
			}
			
			// Show promotion slider depends on previous state
			switch ( currState ) {
				case slider.state.EXPAND:
					wholeOverlay.show();
					handleSafari(mainBodyHeight+topHeight);
					break;
				case slider.state.COLLAPSE:
					topOverlay.show();
					handleSafari(topHeight);
					break;
				case slider.state.CLOSE:
					break;
			}
		}
	}
	
	//set auto minimize
	if(currState == slider.state.EXPAND) {
		var promotionTime = setTimeout(updateSliding, 5000);
		if(config.timeout) {
			promotionTime = setTimeout(updateSliding, config.timeout);
		}
	}
	
	this.close = function() {
		clearTimeout(promotionTime);
		if ( currState != slider.state.CLOSE ) {
			// Write close state to cookie first
			deffect.cookie.createCookie(POPUP_STATE, slider.state.CLOSE, 7, ".hktdc.com");
			
			// Close promotion slider
			switch ( currState ) {
				case slider.state.EXPAND:
					wholeOverlay.hide();
					break;
				case slider.state.COLLAPSE:
					topOverlay.hide();
					break;
			}
		}
		handleSafari(0);
		deffect.cookie.createCookie(BROWSER_POP_STATE, "", null, ".hktdc.com");
	}
	
	this.handleSliding = function() {
		clearTimeout(promotionTime);
		// If client is IE 6.0 or below, ignore to handle sliding
		if ( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 )
			return;
		
		// Expand or collapse promotion slider depends on previous state
		switch ( currState ) {
			case slider.state.EXPAND:
				// Mark updated state into control
				currState = slider.state.COLLAPSE;
				
				// Write state into cookie
				deffect.cookie.createCookie(POPUP_STATE, currState, 36500, ".hktdc.com");
				
				mainBodyOverlay.hide();
				
				handleSafari(topHeight);
				break;
			case slider.state.COLLAPSE:
				// Mark updated state into control
				currState = slider.state.EXPAND;
				
				// Write state into cookie
				deffect.cookie.createCookie(POPUP_STATE, currState, 36500, ".hktdc.com");
				
				mainBodyOverlay.show();
				
				handleSafari(mainBodyHeight+topHeight);
				break;
		}
		deffect.cookie.createCookie(BROWSER_POP_STATE, "", null, ".hktdc.com");
	}
	
	function updateSliding() {
		// If client is IE 6.0 or below, ignore to handle sliding
		if ( deffect.browser().msie && parseFloat(deffect.browser().version) <= 6 )
			return;
		
		// Expand or collapse promotion slider depends on previous state
		switch ( currState ) {
			case slider.state.EXPAND:
				// Mark updated state into control
				currState = slider.state.COLLAPSE;
				
				// Write state into cookie
				deffect.cookie.createCookie(BROWSER_POP_STATE, currState, null, ".hktdc.com");
				
				mainBodyOverlay.hide();
				
				handleSafari(topHeight);
				break;
		}
	}
	
	function updateArrowImg() {
		if ( arrowImg != null ) {
			switch ( currState ) {
				case slider.state.EXPAND:
					arrowImg.src = arrowImgPath.EXPAND;
					break;
				case slider.state.COLLAPSE:
					arrowImg.src = arrowImgPath.COLLAPSE;
					break;
			}
		}
	}
	
	function handleSafari(height) {
		if ( deffect.browser().safari ) {
			var ver1stPart = parseFloat(deffect.browser().version.substr(0, deffect.browser().version.indexOf(".")));
			var ver2ndPart = parseFloat(deffect.browser().version.substr(deffect.browser().version.indexOf(".")+1,
				deffect.browser().version.indexOf(".", (deffect.browser().version.indexOf(".")+1))-deffect.browser().version.indexOf(".")-1));
			if ( ver1stPart <= 533 ) {
				if ( ver2ndPart > 17 )
					return;
					
				if ( height == 0 ) {
					window.onresize = window.onscroll = null;
					el.style.display = "none";
					return;
				}
				
				el.style.top = (window.pageYOffset + window.innerHeight - height) + 'px';
				window.onresize = window.onscroll = function() { el.style.top = (window.pageYOffset + window.innerHeight - height) + 'px'; };
			}
		}
	}
}
