// ExpandableBanners 2.0
// Copyright (c) 2010 JaxCore.com
// Visit http://www.expandablebanners.com to learn how to make amazing advertisements for any website!

var ExpandableBanners = (function() {
	
	var extimer = null;
	var flashURL = null, flashWidth = 0, flashHeight = 0;
	var bannerId = 'banner_ad';  // this is the ID of the link that will activate the expanded ad
	var bannerAd,expandedAd,expandedImg,closeDiv,closeImg;
	var bannerTarget = "top";
	var link = "";  // this link will be automatically populated by the URL of the anchor tag whose ID is "banner_ad"
	var imageURL,closeURL,closeAlign;
	var altText = 'advertisement';
	var vertDirection = 'down';
	var horzDirection = 'right';
	var bannerFlash = false;
	var expandedFlash = false;
	var expandedSwf = null;
	var bannerFlashId = '';
	var bannerAdObject = '';
	var bannerAdEmbed = '';
	var extimer = null;
	
	function setBannerId(id) {
		bannerId = id;
	}
	function setLink(url) {
		link = url;
		echo("banner link : "+url);
	}
	function setExpandedImage(url) {
		imageURL = url;
	}
	function setDirection(vert,horz) {
		vertDirection = vert;
		horzDirection = horz;
	}
	function setCloseImage(url,alignV,alignH) {
		closeURL = url;
		closeAlignV = alignV;
		closeAlignH = alignH;
	}
	function setAlt(text) {
		altText = text;
	}
	
	function setBannerFlash() {
		bannerFlash = true;
	}
	
	function setExpandedFlash(swf, width, height) {
		expandedFlash = true;
		flashURL = swf;
		flashWidth = width;
		flashHeight = height;
	}
		
	function createElements() {
		//echo('creating elements');
		
		if (bannerFlash) {
			/*var ba = id("banner_ad");
			bannerAdObject = ba.getElementsByTagName("object")[0];
			bannerAdEmbed = ba.getElementsByTagName("embed")[0];*/
		}
		else {
			var b = id(bannerId);
			if (isTag(b,'A') && !!b.childNodes && isTag(b.childNodes[0],'IMG') && isString(b.href)) {
				bannerAd = id(bannerId);
				//echo('found anchor '+bannerAd.id);
			}
		}
				
		expandedAd = document.createElement('div');
		expandedAd.id = "expanded_ad";
		style.set(expandedAd,{
			position:'absolute',
			visibility:'hidden'
		});
		
		//echo('created '+expandedAd.id);
		
		var s = '';
		if (flashURL) {
			var attr = {
				id : 'expanded_swf',
				embedId : 'expanded_embed',
				width : flashWidth,
				height : flashHeight,
				codebase : 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0',
				classid : 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
				type : 'application/x-shockwave-flash',
				pluginspage : 'http://www.macromedia.com/go/getflashplayer'
			};
			param = {
				movie : flashURL,
				quality : 'high',
				wmode : 'transparent'
			};
			s = '<object id="'+attr.id+'" classid="'+attr.classid+'" codebase="'+attr.codebase+'" width="'+attr.width+'" height="'+attr.height+'">'+
			'<param name="movie" value="'+param.movie+'" />'+
			'<param name="quality" value="'+param.quality+'" />'+
			'<param name="wmode" value="'+param.wmode+'" />'+
			'<embed id="'+attr.embedId+'" src="'+param.movie+'" quality="'+param.quality+'" wmode="'+param.wmode+'" pluginspage="'+attr.pluginspage+'" type="'+attr.type+'" width="'+attr.width+'" height="'+attr.height+'" />'+
			'</object>';
		}
		else s = '<a href="javascript://" onclick="ExpandableBanners.clickAd()"><img id="expanded_img" src="'+imageURL+'" alt="'+altText+'" border="0" /></a>';
	 	//echo(s);
		expandedAd.innerHTML = s;
		dom.prepend(expandedAd);
				
		if (flashURL) {
			expandedSwf = id('expanded_swf');
			//echo('created '+expandedSwf.id);
		}
		else {
			if (expandedAd.childNodes[0].childNodes[0].nodeName=='IMG') {
				expandedImg = expandedAd.childNodes[0].childNodes[0];
				//echo('created '+expandedImg.id);
			}
		}
		
		if (closeURL) {
			closeDiv = document.createElement('div');
			closeDiv.id = "expanded_close";
			closeDiv.style.position = 'absolute';
			closeDiv.style.cursor = 'pointer';
			closeDiv.style.backgroundColor = "red";
			closeDiv.innerHTML = '<a href="javascript://" onclick="ExpandableBanners.closeAd()"><img id="expanded_closeimg" src="'+closeURL+'" alt="Close" border="0" /></a>';
			dom.append(closeDiv,expandedAd);
			
			closeImg = id("expanded_closeimg");
			if (!closeImg) return false;
			
			closeDiv.childNodes[0].style.outline = "none";
			//echo('created '+closeDiv+', '+closeImg.id);
		}
		return true;
	}
	
	function attachEvents() {
		if (expandedAd) { //bannerAd && 
			if (!bannerFlash) {
				
				dom.addEvent(bannerAd,"mouseover",function(e) {
					//echo('mouseover event');
					openAd();
				});
				
				dom.addEvent(expandedAd,"mouseover",function(e) {
					//echo("close timer cancelled");
					clearTimeout(extimer);
				});
				
				dom.addEvent(expandedAd,"mouseout",function(e) {
					//echo('mouseout event');
					if (!closeURL) {
						//echo("close timer started");
						extimer = setTimeout(function() {
							closeAd();
						},500);
					}
				});
			
			}
			dom.addEvent(window,"resize",function(e) {
				resetAd();
			});
			return true;
		}
		return false;
	}
	
	function clickAd() {
		closeAd();
		if (bannerTarget=='top') top.location = link;
		else document.location.href = link;
	}
	
	function openAd() {
		//echo('openAd()');
		resetAd();
		style.hide('banner_ad');
		style.show(expandedAd);
	}
	
	function closeAd() {
		//echo('closeAd()');
		style.show('banner_ad');
		style.hide(expandedAd);
	}
	
	function main() {
		var c = createElements();
		var a = attachEvents();
		if (c && a) {
			if (!bannerFlash) setLink(bannerAd.href);
		}
		//uncomment the following line for testing
		//else alert('ExpandableBanners Error: failed to find elements '+c+' '+a);
	}
	
	function getElementPosition(e){
		var x=y=0;
		while (e!=null) {
			x += e.offsetLeft;
			y += e.offsetTop;
			e = e.offsetParent;
		}
		return {x:x,y:y};
	}
	
	function resetAd() {
		var expandedItem;
		if (flashURL) {
			expandedItem = expandedSwf;
			expandedWidth = flashWidth;
			expandedHeight = flashHeight;
		}
		else {
			expandedItem = expandedAd;
			expandedWidth = expandedImg.width;
			expandedHeight = expandedImg.height;
		}			
		style.maxZ(expandedAd);
		style.size(expandedAd,expandedWidth,expandedHeight);
		
		var img,bannerW,bannerH,x,y,dim;
		if (bannerFlash) {
			var ba = id('banner_ad');
			dim = getElementPosition(ba);
			bannerW = parseInt(ba.style.width);
			bannerH = parseInt(ba.style.height);
		}
		else {
			img = bannerAd.getElementsByTagName('img')[0];
			bannerW = img.width||0;
			bannerH = img.height||0;
			dim = dimensions(img);
		}
		var x = y = 0;
		
		if (horzDirection=='center') {
			x = dim.x - expandedWidth/2 + bannerW/2;
		}
		else if (horzDirection=='left') {
			x = dim.x - expandedWidth + bannerW;
		}
		else if (horzDirection=='right') {
			x = dim.x;
		}
		
		if (vertDirection=='center') {
			y = dim.y - expandedHeight/2 + bannerH/2;
		}
		else if (vertDirection=='up') {
			y = dim.y - expandedHeight + bannerH; // - expandedImg.height + bannerH;
		}
		else if (vertDirection=='down') {
			y = dim.y;
		}
		style.move(expandedAd,x,y);
		
		if (!!closeURL && !!expandedImg && !!closeImg) {
			x = y = 0;
			if (closeAlignV=='bottom') {
				y = expandedImg.height - closeImg.height;
			}
			if (closeAlignH=='right' && !!expandedImg && !!closeImg) {
				x = expandedImg.width - closeImg.width;
			}
			style.move(closeDiv, x, y);
		}
	}
	
	return {
		main : main,
		clickAd : clickAd,
		openAd : openAd,
		closeAd : closeAd,
		setLink : setLink,
		setBannerFlash : setBannerFlash,
		setExpandedImage : setExpandedImage,
		setExpandedFlash : setExpandedFlash,
		setCloseImage : setCloseImage,
		setAlt : setAlt,
		setBannerId : setBannerId,
		setDirection : setDirection
	};

})();

jaxscript.run(function() {
	ExpandableBanners.main();
});

