// JavaScript Document

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

var uses_downloader=true;

var trackButtonReplacement='Downloaded';
var catalogButtonReplacement='Downloaded';

var hasSongbird = false;
if ( typeof (songbird) != "undefined" ) {
        hasSongbird = true;
        // or just do your songbird stuff here.
		songbird.setSiteScope("", "/");
		var library = songbird.siteLibrary;
		var myMediaList = songbird.siteLibrary.createSimpleMediaList("mylist");
}

function updateTourList(){
		var band_id=$("#tour_select_menu").val();
		 
		$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=updateTourList&band_id="+band_id,
			   success: function(html){
				
				$("#on_tour_holder").html(html);
				 $('table.on_tour_table tbody tr:even').addClass('alt');
			   }
		});
		
	
}
$(document).ready(function(){
   // triggered when doc model is avail
	
	//clear search box when clicked
	$("input[name=search_query]").click(function () { 
	  $(this).val(''); 
	});
	
	
	//forward the page to the search page when shop by artist menu changes
	 $("#shop_by_artist_menu").change(function () { 
		//alert($("#shop_by_artist_menu").val());
	 		
		window.location="http://www.mergerecords.com/store/"+$("#shop_by_artist_menu").val();
	});
	 
	
	//make album cover links spawn popup
	 $(".albumCoverLink").click(function () { 
		//this.getAttribute("href")
		window.open(this.href,'popup','width=465,height=465');
		return false;
	 
	 
	 });
	
	//make album cover links spawn popup
	 $(".moreinfo").click(function () { 
		//this.getAttribute("href")
		window.open(this.href,'popup','width=465,height=465,scrollbars=1');
		return false;
	 
	 
	 });
	 
	$('.view_tracks').click(function() {
		$(this).parent().parent().parent().next('.downloads_tracks_row_hidden').toggle();
		return false;
	});
	
	$('#download_options').toggle(0);
	
	$('#manager_download_btn').click(function(){
		displayDownloadOptions();
	});
		
		
});


$(document).ready(function(){
	$('.tracks_table tr:even').addClass('alt');
	//updateTourList();
});


function useDM(){
	
		$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=useDM",
			   success: function(html){
				
				
				 
				 if(html=='true' || html==true){
					
					uses_downloader=true;
					
				 }else if(html=='false' || html==false){
					 
					 uses_downloader=false;
					 
					 
				 }
				 
			   }
		});
		
}
useDM();
function downloadTrack(trackID,catalog_id,sid,d_type) {
	
	
	if (hasSongbird) {
		
		var myTrack = songbird.siteLibrary.createMediaItem('https://www.mergerecords.com/store/filedownload.php?track_id='+trackID);
		//myTrack.setProperty("http://songbirdnest.com/data/1.0#trackName", "Downloading...");
		myMediaList.add(myTrack);
		
		songbird.downloadItem(myTrack);		
		
	}else{
		var tempIFrame = document.createElement('iframe');
		tempIFrame.width=0;
		tempIFrame.height=0;
		
		if(OSName=='Windows' || uses_downloader==false){
			//alert(uses_downloader);
			tempIFrame.src='filedownload.php?track_id='+trackID+'&sid='+sid+'&d_type='+d_type;
		}else{
			
			tempIFrame.src='dxz://www.mergerecords.com/store/filedownload.php?track_id='+trackID+'&sid='+sid+'&d_type='+d_type;
		}
		
		
		tempIFrame.style.visibility="hidden";
		document.getElementById('iframeholder').appendChild(tempIFrame);
		updateTrackLink(trackID,d_type);
		updateCatalogLink(catalog_id);
	
	}

}


