$(window).load(function () {
	Lightbox.init();
	History.init();
	flash();
	$('body').append('<div id="chat_bar" style="width:98%;"></div>');
	barresize();
	$(window).resize(function(){barresize()});
});

$('#xoverlay').bind('click', function(){
	Lightbox.hide();
});

function barresize() {
	var windowSize = Lightbox.getPageDimensions();
	var l = (windowSize[0] - 980)/ 2;
	$('#playeronair').css('left',l+'px');
}

$('#search').autocomplete({serviceUrl:'ajax/completer.php',width:298,params:{type:'search'},onSelect:jump});

function registeruname() {
	var f = $('#firstname').val();
	var l = $('#lastname').val();
	var a = $('#artistname').val();
	$.post('ajax/uname.php', {f:f,l:l,a:a}, function (data) {
		$.each(data, function(key,value){
			if (key == 'xscript') {
				eval(value);
			}
		});
	}, "json");
}

function checkuname() {
	var u = $('#uname').val();
	$.post('ajax/uname.php', {u:u}, function (data) {
		$.each(data, function(key,value){
			if (key == 'xscript') {
				eval(value);
			}
		});
	}, "json");
}

function onair(id,artist,song) {
	$('#equalizer').fadeIn('slow');
	$('#onair').html(artist+' - '+song);
}

function jump(value, data) {
	if ($.browser.msie) {
		location.hash = data;
		History.ajax(data);
	} else {
		location.hash = data;
	}
	$('#search').val($('#search').attr('text'));
}

function searching() {
	location.hash = 'search/'+$('#search').val();
	$('#search').val($('#search').attr('text'));
}

function updatesong(url) {
	$.post('ajax/flash.php?count','url='+url);
}

function login(){
	$('#formlogin').submit();
}

function boxlogin(){
	$('#boxformlogin').submit();
}

function recovery(){
	ajaxpass('recovery',$('#formpassword').serialize());
}

function newpass(){
	ajaxpass('newpass',$('#formreset').serialize());
}

function previewuname(value){
	$('#myurla').html('www.muzicly.com/'+value);
}

function onlogin(libelle){
	if ($('#ilogin').val() == libelle) {
		$('#ilogin').val('');
	} else if ($('#ilogin').val() == '') {
		$('#ilogin').val(libelle);
	}
}

function onpassl(libelle){
	if ($('#lpass').val() == libelle) {
		$('#lpass').val('')
		$('#lpassdiv').hide();
		$('#ipassdiv').show();
		$('#ipass').focus();
	}
}

function onpass(libelle){
	if ($('#ipass').val() == '') {
		$('#lpass').val(libelle)
		$('#ipassdiv').hide();
		$('#lpassdiv').show();
	}
}

function ajaxpass(type,params){
	$.post('ajax/password.php?m='+type, params, function(data){
		$.each(data, function(key,value){
			if (key == 'xscript') {
				eval(value);
			} else if (key == 'xhash') {
				location.hash = value;
			}
		});
	}, "json");
}

(function($){

	$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- _",
			nchars: "",
			allow: ""
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e)
								{
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false});
									
				}
			);

	};

	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};	

})(jQuery);
