
var aivao_mobile = false;
if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) { 
	aivao_mobile = true;
}

var aivao_browser = 'firefox';
if(navigator.userAgent.match(/Safari/i) || navigator.userAgent.match(/Webkit/i)) { 
	aivao_browser = 'safari';
}



//alert(aivao_browser);
function aivao_catch_a(options){
	
	$("a").click(function(e){
		if($.trim($(this).attr("href")) == "#"){
		 	e.preventDefault();
		}
	});
}

function strrpos (haystack, needle, offset) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   input by: saulius
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strrpos('Kevin van Zonneveld', 'e');
    // *     returns 1: 16
    // *     example 2: strrpos('somepage.com', '.', false);
    // *     returns 2: 8
    // *     example 3: strrpos('baa', 'a', 3);
    // *     returns 3: false
    // *     example 4: strrpos('baa', 'a', 2);
    // *     returns 4: 2
    var i = -1;
    if (offset) {
        i = (haystack + '').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,
        // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
        if (i !== -1) {
            i += offset;
        }
    } else {
        i = (haystack + '').lastIndexOf(needle);
    }
    return i >= 0 ? i : false;
}

function aivao_alertinfo(options){
	if (options['duration'] === undefined) {
		options['duration'] = 2000;
	}
	$.prompt(options['html'],{
		opacity: 0.8,
		top:'33%',
		persistent:true,
		timeout:options['duration'],
		prefix:'ldng'
	});
}


function aivao_alert_close(options){
	$.prompt.close();
}

function aivao_alert(options){
	
	$.prompt(options['html'],{
		opacity: 0.8,
		top:'20%',
		persistent:true
	});
}
function aivao_alert_close(options){
	$.prompt.close();
}

var aivao_isloading = false;
function aivao_loading(options){
	if(!aivao_isloading){
		aivao_isloading = true;
		$.prompt('<img src="/cms/front/pix/loading.gif" />',{
			opacity: 0.8,
			top:'44%',
			persistent:false,
			prefix:'ldng'
		});
	}
}
function aivao_loading_close(options){
	$.prompt.close();
	aivao_isloading = false;
}
function aivao_jsp(options){
	var speed = 5;
	if(aivao_browser == 'safari'){
		speed = 0.1;
	}
	if(!aivao_mobile){
		$('#container').jScrollPane({
			'showArrows': false,
			'mouseWheelSpeed':speed
		});
		
	}
}

function aivao_resize_bg(options){
	
	var w = $(window).width();
	var h = $(window).height();
	
	//alert(w+' '+h);
	$('#whiteframe').css({
		'width':w-30+'px',
		'height':h-50+'px'
	});
	var minpos = 30;
	var thumbsheight = 491;
	var newpos = Math.round((h-thumbsheight)/2)-21;
	
	if(newpos < minpos){
		newpos = minpos;
	}
	
	$('#thumbs .inner').css({
		'top':newpos+'px'
	});
	$('#page .inner').css({
		'top':newpos+'px'
	});
	var arrowheight = 170;
	newpos = Math.round((h-arrowheight)/2)-10;
	
	if(newpos < minpos){
		newpos = minpos;
	}
	
	$('.arrow').css({
		'top':newpos+'px'
	});
	var r = w/h;
	var nw = '';
	var nh = '';
	var x = 0;
	if(r > 1.5){
		 nw = w+'px';
		 nh = 'auto';
	}
	else{
		nw = 'auto';
		nh = h+'px';
	}
	$('#bodybg img').css({
		'width':nw,
		'height':nh
	});
	
}

