SLIDESHOW_isInitialized=0
SLIDESHOW_slideshow_active=0
SLIDESHOW_doShuffle=1
SLIDESHOW_currentPosition=0
SLIDESHOW_imageCounter=0
SLIDESHOW_slideshow_active=0
SLIDESHOW_timer=0
SLIDESHOW_timerID=0
SLIDESHOW_slideshow_interval=12000
SLIDESHOW_fadeouttime=3000
SLIDESHOW_fadeintime=2500
SLIDESHOW_all_images=new Array()
SLIDESHOW_centerImages=1

GALLERY_lastloaded=new Array()
SLIDESHOW_lastloaded=new Array()

SLIDEHOW_IS_MOBILE=0


var a=(navigator.userAgent||navigator.vendor||window.opera)
if (/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) {
	SLIDEHOW_IS_MOBILE=1
}


/* 	-----------------------------------------------------------------------------
	INITIALIZE SLIDESHOW
	----------------------------------------------------------------------------- */

$(function() {
	
	if (SLIDESHOW_ie6) {
		return
	}
	
	$.ajax({
		url: 'http://www.oslojazz.no/_BACKGROUNDS_INCL/getSlideshowImages.php?r='+Math.random(),
		cache: false,
		success: function(result){
			if (result!="no_bgs") {
				setTimeout('SLIDESHOW_start("'+result+'")',1)
			}
		}
	});
	
	$(window).resize(function(){
	  SLIDESHOW_resize();
	});
	SLIDESHOW_resize()
	
});






/* 	-----------------------------------------------------------------------------
	FUNCTIONS
	----------------------------------------------------------------------------- */


function SLIDESHOW_resize() {
	
	var w=$(window).width()
	var h=$(window).height()
	
	if (SLIDEHOW_IS_MOBILE) {
		h+=230 // compensate safari toolbar
		$('#SLIDESHOW_container').css('height',h)
		
	}
	
	var temp=$('.SLIDESHOW_photodiv')
	
	for (var i=0; i<temp.length; i++) {
		
		var e=$(temp[i]).children()
		e=e[0]
	
		var pw=$(e).attr('iW')
		var ph=$(e).attr('iH')
		
		if (pw && ph) {
		
			var aspect=pw/ph
			var neww=w
			var newh=w/aspect
			
			if (newh<h) {
				newh=h
				neww=h*aspect
			}
			var offsetx=Math.min(0, -1*Math.round((neww-w)/2))
			var offsety=Math.min(0, -1*Math.round((newh-h)/2))
			
			neww=Math.round(neww)
			newh=Math.round(newh)
			
			if (SLIDESHOW_centerImages) {
				offsetx=Math.round(offsetx)
				offsety=Math.round(offsety)
			} else {
				offsetx=0
				offsety=0
			}
			
			$(e).attr({'width':neww, 'height':newh})
			$(e).css({'margin-left':offsetx+'px', 'margin-top':offsety+'px'})
			
		}
	}
	
}


function SLIDESHOW_loadVars(sl_arr) {
	
	SLIDESHOW_all_images=sl_arr.split("[**]")
	
	if (SLIDESHOW_doShuffle) {
		var tmp, rand;
		for (var i =0; i<SLIDESHOW_all_images.length; i++){
			rand = Math.floor(Math.random() * SLIDESHOW_all_images.length);
			tmp = SLIDESHOW_all_images[i]; 
			SLIDESHOW_all_images[i] = SLIDESHOW_all_images[rand]; 
			SLIDESHOW_all_images[rand] =tmp;
		
		}
	}
	
}


function SLIDESHOW_start(sl_arr) {
	// start slideshow
	
	SLIDESHOW_loadVars(sl_arr)
	SLIDESHOW_timer=0
	SLIDESHOW_currentPosition=0
	
	if (SLIDESHOW_all_images.length>0) {
		SLIDESHOW_slideshow_active=1
		SLIDESHOW_fetchImage(0)
	}
	
	if (SLIDEHOW_IS_MOBILE) {
		$('#SLIDESHOW_container').css('position', 'absolute')
		setInterval('SLIDESHOW_iphone_repositionImage()',5)
	
		document.body.ontouchend = function(e){
			SLIDESHOW_iphone_repositionImage()
        }
		
	}
}



