(function($){$.fn.lazyload=function(options){var settings={threshold:0,failurelimit:0,event:"scroll",effect:"show",container:window};if(options){$.extend(settings,options);}
var elements=this;if("scroll"==settings.event){$(settings.container).bind("scroll",function(event){var counter=0;elements.each(function(){if($.abovethetop(this,settings)||$.leftofbegin(this,settings)){}else if(!$.belowthefold(this,settings)&&!$.rightoffold(this,settings)){$(this).trigger("appear");}else{if(counter++>settings.failurelimit){return false;}}});var temp=$.grep(elements,function(element){return!element.loaded;});elements=$(temp);});}
this.each(function(){var self=this;if(undefined==$(self).attr("original")){$(self).attr("original",$(self).attr("src"));}
if("scroll"!=settings.event||undefined==$(self).attr("src")||settings.placeholder==$(self).attr("src")||($.abovethetop(self,settings)||$.leftofbegin(self,settings)||$.belowthefold(self,settings)||$.rightoffold(self,settings))){if(settings.placeholder){$(self).attr("src",settings.placeholder);}else{$(self).removeAttr("src");}
self.loaded=false;}else{self.loaded=true;}
$(self).one("appear",function(){if(!this.loaded){$("<img />").bind("load",function(){$(self).hide().attr("src",$(self).attr("original"))
[settings.effect](settings.effectspeed);self.loaded=true;}).attr("src",$(self).attr("original"));};});if("scroll"!=settings.event){$(self).bind(settings.event,function(event){if(!self.loaded){$(self).trigger("appear");}});}});$(settings.container).trigger(settings.event);return this;};$.belowthefold=function(element,settings){if(settings.container===undefined||settings.container===window){var fold=$(window).height()+$(window).scrollTop();}else{var fold=$(settings.container).offset().top+$(settings.container).height();}
return fold<=$(element).offset().top-settings.threshold;};$.rightoffold=function(element,settings){if(settings.container===undefined||settings.container===window){var fold=$(window).width()+$(window).scrollLeft();}else{var fold=$(settings.container).offset().left+$(settings.container).width();}
return fold<=$(element).offset().left-settings.threshold;};$.abovethetop=function(element,settings){if(settings.container===undefined||settings.container===window){var fold=$(window).scrollTop();}else{var fold=$(settings.container).offset().top;}
return fold>=$(element).offset().top+settings.threshold+$(element).height();};$.leftofbegin=function(element,settings){if(settings.container===undefined||settings.container===window){var fold=$(window).scrollLeft();}else{var fold=$(settings.container).offset().left;}
return fold>=$(element).offset().left+settings.threshold+$(element).width();};$.extend($.expr[':'],{"below-the-fold":"$.belowthefold(a, {threshold : 0, container: window})","above-the-fold":"!$.belowthefold(a, {threshold : 0, container: window})","right-of-fold":"$.rightoffold(a, {threshold : 0, container: window})","left-of-fold":"!$.rightoffold(a, {threshold : 0, container: window})"});})(jQuery);
if(!navigator.cookieEnabled){
	alert('Please enable the cookie to use all the services on ListenArabic.com');
}
function toggleLayer(whichLayer) {
	var elem, vis;
	if( document.getElementById )
		elem = document.getElementById( whichLayer );
	else if( document.all )
		elem = document.all[whichLayer];
	else if( document.layers )
		elem = document.layers[whichLayer];
	vis = elem.style;
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0) ? 'block' : 'none';
	vis.display = (vis.display==''||vis.display=='block') ? 'none' : 'block';
}
function addToFavorites() {
	var title = document.title;
	var url = document.location;
	if (window.sidebar) { /* Mozilla Firefox Bookmark */
		window.sidebar.addPanel(title, url,"");
	} else if (window.external) { /* IE Favorite */
		window.external.AddFavorite(url, title); 
	} else if (window.opera && window.print) { /* Opera Hotlist */
		return true; 
	}
}
function ul(user,id,p) {
	$.ajax({
			async: false,
			type: "POST",
			data: "u="+user+"&id="+id+"&p="+p,
			url: api+"getListeners.php"
	});
}