function updateTrackLink(trackID,d_type){
	
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=countTrackTimesDownloaded&track_id="+trackID,
			   success: function(html){
						
						if(html==0){
							
						
							
								$("#downloadTrack_"+d_type+trackID).html(trackButtonReplacement);	
								$("#downloadTrack_"+d_type+trackID).parent().removeClass('downloads_tracks_row_altstyle');
								$("#downloadTrack_"+d_type+trackID).parent().removeClass('downloads_tracks_row');
								$("#downloadTrack_"+d_type+trackID).parent().addClass('downloads_tracks_row_once');
							
							}else if(html>0){
							
								$("#downloadTrack_"+d_type+trackID).html(trackButtonReplacement);	
								$("#downloadTrack_"+d_type+trackID).parent().removeClass('downloads_tracks_row_altstyle');
								$("#downloadTrack_"+d_type+trackID).parent().removeClass('downloads_tracks_row');
								$("#downloadTrack_"+d_type+trackID).parent().removeClass('downloads_tracks_row_once');
								$("#downloadTrack_"+d_type+trackID).parent().addClass('downloads_tracks_row_used');	
															
							}
							
						
			   }
		});
	
}


function updateCatalogLink(catalog_id){
	

	
	
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=countCatalogTimesDownloaded&catalog_id="+catalog_id,
			   success: function(html){
						
						if(html>0){
						
							$("#album_download_status_"+catalog_id).html(trackButtonReplacement);	
							$("#download_catalog_"+catalog_id).removeClass('downloads_top_row');
						    $("#download_catalog_"+catalog_id).addClass('downloads_top_row_used');	
							
						}else{
						
						
						
							
						}
			   }
		});
	
	
}


function downloadAll(catalog_id,sid,option_id) {
	
	
	var tempIFrame = document.createElement('iframe');
	tempIFrame.width=0;
    tempIFrame.height=0;
	if(OSName=='Windows'){
	tempIFrame.src='filedownload.php?catalog_id='+catalog_id+'&sid='+sid+'&option_id='+option_id;
	}else{
	tempIFrame.src='dxz://www.mergerecords.com/store/filedownload.php?catalog_id='+catalog_id+'&sid='+sid+'&option_id='+option_id;
	}
	tempIFrame.style.visibility="hidden";
	updateCatalogLink(catalog_id);
    document.getElementById('iframeholder').appendChild(tempIFrame);

	

}

function downloadLatestPurchase(sid){
	
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=turnDMOn",
			   success: function(html){
			
			   }
		});
	var tempIFrame = document.createElement('iframe');
	tempIFrame.style.visibility="hidden";
	
	tempIFrame.width=0;
    tempIFrame.height=0;
	if(OSName=='Windows'){
	tempIFrame.src='filedownload.php?recient_purchase=true';
	}else{
	tempIFrame.src='dxz://www.mergerecords.com/store/filedownload.php?recient_purchase=true&sid='+sid;	
	}
	tempIFrame.style.visibility="hidden";
    document.getElementById('iframeholder').appendChild(tempIFrame);
	
	$("#downloading_message").show();
	$("#mainbody_holder").hide();
	
		
	
	
}



function downloadMacintosh(){
	
	
	var tempIFrame = document.createElement('iframe');
	tempIFrame.width=0;
    tempIFrame.height=0;
	tempIFrame.src='downloader/MergeDownloader.pkg.zip';
	tempIFrame.style.visibility="hidden";
    document.getElementById('iframeholder').appendChild(tempIFrame);
	
	
}

function downloadPc(){
	
	
	var tempIFrame = document.createElement('iframe');
	tempIFrame.width=0;
    tempIFrame.height=0;
	tempIFrame.src='downloader/MergeDownloader_Installer.exe';
	tempIFrame.style.visibility="hidden";
    document.getElementById('iframeholder').appendChild(tempIFrame);
	
	
}

function goToHistoryOrNot()
{
var r=confirm("This release is now available in your download history. Would you like to go there now?");
if (r==true)
  {
  window.location.href='store_myaccount.php?section=purchase_history';
  }
else
  {
  
  }
}

function addToHistory(catalog_id,type_id,targetx,option_name,sid){
	
	
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=addToHistory&catalog_id="+catalog_id+"&type="+type_id,
			   success: function(html){
				
				if(html=='true' || html=='1'){
					if(uses_downloader==true){
					$(targetx).parent().html('<input class="addtocart" type="submit" onclick="return false;" value="Downloading" name="Submit" disabled="disabled">');
					downloadAll(catalog_id,sid);
					}else{
						
					$(targetx).parent().html('<input class="addtocart" type="submit" onclick="window.location.href=\'store_myaccount.php?section=purchase_history\';return false;" value="Go To Downloads" name="Submit">');
					goToHistoryOrNot();
					}
					
				}else{
					
					$(targetx).html('ERROR!');
					
					
				}
				
				//$(targetx).animate({ color: "orange" }, 1000);
	   
				updateCart();
				 
			   }
		});
	
	
	
}


