	$(function() {
		
		$('#autocomplete').attr('autocomplete', 'off');
	
		$('#autocomplete').blur(function() {
	 		if($(this).attr('value') == '')
				$(this).attr('value', 'Music Search');
		});
	
		$('#autocomplete').focus(function() {
 			if($(this).attr('value') == 'Music Search')
				$(this).attr('value', '');
		});
	
		$('.btn').hover(function() {
			$(this).addClass('btn-hover')
		}, function() {
			$(this).removeClass('btn-hover')
		});
		
		$("a[name='album']").click(function() {
			$(this).toggleClass('open-lnk');
			if($(this).hasClass('open-lnk'))
			{
				$(this).parent().parent().next().show();
			}
			else
				$(this).parent().parent().next().hide();
			return false;
		});
		$(":checkbox[name='tids[]']").click(function() {
			var pr = $(this).parent().parent().parent();
			if($('input:checked', pr).length == $('input:checkbox', pr).length)
				$(':checkbox[name="trch_all"]', pr.parent().parent()).attr('checked', 'checked');
			else
				$(':checkbox[name="trch_all"]', pr.parent().parent()).attr('checked', '');
		});
		
		$(":checkbox[name='trch_all']").click(function() {
			var pr = $(this).parent().parent().parent();
			$(":checkbox[name='tids[]']", pr).attr('checked',  $(this).attr('checked'));
			
		});
		$("#trch_all").click(function() {
			var pr = $(this).parent().parent().parent();
			$('input:checkbox', pr).attr('checked',  $(this).attr('checked'));
			calc();
			
		});
		$(":checkbox[name='t_ids[]']").click(function() {
			var pr = $(this).parent().parent().parent();
			if($('input:checked', pr).length == $('input:checkbox', pr).length)
				$("#trch_all").attr('checked', 'checked');
			else
				$("#trch_all").attr('checked', '');
			calc();
		});
		
		$("a[name='cart_act']").click(function() {
			var act = $(this).attr("href");
			var pr = $(this).parent().parent().parent().parent();
			$("input[name='oper']", pr).attr('value', act);
			$("form[name='c_form']", pr).submit();
			return false;
		});
		
		$("a[name='cart_act']").click(function() {
			var act = $(this).attr("href");
			var pr = $(this).parent().parent().parent().parent();
			$("input[name='oper']", pr).attr('value', act);
			$("form[name='c_form']", pr).submit();
			return false;
		});
		
		$("a[name='dld_act']").click(function() {
			var pr = $(this).parent().parent().parent().parent();
			if(window.confirm('Are you sure that you want to delete selected album? All purchased music can be re-downloaded absolutely free of charge! In case you delete it right now and it won\'t download correctly you will have to re-purchase it.'))
				$("form[name='d_form']", pr).submit();
			return false;
		});
		
		$("#ch-1").click(function() {
			if(!$(this).hasClass('hr'))
			{
				$('#ch-2').removeClass('hr');
				$(this).addClass('hr');
				$('#newalbums').show();
				$('#bestsellers').hide();
				$('#ch_link').attr("href", $(this).attr("href"));
			}
			return false;
		});
		
		$("#ch-2").click(function() {
			if(!$(this).hasClass('hr'))
			{
				$('#ch-1').removeClass('hr');
				$(this).addClass('hr');
				$('#newalbums').hide();
				$('#bestsellers').show();
				$('#ch_link').attr("href", $(this).attr("href"));
			}
			return false;
		});
		
		$("#cht-1").click(function() {
			if(!$(this).hasClass('hr'))
			{
				$('#cht-2').removeClass('hr');
				$(this).addClass('hr');
				$('#chartsalbums').show();
				$('#chartssongs').hide();
			}
			return false;
		});
		
		$("#cht-2").click(function() {
			if(!$(this).hasClass('hr'))
			{
				$('#cht-1').removeClass('hr');
				$(this).addClass('hr');
				$('#chartsalbums').hide();
				$('#chartssongs').show();
			}
			return false;
		});
		
		$(".buy-album").click(function() {
			var pr = $(this).parent().parent().parent().next();
			$('input:checkbox', pr).attr('checked', 'checked');
			$("input[name='oper']", pr).attr('value', 'buy');
			$("form[name='c_form']", pr).submit();
			return false;
		});
		/*
		$(".download-album").click(function() {
			var pr = $(this).parent().parent().parent().next();
			$('input:checkbox', pr).attr('checked', 'checked');
			$("form[name='d_form']", pr).attr("action", '/download.html');
			$("form[name='d_form']", pr).submit();
			return false;
		});
		*/
		$("a[name='ico_check']").click(function() {
			var pr = $(this).parent().parent().parent();
			$('input:checkbox', pr).attr('checked', 'checked');
			if($(":checkbox[name='trch_all']", pr.parent().next()).attr('checked'))
			{
				$(":checkbox[name='trch_all']", pr.parent().next()).attr('checked', '');
				$('input:checkbox', pr).attr('checked', '');
			}
			else
			{
				$(":checkbox[name='trch_all']", pr.parent().next()).attr('checked', 'checked');
				$('input:checkbox', pr).attr('checked', 'checked');
			}
			
			return false;
		});
		$("a[name='ico_check_r']").click(function() {
			var pr = $(this).parent().parent().parent();
			if($("#trch_all").attr('checked'))
			{
				$("#trch_all").attr('checked', '');
				$('input:checkbox', pr).attr('checked', '');
			}
			else
			{
				$("#trch_all").attr('checked', 'checked');
				$('input:checkbox', pr).attr('checked', 'checked');
			}
			calc();
			return false;
		});
		
		$("#r_addtocart").click(function() {
			var pr = $(this).parent().parent().parent();
			if($('input:checked', pr).length == 0)
			{
				$("#r_addtocart", pr).attr('name', 'add'); 
				document.getElementById('trch_all').click();
			}
			$("#rform", pr).submit();
			return false;
		});
		
	});