var aivao = {
	images:[],
	pagepos:0,
	imgpos:0,
	gallerypos:0,
	gallery_id:0,
	next:0,
	prev:0,
	lines:4,
	cols:5,
	lineheight:118
};
function aivao_loadimgarr(options){
	var aw = window.screen.availWidth;
	var repl = 'small/';
	if(aw > 1800){
		repl = '';
	}
	else if(aw > 1200){
		repl = 'medium/';
	}
	$('#innerthumbs').find('.color').each(function(i){
		var src = $(this).attr('src');
		var gallery_id = $(this).parent().attr('gallery_id');
		var bigsw = $(this).parent().attr('bigsw');
		var bigimg = src.replace('gallerythumb/', repl);
		 bigsw = bigsw.replace('gallerythumb/', repl);
		var obj = {src:src,gallery_id:gallery_id,img_id:$(this).parent().attr('img_id'),bigimg:bigimg,bigsw:bigsw,title:$(this).parent().attr('title')};
		
		aivao['images'].push(obj);
		
		$('#preloadimg').append('<img src="'+bigimg+'" /><img src="'+bigsw+'" />');
		if(i==0){
			aivao['gallery_id'] = gallery_id;
		}
	});
	
	aivao['prev'] = aivao['images'].length-1;
	aivao['next'] = 1;
	
	$('#arrowleft .view').html('<img src="'+aivao['images'][aivao['prev']]['src']+'" />');
	$('#arrowright .view').html('<img src="'+aivao['images'][aivao['next']]['src']+'" />');
	$('#innerthumbs').find('div[img_id="'+aivao['images'][aivao['imgpos']]['img_id']+'"]').find('.sw').hide();
	$('#innerthumbs').find('div[gallery_id="'+aivao['images'][aivao['imgpos']]['gallery_id']+'"]').find('.blue').hide();
}
function aivao_switchimg(options){
	
	$('#innerthumbs').find('div[img_id="'+aivao['images'][aivao['imgpos']]['img_id']+'"]').find('.sw').show();
	options['direct'] = options['direct'] || 'next';
	options['posi'] = options['posi'] || -1;
	if(options['direct'] == 'single'){
		aivao['imgpos'] = parseInt(options['posi']);
	}
	else{
		if(options['direct'] == 'next'){
			aivao['imgpos']++;
			if(aivao['imgpos'] >= aivao['images'].length){
				aivao['imgpos'] = 0;
			}
		}
		else if(options['direct'] == 'prev'){
			aivao['imgpos']--;
			if(aivao['imgpos'] < 0){
				aivao['imgpos'] = aivao['images'].length-1;
			}
		}
	}
	
	var title = aivao['images'][aivao['imgpos']]['title'];
	$('#imgtitle').html(title);
	$('#innerthumbs').find('div[img_id="'+aivao['images'][aivao['imgpos']]['img_id']+'"]').find('.sw').hide();
	
	
	
	if(aivao['gallery_id'] != aivao['images'][aivao['imgpos']]['gallery_id']){
		var newgalpos = (aivao['imgpos']+1)/10;
		var steps = 1;
		//alert(newgalpos+' '+(aivao['gallerypos']+1));
		if(newgalpos > (aivao['gallerypos']+1)){
			steps = Math.ceil(newgalpos-(aivao['gallerypos']+1));
			aivao_thumbsmove({direct:'down',steps:steps});
		}
		if(newgalpos < (aivao['gallerypos']+0)){
			steps = Math.ceil((aivao['gallerypos']+0)-newgalpos);
			aivao_thumbsmove({direct:'up',steps:steps});
		}
		
		//alert(steps);
		$('#thumbsnavi').find('a[gallery_id="'+aivao['gallery_id']+'"]').removeClass('on');
		$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').show();
		aivao['gallery_id'] = aivao['images'][aivao['imgpos']]['gallery_id'];
		$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').hide();
	}
	
	aivao['prev'] = aivao['imgpos']-1;
	if(aivao['prev'] < 0){
		aivao['prev'] = aivao['images'].length-1;
	}
	aivao['next'] = aivao['imgpos']+1;
	if(aivao['next'] >= aivao['images'].length){
		aivao['next'] = 0;
	}
	$('#bodybg img.color').attr('src', aivao['images'][aivao['imgpos']]['bigimg']);
	$('#bodybg img.bigsw').attr('src', aivao['images'][aivao['imgpos']]['bigsw']);
	$('#arrowleft .view').html('<img src="'+aivao['images'][aivao['prev']]['src']+'" />');
	$('#arrowright .view').html('<img src="'+aivao['images'][aivao['next']]['src']+'" />');
	
	
}


function aivao_thumbsmove(options){
	var speed = 500;
	var steps = options['steps'] || 1;

	if(options['direct'] == 'down'){
		var maxpos = (aivao['images'].length)/(aivao['lines']*aivao['cols'])+3;
		aivao['gallerypos'] += steps;
		$('#arrowup').show();
		if(aivao['gallerypos'] >= maxpos){
			$('#arrowdown').hide();
		}
	}
	else if(options['direct'] == 'up'){
		aivao['gallerypos'] -= steps;
		$('#arrowdown').show();
		if(aivao['gallerypos'] == 0){
			$('#arrowup').hide();
		}
	}
		
	
	$('#thumbsstage').animate({
		top: -(aivao['gallerypos']*(aivao['lineheight']*2))
	}, speed);

	
}

function aivao_showimage(options){
	var title = $('#imgtitle').html();
	if($.trim(title) == ''){
		$('#imgtitle').hide();
	}
	else{
		$('#imgtitle').show();
	}
	$('#close').hide();
	$('#blue').hide();
	$('#thumbs').hide();
	$('#page').hide();
	$('#thumbsbt').show();
	$('#about').hide();
	$('#bodybg .bigsw').hide();
	$('#bodybg .color').show();
	$('#about2').show();
	$('#imgtitle').show();
}

