var partner_id = 0;
var need_captcha = false;
function base64_decode( data ) {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    do {
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64)      enc += String.fromCharCode(o1);
        else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        else               enc += String.fromCharCode(o1, o2, o3);
    } while (i < data.length);
 
    return enc;
}

captcha = {
	url: '/default/index/captcha/',
	
	trigger: function() {
		$("#captcha form").submit(function(){
			captcha.check();
			return false;
		});
	},
	
	show: function() {
		if (need_captcha) {
			Boxy.load(this.url, 
				{title: 'Input letters from picture', 
				modal:true,
				afterShow: function(){
					captcha.trigger();
				}});
		} else {
			document.location = site.url + 'users/registration/?partner='+partner_id;
		}
	},
	
	check: function(callback) {
		text = $("#captcha-input").val();
		id = $("#captcha-id").val();
		$.post(captcha.url, {'captcha[id]': id, 'captcha[input]': text}, function(data){
			if (data=='["valid"]') {
				document.location = site.url + 'users/registration/?partner='+partner_id;
			} else {
				$("#captcha").html(data);
				captcha.trigger();
			}
		});
	}
}

site = {
	url: '',
	setUrl: function(url) {
		this.url = base64_decode(url);
	}
}

player = {
	object: null,
	current_song: 0,
	preview_url: '/preview/',
	
	init: function(obj) { 
		this.object = obj;
	},
	
	onInit: function() {
	},
	
	onUpdate: function() {
	},
	
	playSong: function(url) {
		if (url!=this.current_song) {
			this.current_song = url;
			this.object.SetVariable('method:setUrl', site.url + this.preview_url + url);
			this.object.SetVariable('method:play',  "");
			this.object.SetVariable('enabled',  "true");
		}
	},
	
	stopSong: function() {
		if (this.current_song==0) return;
		this.url = '';
		this.object.SetVariable("method:stop", "");
	}
}

songs = {
	action:'',
	setAction: function(action) {
		this.action = base64_decode(action);
	},
	
	init: function() {
		$(".preview").click(function(){
			if ($(this).hasClass('playing')) {
				player.stopSong();
				$(this).removeClass('playing');
			} else {
				id = $(this).attr('song_id');
				url = id.substr(0,3)+'/'+id.substr(4,7)+'/'+id+'.mp3';
				player.playSong(url);
				$(".preview").removeClass('playing');
				$(this).addClass('playing');
			}
			return false;
		});
		$('.download_songs').click(function(){
			captcha.show();
			return false;
		});
	}
}

albums = {
	action: '',
	setAction: function(action) {
		this.action = base64_decode(action);
		alert(this.action);
	},
	
	init: function() {
		$('.download_albums').click(function(){
			captcha.show();
			return false;
		});
		$('.download_album').click(function(){
			captcha.show();
			return false;
		});
	}
}

$(document).ready(function(){
	player.init(document.getElementById('player'));
	songs.init();
	albums.init();
	$('th input:checkbox').click(function(){
		var checked = $(this).attr('checked');
		if (checked) {
			$('table.data input:checkbox').attr('checked', 'checked');
		} else {
			$('table.data input:checkbox').removeAttr('checked');
		}
	});
});