
var APopup = {
	
	show: function(id,file,data,success) {

		var scrollPosition = Math.max($(window).scrollTop() + ($(window).height()/2)-180,$(window).scrollTop()+10); 
	
		html = '<div id="'+id+'" style="position:absolute;top:'+scrollPosition+'px;left:0px;width:100%;z-index:90;background:none;">';
		html += '<div class="popup" style="margin:0px auto;width:500px;">';
		html += '<a class="close-btn hide" href="javascript:;" onclick="$(\'#'+id+'\').remove();"></a>';
		html += '<div class="outer">';
		html += '<div class="inner">';
		html += '<img src="/img/spinner.gif" width="14" height="14" border="0"/>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		
		$('#'+id).remove();
		$('body').prepend(html);

		$.post("ajax/"+file,data,function(json) {
				if(json.error) {
					$('#'+id+' .inner').html("Unknown Error / Unbekannter Fehler!");
					$('#'+id).pause(2000).fadeOut(0,function() {$('#'+id).remove();});
					return;
				}
				$('#'+id+' .inner').html(json.content);
				$('#'+id+' .close-btn').removeClass('hide');
				if(success) success();
			}, "json");

	}
	
}


var GSlider = {
	
	box_width: 336, 
	position: 0,
	boxes: 1,
	block: false,
	
	init: function(pos) {

		$('#group-slider li').each(function(i) {
			$(this).css('left',i*GSlider.box_width);
			$(this).removeClass('hide');
			GSlider.boxes = (i+1); 
		});

		this.checkButtons();

		$('#right-btn').bind('click',function(event) {
			event.preventDefault();

			if(!(GSlider.position<GSlider.boxes-1) || GSlider.block) return;

			GSlider.deactivateAll();
		
			$('#group-slider ul').animate({
				left: '-='+GSlider.box_width
			  }, 800, function() {
				GSlider.setPos(GSlider.position+1);
				GSlider.checkButtons();
			  });
		});
	
		$('#left-btn').bind('click',function(event) {
			event.preventDefault();

			if(GSlider.position<1 || GSlider.block) return;

			GSlider.deactivateAll();
	
			$('#group-slider ul').animate({
				left: '+='+GSlider.box_width
			  }, 800, function() {
				GSlider.setPos(GSlider.position-1);
				GSlider.checkButtons();
			  });
		});
		
		if(this.boxes>1) {
			$('#group-slider ul').animate({
				left: '-='+40
			},300).animate({
				left: '+='+40
			},300);
		}
	},
	goToPos: function(pos) {

		if(GSlider.block) return;
		
		GSlider.deactivateAll();

		$('#group-slider ul').animate({
			left: -GSlider.box_width*pos
		  }, 800, function() {
			GSlider.setPos(pos);
			GSlider.checkButtons();
		  });
			
	},
	checkButtons: function() {
		this.activate('left-btn',(this.position>0?true:false));
		this.activate('right-btn',(this.position<this.boxes-1?true:false));			
		this.block = false;
	},
	activate: function(btn,state) {
		if(state) $('#'+btn).removeClass('inactive');
		else $('#'+btn).addClass('inactive');
	},
	deactivateAll: function() {
		this.activate('right-btn',false);
		this.activate('left-btn',false);
		this.block = true;
	},
	setPos: function(pos) {
		tmp = $('#p'+this.position).attr('src');
		$('#p'+this.position).attr('src',$('#p'+pos).attr('src'));
		$('#p'+pos).attr('src',tmp);

		this.position = pos;
	
	}
}



