/*
 * Ads
 * 
 * This script wil find all divs with the classname "adcontainer"
 * and get the ads by the ID of this field
 * 
 */


//adevent = "index.cfm?event=json_getAds";
//adevent = 'remote/remoteAdService.cfc?method=getAdsGateway&returnformat=json'
adevent = "index.cfm?event=json_getAds";
registerevent = 'index.cfm?event=registerAdClick'; 
AJAX_loader = '/CFIDE/itoolspro/ajax-loader.gif';


/* do not touch */

jQuery(function(){
	jQuery.fn.requestAds();
});

jQuery.fn.extend({

	requestAd: function(id){
		var aa = new Array(0), self = this;
		aa.push(id);
		
		jQuery.post(adevent, {
			adids: aa
		}, function(json){
			for (key in json.data) {
				self.drawAdColumn(json.data[key], key);
			}
			
		}, 'json');
	},
	requestAds: function(){
		var aa = new Array(0), ab = jQuery("div.adcontainer"), key, self = this;
		ab.each(function(){
			aa.push(this.id);
		});
		
		jQuery.post(adevent, {
			adids: aa
		}, function(json){
			for (key in json.data) {
			
			
				if (jQuery(document.getElementById(key)).hasClass('adchanger')) {
					self.drawAdChanger(json.data[key], key);
				}
				else {
					self.drawAdColumn(json.data[key], key);
				}
				
			}
			
		}, 'json');
	},
	drawAdColumn: function(a, p){
		var i, el, mw, mh, tmp;
		if (a.length < 0) 
			return;
		
		// box info 
		mw = a[0].maxWidth || 'auto';
		mh = a[0].maxHeight || 'auto';
		
		el = jQuery(document.createElement("div"));
		
		// box style
		if (!/auto/.test(mw) || !/auto/.test(mh)) 
			el.css("overflow", "hidden");
		
		el.css({
			width: mw,
			height: mh
		});
		
		for (i = 0; i < a.length; i++) {
		
			if (a[i].type == 'gfx') {
				if (a[i].mimetype == 'application/x-shockwave-flash') 
					el.append(this.drawFlashAd(a[i]));
				else 
					el.append(this.drawImageAd(a[i]));
			}
			else {
				if ( a[i].html.length > 0 ) {
					try {
						el.append(jQuery('<div />').addClass("AD_html").html(jQuery(a[i].html)));
					} catch (e){
						
					}
				}
			}
		}
		
		jQuery("#" + p).append(el);
		
	},
	
	drawAdChanger: function(a, p){
		var i, el, mw, mh, adCurrent = -1, adCount = a.length, _self = this;
		// check if ads
		if (adCount <= 0) 
			return;
		
		// check if easing effect exists
		if (typeof(jQuery.effects.puff) != 'function') {
			alert("[jQuery.effects.puff] easing effect is missing");
			return;
		}
		
		// box info 
		mw = a[0].maxWidth || 'auto';
		mh = a[0].maxHeight || 'auto';
		
		el = jQuery('<div />');
		
		// box style
		if (!/auto/.test(mw) || !/auto/.test(mh)) 
			el.css("overflow", "hidden");
		
		el.css({
			width: mw,
			height: mh,
			position: "relative"
		});
		
		
		var setAd = function(p){
			var cur = adCurrent == (adCount - 1) ? 0 : adCurrent + 1;
			
			var adel;
			
			if (a[cur].mimetype == 'application/x-shockwave-flash') 
				adel = _self.drawFlashAd(a[cur]);
			else 
				adel = _self.drawImageAd(a[cur]);
			
			adel.css({
				width: mw,
				height: mh,
				position: "absolute",
				top: 0,
				left: 0
			});
			
			jQuery(el).append(adel);
			
			// update global current
			adCurrent = cur;
		}
		
		// set First ad
		setAd(el);
		
		// set change iterval
		setInterval(function(){
		
			jQuery(el).find('>div').hide('puff', {
				easing: "easeOutQuint"
			}, 800, function(){
				$(this).remove();
				setAd(el);
			})
			
		}, 10000);
		
		// add to document
		jQuery(document.getElementById(p)).append(el);
	},
	
	drawImageAd: function(a){
		var el;
		
		el = jQuery(document.createElement("div"));
		lnk = jQuery(document.createElement("a"));
		img = jQuery(document.createElement("img"));
		
		el.addClass("AD_image");
		//if a link doesnt exist, only put the picture
		//and register click to it
		if (a.link.length > 0) {
			lnk.attr({
				href: a.link,
				target: '_blank',
				id: 'ad_' + a.adid
			}).click(function(){
				jQuery(this).registerAdClick();
			}).appendTo(el);
			img.attr({
				src: a.src,
				alt: '',
				width: a.width,
				height: a.height
			}).appendTo(lnk);
		} else {
			img.attr({
				src: a.src,
				alt: '',
				width: a.width,
				height: a.height,
				id: 'ad_' + a.adid
			}).click(function(){
				jQuery(this).registerAdClick();
			}).appendTo(el);
		}
		
		
		return el;
		
	},
	drawFlashAd: function(a){
		var el = jQuery(document.createElement("div"));
		var protocol = window.location.protocol;
		var hostname = window.location.hostname;
		var pathname = window.location.pathname;
		// EW 04-Jun-2010 Added the clickTAG extracting code to pass to flash
		var tempLink = a.link;
		var tempClickTag = tempLink.split('clickTAG=')[1];

		if(!tempClickTag){
			tempClickTag = tempClickTag //protocol + '//' + hostname + pathname + '?adid=' + a.adid + '&page=' + window.location.href;
		}
		
		el.addClass("AD_flash");
		
		el.flash({
			src: a.src,
			width: a.width,
			height: a.height,
			flashvars: {
				hyperlink: a.link,
				clickTAG: protocol + '//' + hostname + pathname + '?adid=' + a.adid + '&page=' + window.location.href
			}
		}, {
			version: 8
		}, function(htmlOptions){
			$this = $(this);
			htmlOptions.wmode = "transparent";
			$this.append($.fn.flash.transform(htmlOptions));
		});
		return el;
	},
	registerAdClick: function(){
		var adId = $(this).attr("id");
		jQuery.ajax({
			type: "GET",
			url: registerevent,
			data: {
				adid: adId.split('_')[1],
				page: window.location.href
			
			}
		});
	}
});