$(document).ready(function(){
	/*Tabs */
	function afterSubmit() {
		$.cookie('welcome_dialog','done',{path: '/', expires: 14});
		$("#welcome-dialog").dialog('close');
	}
	$("#tabs").removeClass("h");
	$("#tabs1").removeClass("h");
	$('#tabs').tabs({ cache: true, ajaxOptions: { async: false, cache: false }, show: function(event, ui) {$(window).scroll();} }).find( ".ui-tabs-nav" ).sortable({ axis: "x" });
	$('#tabs1').tabs({ cache: true, ajaxOptions: { async: false, cache: false }, show: function(event, ui) {$(window).scroll();} }).find( ".ui-tabs-nav" ).sortable({ axis: "x" });
	$( "#welcome-dialog" ).dialog({
		autoOpen: ($.cookie('welcome_dialog') == null) ? true : false,
		height: 100,
		modal: true,
		resizable: false,
		closeOnEscape: false,
		open: function(event, ui) { gapi.plusone.go(); }
		/*beforeClose: function(event, ui) { return ($.cookie('welcome_dialog') != null) }*/
	});
/*	$("#tabs > ul").tabs({ cache: true, ajaxOptions: { async: false, cache: false }, show: function(event, ui) {$(window).scroll();} });*/
/*	$("#tabs1 > ul").tabs({ cache: true, ajaxOptions: { async: false, cache: false }, show: function(event, ui) {$(window).scroll();} });*/

	/*Playlist */
	if (sessvars.playlist) {
		$("#playlist").html(sessvars.playlist);
		$("#controllers").show();
	} else {
		$("#controllers").hide();
	}
	$("td > a[type='playlist']").click(function(){
		var title_ar = $(this).attr("title");
		var song_id = $(this).attr("sid");
		var name_ar = $(this).attr("singer");
		var url = $(this).next().attr("href");
		var playlist_html = '<div style="margin-top:5px;"><img title="Remove '+title_ar+'" src="http://images.listenarabic.com/images/cross.png" class="remove" rel="remove" id="'+song_id+'" align="absmiddle" /> <a href="'+url+'"><span class="txt12 b">'+name_ar+' - '+title_ar+'</span></a></div>';
		if(sessvars.playlist) {
			sessvars.playlist += playlist_html;
			sessvars.song_ids += ','+song_id;
			$("#playlist").append(playlist_html);
		} else {
			sessvars.playlist = playlist_html;
			sessvars.song_ids = song_id;
			$("#playlist").html(playlist_html);
		}
		$("img[rel='remove']").click(function(){
			removeItem($(this));
		});
		$("#controllers").show("slow");
	});
	$("img[rel='remove']").click(function(){
		removeItem($(this));
	});
	function removeItem(e) {
		e.parent().remove();
		sessvars.playlist = '';
		$("#playlist").each(function(i){
			sessvars.playlist += this.innerHTML;
		});
		sessvars.song_ids = '';
		$("img[rel='remove']").each(function(i){
			sessvars.song_ids += (sessvars.song_ids == '') ? this.id : ','+this.id;
		});
		if(sessvars.playlist == '') $("#controllers").hide("slow");
	}
	$("#resetPlaylist").click(function(){
		sessvars.playlist = '';
		sessvars.song_ids = '';
		$('#playlist').html('');
		$('#controllers').hide("slow");
	});
	$("#savePlaylist").click(function(){
		$("#form").slideDown("slow");
	});
	function handleMsg(msg) {
		var message;
		switch(msg) {
			case '0':
				message = 'Playlist Saved!';
			break;
			case '1':
				message = '<a rel="nofollow" href="http://www.listenarabic.com/fb/login.php?return_url=/">Please login before you save a playlist</a>';
			break;
			default:
				message = 'An error has occured';
			break;
		}
		$("#form").html(message);
	}
	$("#save").click(function(){
		$.ajax({
			async: false,
			type: "POST",
			data: "pl_title="+$("#pl_title").val()+"&ids="+sessvars.song_ids,
			url: api+"setPlaylist.php",
			beforeSend: function(){
				if($("#pl_title").val() == '') {
					alert('Please enter a title for the playlist');
					return false;
				} else return true;
			},
			success: function(msg) {
				handleMsg(msg);
			},
			complete: function() {
				/*$("#form").hide("slow"); */
			}
		});
	});
	$("#cancel").click(function(){
		$("#form").hide("slow");
	});
	
	/*Share */
	$("#tellafriend").hide();
	$("#tellafriend_btn").click(function() {
		$(this).hide();
		$("#tellafriend").show('slow');
	});
	var domain='',email='',pass='';
	/*$("textarea[name=msg]").val(imsg); */
	$("#sharemessage").hide();
	$("#importresult").hide();
	$("#contactimporter").submit(function() {
		$.ajax({
			type:'POST',
			url: api+'oi_getContacts.php',
			dataType:'html',
			data:$(this).serialize(),
			success:function(i){
				$("#importresult").hide();
				$("#contactimporter").hide('slow');
				$("#importresult").html(i);
			},
			beforeSend:function(){
				if (domain == '' || $('#usr').val() == '' || $('#pwd').val() == '') {
					alert("Please choose your webmail provider then enter your username and password");
					return false;
				} else {
					$("#importresult").html('<div id="importloader" align="left"></div> loading...');
					$("#importresult").fadeIn();
				}
			},
			complete:function(){
				$("#importresult").fadeIn();
				$("#sharemessage").fadeIn();
				$('#sharemessage').submit(function() {
					var emails = '';
					$("input[name=emails]").each(function() {
						if ($(this).attr('checked')) {
							if (emails != '') emails += ',';
							emails += $(this).attr('value');
						}
					});
					$('#importresult').html('<div id="importloader"></div> loading...');
					$('#sharemessage').hide();
					$.ajax({
						async:false,
						type:'POST',
						url: api+'share.php',
						dataType:'html',
						data:$(this).serialize()+'&e='+emails+'&t='+document.title+'&u='+window.location+'&usr='+$('#usr').val()+'&domain='+domain,
						success:function(i){
							$('#importresult').html(i);
						},
						complete:function(){
							$("#tellafriend").hide('slow');
						}
					});
					return false;
				});
				var emailstog = true; /* or true if they are checked on load */
				$('#emailstoggle').click(function() {
					$("input[name=emails]").attr("checked",!emailstog);
					emailstog = !emailstog;
					return false;
				});
			}
		});
		return false;
	});
	$("input[@name='domain']").click(function() {
	if ($("input[@name='domain']").is(":checked")) {
		domain = $(this).val();
		/*
		switch(domain) {
			case 'gmail.com':
				$("#iusr").html("Username:");
			break;
			case 'yahoo.com':
				$("#iusr").html("Yahoo! ID:");
			break;
			case 'hotmail.com':
			case 'aol':
				$("#iusr").html("Email:");
			break;
			default:
				$("#iusr").html("Email:");
			break;
		}
		*/
	}
	});
	/*dl */
	$("td > a[type='download']").click(function(){
		$(this).replaceWith('loading...');
		$('iframe#dl').attr('src', api+"download.php?url="+$(this).attr("singer")+"&id="+$(this).attr("sid"));
	});
	/*chat */
	$("a[type='change_room']").click(function() {
		$("#room_links").slideDown("slow");
	});
	$("a[room]").click(function() {
		$("#chatbox").parent().find("h3:first").html($(this).attr("title"));
		$("object > embed[src^='http://widget.meebo']").attr({ src: "http://widget.meebo.com/mcr.swf?id="+$(this).attr("room") });
		$("object > param[name='movie']").attr({ src: "http://widget.meebo.com/mcr.swf?id="+$(this).attr("room") });
		$("#room_links").hide("slow");
	});	
});
jQuery(document).ready(function() {
jQuery("#dropmenu ul").css({display: "none"});
jQuery("#dropmenu li").hover(function(){
	jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).show(10);
	$(window).scroll();
	},function(){
	jQuery(this).find('ul:first').css({visibility: "hidden"});
	});
});
