/*
	Exhibition.js
	full screen foto fader
	201011*pike

	this lib requires jQuery

*/
var Exhibition = {
	timer		: null,
	infotimer	: null,
	infoshown	: false,
	mode		: "background",
	current		: -1,
	previous	: -1,
	sequence	: new Array(),
	delay		: 60000,
	bgdelay		: 60000,
	fsdelay		: 15000,
	infodelay	: 7500,
	speed		: 5000,
	bgspeed		: 5000,
	fsspeed		: 3000,
	deftitle	: "Untitled",
	defdescription	: "",
	defcopyright	: "",
	defcaption	: "",
	initFlickr	: XHInitFlickr,
	flickrapikey: '7ab2596643e714ac47e1982c72f0a55b',
	flickrtags		: new Array('intercultural'),
	flickrbantags	: new Array('santander','w35'),
	flickrwoeid	: '24865675',
	init		: XHInit,
	first		: XHFirst,
	next		: XHNext,
	prev		: XHPrev,
	preload		: XHPreload,
	onload		: XHOnLoad,
	toggleFS	: XHToggleFS,
	showInfo	: XHShowInfo,
	hideInfo	: XHHideInfo,
	stopInfo	: XHStopInfo,
	imgdir		: "/_files/Exhibition",
	imgjson		: "/_files/Exhibition/json.js",
	images		: new Array()
}
	
function XHInitFlickr() {

	var flickrurl 	= 'http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key='
		+this.flickrapikey+'&tags='+this.flickrtags.join(',')+'&content_type=1&format=json&tag_mode=all'
		+'&woe_id='+this.flickrwoeid+'&safe_search=1&media=photos&extras=description,tags,owner_name,url_l'
		+'&jsoncallback=?';
	//window.open(flickrurl);
	jQuery.getJSON( flickrurl, function(data) {
		//alert(data.photos.total);
		jQuery.each(data.photos.photo, function(i,item){
			if (item.url_l) {
				var ban = false;
				if (item.tags) {
					for (var bc=0; bc < Exhibition.flickrbantags.length; bc++) {
						//alert(this.flickrbantags[bc]);
						if (item.tags.indexOf(Exhibition.flickrbantags[bc])!=-1) {
							ban=true;
						}
					}
				}
				if (!ban) {
					if (item.tags) {
						var tagstart = Math.max(0,item.tags.indexOf('intercultural')-32);
						var caption = ".. "+item.tags.substring(tagstart,tagstart+64)+" ..";
					}
					if (item.description._content) {
						var description = item.description._content.substring(0,256);
					} else {
						if (item.tags) {
							var description = 'Tagged: '+item.tags;
							caption = "";
						}
					}
					Exhibition.images[Exhibition.images.length] = {
						"url" : item.url_l,
						"title" : item.title.substr(0,64),
						"copyright" : "<a href='http://flickr.com/photos/"+item.owner+"' "
									+ " target='_blank'>"+item.ownername+" @ Flickr</a>",
						"description": description,
						"caption": caption
					};
				}
			}
		});
		//alert('callin');
		Exhibition.init();
	});

}
function XHInit() {
	
	// you can either start the slideshow with this
	// 		Exhibition.init();
	// or with flickr
	// 		Exhibition.initFlickr();
	// flickr will call this when done.
	
	// prepare the html
	var imghtml = "\
		<div id='xhbackdrop' style='position:absolute; top:0; left:0; width:100%; height:100%; overflow:hidden; z-index:-2'>\
			<img id='xhbackimg' style='position:absolute; z-index:-2;display:none;' />\
		</div>\
		<div id='xhfrontdrop' style='position:absolute; top:0; left:0; width:100%; height:100%; overflow:hidden; z-index:-1'>\
			<img id='xhfrontimg' style='position:absolute; z-index:-1; display:none;'/>\
		</div>\
	";
	jQuery('body').prepend(imghtml);
	Exhibition.backimg=jQuery('#xhbackimg');	
	Exhibition.frontimg=jQuery('#xhfrontimg');	
	Exhibition.frontimg.load(XHOnLoad);
	
	// prepare the exhibition images.
	jQuery.getJSON( this.imgjson, function(data) {
		for (var key in data) {
			Exhibition.images[Exhibition.images.length]=data[key];
		}
		
		// init sequence
		for (var sc=0;sc<Exhibition.images.length;sc++) {
			Exhibition.sequence[sc]=sc;		
		}
		// shuffle sequence
		for (var sc=0;sc<Exhibition.sequence.length;sc++) {
			rnd = Math.floor(Math.random()*Exhibition.sequence.length);	
			var org=Exhibition.sequence[sc];
			Exhibition.sequence[sc]=Exhibition.sequence[rnd];
			Exhibition.sequence[rnd]=org;
		}
		//var msg = "";
		//for (var sc=0;sc<this.images.length;sc++) {
		//	msg+=sc+":"+this.sequence[sc]+" -- ";
		//}
		//alert(msg);
		Exhibition.first();
	});

}

