(function($){
	$.fn.toggleVoteBox = function() {
		var target = $(this);
		if(target.hasClass('Vote')) { 
			target.removeClass('Vote').addClass('VoteExtended'); 
		} else { 
			target.removeClass('VoteExtended').addClass('Vote'); 
		}
	}
	$.fn.configTopMenu = function() {
		$('#Top_Menu_UL .subParent > a').append(' <span class"rightArrow">&raquo;</span>');
		$('#Top_Menu_UL li').hover(
				function(){ $(this).addClass('a1_hover').find('ul:first').show(); },
				function(){ $(this).removeClass('a1_hover').find('ul').hide().parent().find('.rightArrow').remove(); }
		);
	}
	$.fn.createUserTooltip = function(callerObject, e) {
		target = $(this);
		var height = target.height();
		var width = target.width();
		leftVal=e.pageX-(width/2)+"px";
		topVal=e.pageY-20-(height/2)+"px";		
		target.css({left:leftVal,top:topVal}).show().animate({opacity:1},200).html('Laddar...').dropShadow();
		$.ajax({
			type: 'POST',
			url: WebSiteUrl+'Actions/User/FastInfo.asp',
			data: { 'id': callerObject.attr('alt') },
			cache: false,
			success: function(msg){ 
				target.html(msg);
				$('.sendPM').click(function(e){
					e.preventDefault();
					targetLink = target.find('.sendPM');
					name = targetLink.attr('id').substring(5,targetLink.attr('id').length);
					$('#Send_Pm_Global').show('slow', function(){
						$.ajax({
							url: 'Actions/Mail/ComposeGlobal.asp',
							success: function(msg){ 
								$('#Send_Pm_Global').html(msg);
								$('#Pm_Ans_User').val(name).attr('readonly','readonly');
								$('#Pm_Ans_Cancle').click(function(){ $('#Send_Pm_Global').html('').hide('slow'); });
							}
						});
					});
				});
				$('.friendify').click(function(e){
					e.preventDefault();
					id = $(this).attr('id').substring(5,$(this).attr('id').length);
					$.ajax({
						type:'POST',
						url: 'Actions/User/Friendify.asp',
						data: {'id':id},
						success: function(msg){ alert(msg); }
					});
				});
			}
		});
	}
	$.fn.hideUserTooltip = function() {
		$('#User_ToolTip').html('');
		$('.dropShadow').hide();
		$('#User_ToolTip').hide().animate({opacity:0},200);
	}
	$.fn.markEventAsRead = function() {
		$.ajax({
			type: 'POST',
			url: 'Actions/Misc/EventRead.asp',
			data: { 'id': $(this).attr('name') },
			cache: false,
			success: function(msg){ $(this).removeClass('eventBoxNew').addClass('eventBox'); }
		});
	}
	$.fn.markCheckboxes = function(containerId) {
		$("#"+containerId).find("input[type$='checkbox']").each(function(){ if(this.checked == true){ this.checked = false; } else { this.checked = true; }});
	}
	$.fn.readEvent = function(){
		caller = $(this);
		$.ajax({
			type:'POST',
			url:'Actions/Misc/EventRead.asp',
			data: {'id':$(this).attr('name')},
			cache: false,
			success: function(){
				caller.removeClass('eventBoxNew').addClass('eventBox');
			}
		});
	}
	$.fn.doSearch = function(section, page){
		if(page==null||page<1){page=1;}
		if(section=='forum'){ sData = {'page':page, 'amount':$('#_Amount').val(),'sort':$('#_Sort').val(),'word':$('#_Topic').val(),'forum':$('#_Forum').val(),'type':$('#_Type').val()}; }
		else if(section=='article'||section=='script'||section=='video'){ sData = { 'page':page, 'word': $('#_Topic').val(), 'cat': $('#_Cat').val() }; }
		$('#Search_Result').html('<div style="text-align:center;"><img src="'+WebSiteUrl+'Theme/'+theme+'/Images/AjaxLoadingIcon.gif" border="0" alt="" /></div>');
		$.ajax({
			type: 'POST',
			url: 'Actions/Search/'+section+'.asp',
			data: sData,
			cache: false,
			success: function(msg){
				$('#Search_Result').html(msg);
				$(".ttInfoImg").click(function(e){	$('#User_ToolTip').createUserTooltip($(this), e); });
				$('.Search_Paging').click(function(e){
					e.preventDefault();
					$.fn.doSearch('forum',$(this).text());
				});
			}
		});
	}
	$.fn.checkRegistration = function() {
		var userName = $('input#_RegUser');
		var userPass = $('input#_RegPass');
		if(userName.val().length < 4 || userName.val().length > 15) { alert(unescape('Anv%E4ndarnamnet ska vara mellan 4-15 tecken l%E5ngt!')); return false; }
		else if(userPass.val().length < 4 || userPass.val().length > 15) { alert(unescape('L%F6senordet ska ska vara mellan 4-15 tecken l%E5ngt!')); return false; }
		else if(!/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/.test($('input#_RegMail').val())){ alert('Felaktig e-post adress!'); return false; }
		else if($('input#_RegCheck').val().length!=5){ alert('Felaktig repetition av bildtext!'); return false; }
		else{ for(var i=0; i<=members.length; i++){ if(userName.val().toLowerCase()==members[i].toLowerCase()){ alert(unescape('Anv%E4ndarnamnet existerar redan!')); return false; } } }
	}
	$.fn.previewArticle = function() {
		$.ajax({
			type: 'POST',
			url: 'Actions/Forum/PreviewThread.asp',
			data: { 'text': tinyMCE.get('_Text').getContent() },
			cache: false,
			success: function(msg){ $('#PreviewArticleContent').html(msg); }
		});
	}
})(jQuery);

$(document).ready(function(){
	$.fn.configTopMenu();
	$('#User_ToolTip').animate({opacity:0},0);
	$(".ttInfoImg").click(function(e){	$('#User_ToolTip').createUserTooltip($(this), e); });
	$('#User_ToolTip').hover(function(){},function(){ $.fn.hideUserTooltip(); });	
	$('.eventBoxNew').click(function(){ $.fn.markEventAsRead();	});
	$('#Mark_Guard_Checkboxes').click(function() { $.fn.markCheckboxes('GuardTable'); });
	$('#VoteExpand').click(function(e){ e.preventDefault(); $('#VoteDiv').toggleVoteBox(); });
	$('.eventBoxNew').hover(function(){ $(this).readEvent(); },function(){});
	$('#Search_Forums').click(function(){ $.fn.doSearch('forum', null); });
	$('#Search_Articles').click(function(){ $.fn.doSearch('article', null); });
	$('#Search_Scripts').click(function(){ $.fn.doSearch('script', null); });
	$('#Search_Videos').click(function(){ $.fn.doSearch('video', null); });
	$('#_PreviewArticle').click(function(){ $.fn.previewArticle(); });
});