var Util = new Object();
var loadedPhotos = new Array();

Util.stripPx = function(number){
	return number.replace(new RegExp('px'), '');
}
Util.stripTags = function(text){
	return text.replace(new RegExp('<([^>]+)>', 'g'), ''); 
}
$(function(){
	$('area').hover(function(){
		var area = $(this).attr('alt');
		$(this).parent().parent().find('img').addClass('hidden');
		$(this).parent().parent().find('img.'+area).removeClass('hidden');
	}, function(){
		$(this).parent().parent().find('img').addClass('hidden');
		$(this).parent().parent().find('img.default').removeClass('hidden');
	});
	
	$('.quality-switch').data('high', true).live('click', function(){
		if($(this).data('high') == true){
			$(this).data('high', false); 
			var src = $('#low').val();
			$(this).html('Switch to HQ');
		}else{
			$(this).data('high', true); 
			var src = $('#high').val();
			$(this).html('Switch to LQ');
		}
		$('#jstest2').get(0).sendEvent('LOAD', src);
		$('#jstest2').get(0).sendEvent('PLAY', false);
		return false;
	});
});
function expand(ex){
	if($('#'+ex).data('state') == 'down'){
		$('#'+ex).slideUp(500, function(){
			$(this).data('state', 'up');
		});			
	}else{
		$('#'+ex).slideDown(500, function(){
			$(this).data('state', 'down');
		});	
	}
}
function showButton(){
	$('#header h2').fadeIn('slow');
}
function next(photos){
	var next = 0;

	$.each(loadedPhotos, function(i){
		if(loadedPhotos[i].src == $('.image').find('img').attr('src')){
			next = i+1;
		}
	});
	return next < loadedPhotos.length ? show(loadedPhotos[next]) : show(loadedPhotos[0]);
}

function popup(src){
	$('#paper').append('<div id="overlay" style="background-image:url(' + src + ');"><div id="close"></div></div>');	
	$('#subNavigation').css('visibility','hidden');
	$('#content').css('visibility','hidden');
	$('#paper').find('#overlay').show().click(function(){
			$(this).fadeOut('slow', function(){ $(this).remove()});
	});
	
	window.setTimeout(function(){
		$('#subNavigation').css('visibility','visible');
		$('#content').css('visibility','visible');
		$('#paper').find('#overlay').fadeOut('slow', function(){ $(this).remove()});
	}, 4000);
}

function prev(photos){
	var prev = 0;

	$.each(loadedPhotos, function(i){
		if(loadedPhotos[i].src == $('.image').find('img').attr('src')){
			prev = i-1;
		}
	});
	
	return prev < 0 ? show(loadedPhotos[photos.length-1]) : show(loadedPhotos[prev]);
}
function assignInfo(photo){
	
	var url = $('.image #buyable a').attr('href');
	if(photo.title.match(new RegExp('<([^>]+)>'))){
		$('.image #buyable a').attr('href', url.replace(new RegExp('([0-9]+)'), photo.id));
		$('.image #buyable').css('display', 'block');
	}else{
		$('.image #buyable').css('display', 'none');					
	}
	$('.image .info .title').find('strong').html(Util.stripTags(photo.title));
	$('.image .info .subtitle').find('strong').html(photo.subtitle);
	$('.image .info .infoText').find('strong').html(photo.text);
	$('.image .info .download').find('a').attr('href', ($('.image .info .download').find('a').attr('href')).replace(new RegExp('([0-9]+)'), photo.id));
	
	return true;
	
}
function show(photo){
	if($('body').data('state') == 'ready'){
		$('body').data('state', 'loading');

		/* FALLBACK! */
		window.setTimeout(function(){
			if($('body').data('state') != 'ready') fadeIn(photo);
		}, 1000);

		$(new Image).attr('src', photo.src).load(function(){
			fadeIn(photo);
		});		
	}
	return false;
}
function fadeIn(photo){
	$('body').data('state', 'ready');

	$('.image .slide').attr('src', photo.src);
	$('.image').find('.img').fadeOut(400, function(){
		if(assignInfo(photo)){
			$(this).attr('src', photo.src).css('display', 'block');
			$('.image .slide').fadeOut();	
		}
	});
}
$(function(){
	$('body').data('state', 'ready');
	
	$('#paper').append('<img src="" id="preload" style="display:none" />');
	$('.prev').click(function(){ if(!$(this).hasClass('noscript')) return prev(loadedPhotos);});
	$('.next').click(function(){ if(!$(this).hasClass('noscript')) return next(loadedPhotos);});
	
	$('.imageWrapper').hover(function(){
		$(this).find('.info').fadeIn('fast');			
	}, function(){
		$(this).find('.info').fadeOut('fast');
	});
	
	fillScroller();
});

