$(window).bind('load', function() {
	var startDemo = function(){
		var ad, ads = {}, asset, ggrto, fired = false;

		asset = $('#SampleAsset');
		ads.imageAd = $('<div><a target="_blank" href="http://www.gumgum.com/advertisers">&nbsp;</a><img src="http://ads.gumgum.com/com/gumgum/close-ad_11x11.jpg" /><img src="http://ads.gumgum.com/com/orlybeauty/orly_ad_300x60.jpg" width="300" height="60"/></div>');
		ads.textAd = $('<div><a target="_blank" href="http://www.gumgum.com/advertisers">&nbsp;</a><img src="http://ads.gumgum.com/com/gumgum/close-ad_11x11.jpg" /><iframe src="http://demo.gumgum.com/text-ad-sample.html" width="300" bgcolor="#414141" scrolling="no" height="60" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0" vspace="0" hspace="0"/></div>');
		ads.flashAd = $('<div><a target="_blank" href="http://www.gumgum.com/advertisers">&nbsp;</a><img src="http://ads.gumgum.com/com/gumgum/close-ad_11x11.jpg" /><object type="application/x-shockwave-flash" data="http://ads.gumgum.com/com/dailydosepublishing/CelebDailyFlashAd.swf" width="300" height="60"><param name="wmode" value="transparent" /><param name="movie" value="http://ads.gumgum.com/com/dailydosepublishing/CelebDailyFlashAd.swf" /></object></div>');
		$.each(ads,function(i,n){
			var fullAd = this;
			// Without housing
			//this.css({'border':0,'margin':0,'padding':0,'position':'absolute','width':310,'height':82,'background':'transparent url(none) no-repeat scroll 0 0'});
			// With housing
			this.css({'border':0,'margin':0,'cursor':'pointer','padding':0,'position':'absolute','width':310,'height':82,'background':'transparent url(http://ads.gumgum.com/com/gumgum/housing.png) no-repeat scroll 0 0'});
			// If IE < 7, lets use
			if($.browser.msie && Math.floor(parseInt($.browser.version)) < 7) {
				this.css({'background-image':'url(http://ads.gumgum.com/com/gumgum/housing.gif)'});
			}
			this.appendTo(document.body);
			this.data('dim',{'w':this.outerWidth(),'h':this.outerHeight(),'off':this.offset()});
			// close button
			$('img:first',this).css({'position':'absolute','right':5,'top':4,'margin':0,'padding':0,'cursor':'pointer'}).click(function(){
				fullAd.fadeOut();
			});
			// ad
			$('img:eq(1), object, iframe',this).css({'position':'absolute','right':5,'bottom':6,'margin':0,'padding':0,'cursor':'pointer','background-color':'#414141'});
			$('img:eq(1)',this).click(function(){
				newWin('http://www.orlybeauty.com/ggcolor.php');
			});;
			$('object',this).focus(function(){
				newWin('http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=292619445&mt=8');
			});;
					
			//gglink
			$('a:first',this).css({'position':'absolute','left':5,'top':4,'width':122,'height':10,'margin':0,'padding':0,'border':'none','text-decoration':'none','cursor':'pointer'}).mouseover(function(){ $(this).css({'position':'absolute','left':5,'top':4,'width':122,'height':10,'margin':0,'padding':0,'border':'none','text-decoration':'none','cursor':'pointer'}); });
			this.hide();
		});
		
		asset.one('load',function(){
			fired = true;
			$(this).data('dim',{'w':$(this).outerWidth(),'h':$(this).outerHeight()});
			$(this).data('repo',function(){
				try{
					var ad, asset;
					asset = $('#SampleAsset');
					ad = asset.data('activeAd');
					ad = {el:ad,dim:ad.data('dim')};
					asset = {el:asset,pos:asset.offset(),dim:asset.data('dim')};

					var margin = (asset.dim.w - ad.dim.w)/2;
					ad.el.animate({'left':(asset.pos.left + margin) + 'px', 'top': (asset.pos.top + asset.dim.h - 15 - ad.dim.h) + 'px'},{'queue':true,'duration':'normal','complete':function(){ $(this).show().css('visibility','visible'); }});
				}catch(e){
					alert(e.message);
				}
			});

			$('#AdSelector li').click(function(){
				if(typeof $('#SampleAsset').data('activeAd') != 'undefined'){
					$('#SampleAsset').data('activeAd').hide();
				}
				switch($.trim($(this).html())){
					case 'Text Ad': $('#SampleAsset').data('activeAd',ads.textAd); break;
					case 'Banner Ad': $('#SampleAsset').data('activeAd',ads.imageAd); break;
					case 'Flash Ad': $('#SampleAsset').data('activeAd',ads.flashAd); break;
					default: return false;
				}
				$('#SampleAsset').data('repo')();
			}).hover(
				function(){ $(this).addClass('hovered'); },
				function(){	$(this).removeClass('hovered'); }
			);
			$('#AdSelector li:eq(2)').click();
		});
		setTimeout(function(){
			if(!fired) $('#SampleAsset').load();
		},1000);
		function newWin(url){
			window.open(url,'ggAd');
		}
	}();
	
	ggrto = null;
	$(window).resize(function(){
		clearTimeout(ggrto);
		ggrto = setTimeout($('#SampleAsset').data('repo'),200);
	});
});