//change pass field
function pass_ch(l)
{
	if(l == 'p')
	{
		document.getElementById('pass_ch_t').style.display = 'none';
		document.getElementById('pass_ch_p').style.display = 'block';
		document.getElementById('pass_ch_p').focus();
	}
	else
	{
		document.getElementById('pass_ch_t').style.display = 'block';
		document.getElementById('pass_ch_p').style.display = 'none';
	}
}
//-------------------------------------
var ajaxObjects = new Array();
function cart(type, id)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = '/ajax-cart.php';
	ajaxObjects[ajaxIndex].setVar('type', type);
	ajaxObjects[ajaxIndex].setVar('id', id);
	ajaxObjects[ajaxIndex].onCompletion = function(){ change_content(ajaxIndex, type, id); };
	ajaxObjects[ajaxIndex].runAJAX();
}

function change_content(ajaxIndex, type, id)
{
	var r = ajaxObjects[ajaxIndex].response.split('|||');
	if(r[0] == 'ok')
	{
		if(type == '0')
		{
			document.getElementById('t' + id).innerHTML = '<a href="#" class="buy" onclick="cart(\'1\', '+ id +'); return false;">Buy now</a><br />Added!';
		}
		else if(type == '1')
		{
			document.getElementById('t' + id).innerHTML = '<span class="download"><a href="'+ r[5] +'">Download</a></span>';
			if(document.getElementById('t_rn' + id))
				document.getElementById('t_rn' + id).innerHTML = '<a href="'+ r[6] +'?create"><img src="/img/ringtone.png" width="26" height="16" alt="Make a ringtone!" title="Make a ringtone!" /></a>';
			if(document.getElementById('t_cb' + id))
				document.getElementById('t_cb' + id).innerHTML = '';
		}
		else if(type == '2')
		{
			document.getElementById('r' + id).innerHTML = '<a href="#" class="buy" onclick="cart(\'3\', '+ id +'); return false;">Buy now</a><br />Added!';
		}
		else if(type == '24')
		{
			document.getElementById('r' + id).innerHTML = '<a href="#" class="buy" onclick="cart(\'31\', '+ id +'); return false;">Buy now</a><br />Added!';
		}
		else if(type == '3')
		{
			if(r[5] && r[5] == 'purchased')
			{
				document.getElementById('r' + id).innerHTML = '<span class="download"><a href="/download/'+ id +'.zip">Download</a></span>';
			}
		}
		else if(type == '31')
		{
			if(r[5] && r[5] == 'purchased')
				document.getElementById('r' + id).innerHTML = '<span class="download"><a href="/download/'+ id +'.zip" class="buy">Download</a></span>';
		}
		else if(type == '22')
		{
			document.getElementById('r' + id).innerHTML = 'Added!';
		}
		else if(type == '101' || type == '23')
		{
			document.getElementById('c' + id).innerHTML = 'Added!';
		}
		else if(type == '81')
		{
			document.getElementById('a' + id).innerHTML = '<a href="#" onclick="cart(\'82\', '+ id +'); return false;"><i></i>Remove bookmarks</a>';
		}
		else if(type == '82')
		{
			document.getElementById('a' + id).innerHTML = '<a href="#" onclick="cart(\'81\', '+ id +'); return false;"><i></i>Add to bookmarks</a>';
		}
		else if(type == '91')
		{
			document.getElementById('f' + id).innerHTML = '<a href="#" onclick="cart(\'92\', '+ id +'); return false;" title="Remove bookmarks"><i class="ico-clock"></i></a>';
		}
		else if(type == '93')
		{
			document.getElementById('fa' + id).innerHTML = '<a href="#" onclick="cart(\'94\', '+ id +'); return false;">Remove bookmarks</a>';
		}
		else if(type == '92')
		{
			document.getElementById('f' + id).innerHTML = '<a href="#" onclick="cart(\'91\', '+ id +'); return false;" title="The album will become available soon. Add to bookmarks."><i class="ico-clock"></i></a>';
		}
		else if(type == '94')
		{
			document.getElementById('fa' + id).innerHTML = '<a href="#" onclick="cart(\'93\', '+ id +'); return false;">Add to bookmarks</a>';
		}
		
		document.getElementById('usr_balance').innerHTML = r[1];
		document.getElementById('usr_tracks').innerHTML = r[2];
		document.getElementById('usr_releases').innerHTML = r[3];
		document.getElementById('usr_cart_price').innerHTML = r[4];
	}
	//else
		//alert(ajaxObjects[ajaxIndex].response);
	ajaxObjects[ajaxIndex] = false;
}