var LSlider = {
	
	box_width: 904, 
	position: 0,
	boxes: 1,
	block: false,
	
	init: function(pos) {
		$('#group-slider-l li').each(function(i) {
			$(this).css('left',i*LSlider.box_width);
			$(this).removeClass('hide');
			LSlider.boxes = (i+1); 
		});

		this.checkButtons();

		$('#right-btn-l').bind('click',function(event) {
			event.preventDefault();

			if(!(LSlider.position<LSlider.boxes-1) || LSlider.block) return;

			LSlider.deactivateAll();
		
			$('#group-slider-l ul').animate({
				left: '-='+LSlider.box_width
			  }, 800, function() {
				LSlider.setPos(LSlider.position+1);
				LSlider.checkButtons();
			  });
		});
	
		$('#left-btn-l').bind('click',function(event) {
			event.preventDefault();

			if(LSlider.position<1 || LSlider.block) return;

			LSlider.deactivateAll();
	
			$('#group-slider-l ul').animate({
				left: '+='+LSlider.box_width
			  }, 800, function() {
				LSlider.setPos(LSlider.position-1);
				LSlider.checkButtons();
			  });
		});
		
		if(this.boxes>1) {
			$('#group-slider-l ul').animate({
				left: '-='+40
			},300).animate({
				left: '+='+40
			},300);
		}
	},
	goToPos: function(pos) {

		if(LSlider.block) return;
		
		LSlider.deactivateAll();

		$('#group-slider-l ul').animate({
			left: -LSlider.box_width*pos
		  }, 800, function() {
			LSlider.setPos(pos);
			LSlider.checkButtons();
		  });
			
	},
	checkButtons: function() {
		this.activate('left-btn-l',(this.position>0?true:false));
		this.activate('right-btn-l',(this.position<this.boxes-1?true:false));			
		this.block = false;
	},
	activate: function(btn,state) {
		if(state) $('#'+btn).removeClass('inactive');
		else $('#'+btn).addClass('inactive');
	},
	deactivateAll: function() {
		this.activate('right-btn-l',false);
		this.activate('left-btn-l',false);
		this.block = true;
	},
	setPos: function(pos) {
		tmp = $('#p_l'+this.position).attr('src');
		$('#p_l'+this.position).attr('src',$('#p_l'+pos).attr('src'));
		$('#p_l'+pos).attr('src',tmp);

		this.position = pos;
	
	}
}


function search_group(offset) {
	if(!offset) {
		$('#group-loader').removeClass('hide');
		$('#search-q').attr("disabled", "disabled"); 
		$('#search-b').attr("disabled", "disabled"); 
		$('#search-b-span').addClass("disabled"); 
		$('#search-q').blur();	
	}
	$.getJSON("ajax/groups",
		{
			  q:$('#search-q').attr("value"),
			  o:(offset?offset:0),
			  s:$('#sort-criteria').attr("value")
		},
		function(json) {
			if(json.r) {
				$('#group-content').html(json.content);
				$('#group-loader').addClass('hide');
				$('#search-q').removeAttr("disabled"); 
				$('#search-b').removeAttr("disabled"); 	
				$('#search-b-span').removeClass("disabled"); 
			} else {
				$('#group-content').append(json.content);
				$('#more-button-box').remove();
			}
			addMoreListener(json.o);
		}
	);	
}

function addMoreListener(o) {

	if($('#more-button').length) {
		// AUTOSCROLL
		if(o<35) {
			$(document).scroll(function() {
				var btnoffset = $('#more-button').offset();
				var y = Math.round(btnoffset.top);
				var scrollPosition = $(window).height() + $(window).scrollTop(); 
				if(y-scrollPosition<0) {
					$(document).unbind('scroll');
					$('#more-button').unbind('click');
					search_group(o);
					$('#more-loader').removeClass('hide');
				}
			});
		}
		// AUTOSCROLL END

		$('#more-button').click(function() {
			$(document).unbind('scroll');
			search_group(o);
			$('#more-loader').removeClass('hide');
		});
	} else {
		$(document).unbind('scroll');
	}	
}

function show_search_as(s) {
	if(s=='list') {	
		$('#search-icon-list').removeClass("ll-inactive");
		$('#search-icon-list').addClass("ll-active");
		$('#search-icon-icons').removeClass("li-active");
		$('#search-icon-icons').addClass("li-inactive");
		$('#group-content').removeClass("group-list-icons");	
		$('#group-content').addClass("group-list-long");	
	} else {
		$('#search-icon-list').removeClass("ll-active");
		$('#search-icon-list').addClass("ll-inactive");
		$('#search-icon-icons').removeClass("li-inactive");
		$('#search-icon-icons').addClass("li-active");
		$('#group-content').removeClass("group-list-long");	
		$('#group-content').addClass("group-list-icons");			
	}
}

function sort_search(s) {
	if(s=='size') {
		$('#search-icon-alpha').removeClass("active");
		$('#search-icon-size').addClass("active");
		$('#sort-criteria').attr("value","s");
	} else {
		$('#search-icon-alpha').addClass("active");
		$('#search-icon-size').removeClass("active");
		$('#sort-criteria').attr("value","a");
	}
	search_group();
}

function show_open(s) {
	$('#search-q').attr("value",s);
	search_group();
}

function chooseLanguage() {
//	$("#ld-button").fadeOut(100); 	
	$("#ld-button").animate({
			top: "-20px"	
		},120,"linear"); 	
	
	$("#ld-spinner").fadeIn();	

	$.get("ajax/language_chooser",
		{ },
		function(html) {
			$("#ldchooser").html(html);
			$("#ldchooser").removeClass('hide');
			$("#ldchooser").animate({ 
				top: "4px"
			  }, 120, "linear", function(e) {
				$("#ld-button").remove();
				$("#toprow").css('overflow','visible');
			  });
		}
	);	
}

