Event.onDOMReady(function() {
	var registerDownload = new RegisterDownload('.downloadbutton');
	var lookingfor = new LookingFor();
	AC.OverlayPanel.overlay.setDelegate(overlayDelegate);
}, false);


var overlayDelegate = {
	willShow: function(overlayPanel, outgoing, incoming) {
		overlayPanel.setOverlayShadowImageSrc('http://images.apple.com/euro/itunes/affiliates/download/images/overlay20080909.png');
	}
}


LookingForPromos = {
	
	NoPromo: {
		name: 'No Promotion',
		
		detect: function() {
			return false;
		},
		
		render: function() {
			// manipulates various parrts of the DOM where the information regarding
			// the requested song needs to be inserted

			// note this will not show the information panel on the top of the page
			// it only ensures the markup is generated for whatever needs it
			var fullTitle = null;

			if (this.trackName) {
				var fullTitle = '<strong>' + this.trackName + '</strong> ' + this.localizedStrings['by'] +  ' <strong>' + this.artistName + '</strong>';
			} else if (this.albumName) {
				fullTitle = '<strong>' + this.albumName + '</strong> ' + this.localizedStrings['by'] +  ' <strong>' + this.artistName + '</strong>';
			} else if (this.artistName) {
				fullTitle = '<strong>' + this.artistName + '</strong>';
			}

			$('albumname').innerHTML = fullTitle || 'the item you just requested';


			if ($('itmslaunchlink')) {
				if(fullTitle) {
					$('itmslaunchlink').innerHTML = fullTitle;
				}

				if(this.itmsUrl) {
					$('itmslaunchlink').setAttribute('href', this.itmsUrl);
				}
			}

			if (this.thumbnailUrl) {
				var albumArt = Builder.node('img', { src:this.thumbnailUrl });
				if (this.thumbnailWrapper) this.thumbnailWrapper.appendChild(albumArt);

				if (this.thumbnailWrapper2) this.thumbnailWrapper2.appendChild(albumArt.cloneNode(true));
			}
		},
		
		showInfoPanel: function () {
			// shows the information panel atop the page if we have an artist name
			// meaning this will work if weh ave a track/album/artist, album/artist, 
			// or artist alone

			// as this is now we will not display if the request indicates a celebrity
			// playlist, or otherwise.

			if((this.artistName != null) && (this.artistName != '')) {
				this.container.style.display = 'block';
			}
		}
	}
};


// -- Looking for Text ------
LookingFor = Class.create();
LookingFor.prototype = {
	trackName: '',
	albumName: '',
	artistName: '',
	thumbnailUrl: '',
	itmsUrl: '',
	localizedStrings: [],
	
	initialize: function() {
		// retrieve the URL parameters and display parsed information 
		// as appropriate
		this.localizedStrings['by'] = 'by';

		// initial DOM stuff
		this.container = $('lookingfor') || false;
		this.thumbnailWrapper = $('albumart') || false;
		this.thumbnailWrapper2 = $('albumart2') || false;

		this.populateFrom(document.location.search);
		this.promo = this.identifyPromo();
		this.render();
		this.showInfoPanel();
	},

	getLocalizedString: function(key) {
		try {
			var ret = localizedStrings[key];
			if (ret === undefined) { ret = key; }
			return ret;
		} catch(e) {}
		return key;
	},

	populateFrom: function(queryString) {
	 // populate this affiliates object from the query parameters passed in

	 // @param {string} queryString the entire query string attached to the
	 // requested URL

		if(queryString == '') {
			return;
		}
		
		var songValues = queryString.toQueryParams();
		
		//for each key/value pair set the song[key] = value
		for(var key in songValues) {
			
			//avoid creating new properties
			//if we have a property that matches that key; populate it
			if (this[key] != undefined) {
				songValues[key] = decodeURIComponent(songValues[key]).replace(/\+/g, ' ');
				this[key] = songValues[key];
			}
		}
	},

	render: function() {
		this.promo.render.call(this);
	},

	showInfoPanel: function () {
		this.promo.showInfoPanel.call(this);
	},
	
	identifyPromo: function() {
		for (var promoId in LookingForPromos) {
			if (LookingForPromos[promoId].detect.call(this)) {
				return LookingForPromos[promoId];
			}
		}
		
		return LookingForPromos.NoPromo;
		
	}
};

RegisterDownload = Class.create();
RegisterDownload.prototype = {
	recorded: false,
	
	initialize: function(selector) {
		var downloadLinks = $$(selector);
		
		for (var i=0; i<downloadLinks.length; i++) {
			downloadLinks[i].observe('click', this.recordDownload.bind(this));
		}
	},

	recordDownload: function() {
		if (this.recorded) return;
		
		var axel = Math.random()+"";
		var a = axel * 10000000000000;
			
		var dartframe = document.createElement('iframe');
			dartframe.setAttribute("src",'http://fls.doubleclick.net/activityi;src=1566767;type=itune088;cat=apple602;ord=1;num=' + a + '?');
			dartframe.setAttribute("width",1);
			dartframe.setAttribute("height",1);
			dartframe.setAttribute("frameborder",0);
		
		document.body.appendChild(dartframe);
		
		this.recorded = true;
	}
}
