// Functions
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}
function closeMsg(){
	$('#msg').fadeOut('slow');	
}
function msg(msg,cssClass){
	$('#msg').removeClass('ok').removeClass('info').removeClass('err').addClass(cssClass).html(stripslashes(msg)).fadeIn('slow');
	setTimeout("closeMsg()", 3000);
}
function nl2br(myString){
	return myString.replace(/\\n/g, '<br /> \\n');
}
$(document).ready(function(){
	$('.moduleOut').corner("15px cc:#ffffff");
	$('.moduleIn').corner("12px cc:#E9E9E9");
	$('a#loginLink').click(function(event){
		event.preventDefault();
		$("#loginBox").dialog({
			bgiframe: true,
			width: 400,
			height: 240,
			modal: true
		});
		$('#loginBoxBtn').click(function(event){
			event.preventDefault();
			$('#loginBoxError').hide();
			if($('#login-username').val() !== '' && $('#login-password').val() !== ''){
				$('#loginBoxContent').hide();
				$('#loginBoxLoading').show();
				$.post("/process.php", { type:'login', ajax:'true', email:$('#login-username').val(), password:$('#login-password').val()},
					  function(data){
							if(data.done == 'false'){
								if(data.msg.length > 0){
									$('#loginBoxLoading').hide();
									$('#loginBoxError').show().html(data.msg);
									$('#loginBoxContent').show();
								}else{
									$('#loginBoxLoading').hide();
									$('#loginBoxError').show();
									$('#loginBoxContent').show();
								}
							}else{
								$('#loginBoxLoading').hide();
								$('#loginBoxContent').show().html('<p>Logged in!</p>');
								$('#user_meta').hide().html('Welcome <a href="/profile/">'+$('#login-username').val()+'</a> &bull; <a href="/process.php?type=logout">Logout</a>').fadeIn('slow');
								var page = window.location.pathname.substring(window.location.pathname.lastIndexOf('\\') + 1);
								if(page == '/'){
									window.location = '/profile/';
								}else{
									window.location = page;
								}
							}
					  }, "json");
			}else{
				$('#login-username').focus();	
			}
		});
		$('#cancelLogin').click(function() { 
			$("#loginBox").dialog('destroy');
			return false; 
		});
	});
	$('a.contract').click(function(event){
		event.preventDefault();
		$('#'+this.id+'Module').slideToggle('slow');
	});
	$('#searchQuery').blur(function(){
		this.style.color = '#666666';
		if(this.value == ''){
			this.value = 'Search...';
		}
	});
	$("a[rel='newMusicComs']").click(function(event){
		event.preventDefault();
		if($("#moreMusicComments").length > 0){
			$("#moreMusicComments").remove();
		}else{
			$('#notifCon').append('<div style="position:absolute; top:20px; left:40%; border:#CCCCCC solid 2px; font-size:14px; color:#333333; background:#FFFFFF;" id="moreMusicComments">Loading... Please wait</div>');
			$.ajax({
				type: "POST",
				url:"/process.php",
				data:{ type:'getMusicComments',ajax:'true' },
				dataType:"xml",
				success:function(xml){
					$('#moreMusicComments').html('');
					$(xml).find('song').each(function(){
						if($(this).find('id').text().length > 0){
							$('#moreMusicComments').append('<div class="item"><a href="/music/'+$(this).find('id').text()+'?new=1#wall">'+$(this).find('title').text()+'</a></div>');
						}
					});
	
				},
				error:function(xhr,err,e){
					msg('An unexpected eror occured, please try again later.','err')
				}
			}); // $.ajax()
		}
	});
	// LOGIN
	$('.loginBtnPage').click(function(event) {
			event.preventDefault();
			if($('#login-username-page').val() == '' || $('#login-password-page').val() == ''){
				msg('Please complete all fields','err')
			}else{
				$.blockUI({ message: '<p><img src="http://static.djs-music.com/img/icn/loadingBar.gif" alt="Logging in..."></p><p>Logging in...</p>', css: { width: '275px' } });
				$.post("/process.php", { type:'login', ajax:'true', email:$('#login-username-page').val(), password:$('#login-password-page').val()},
				  function(data){
						if(data.done == 'false'){
							if(data.msg.length > 0){
								msg(data.msg,'err');
							}else{
								$.unblockUI();
								msg('Wrong Email/Password, please try again. <a href="/forgot-pass/" title="Recover your password">Forgot your password?</a>','err')
							}
							$.unblockUI();
						}else{
							$.blockUI({ message: '<p><img src="http://static.djs-music.com/img/icn/loadingBar.gif" alt="Redirecting"></p><p>Logged in! Redirecting...</p>', css: { width: '275px' } });
							msg('Logged in','ok')
							$('#loginBox').slideUp('slow');
							$('#user_meta').hide().html('Welcome <a href="/profile/">'+$('#login-username-page').val()+'</a> &bull; <a href="/process.php?type=logout">Logout</a>').fadeIn('slow');
							window.location=$('#nextPage').val();
						}
				  }, "json");
			}
    });
	$("a[rel='openRadioPopup']").click(function(event){
		event.preventDefault();
		window.open("http://djs-music.com/radio.php","Radio DJs Music","location=0,status=0,resizable=0,scrollbars=0,toolbar=0,menubar=0,width=460,height=300")
	});
});