function setLang(l) {
	$('#lang-input').attr('value',l);
	$('#lang-link-de').removeClass('active');	
	$('#lang-link-en').removeClass('active');	
	$('#lang-link-'+l).addClass('active');	
//	$('#ldform').submit();
	document.ldchooser.action = $('#ldurl').attr('value');
	$('#ldurl').remove();
	document.ldchooser.submit();
}
function setTimezone(tz,i) {
	tzold = $('#tz-active').attr('value');
	$('#tz-active').attr('value',i);
	$('#timezone-input').attr('value',tz);
	$('#timezone-link-'+tzold).removeClass('active');	
	$('#timezone-link-'+i).addClass('active');	
//	$('#ldform').submit();
	document.ldchooser.action = $('#ldurl').attr('value');
	$('#ldurl').remove();
	document.ldchooser.submit();
}
function go_to(id) {
	var target = $('#'+id);
	var top = target.offset().top-10;
	$('html,body').animate({scrollTop: top}, 500);
	return false;
}

function read_twitter() {
	$.getJSON("ajax/twitter",function(json){
		$('#twittertime').html(json.time);
		$('#twitterstatus').html(json.status);
	 });	
}
function deleteUser(id) {
	location.href=$('#'+id).attr('href');
	return false;
}
function printHighscore() {
	var w = (window.open($("#print-highscore").attr('href'),"Print_Highscore","width=750,height=450,menubar=no,status=no,toolbar=no,scrollbars=yes",false));	
	w.focus();
}
$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};

$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};

function setMemoStatus(gid,status) {
	$.post("ajax/set_memo_status",{gid:gid,status:status},function(json) {
			if(json && json.success) {
				if(status) $('#msi_'+gid).attr("checked","checked");
				else $('#msi_'+gid).attr("checked",null);
				$('#msi_'+gid).blur();
				return;
			}
		}, "json");
}

function sendMemo(gid) {
	$('#mdd_'+gid).addClass('hide');
	APopup.show('memo_popup','send_memo',{gid:gid,action:"show_popup"},function() {
	//	var memo_change = new Function("","$('#memo_char_count').html($('#memo_message').value());");
		var max_chars = 350;
		var memo_change = function() {
			var free_chars = max_chars-$('#memo_message').val().length;
			$('#memo_char_count').html(free_chars);
			if(free_chars<0) $('#memo_char_count').css('color','red');
			else if(free_chars>=10) $('#memo_char_count').css('color','#999999');
			else $('#memo_char_count').css('color','#C00');
		};
		$('#memo_message').keydown(memo_change);
		$('#memo_message').keyup(memo_change);
		$('#memo_message').change(memo_change);
		$('#memo_message').focus(memo_change);
		$('#memo_message').focus();
		
		$('#memo_send_button').click(function(e) {
			$('#memo_message').attr('disabled',true);
			$('#memo_send_button').attr('disabled',true);
			$('#memo_send_error').hide();
			$('#memo_send_spinner').show();
			
			var message = ($('#memo_message').val()).substr(0,max_chars);
			$('#memo_message').val(message);
		
			$.post("ajax/send_memo",{gid:gid,action:"send",message:message},function(json) {
					if(json && json.success) {
						$('#memo_send_ok').show();
						$('#memo_send_spinner').hide();
						$('#memo_popup').pause(2000).fadeOut(0,function() {$('#memo_popup').remove();});
						return;
					}
					$('#memo_message').attr('disabled',false);
					$('#memo_send_button').attr('disabled',false);
					$('#memo_send_spinner').hide();
					$('#memo_send_error').show();
				}, "json");
			
			return false;
		});
		
	});
	// To be implemented
}

function weitersagen2() {
	$('#tell_others_spinner').show();

	$('#tell_others_emails').attr('disabled',true);
	$('#tell_others_von').attr('disabled',true);
	$('#tell_others_button').attr('disabled',true);

	$.ajax({
	   type: "POST",
	   url: "ajax/tell",
	   data: {emails:$('#tell_others_emails').val(),von:$('#tell_others_von').val()},
	   success: function(msg){
			$('#tell_others_ok').show();
			$('#tell_others_spinner').hide();

			$('#tell_others').pause(2000).fadeOut(0,function() {
				$('#tell_others').hide();
				document.weitersagen.reset();
				$('#tell_others_ok').hide();
	
				$('#tell_others_emails').attr('disabled',false);
				$('#tell_others_von').attr('disabled',false);
				$('#tell_others_button').attr('disabled',false);
							
			});
	
	   }
	 });
}
