// Global ATMO object
var ATMO = (function () {
	var that = {};
	
	// method for the voices box borders.
	// alters classes and not styles
	that.borderizeVoices = function (name) {
		var voicesGrid = $("#voices-grid"),
		    gridChildren, oddBase, voiceBox, i;
		
		// if a name or names are given, remove their div(s)
		if (name) {
			if ("string" === typeof name) {
				$("#voice-box-" + name).remove();
			} else if ($.isArray(name)) {
				for (i = 0; i < name.length; i += 1) {
					$("#voice-box-" + name[i]).remove();
				}
			}
		}
		
		gridChildren = voicesGrid.children('div');
		oddBase = gridChildren.length % 2;
		
		// apply border classes to odd voices divs
		gridChildren.each(function (i) {
			voiceBox = $(gridChildren.get(i));
			if (!(i % 2)) {
				voiceBox.addClass('border');
			}
			
			// single out the bottom row
			if (oddBase) {
				if (gridChildren.length - i <= 1) {
					voiceBox.addClass('last');
				}
			} else {
				if (gridChildren.length - i <= 2) {
					voiceBox.addClass('last');
				}
			}
		});
	};
	
	return that;
}());

// ATMO Ad Display
ATMO.ads = (function () {
	var that = {},
	serializeArgs = function (obj) {
		var i, str = '';
		for (i in obj) {
			if (obj.hasOwnProperty(i) && obj[i]) {
				str += i + "=" + obj[i] + ";";
			}
		}
		return str;
	};
	
	that.createFactory = function (site, zone, args) {
		var ord = Math.round(Math.random() * 10000000000000000),
		    tileCount = 0,
		    adSite = site || "dctestsite",
		    adZone = zone || "dctestzone",
		    adArgs = args || '',
		    zoneSite = function (nSite, nZone) {
		    	return (nSite || adSite) + "/" + (nZone || adZone);
		    },
		    self = {};
		
		if ('string' !== typeof adArgs) {
			adArgs = serializeArgs(adArgs);
		}
		
		self.displayAtlanticAd = function (position, adSizes, nSite, nZone) {
			var adSrc = 'http://ad.doubleclick.net/adj/' + zoneSite(nSite, nZone) + ';';
			
			tileCount += 1;
			adSrc += "pos=" + position + ";";
			adSrc += "tile=" + tileCount + ";";
			
			// force 'topleader' to have interstitial flag
			// should be a param?
			if ("topleader" === position) {
				adSrc += "dcopt=ist;";
			}
			adSrc += adArgs + "sz=" + adSizes + ";ord=" + ord + "?";

			document.write('<script src="' + adSrc + '" type="text/javascript"><\/script>');
		};
		
		return self;
	};
	return that;
}());



$(document).ready(function(){
	$(".ad").each(function(){
		if( $(this).html().search("grey.gif") != -1 ) {
			$(this).hide();
		}
	});
});

