$j = jQuery.noConflict();
var $currentpage = 0;

$j(document).ready(function(){
	
	/* Dropshadow
	---------------------------------------*/
	$j(".dropshadow").each(function() {
		$mode = $j('#mainbanner').length > 0 ? 'wide' : '';
		$w = $j(this).width() + 8;
		$h = $j(this).height() + 8;
		$h -= $mode == "wide" ? 0 : 9;
		//$j(".wideshadow").css({'height':($h+10)+'px'});
		//$j(".shadow").css({'height':($h+1)+'px'});
		$j(this).prepend('<div class="abs"><div class="'+$mode+'shadow" style="height:'+($h+10)+'px;"><!-- --></div><div class="'+$mode+'shadowbot"><!-- --></div></div><div class="clear"><!-- --></div>');
	});

	/* IE6 PNG fix
	---------------------------------------*/
	if($j.browser.msie && $j.browser.version < 7 ) {
		ie6png();
	}
	
	/* sidebar news list pagination
	---------------------------------------*/
	$lists = $j("#sidebar .sidebarnews");
	if($lists.length > 0) {
		// find the current list to display
		$find = $j($j('#content p').get(0)).html();
		$searchfor = $j($j('#content p.ingress').get(0)).html();
		$pagination = "";
		$current = false;
		$maxh = 0;
		for($i=0;$i<$lists.length;$i++) {
			$list = $j($lists.get($i));
			$current = false;
			if($find != "") {
				$items = $list.find('li');
				for($k=0;$k<$items.length;$k++) {
					$item = $j($items.get($k));
					$html = $item.html();
					if($html.indexOf($find) != -1) {
						// this is the current item
						$current = true;
						$item.addClass("current");
					}
				}
			}
			if(!$current) {
				$list.css({'display':'none'});
			}
			$maxh = Math.max($maxh,$list.height());
			$pagination += '<li class="'+($current?'current':'')+'"><a href="javascript:newspage('+$i+');">'+($i+1)+'</a></li>';
		}
		for($i=0;$i<$lists.length;$i++) {
			$list = $j($lists.get($i));
			$list.css({'height':$maxh+'px'});
		}
		if($lists.length > 1) {
			$j("#sidebar").append('<div class="clear"><!-- --></div><div id="pagination"><div class="title">Sida:</div><ul class="pages">'+$pagination+'</ul></div>');
		}
	}
	
	/* remove border on linked images
	---------------------------------------*/
	$j('#footer a').each(function(){
		$img = $j(this).find('img');
		if($img.length > 0) {
			$j(this).addClass('noborder');
		}
	});
	
	/* fix vignette position
	---------------------------------------*/
	$h1 = $j($j('#content h1').get(0)).html();
	if($h1.indexOf('y') != -1 || $h1.indexOf('p') != -1 || $h1.indexOf('g') != -1 || $h1.indexOf('j') != -1 ) {
		$j('.vignette').css({'paddingTop':'.3em'});
	}
	
	/* fix ingress position if vignette is missing
	---------------------------------------*/
	if($j('.vignette').length <= 0) {
		$j('.ingress').css({'paddingTop':'.3em'});
	}
	
	/* fix content position if vignette and ingress is missing
	---------------------------------------*/
	if($j('.vignette').length <= 0 && $j('.ingress').length <= 0) {
		$j($j('#content p').get(0)).css({'paddingTop':'1em'});
	}
	
	/* fitted
	---------------------------------------*/
	$j('.fitted').fitted({
		'status' : true,
		'title'  : false
	});
	$j('#mainbanner li').fitted({
		'status' : true,
		'title'  : false
	});
	$j('.sidebarnews li').fitted({
		'status' : true,
		'title'  : false
	});
	$j('#pagination li').fitted({
		'status' : true,
		'title'  : false
	});
	

		
});

this.newspage = function($p) {
	$p = parseInt($p);
	$lists = $j("#sidebar .sidebarnews");
	for($i=0;$i<$lists.length;$i++) {
		$list = $j($lists.get($i));
		$list.css({'display':($i!=$p?'none':'block')});
	}
	$pagination = $j("#sidebar .pages li");
	for($i=0;$i<$pagination.length;$i++) {
		$item = $j($pagination.get($i));
		$item.removeClass("current");
		if($i==$p) {
			$item.addClass("current");
		}
	}
}

this.ie6png = function() {
	DD_belatedPNG.fix('.shadow');
	DD_belatedPNG.fix('.shadowbot');
	DD_belatedPNG.fix('.wideshadow');
	DD_belatedPNG.fix('.wideshadowbot');
	DD_belatedPNG.fix('#decoration');
}

jQuery.fn.outerHTML = function(s) {
	return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}