//
//


function addToCart(catalog_id,type_id,targetx,option_name){
	
	
	$.ajax({
			   type: "POST",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=addToCart&n_add_to_cart=true&catalog_id="+catalog_id+"&type="+type_id,
			   success: function(html){
				
			  	$(targetx).html('<a href="#" class="menu_item added" onclick="return false;"><span class=\"menu_item_format\">'+option_name+'</span><span class="menu_item_addtocart">IN CART</span></a>');
			  	$(targetx).animate({ color: "white" }, 1000);
		
				updateCart();
				 
			   }
		});
	
	
	
}
//
//
//
function addToCartDetail(catalog_id,type_id,targetx,option_name){
	
				$(targetx).animate({ color: "white" }, 100);
	
	$.ajax({
			   type: "POST",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=addToCart&n_add_to_cart=true&catalog_id="+catalog_id+"&type="+type_id,
			   success: function(html){
				  
				$(targetx).parent().html('<input name=\"Submit\" class=\"addtocart_added\" type=\"submit\" value=\"IN CART\" disabled=\"disabled\" />');
				
	   
				updateCart();
				 
			   }
		});
	
	
	
}
//
//
//
function addToCartTrack(catalog_id,type_id,targetx,option_name){
	
				$(targetx).animate({ backgroundColor: "white" }, 100);
	
	$.ajax({
			   type: "POST",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=addToCart&n_add_to_cart=true&catalog_id="+catalog_id+"&type="+type_id,
			   success: function(html){
				  
				$(targetx).parent().html('<input name=\"Submit\" class=\"addtocart_added\" type=\"submit\" value=\"IN CART\" disabled=\"disabled\" />');
				
	   
				updateCart();
				 
			   }
		});
	
	
	
}
//
//
//
function addToCartSearch(catalog_id,type_id,targetx,option_name){
	
				$(targetx).animate({ backgroundColor: "white" }, 100);
	
	$.ajax({
			   type: "POST",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=addToCart&n_add_to_cart=true&catalog_id="+catalog_id+"&type="+type_id,
			   success: function(html){
				  
				$(targetx).parent().html('<input name=\"Submit\" class=\"addtocart_added\" type=\"submit\" value=\"IN CART\" disabled=\"disabled\"  />');
				
	   
				updateCart();
				 
			   }
		});
	
	
	
}
//
//
//
function updateCart(){
	
	
		$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=updateCart",
			   success: function(html){
				
				$("#header_cart").html(html);
				 $("a.items_in_cart").animate({ color : "white" }, 50)
				 .animate({ color: "#FFC466" }, 500);
			   }
		});
	
}
	

function displayDownloadOptions(){
	
	$('#download_options').toggle(100);
	
}



function turnDmOn(){
	$('#dm_status_message').html('Please Wait...');
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=turnDMOn",
			   success: function(html){
					$('#dm_status_message').html('You are using the Merge Downloader.');
					$("#offBackground").animate({ color: "#FFFFFF" }, 100);
					$("#onBackground").animate({ color: "#000000" }, 100);
					$("#offBackground").animate({ backgroundColor: "#362d25" }, 100);
					$("#onBackground").animate({ backgroundColor: "#FF9900" }, 100);
			   }
		});	
	
	
}

function turnDmOff(){
	$('#dm_status_message').html('Please Wait...');
	$.ajax({
			   type: "GET",
			   url: "/store/includes/ajax_functions.php",
			   data: "func_name=turnDMOff",
			   success: function(html){
					$('#dm_status_message').html('You are NOT using the Merge Downloader.');
					$("#offBackground").animate({ color: "#000000" }, 100);
					$("#onBackground").animate({ color: "#FFFFFF" }, 100);
					$("#offBackground").animate({ backgroundColor: "#FF9900" }, 100);
					$("#onBackground").animate({ backgroundColor: "#362d25" }, 100);
			   }
		});	
	
	
}

