$(document).ready(function(){
	
	 jQuery.fn.fadeSliderToggle = function(settings) {
		 settings = jQuery.extend({
			speed:500,
			easing : "swing"
		}, settings)
		
		caller = this
		if($(caller).css("display") == "none"){
			$(caller).animate({
				opacity: 1,
				height: 'toggle'
			}, settings.speed, settings.easing);
		}else{
			$(caller).animate({
				opacity: 0,
				height: 'toggle'
			}, settings.speed, settings.easing);
		}
	}; 
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	}; 
	
	$("a[rel='login']").click(function(){
		$("div.loginDialog").animate({opacity: 'toggle'}, 'slow');
		if($("form[name='login']")){
			$("form[name='login'] input[name='user']").focus();
		}
	});
	
	$("a#changePassword").click(function(){
		$('form#newPasswordForm').toggle();
	});
	
	/*
	// Main menu dropdown
	$("ul.topnav li a").hover(function() {
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		
		$(this).parent().hover(function() {}, function(){  
			$(this).parent().find("ul.subnav").hide();
		});
	}).hover(function() {  
		$(this).addClass("subhover");
	}, function(){
		$(this).removeClass("subhover");
	});
	*/
	
	// Searchform
	$("form#seachform input#sbi").blur(function(){
		if ($(this).val() == '') {
			$(this).val($(this).attr("alt"));
		}
	});
	$("form#seachform input#sbi").focus(function(){
		if (encodeURIComponent($(this).val()) == 'S%C3%B8k%20i%20avisen') {
			$(this).val('');
		}
	});
	
		
	// Read more articles button
	$("a#read-more-articles").click(function(){
		$(this).html('<img src="/htdocs/images/ajax-loader.gif" alt="" />');
		var theLink = $(this);
		var href = theLink.attr("href");
		var hrefSplit;
		var page;
		var nextPage;
		var newHref;
		
		var catOrTag = '';
		if(href.search(/\/category\//i) == 0){ 
			// Category
			hrefSplit = href.split("/");
			catOrTag = "&cat=" + hrefSplit[2];
			page = hrefSplit[hrefSplit.length-2];
			nextPage = page*1+1;
			newHref = href.substr(0, (href.length - (page.length + 1))) + nextPage + "/";
		} else if(href.search(/\/tag\//i) == 0){
			// Tag
			hrefSplit = href.split("/");
			catOrTag = "&tag=" + hrefSplit[2];
			page = hrefSplit[hrefSplit.length-2];
			nextPage = page*1+1;
			newHref = href.substr(0, (href.length - (page.length + 1))) + nextPage + "/";
		} else {
			// Frontpage
			hrefSplit = href.split("=");
			page = hrefSplit[hrefSplit.length-1];
			nextPage = page*1+1;
			newHref = hrefSplit[0] + "=" + nextPage;
		}
		
		$.ajax({
			type: "POST",
			url: "/includes/article/ajax-get-more-articles.php",
			data: "page=" + page + catOrTag,
			cache: false,
			success: function(html){
				if(html == 0){
					$("div#more-articles").append("<div class=\"clear\"></div><br />Det finnes ikke flere artikler.");
					$("div#pagenation").hide();
				} else {
					$("div#more-articles").append(html);
					$(theLink).html('Vis flere artikler');
					$(theLink).attr("href", newHref);
				}
			}
		});
		return false;
	});
	
	// Facebook fan
	//$("div#contentRight div#left").append('<br /><iframe scrolling="no" frameborder="0" src="http://www.facebook.com/connect/connect.php?id=175205807681&connections=10&stream=0&css=PATH_TO_STYLE_SHEET&locale=nb_NO" allowtransparency="true" style="border: none; width: 278px; height: 550px;"></iframe>');
});