function SLIDESHOW_iphone_repositionImage() {
	var s=$(window).scrollTop() 
	$('#SLIDESHOW_container').css('top', s)
}


function SLIDESHOW_fetchImage() {
	// preloads the next image in the slideshow
	
	if (!window.SLIDESHOW_all_images) {
		return
	}
	
	
	var temp=SLIDESHOW_imageCounter
	$('#SLIDESHOW_container').append('<div class="SLIDESHOW_photodiv"><img id="SLIDESHOW_img'+SLIDESHOW_imageCounter+'" class="SLIDESHOW_pic" onload="SLIDESHOW_showpic('+temp+')" src="'+SLIDESHOW_all_images[SLIDESHOW_currentPosition]+'"></div>');
	
	SLIDESHOW_imageCounter++
	SLIDESHOW_currentPosition++
	
	if (SLIDESHOW_currentPosition>=SLIDESHOW_all_images.length) {
		SLIDESHOW_currentPosition=0
	}

}



function SLIDESHOW_showpic(imageID) {
	// displays the next slide pic, fades the old one out
	// initializes also preloading of the next images as soon as image is faded out
	// does that only if >1 images in slideshow
	
	if (!window.SLIDESHOW_all_images) {
		return
	}
	
	var currentTime=new Date()
	var ms = currentTime.getTime()
	var tempImg=$('#SLIDESHOW_img'+imageID)
	
	if (ms>SLIDESHOW_timer && SLIDESHOW_slideshow_active==1) {
		
		if (SLIDESHOW_in_array(imageID,SLIDESHOW_lastloaded)) {
			return // double triggered onload
		}
		
		// prevent double triggering of onload event of image
		SLIDESHOW_lastloaded.push(imageID)
		if (SLIDESHOW_lastloaded.length>2) {
			SLIDESHOW_lastloaded=SLIDESHOW_lastloaded.slice(1)
		}
		
		SLIDESHOW_timer=ms+SLIDESHOW_slideshow_interval
		
		tempImg.fadeOut(1)
		tempImg.addClass('SLIDESHOW_pic_visible')
		tempImg.attr({'iW':tempImg.width(), 'iH':tempImg.height()})
		SLIDESHOW_resize()
		tempImg.fadeIn(SLIDESHOW_fadeintime, function() { SLIDESHOW_removeOldImages() } )
		
		if (SLIDESHOW_all_images.length>1) {
			SLIDESHOW_slideshow_active=1					 
			SLIDESHOW_fetchImage()
		} else {
			SLIDESHOW_slideshow_active=0
			clearTimeout(SLIDESHOW_timerID)
		}
		
	
	} else {
		
		clearTimeout(SLIDESHOW_timerID)
		SLIDESHOW_timerID=setTimeout('SLIDESHOW_showpic('+imageID+')', 300) // not yet time for next slide
	}

}

function SLIDESHOW_removeOldImages() {
	// remove old divs, just leave 2 upper ones
	
	var temp=$('.SLIDESHOW_photodiv')
	
	if (temp.length>4) {
		for (var i=0; i<temp.length-4; i++) {
			$(temp[i]).remove()
		}
	}	
}


function SLIDESHOW_setState(act) {
	// sets the slideshow active/non-active
	// 0: paused / 1: go
	SLIDESHOW_slideshow_active=act
}

function SLIDESHOW_forget() {
	// deactivates the mood slideshow completely
	SLIDESHOW_slideshow_active=0
	SLIDESHOW_currentPosition=1
	SLIDESHOW_timer=0
	clearTimeout(SLIDESHOW_timerID)
}


function SLIDESHOW_in_array(id, arr) {
	var found=0
	for (var i=0; i<arr.length; i++) {
		if (arr[i]==id) {
			found=1
			break;
		}
	}
	return found
}