function aivao_loadpage(options){
	aivao['pagepos'] = 0;
	$('#container .main').fadeOut(300, function () {
		$.post('/cms/front/ajax/loadpage.php', options, function(data) {
			
			
			if(data['num'] > 1){
				$('#container .main').addClass('advertising');
				$('#container .pagearrow').fadeIn(200);
			}
			else{
				$('#container .pagearrow').fadeOut(200);
				$('#container .main').removeClass('advertising');
			}
			/*
			if(data['html'].search(/\.mov/) > -1 && data['html'].search(/embed/) > -1){
				data['html'] = '<div class="video">'+data['html']+'</div>';
			}
			*/
			$('#container .main').html(data['html']);
			checkloadvideo();
			
			/*
			$('.main a').each(function(i){
				var href = $.trim($(this).attr('href'));
				//alert(href);
				if(href.search(/\.mp4/) > -1 || href.search(/\.webm/) > -1 || href.search(/\.ogv/) > -1){
					//alert(1);
					$(this).wrap('<div class="video" />');
					$('.video').empty();
					//href = 'http://www.dietmartheis.de'+href;
					var shorthref = href.substr(0, strrpos(href, '.'));
					alert(shorthref);
					var html = '';
					$('.video').html(' <video class="video-js" width="640" height="360" controls preload><source src="'+shorthref+'.mp4" type="video/mp4" /><source src="'+shorthref+'.ogv" type="video/ogg" /><source src="'+shorthref+'.webm" type="video/webm" /><object width="640" height="360" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf"><param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" /> <param name="allowfullscreen" value="true" /> <param name="flashvars" value=\'config={"playlist":[{"url": "'+href+'","autoPlay":false,"autoBuffering":true}]}\' /> </object> </video> </div>');
					//$('video').VideoJS();
				}
			});
			*/
			$('#container .main').fadeIn(300);
				
		}, 'json');
	});
}

function checkloadvideo(){
	$("#multipages > .multipage").eq(aivao['pagepos']).find('a').each(function(i){
		var href = $.trim($(this).attr('href'));
		//alert(href);
		if(href.search(/\.mov/) > -1 || href.search(/\.m4v/) > -1){
			//alert(1);
			$(this).wrap('<div class="video" />');
			$("#multipages > .multipage").eq(aivao['pagepos']).find('.video').empty();
			//href = 'http://www.dietmartheis.de'+href;
			var html = '';
			$("#multipages > .multipage").eq(aivao['pagepos']).find('.video').html('<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="376" width="640"><param name="SRC" value="'+href+'" /><param name="AUTOPLAY" value="true" /><param name="CONTROLLER" value="true" /><param name="TYPE" value="video/quicktime" /><param name="SCALE" value="ASPECT" /><param name="TARGET" value="myself" /> <embed src="'+href+'" target="myself" controller="true" loop="false" autoplay="true" plugin="quicktimeplugin" type="video/quicktime" cache="false" pluginspage="http://www.apple.com/quicktime/download/" scale="ASPECT" height="376" width="640"></embed></object>');
			$("#multipages > .multipage").eq(aivao['pagepos']).find('p').each(function(){
				if($(this).find('.video').length < 1){
					$(this).addClass('with-video');
				}
			});
			
		}
	});

}


function aivao_switchpage(options){
	
	var num = $("#multipages > .multipage").size();
	options['direct'] = options['direct'] || 'next';
	$("#multipages > .multipage").eq(aivao['pagepos']).fadeOut(300, function(){
		if(options['direct'] == 'next'){
			aivao['pagepos']++;
			if(aivao['pagepos'] >= num){
				aivao['pagepos'] = 0;
			}
		}
		else if(options['direct'] == 'prev'){
			aivao['pagepos']--;
			if(aivao['pagepos'] < 0){
				aivao['pagepos'] = num-1;
			}
		}
		$("#multipages > .multipage").eq(aivao['pagepos']).fadeIn(300);
		checkloadvideo();
	});
	
	
	
	
}

