var HTTPRequest = function (){
	if (window.XMLHttpRequest){
		this.req = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try{
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){}
		}
	}
	return this.req;
}

function get_track_formats( track_id ){
	var http = new HTTPRequest();
	var poststr = "action=gettrackformats&trackid=" + encodeURI(track_id);

	http.onreadystatechange = function(){
		if( http.readyState == 4 ){
			if( http.status == 200 ){
				var json = eval('(' + http.responseText + ')');
				if(!json.status){
					//Success
					dl_container = document.getElementById('dl_t'+track_id);
					
					if( document.getElementById('FormatBox') )
						document.getElementById('FormatBox').parentNode.removeChild(document.getElementById('FormatBox'));
						
					var format_box = document.createElement('div');
						format_box.id = 'FormatBox';
						
					
					for(var i=0; i<json.records.length; i++){
						//format_box.innerHTML += '<div><div class="formatName"><a href="#" onclick="add_track_to_cart('+json.records[i].fileId+'); return false;" class="blueLink">' + json.records[i].format + '</a></div><div class="price">'+format_price(json.records[i].price)+'</div></div>';
						format_box.innerHTML += '<div><div class="formatName"><a href="#" id="add_track_link" onclick="add_track_to_cart('+track_id+','+json.records[i].formatId+'); return false;" class="blueLink">' + json.records[i].format + '</a></div><div class="price">'+format_price(json.records[i].price)+'</div><div class="loader"><img id="add_track_loader" style="display: none;" src="/images/store/arrow_loader.gif" /></div></div>';
					}
					
					dl_container.appendChild(format_box);
				}else if(json.status == 2){
					//Not Logged in
					parent.setContentFrame('content/reg_catch.php');
				}else{
					//Other Error
					alert(json.message);
				}
			}
		}
	}

	http.open('POST', '/store/api/ajax.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", poststr.length);
	http.setRequestHeader("Connection", "close");
	http.send(poststr);
}


function get_album_formats( album_id ){
	var http = new HTTPRequest();
	var poststr = "action=getalbumformats&albumid=" + encodeURI(album_id);

	http.onreadystatechange = function(){
		if( http.readyState == 4 ){
			if( http.status == 200 ){
				var json = eval('(' + http.responseText + ')');
				if(!json.status){
					//Success
					dl_container = document.getElementById('dl_a'+album_id);
					
					if( document.getElementById('FormatBox') )
						document.getElementById('FormatBox').parentNode.removeChild(document.getElementById('FormatBox'));
						
					var format_box = document.createElement('div');
						format_box.id = 'FormatBox';
						
					for(var i=0; i<json.records.length; i++)
						format_box.innerHTML += '<div><div class="formatName"><a href="#" id="add_track_link" onclick="add_album_to_cart('+album_id+','+json.records[i].formatId+'); return false;" class="blueLink">' + json.records[i].format + '</a></div><div class="price">'+format_price(json.records[i].price)+'</div><div class="loader"><img id="add_track_loader" style="display: none;" src="/images/store/arrow_loader.gif" /></div></div>';
					
					dl_container.appendChild(format_box);
				}else if(json.status == 2){
					//Not Logged in
					parent.setContentFrame('content/reg_catch.php');
				}else{
					//Other Error
					alert(json.message);
				}
			}
		}
	}

	http.open('POST', '/store/api/ajax.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", poststr.length);
	http.setRequestHeader("Connection", "close");
	http.send(poststr);
}



function add_track_to_cart( file_id, format_id ){
	var http = new HTTPRequest();
	var poststr = "action=addtrack&fileid=" + encodeURI(file_id) + "&formatid=" + encodeURI(format_id);
	
	http.onreadystatechange = function(){
		if( http.readyState == 4 ){
			if( http.status == 200 ){
				var json = eval('(' + http.responseText + ')');
				if(!json.status){
					//Success
					disable_download_button(file_id, 't');
					parent.subheader.showCart();
				}else if(json.status == 2){
					//Not Logged in
					var forwardUrl = '/store/viewcart.php?add=1&trackid=' + file_id + '&formatid=' + format_id;
					parent.setContentFrame('content/reg_catch.php?forwardUrl=' + escape(forwardUrl));
				}else{
					//Other Error
					alert(json.message);
				}
			}
		}
	}
	
	Effect.Appear('add_track_loader');
	document.getElementById('add_track_link').onclick = function(){ return false; }

	http.open('POST', '/store/api/ajax.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", poststr.length);
	http.setRequestHeader("Connection", "close");
	http.send(poststr);
}


function add_album_to_cart( file_id, format_id ){
	var http = new HTTPRequest();
	var poststr = "action=addalbum&fileid=" + encodeURI(file_id) + "&formatid=" + encodeURI(format_id);
	
	http.onreadystatechange = function(){
		if( http.readyState == 4 ){
			if( http.status == 200 ){
				var json = eval('(' + http.responseText + ')');
				if(!json.status){
					//Success
					disable_download_button(file_id, 'a');
					disable_album_downloads(file_id);
					parent.subheader.showCart();
				}else if(json.status == 2){
					//Not Logged in
					parent.setContentFrame('content/reg_catch.php');
				}else{
					//Other Error
					alert(json.message);
				}
			}
		}
	}
	
	Effect.Appear('add_track_loader');
	document.getElementById('add_track_link').onclick = function(){ return false; }

	http.open('POST', '/store/api/ajax.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", poststr.length);
	http.setRequestHeader("Connection", "close");
	http.send(poststr);
}


function disable_download_button( id, type ){
	document.getElementById('FormatBox').innerHTML = 'Added to cart!';
	new Effect.Highlight('FormatBox',{startcolor:'#333333',endcolor:'#FFFFFF',restorecolor:'#333333'});
	if( type == 'a' )
		var t = setTimeout(function(){ Effect.BlindUp('FormatBox',{'afterFinish':function(){document.getElementById('dl_' + type + id).innerHTML = "<img src='/images/store/buy_album_grey.gif' alt='Added to Cart' title='Added to Cart' />";}}); }, 1500);
	else
		var t = setTimeout(function(){ Effect.BlindUp('FormatBox',{'afterFinish':function(){document.getElementById('dl_' + type + id).innerHTML = "<img src='/images/store/mp3_grey.gif' alt='Added to Cart' title='Added to Cart' />";}}); }, 1500);
}

function disable_album_downloads(albumId){
	var disabledImage = "<img src='/images/store/mp3_grey.gif' alt='Added to Cart' title='Added to Cart' />";
	var tracks = document.getElementsByName('trackAddBtns');
	
	for(var i = 0; i < tracks.length; i++){
		var currId = tracks[i].id;
		if( currId.match('r'+albumId) ){
			var trackId = String(tracks[i].id.match(/t[0-9]+(?=\-)/)).replace('t','');

			if( document.getElementById('dl_t' + trackId) && document.getElementById('dl_t' + trackId).innerHTML)
				document.getElementById('dl_t' + trackId).innerHTML = disabledImage;
		}
	}
		
}

function format_price( pennies ){
	return '$' + (parseFloat(pennies) / 100);
}

function show_download_link(){
	parent.document.getElementById('youHaveDownloads').style.display = 'inline';
}