function checkr(ch)
{
	var ts = document.getElementsByName('tids[]');
	if(ch.id == 'selall')
	{
		for (k = 0; k < ts.length; k++)
		{
			ts[k].checked = ch.checked;
		}
	}
	else
	{
		if(!ch.checked)
		{
			document.getElementById('selall').checked = false;
		}
		else
		{
			var all_ch = true;
			ts = document.getElementsByName('tids[]');
			for (k = 0; k < ts.length; k++)
			{
				if (!ts[k].checked)
				{
					all_ch = false;
					break;
				}
			}
			document.getElementById('selall').checked = all_ch;
		}
	}
	//calc();
	return true;		
}

function calc()
{
	var n_ch = 0;
	var ch_dr = 0;
	var ch_sz = 0;
	var price = 0;
	ts = document.getElementsByName('t_ids[]');
	for (k = 0; k < ts.length; k++)
	{
		if (ts[k].checked)
		{
			ch_sz += sz[ts[k].value];
			ch_dr += dr[ts[k].value];
			n_ch++;
		}
	}

	if(n_ch == ts.length && n_ch == total)
		price = Math.round(n_ch*track_rprice);
	else
		price = Math.round(n_ch*track_price);
	price /= 100;
	if((price - Math.floor(price)) == 0)
		price = price+'.00';
	else if( Math.round(((price - Math.floor(price))*100))%10 == 0)
		price = price+'0';

	document.getElementById('t_price').innerHTML = price;
	
	var size = Math.round(ch_sz*100)/100;
	if(size == 0)
		size = '00.00';
	document.getElementById('t_size').innerHTML = size;
	document.getElementById('t_duration').innerHTML = modifier_duration(ch_dr);
	document.getElementById('t_tracks').innerHTML = n_ch;
	return true;
}
function modifier_duration(duration)
{
	var out = '';
	var num_mins = Math.floor(duration / 60);
	if(num_mins == 0)
		out +='00:';
	else if(num_mins < 10)
		out += '0'+num_mins+':';
	else
		out += num_mins+':';
	tmp = duration - (num_mins * 60);

	var num_secs = tmp;
	if(num_secs < 10)
		num_secs = '0'+num_secs;
	out += num_secs;
	return out;
}