$(function() {	
	var ww = $(window).width();
	var aw = window.screen.availWidth;
	var ah = window.screen.availHeight-1;
	window.moveTo(0,1);
	window.resizeTo(ww, ah);
	var wh = $(window).height();
	var neww = Math.round(wh*1.5);
	var minw = 1140;
	if(neww < minw){
		neww = minw;
	}
	if(neww > aw){
		neww = aw;
	}
	window.resizeTo(neww, ah);
	aivao_resize_bg({});
	//alert(ww+' '+dw+' '+wh+' '+dh+' '+aw+' '+ah);
	//$('#feedback').html(ww+'x'+wh+' | '+aw+' '+ah);
	
	$('#navi a').click(function(e) {
	 	e.preventDefault();
	 	$('#navi a').removeClass('on');
	 	$(this).addClass('on');
	 	$.address.value($(this).attr('href')); 
	 	aivao_loadpage({folder_id:$(this).attr('folder_id')});
	 	
	});
	
	$('.hide').hide();
	$('#container').focus(function() {
	  $(this).blur();
	});
	$('.pagenavi a').focus(function() {
	  $(this).blur();
	});
	aivao_catch_a({});
	$('#close').click(function() {
		aivao_showimage({});
	});
	$('#startlogo').click(function() {
		
		$('#startlogo').hide();
		aivao_showimage({});
		
	});
	$('#thumbsbt').click(function() {
		$('#blue').show();
		$('#close').show();
		$('#thumbs').show();
		$('#thumbsbt').hide();
		$('#about2').hide();
		$('#about').show();
		$('#imgtitle').hide();
		$('#bodybg .bigsw').show();
		$('#bodybg .color').hide();
	});
	$('#about, #about2').click(function() {
		$('#blue').show();
		$('#close').show();
		$('#page').show();
		$('#thumbs').hide();
		$('#thumbsbt').hide();
		$('#about2').hide();
		$('#about').show();
		$('#imgtitle').hide();
		$('#bodybg .bigsw').show();
		$('#bodybg .color').hide();
	});
	
	$('#arrowup').click(function() {
		aivao_thumbsmove({direct:'up'});
	});
	$('#arrowdown').click(function() {
		aivao_thumbsmove({direct:'down'});
	});
	
	var arwidth = 165;
	var artimeout = 600;
	var arspeed = 420;
	$("#arrowleft").hoverIntent({
		over: function(){
			$(this).animate({
				left: 0
			}, arspeed);
		},
		timeout: artimeout,
		out: function(){
			$(this).animate({
				left: -arwidth
			}, arspeed);
		}
	});
	$("#arrowright").hoverIntent({
		over: function(){
			$(this).animate({
				right: 0
			}, arspeed);
		},
		timeout: artimeout,
		out: function(){
			$(this).animate({
				right: -arwidth
			}, arspeed);
		}
	});
	
	$("#thumbsnavi a").hover(
		function(){
			
			if($(this).attr('gallery_id') != aivao['gallery_id']){
				$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').show();
				$('#innerthumbs').find('div[gallery_id="'+$(this).attr('gallery_id')+'"]').find('.blue').hide();
			}
		},
		function(){
				
			if($(this).attr('gallery_id') != aivao['gallery_id']){
				
				$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').hide();
				$('#innerthumbs').find('div[gallery_id="'+$(this).attr('gallery_id')+'"]').find('.blue').show();
			}
		}
	);
	
	$("#thumbsnavi a").click(function(){
		if($(this).attr('gallery_id') != aivao['gallery_id']){
			var posi = 0;
			$('#innerthumbs').find('div[gallery_id="'+$(this).attr('gallery_id')+'"]').each(function(i){
				if(i==0){
					posi = $(this).attr('posi');
				}
			});
			//aivao_showimage({});
			aivao_switchimg({direct:'single',posi:posi});
		}
	});
	
	$(".thumb").hover(
		function(){
			
			if($(this).attr('posi') != aivao['imgpos']){
				$(this).find('.sw').hide();
				
				if($(this).attr('gallery_id') != aivao['gallery_id']){
					$(this).find('.blue').hide();
					$('#thumbsnavi').find('a[gallery_id="'+$(this).attr('gallery_id')+'"]').addClass('on');
					$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').show();
					$('#innerthumbs').find('div[gallery_id="'+$(this).attr('gallery_id')+'"]').find('.blue').hide();
				}
			}
		},
		function(){
			if($(this).attr('posi') != aivao['imgpos']){
				$(this).find('.sw').show();
				
				if($(this).attr('gallery_id') != aivao['gallery_id']){
					$(this).find('.blue').show();
					
					$('#thumbsnavi').find('a[gallery_id="'+$(this).attr('gallery_id')+'"]').removeClass('on');
					$('#innerthumbs').find('div[gallery_id="'+aivao['gallery_id']+'"]').find('.blue').hide();
					$('#innerthumbs').find('div[gallery_id="'+$(this).attr('gallery_id')+'"]').find('.blue').show();
				}
			}
		}
	);
	
	$(".thumb").click(function(){
		
		if($(this).attr('posi') != aivao['imgpos']){
			
			aivao_switchimg({direct:'single',posi:$(this).attr('posi')});
			
		}
		aivao_showimage({});
	});
	
	
	$("#arrowleft").click(function() {
		aivao_switchimg({direct:'prev'});
	});
	$("#arrowright").click(function() {
		aivao_switchimg({direct:'next'});
	});
	
	$("#pagearrowleft").click(function() {
		aivao_switchpage({direct:'prev'});
	});
	$("#pagearrowright").click(function() {
		aivao_switchpage({direct:'next'});
	});
	
	$(window).resize(function(){
		aivao_resize_bg({});
	});
	aivao_loadimgarr({});
	
	
	
	
});