//function XHOnPreload() {
//	var loader 	= jQuery('#xhloader');
//	var wdw		= jQuery(window);
//	if (loader.height()<wdw.height()) loader.css('height',wdw.height());
//}

function XHFirst() {
	Exhibition.current=0;
	
	var imgurl = Exhibition.images[Exhibition.sequence[Exhibition.current]]["url"];
	if (!imgurl) {
		imgurl = Exhibition.imgdir+"/"+Exhibition.images[Exhibition.sequence[Exhibition.current]]["filename"];
	}

	Exhibition.frontimg.attr('src',imgurl);
}

function XHNext(speed) {
	//alert('here');
	clearTimeout(Exhibition.timer);
	
	// set speed
	if (!speed) {
		if (Exhibition.mode=="background") Exhibition.speed = Exhibition.bgspeed;
		else Exhibition.speed = Exhibition.fsspeed;
	} else Exhibition.speed=speed;
	
	// cp frontimg to backimg to foreground
	Exhibition.backimg.hide();	
	Exhibition.backimg.attr('src',Exhibition.frontimg.attr('src'));

	// move the counter fwd
	Exhibition.current=(Exhibition.current+1)%Exhibition.sequence.length;
	
	// dont immediately show the back img
	// give the browser a second to change the source attr
	Exhibition.timer=setTimeout("Exhibition.preload()",100);		
}

function XHPrev(speed) {
	//alert('here');
	clearTimeout(Exhibition.timer);

	// set speed
	if (!speed) {
		if (Exhibition.mode=="background") Exhibition.speed = Exhibition.bgspeed;
		else Exhibition.speed = Exhibition.fsspeed;
	} else Exhibition.speed=speed;

	// cp front image to back image
	Exhibition.backimg.hide();
	Exhibition.backimg.attr('src',Exhibition.frontimg.attr('src'));

	// move the counter bwd
	Exhibition.current=(Exhibition.current-1)%Exhibition.sequence.length;
	if (Exhibition.current<0) Exhibition.current+=Exhibition.sequence.length;

	// dont immediately show the back img
	// give the browser a second to change the source attr
	Exhibition.timer=setTimeout("Exhibition.preload()",100);	

}
function XHPreload() {
	var imgurl = Exhibition.images[Exhibition.sequence[Exhibition.current]]["url"];
	if (!imgurl) {
		imgurl = Exhibition.imgdir+"/"+Exhibition.images[Exhibition.sequence[Exhibition.current]]["filename"];
	}
	Exhibition.backimg.css({
		"width"		: Exhibition.frontimg.width(),
		"height"	: Exhibition.frontimg.height(),
		"top"		: Exhibition.frontimg.css('top'),
		"left"		: Exhibition.frontimg.css('left')
	}).show();
	
	Exhibition.frontimg.hide().attr('src',imgurl);
	
	// the onload trigger will do the rest
}

