var $dialog = $('<div></div>');	
var $purchase_uid;
var $purchase_key;

function displayVerificationMessage( credits, charge, hash, identifier )
{
	$verificationDialog = $('<div></div>')
		.html('This dialog will show every time!')
		.dialog({
			autoOpen: false,
			title: 'Purchase Credits',
			modal: true,
			resizable: false,
			show: 'drop',
			hide: 'drop',
			width: 550,
			height: 300,
			buttons: {
				"Purchase": function() {
					var identifier = $( '#mix-identity' ).val();
					receiveDownload( hash, identifier );
					displayNumCredits();
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function(event, ui) {
				displayNumCredits();
				}
			
		});
		$( '#mix-identity' ).remove();
		$verificationDialog.dialog('open');
		$verificationDialog.html('<p>This will cost you '+charge+' credit(s) out of an available '+credits+' credit(s)</p><label for="mix-identity">Mix Name</label><input type="text" name="mix-identity" id="mix-identity" class="text ui-widget-content ui-corner-all" value="'+identifier+'" />');

}	

function buyMixNonAsync(identifier, hash)
{
	if (user_uid == -1)
	{
		var loginHREF = "<a href=\"login.php?login&register&redirect="+document.URL+"\">Click here to register with your Facebook account</a>";
		
		displayErrorMessage("You must be logged in or registered to use this feature!<br /><br />Register now and get a FREE song.<br /><br />"+loginHREF, "Log In or Register");
		return;
	}
        var page = 'query.php';
        var get = {
                command: 3,
                uid: user_uid,
                key: user_key,
                sid: sid,
                hash: hash
        };

        $.get(page, get, function(data) {
                var message = eval('('+data+')');
                var stat = message["status"];
                var credits = message["credits"];
                var charge = message["charge"];
                switch (stat)
                {
                        case -1:
                                var error = message["message"];
                                alert(error);
                                break;
                        case 0:
                                receiveDownload(hash, null);
                                break;
                        case 1:
                                displayVerificationMessage( credits, charge, hash, identifier );
                                break;
                }
        });
	
}
function addMixNonAsync(identifier, hash)
{
	if (user_uid == -1)
	{
		var loginHREF = "<a href=\"login.php?login&register&redirect="+document.URL+"\">Click here to register</a>";

		displayErrorMessage("You must be logged in or registered to use this feature!<br />Register now with your Facebook account to get a FREE song.<br />"+loginHREF);
		return;
	}
	$verificationDialog = $('<div></div>')
		.html('This dialog will show every time!')
		.dialog({
			autoOpen: false,
			title: 'Save Mix',
			modal: true,
			resizable: false,
			show: 'drop',
			hide: 'drop',
			width: 550,
			height: 300,
			buttons: {
				"Save Mix": function() {
					identifier = $( '#mix-identity' ).val();
					seriouslyAddMix( identifier, hash );
					$( this ).dialog( "close" );
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function(event, ui) {
				}
			
		});
	$( '#mix-identity' ).remove();
	$verificationDialog.dialog('open');
	$verificationDialog.html('<p>Please name your mix. Naming it the same as an existing mix will overwrite that mix.</p><label for="mix-identity">Mix Name</label><input type="text" name="mix-identity" id="mix-identity" class="text ui-widget-content ui-corner-all" value="'+identifier+'" />');
}
function seriouslyAddMix(identifier, hash)
{
        var page = 'query.php';
        var get = {
		command: 200,
		uid: user_uid, // Provided globally
		key: user_key, // Provided globally
		hash: hash,
		sid: sid, // Provided globally
		identifier: identifier
        };

        $.get(page, get, function(data) {
                var message = eval('('+data+')');
                var stat = message["status"];
                switch (stat)
                {
                        case 0:
				// Saved
				getMyMixes();
				var flashObject = document.getElementById('cobaltPlayer');
				flashObject.SetMixName(identifier, hash);
                                break;
                        case 1:
				// Updated
				getMyMixes();
				var flashObject = document.getElementById('cobaltPlayer');
				flashObject.SetMixName(identifier, hash);
                                break;
                        case -1:
                                alert("failed: " + message["message"]);
                                break;
                }
        });
}
function updateMixNonAsync(hash)
{
	var likeObj = $('#like');
	likeObj.html('<fb:like href="'+like_page_url_base+'&mixkey='+hash+'" send="true" width="450" show_faces="true" font=""></fb:like>');
	FB.XFBML.parse(document.getElementById('like'), function() {
			});

}
function addMix( identifier, hash )
{
	var t = setTimeout(function(){addMixNonAsync(identifier, hash)}, 1);
}
function buyMix( identifier, hash )
{
	var t = setTimeout(function(){buyMixNonAsync(identifier, hash)}, 1);
}
function updateMix( hash )
{
	var t = setTimeout(function(){updateMixNonAsync(hash)}, 1);
}
function getDownload(hash)
{
        var page = 'query.php';
        var get = {
                command: 3,
                uid: user_uid,
                key: user_key,
                sid: sid,
                hash: hash
        };

        $.get(page, get, function(data) {
                var message = eval('('+data+')');
                var stat = message["status"];
                var credits = message["credits"];
                var charge = message["charge"];
                switch (stat)
                {
                        case -1:
                                var error = message["message"];
                                alert(error);
                                break;
                        case 0:
                                receiveDownload(hash, null);
                                break;
                        case 1:
                                displayVerificationMessage( credits, charge, hash );
                                break;
                }
        });
}
function receiveDownload(hash, identifier)
{
        var page = 'download.php';

        $('#slInformationArea').removeClass('slInfoModeNull');
        $('#slInformationArea').removeClass('slInfoModeDownload');
        $('#slInformationArea').addClass('slInfoModeGenMix');
        var get = {
		sid: sid, // Provided globally
                uid: user_uid, // Provided globally
                key: user_key, // Provided globally
                mixdata: hash,
                identifier: identifier
        };
        $.get(page, get, function(data) {
                var message = eval('('+data+')');
                var stat = message["status"];
                switch (stat)
                {
                        case -1:
                                $('#slInformationArea').html('');
                                $('#slInformationArea').removeClass('slInfoModeGenMix');
                                $('#slInformationArea').removeClass('slInfoModeDownload');
                                $('#slInformationArea').addClass('slInfoModeNull');
                                alert(message["message"]);
                                break;
                        case 0:
                                $('#slInformationArea').addClass('slInfoModeDownload');
                                url = message["url"];
                                $('#slInformationArea').html('<a href="'+url+'"><div style="width: 200px; height: 57px;"></div></a>');
				getMyPurchases();
                                displayNumCredits();
                                break;
                }
        });
}
function displayNumCredits()
{
        var page = 'query.php';
        var get = {
                command: 0,
                uid: user_uid, // Provided globally
                key: user_key // Provided globally
        };

        $.get(page, get, function(data) {
                var message = eval('('+data+')');
                var stat = message["status"];
                var credits = message["credits"];
                switch(stat)
                {
                        case -1:
                                $('#creditText').text(credits);
                                break;
                        case 0:
                                $('#creditText').text(credits);
                                break;
                }
        });
}
function purchaseCredit()
{
        openPurchaseDialog(user_uid, user_key);
}
function openPurchaseDialog(uid, key)
{
	$purchase_uid = uid;
	$purchase_key = key;
	$dialog = $('<div></div>')
		.html('This dialog will show every time!')
		.dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			show: 'drop',
			hide: 'drop',
			width: 700,
			dialogClass: 'alert',
			close: function(event, ui) {
				displayNumCredits();
				},
			height: 550
		});
	$dialog.dialog('open');
	$dialog.html('<p class="purchaseCredits">Purchase Credits</p><p class="purchaseCreditsLite">Credits can be purchased via PayPal.  Credits work out to 100 credits per song.</p><table id="creditsTable" width="100%"><tr><td><img src="http://c3414997.r97.cf0.rackcdn.com/buy1credits.png" onClick="purchaseCredits(100);"></td><td><img src="http://c3414997.r97.cf0.rackcdn.com/buy5credits.png" onClick="purchaseCredits(500);"></td></tr><td><img src="http://c3414997.r97.cf0.rackcdn.com/buy10credits.png" onClick="purchaseCredits(1000);"></td><td><img src="http://c3414997.r97.cf0.rackcdn.com/buy20credits.png" onClick="purchaseCredits(2000);"></td><tr></tr>');
}
function purchaseCredits( credits )
{
	$dialog.html('Transferring to PayPal.  Please close this dialog after your transaction has been completed to verify that you have obtained your credits.  Note that a pop-up window is required to transition.  Please allow this pop-up window if your browser has blocked it.  If you allow popups from MixMeIn.com you may need to close this dialog and click "Add Credits" again to get to PayPal correctly.');
	$dialog.dialog( { close: function(event, ui) {
		displayNumCredits();
		} } );
	var page = 'query.php';
	var get = {
		command: 101,
		credits: credits,
		uid: $purchase_uid,
		key: $purchase_key
	};
	
	$.get(page, get, function(data) {
		var message = eval('('+data+')');
		var stat = message["status"];
		var word = message["message"];
		switch (stat)
		{
			case -1:
				alert(word);
				break;
			case 0:
				if (word == "")
				{
					alert("Unable to retrieve PayPal URL");
				}
				else
				{
					window.open(word);
				}
				break;
		}
		
	});
	
}