function enableScroller(){
	if(loadedPhotos.length > 20){
		var h = (Math.ceil(loadedPhotos.length / 2)) * 55;
		$('#scroller').mousemove(function(e){
			var y = e.pageY - ($(this).offset()).top;
			var test = (y * (h - 472)) / 472;
			$(this).find('ol').css('top', -test + 'px');
		});	
	}
}
function enableScroller2(){
	if(loadedPhotos.length > 12){
		var h = (Math.ceil(loadedPhotos.length / 2)) * 55;
		$('.videoSite #scroller').mousemove(function(e){
			var y = e.pageY - ($(this).offset()).top;
			var test = (y * (h - 279)) / 279;
			$(this).find('ol').css('top', -test + 'px');
		});	
	}
}
function fillScroller(){
	if(photos.length > 0){
		var thumb = document.createElement('img');
		thumb.src = (photos[0].src).replace(new RegExp('large'), 'thumb');
		var intv = window.setInterval(function(){
			if(thumb.complete){
				clearInterval(intv);
				loadedPhotos.push(photos[0]);	
				$('.imageScroller #scroller ol').append('<li id="photo_' + loadedPhotos.length + '"><a href="' + (photos[0].url ? photos[0].url : '#') + '"><img src="' + thumb.src + '" style="display: none"/></a></li>');
				$('.videoSite #scroller #grid').append('<li id="photo_' + loadedPhotos.length + '"><a href="' + (photos[0].url ? photos[0].url : '') + '"><img src="' + thumb.src + '" style="display: none"/></a></li>');	
				$('#photo_' + loadedPhotos.length).find('a').click(function(){
					var id = ($(this).parent().attr('id')).replace(new RegExp('^photo_([0-9]+)$'), '$1');
					if(($(this).attr('href')).match('#')) return show(loadedPhotos[(id-1)]);
				});
				$('#photo_' + loadedPhotos.length).find('img').fadeIn(400);
				photos.shift();
				fillScroller();									
			}
		}, 1);
		return true;
	}
	enableScroller();
	enableScroller2();
	return false;
}

function overview(){
	$('#overview').find('li a').each(function(i, category){
		/* SLIDESHOW FOR CATEGORIES WITH PREVIEWS > 1 */
		if($(category).find('img.hidden').length > 1){
			$(category).hover(function(){
				if($(category).data('running') == false){
					if($(this).find('img.hidden').length == 0){
						$(category).data('running', true);
						overviewNext(category, 0);					
					}
					
				}
			},function(){
				$(category).data('running', false);
			});
		}
		/* PRELOADER */
		$(category).find('img.hidden').load(function(){
			$(this).removeClass('hidden');
			if($(category).find('img.hidden').length > 0){
				$(category).find('img.hidden').eq(0).attr('src', $(category).find('img.hidden').eq(0).attr('alt'));
			}else{
				$(category).fadeIn(250);
			}
		});
		$(category).data('running', false).find('img.hidden').eq(0).attr('src', $(category).find('img.hidden').eq(0).attr('alt'));
	});	
}
function overviewNext(category, i){	
	var l = $(category).find('img').length-1;
	if($(category).data('running')){
		$(category).find('img').eq(l-i).fadeOut(1500, function(){
			$(this).css('z-index', 0).css('display', 'block');
			$(category).find('img').each(function(i2, img){
				var z = parseInt($(img).css('z-index'))+1; // 3 statt 21 bei 2+1
				$(img).css('z-index', z);
				if(i2 == l){
					if($(category).data('running')) overviewNext(category, i+1 <= l ? i+1 : 0);					
				}
			});
		});		
	}
}