function XHOnLoad() {
	//alert('onload');
	// the front image is hidden. check the sizes, position it, and fade in
	
	// when calculates the height, it shows the 
	// image for a second, so hide it below the 
	// backimg. 
	Exhibition.frontimg.css({'z_index':-99,'width':'auto','height':'auto'});
		
	var wdw 	= jQuery(window);
	var wdwh 	= wdw.height();
	var wdww 	= wdw.width();
	var imgh 	= Exhibition.frontimg.height();
	var imgw	= Exhibition.frontimg.width()
	var vscale 	= wdwh/imgh;
	var hscale 	= wdww/imgw;
	var scale	= Math.max(vscale,hscale);
	
	Exhibition.frontimg.height(imgh*scale);
	Exhibition.frontimg.width(imgw*scale);
	Exhibition.frontimg.css({
		"top":(wdwh-imgh*scale)/2,
		"left":(wdww-imgw*scale)/2	
	});
	window.status=wdww+"/"+imgw+":"+wdwh+"/"+imgh+"="+scale;
	
	// show it again
	Exhibition.frontimg.css('z_index',-1);
	
	Exhibition.frontimg.fadeIn(Exhibition.speed,function() {
		//alert('done');
		Exhibition.timer=setTimeout("Exhibition.next()",Exhibition.delay);			
	});
	
	var curimg = Exhibition.images[Exhibition.sequence[Exhibition.current]];
	
	jQuery('#exhibition-title').html(curimg['title']?curimg['title']:Exhibition.deftitle);
	jQuery('#exhibition-description').html(curimg['description']?curimg['description']:Exhibition.defdescription);
	jQuery('#exhibition-caption').html(curimg['caption']?curimg['caption']:Exhibition.defcaption);
	jQuery('#exhibition-copyright').html(curimg['copyright']?curimg['copyright']:Exhibition.defcopyright);
	if (Exhibition.mode=="ToggleFS") Exhibition.showInfo();
	
}

function XHShowInfo() {
	//alert('here');
	if (!Exhibition.infoshown) {
		jQuery('#botrow').slideDown('slow');
		jQuery('#exhibition-info').fadeIn('slow');
		Exhibition.infoshown=true;
		Exhibition.infotimer = setTimeout('Exhibition.hideInfo()',Exhibition.infodelay);
	}	
}

function XHHideInfo() {
	clearTimeout(Exhibition.infotimer);
	Exhibition.infoshown=false;
	jQuery('#botrow').slideUp('slow');
	jQuery('#exhibition-info').fadeOut('slow');
}

function XHStopInfo() {
	clearTimeout(Exhibition.infotimer);
	Exhibition.infoshown=false;
	jQuery('#exhibition-info').hide();
}


function XHToggleFS() {
	if (Exhibition.mode!="ToggleFS") {
		Exhibition.mode="ToggleFS";
		jQuery('#midrow').hide();
		jQuery('#toprow').hide();
		jQuery('a.hide-exh').hide();
		jQuery('a.show-exh').show();
		Exhibition.showInfo();
		// bind mousemove
		//alert(jQuery('#xhbackimg'));
		jQuery('body').bind('mousemove.exhibition',XHShowInfo);
		Exhibition.delay = Exhibition.fsdelay;
		clearTimeout(Exhibition.timer);		
		Exhibition.timer=setTimeout("Exhibition.next()",Exhibition.delay);	
		setTimeout("jQuery('body').bind('click.exhibition',XHToggleFS)",1000);
	} else {
		Exhibition.mode="background";
		Exhibition.stopInfo();
		// unbind mousemove
		jQuery('body').unbind('mousemove.exhibition');
		jQuery('body').unbind('click.exhibition');
		jQuery('#toprow').show();
		jQuery('#midrow').show();
		jQuery('#botrow').show();	
		jQuery('a.hide-exh').show();
		jQuery('a.show-exh').hide();
		jQuery('#exhibition-info').hide();
		Exhibition.delay = Exhibition.bgdelay;
		clearTimeout(Exhibition.timer);
		Exhibition.timer=setTimeout("Exhibition.next()",Exhibition.delay);	
	}
}














