
jQuery(function($) {
	$('ul.imagenav li a').not('.left').not('.right').click(function(){
		var allLinks = $('ul.imagenav li a').not('.left').not('.right');
		allLinks.removeClass("active");
		$(this).addClass("active");
		currentIndex = $(this).html();
		if(currentIndex == 1)
			$('ul.imagenav li a.left').attr('id', prevJob);
		else {
			$('ul.imagenav li a.left').attr('id', '');
		}
		if(currentIndex == allLinks.length)
			$('ul.imagenav li a.right').attr('id', nextJob);
		else {
			$('ul.imagenav li a.right').attr('id', '');
		}
		changeBGImage($(this).attr('id'), $(this).attr('imgw'), $(this).attr('imgh'));
	});
	$('ul.imagenav li a.left').add('ul.imagenav li a.right').click(function(e){
		if($(this).attr('id').length > 0)
			location.href = $(this).attr('id');
		else {
			if($(this).hasClass("left")) {
				if(currentIndex > 1)
					currentIndex--;
			}
			else {
				if(currentIndex < $('ul.imagenav li a').not('.left').not('.right').length)
					currentIndex++;
			}
			$('ul.imagenav li a[imgindex*='+currentIndex+']').click();
		}
	});

	$(window).resize(function() {
		//changeBGImage($('ul.imagenav li a[imgindex*='+currentIndex+']').attr('id'), $('img#background').attr('width'), $('img#background').attr('height'))
		cgbi();
	});
	$(document).keydown(function (e) {
		var keyCode = e.keyCode || e.which,
		arrow = {left: 37, up: 38, right: 39, down: 40};

		switch (keyCode) {
			case arrow.left:
				$('ul.imagenav li a.left').click();
				return false;
			break;
			case arrow.right:
				$('ul.imagenav li a.right').click();
				return false;
			break;
		}
	});
	if($('ul.imagenav').length) {
		$('#go-left').click(function(){
			$('ul.imagenav li a.left').click();
		});
		$('#go-right').click(function(){
			$('ul.imagenav li a.right').click();
		});
	}
	//$('<div id="x" style="border:1px dashed #111;position:fixed;top:0;right:0;width:250px;z-index:1000;display:none"></div>').prependTo('body');

	$('#meny-toggle').live('click',function(){
		$('.nav ul').toggle('fast');
		$(this).toggleClass('open');
	});
	if(!show_menu) { //1=menyn är statisk
		$('#meny-toggle').show().removeClass('open');
		$('.nav ul').hide();
	}

	$('<div id="load" style="background:url(/pix/ajax.gif) 0 0 transparent no-repeat;position:fixed;top:50%;left:50%;width:50px;z-index:1000;">&nbsp;</div>').prependTo('body');
	$(window).load(function(){
		$('#load').hide();
	});
	if(staticimage){
		$('a.left,a.right').each(function(){
			//$(this).attr('href',$(this).attr('href')+'?test=1');
		})
	}

});
function c(s){
	//$('#x').append('<div>'+s+'</div>');
}
function changeBGImage(src,w,h) {

	if(staticimage){
		
		$('body').css('background-image', 'url(/images/' + src+')')
		$('img#background').css('position','absolute');
		$('img#background').css('width','1600px');
		$('img#background').css('height','1200px');
		return;
	}

	var browser_ratio = $(window).height() / $(window).width();
	var image_ratio = w / h;
	var c_conf = {expires: 30, path: '/'};
	var selectedFormat = formats[0][1].toLowerCase();

	for(var i=0; i<formats.length; i++)
		if($(window).width()+100 > formats[i][0])
			selectedFormat = formats[i][1].toLowerCase();
	
	if($(window).height()>1)
		$.cookie('window_height', $(window).height(), c_conf);

	if(selectedFormat_cookie != selectedFormat) { // Nytt format!
		$.cookie('selected_format', selectedFormat, c_conf);
	}
	
	selectedFormat = (!selectedFormat.length) ? selectedFormat : selectedFormat + '/';
	src = '/images/'+selectedFormat +src;

	var new_w = (image_ratio >= browser_ratio) ? $(window).width() : null;
	var new_h = (image_ratio >= browser_ratio) ? null : $(window).height();
	if($.browser.msie)
		new_h = $(window).width() / image_ratio;
	c('img#bg-w:'+ $('img#background').width() + " w-h: " + $(window).height() + " img#bg-h: " + $('img#background').height() + " top: " + (-1*($('img#background').height()-$(window).height())/2));
	selectedFormat_cookie = selectedFormat;

	if($('img#background').attr('src') != src)
		$('img#background').attr('src', src)
	if(new_w)
		$('img#background').attr('width', new_w)
	if(new_h)
		$('img#background').attr('height', new_h);

	if($('img#background').height() > $(window).height() &&
		$('img#background').height()*1 > 1 &&
		$('img#background').height() != $('img#background').width() ) {
		var top = -1*($('img#background').height()-$(window).height())/2;
		if(top <= 0 ) {//&& $(window).height() != window_height) {
			$('img#background').css('top', top);
			$.cookie('image_offset', top, c_conf);
		}
	} //else
	//	$('img#background').css('top',0);
}

var cache = [];
// Arguments are image paths relative to the current page.
$.preLoadImages = function() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}
