/*
* Provides functionality for maintaining a single popup window per browser application instance.
* Browser compliance: PC - Safari, Firefox, IE5, IE5.5, IE6, IE7; Mac - Safari, Firefox.
*/

var popupWindow = null;
var POPUP_URL = 'Download-Manager.asp';
var POPUP_PARAMETERS = 'width=350,height=250,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=20,left=20';
var CURRENT_DOMAIN = 'http://www.umpgsongs.com';

function downloadManager(){
    popupnr(POPUP_URL,'myPop',true);
}

function isIE6(){
    if (typeof document.body.style.maxHeight == "undefined"){
        return true;
    } else {
        return false
    }
}

function updatePopup(win){
    popupWindow = win;
}

function popupnr(mylink, windowname, refocus)
{
    if (isIE6()) {
        if ((!popupWindow)||(popupWindow.closed)){
            window.open(mylink, windowname, POPUP_PARAMETERS, true);
        } else {
            popupWindow.focus();
        }
    } else {   
        var mywin, href;
        if (typeof(mylink) == 'string')
           href=mylink;
        else
           href=mylink.href;
        mywin = window.open('', windowname, POPUP_PARAMETERS);

        if (
           mywin.closed || 
           (! mywin.document.URL) || 
           (mywin.document.URL.indexOf("about") == 0)
           )
           mywin.location=href;
        else if (refocus)
           mywin.focus();
        return false;
    }
}


function updateParent(){
	if (isIE6){
		try {
			if (window.opener && !window.opener.closed){
				if (window.opener && 
					!window.opener.closed && 
					window.opener.location.href.indexOf(CURRENT_DOMAIN) > -1){
					window.opener.updatePopup(window);
				}
			}
		} catch(err) {
		
		}
	}
}

function initialisePopupUpdates(){
	setInterval('updateParent();',100);
	if (isIE6()){
		window.onunload = function(){
			if (window.opener && !window.opener.closed){
				if (window.opener.location.href.indexOf(CURRENT_DOMAIN) > -1){
				   window.opener.updatePopup(null);
				}
			}
		}
	}
}

// Add track to download queue with AJAX
function downloadTrack(Track) {
// Create AJAX request
new Ajax.Request('/NoIndex/AJAX/Download.asp',   {    
method:'post',
parameters: {Download:Track},
onSuccess: function(transport){
	//alert(Track)
	var response = transport.responseText;
	// Are we still logged in?
	if (response == 'Login') {
	alert('Sorry, your session has expired. To download more\ntracks logout and sign back in to UMPG Songs.');	
	// Have reached the download limit for this session?
	} else if (response == 'Limit') {
	alert('Sorry, you\'ve reached the download limit\nfor this session.To download more tracks \nlogout and sign back in to UMPG Songs.')
	} else {
	var downloadArray = response.split("|");
	new Effect.Pulsate('downloads', {duration:0.5,pulses:1});
	setTimeout("document.getElementById('activeCount').innerHTML = '" + downloadArray[0] + "';",250)
	setTimeout("document.getElementById('downloadCount').innerHTML = '" + downloadArray[1] + "';",250)
	}
	},
	onFailure: function(){
	alert('Sorry, there was an error and the track was not added to your download queue.')
	}
});
}

// Add multiple tracks to download queue with AJAX
function downloadTracks() {
// We need a variable for the track ID's
var tracks = ''
// Loop to see which checkboxes are checked  
var count = document.Playlist.elements.length;
for (i=0; i < count; i++) {
	// Is it a checkbox?
	if (document.Playlist.elements[i].type == 'checkbox') {
	// If the box is checked then get the value
	if (document.Playlist.elements[i].checked == 1) {
	tracks = tracks + document.Playlist.elements[i].value + ',';
	}
	}
}
// If we have any tracks then call the AJAX page
if (tracks.length != 0) {
// Get playlist name
var name = document.Playlist.PlaylistName.value
// Create AJAX request
new Ajax.Request('/NoIndex/AJAX/Download-Tracks.asp',   {    
method:'post',
parameters: {Download:tracks,PlaylistName:name},
onSuccess: function(transport){
	//alert(Track)
	var response = transport.responseText;
	//alert(response)
	// Are we still logged in?
	if (response == 'Login') {
	alert('Sorry, your session has expired. To download more\ntracks logout and sign back in to UMPG Songs.');	
	// Have reached the download limit for this session?
	} else if (response == 'Limit') {
	alert('Sorry, you\'ve reached the download limit\nfor this session.To download more tracks \nlogout and sign back in to UMPG Songs.')
	} else {
	var downloadArray = response.split("|");
	new Effect.Pulsate('downloads', {duration:0.5,pulses:1});
	setTimeout("document.getElementById('activeCount').innerHTML = '" + downloadArray[0] + "';",250)
	setTimeout("document.getElementById('downloadCount').innerHTML = '" + downloadArray[1] + "';",250)
	}
	},
	onFailure: function(){
	alert('Sorry, there was an error and the track(s) were not added to your download queue.')
	}
});
}
}
