wesc.fs = {};

wesc.fs.init = function() {
	$(window).resize(function(){
		//adjust itemdisplay
		// $('.Picture .itemdisplay').css({
		// 	height:($(window).height()-$('#header').height()-$('.iteminfo').height())+'px'
		// });
		
		$('.Video .itemdisplay').css({
			height:($(window).height()-$('#header').height())+'px'
		});
	}).trigger('resize');
	
	wesc.fs.setupFullScreenImage();
	wesc.fs.tagZoom();
	wesc.fs.setupSleeptimer();
}

//hide the interface after a certain time without mouse movement
wesc.fs.setupSleeptimer = function(){
	if(wesc.mobileOrTouch())return;
	//remove fsif class from info for images
	$('.Picture .iteminfo').addClass('nosleep');
	
	$(window).mousemove(wesc.fs.onMouseMoved);
	$('.fsif').hover(function(){
		wesc.fs.wakeup();
	},function(){
		wesc.fs.startSleep();
	});
	
	wesc.fs.startSleep();
}
wesc.fs.startSleep = function(){
	clearTimeout(wesc.fs.sltimer);
	if(wesc.fs.sleepdisabled)return;
	s = $('.Picture').length ? 3000 : 1800;
	wesc.fs.sltimer = setTimeout(wesc.fs.sleep,s);	
	// console.log('sleep in '+s);
}
wesc.fs.sleep = function(){
	clearTimeout(wesc.fs.sltimer);
	wesc.fs.sleeping = true;
	$('.fsif:not(.nosleep)').stop().animate({opacity:0},400);
}

wesc.fs.onMouseMoved = function(){
	if(!wesc.fs.sleeping)return;
	wesc.fs.wakeup();
	wesc.fs.startSleep();
}
wesc.fs.wakeup = function(){
	$('.fsif:not(.nosleep)').stop().animate({opacity:1},50);
	wesc.fs.sleeping = false;
	clearTimeout(wesc.fs.sltimer);
}

wesc.fs.disableSleep = function(){
	// console.log('disableSleep');
	wesc.fs.sleepdisabled = true;
	wesc.fs.wakeup();
	
}
wesc.fs.enableSleep = function() {
	// console.log('enableSleep');
	wesc.fs.sleeping = false;
	wesc.fs.sleepdisabled = false;
	wesc.fs.startSleep()
}

wesc.fs.tagZoom = function(){
	if(!$('.fulltagslist').length)return;
	$('.tagsinfo').css({opacity:0});
	$('<div/>',{
		id:'popup',
		css: {
			position: "absolute",
			display:"none",
		}
	}).prependTo('.fulltagslist');
	
	$('.fulltagslist .taglist li').hover(function(){
		//over
		if(!$('.tagsinfo').data('oldc')) {
			$('.tagsinfo').data('oldc',$('.tagsinfo .cinfo').text());
		}
		l = $('a',this).eq(0);
		$('.tagsinfo .cinfo').text(l.attr('title'));
		$('.tagsinfo .ginfo').css({opacity:.5});
		p = '';
		$('.tagsinfo').css({opacity:1});
		if($('.shopinfo',this).length) {
			p = $('.shopinfo',this).html();
		}
		i = $('img',l);
		i.css({opacity:.6});
		$('#popup').html('<img src="'+wesc.adjustImageSize(i.attr('src'),'bigsquare','default')+'"/>'+p).show();
	},function(){
		//out
		$('.tagsinfo').css({opacity:0});
		//$('.tagsinfo .ginfo').css({opacity:1})
		$('#popup').hide();
		$('img',this).css({opacity:1});
		if($('.tagsinfo').data('oldc')) $('.tagsinfo .cinfo').text( $('.tagsinfo').data('oldc') );
	});
}





/**
 * Enable zoom on fullscreen image/item view
 */
wesc.fs.setupFullScreenImage= function()
{
	var img = $('.itemdisplay .image img');
	if (!img.length) return false;
	
	$('.itemdisplay').css({ position: 'relative' });
	img.css({ position: 'absolute', display: 'block' ,opacity:0});
	img.animate({opacity:1},{duration:1000,easing:'easeOutExpo'});
	wesc.fs.oldzoom = wesc.fs.zoom = false;
	img.load(function() {
		$(window).trigger('resize');
	})
	
	img.click(function() {
		// if($(this).data('ow') <= 520) return false;
		wesc.fs.oldzoom = wesc.fs.zoom;
		wesc.fs.zoom = !wesc.fs.zoom;
		ov = (wesc.fs.zoom) ? 'scroll' : 'hidden';
		if(wesc.fs.zoom) {
			$('.iteminfo').hide();
		} else {
			$('.iteminfo').show();
		}
		wesc.fs.resizeFS();
		$('.itemdisplay').css({overflow: ov}).animate({scrollTop:0},{duration:500,easing:'easeInOutExpo'});
	});
	
	// if (img[0].complete) {
	// 	img.trigger('load');
	// }
	// window-resize re-position image in center
	$(window).resize(function() {
		wesc.fs.resizeFS();
	}).trigger('resize').load(function(){
		wesc.fs.resizeFS();
	});

}

wesc.fs.resizeFS= function(first)
{
	i = $('.itemdisplay .image img');
	
	if(!i.data('ow')) {
		// if(i.attr('width')) {
			i.data('ow',i.attr('width'));
			i.data('oh',i.attr('height'));
		// } else if(!i.width() > 0) {
		// 	i.data('ow',i.width());
		// 	i.data('oh',i.height());
		// };
	}

	w = i.data('ow');
	h = i.data('oh');
	ww = mw = $(window).width();
	wh = mh = $(window).height();
	r = w/h;
	tx = ty = 0;
	

	if(!wesc.fs.zoom) {
		//unzoomed
		mh -= ($('.iteminfo').height() + $('#header').height()+20);
		wh = mh;
	} else {
		mh -= $('#header').height();
		// wh = mh;
		//zoomed
		if(r>1) {
			mh = h*2;
		} else {
			mh = h*1.5;
		}
		
	}
	
	if(r < 1 && r*mh < mw){  //hochformat
		h = mh;
		w = r*h;
	} else if(r > 1 && mw/r < mh){  //querformat
		w = mw;
		h = w/r;
	} else {
		h = mh;
		w = r*h;
	}

	
	w = Math.round(w);
	h = Math.round(h);
	ty = Math.round((wh-h)/2);
	tx = Math.round((ww-w)/2);
	ty = (ty<0) ? 0 : ty;
	
	if(w == 0 || h== 0)return;
	ani = (wesc.fs.oldzoom != wesc.fs.zoom)
	if(!ani){
		i.css({overflow:'hidden',width:w+'px',height:h+'px',top:ty+'px',left:tx+'px'});
	} else {
		i.animate({overflow:'hidden',width:w+'px',height:h+'px',top:ty+'px',left:tx+'px'},500,'easeInOutExpo');
	}
	// i.css({overflow:'hidden',width:w+'px',height:h+'px',top:ty+'px',left:tx+'px'});
}


//
wesc.fs.hideInterface =  function() {
	$('.fsif').hide();
}

wesc.fs.showInterface =  function() {
	$('.fsif').show();
}

$(wesc.fs.init);
