var bg_img_width_max = 1300;
var bg_img_height_max = 1000;
var bg_img_ratio = 1.3;
//var bg_img_offset = 250;
var thumb_dim_big = {
		width: 150,
		height: 100
	};
var x_mv = 15;
var y_mv = 15;
var thumb_dim = {
		width: 150,
		height: 100
	};

//var lb;

var asyncUrl = "/async.php";

var tl;

$(document).ready(function(){
	//hcb_resize();
	$(window).resize(function() {
		hcb_resize();
 	});
	
	lb = new lightbox();
	
	page_init();

//	$('#bildtext').html("bildtext  ");
	
});


function lightbox(){
	var img=null;
	var myLb = $('#mylb_content_innen');
	var type="";
	this.mylb_start= function(_img){
		$('#page_margin').css('overflow','hidden');
		itemstr = _img.id.split('/').pop();
		
		if(itemstr.substr(itemstr.length-5,5)=='movie'){
			type='m';
			itemstr=itemstr.substr(0,itemstr.length-5);
			ifr=document.createElement('iframe');
			$(ifr).attr('id',"myLb_iFrame");
			$(myLb).append(ifr);
			
		}
		
		if(itemstr.substr(itemstr.length-5,5)=='image'){
			type='i';
			
		}
		
		if(type=='i'){
			itemstr=itemstr.substr(0,itemstr.length-5);
			getDirContent(itemstr,myLb);
		}
		
		if(type=='m'){
			
			$('#myLb_iFrame').attr({
				'src':"http://player.vimeo.com/video/"+itemstr+"?title=0&amp;byline=0&amp;portrait=0",
				'width':"754",
				'height':"530",
				'frameborder':"0",
				'autoplay':"1"
			});
			$('#mylb_content_innen').css({'vertical-align':"top",'padding-top':"0px"});
			
			$(ifr).css('display','block');
			$('.mylb_arrow').css('visibility','hidden');
			$('.mylb_arrow').css('visibility','hidden');
		}
		
		$('#mylb_layer').css({'display':'block'});
		$('#mylb_layer').animate({opacity:1.0},500);
		return false;
	};
	
	this.mylb_showfirst = function(){
		img=myLb.children(':first');
		img.css('display','block');
		$('.mylb_arrow').css('visibility','hidden');
		if(myLb.children().length>1){
			 $('.mylb_arrow').css('visibility','visible');
			 };
		
		
	};
	
	 this.mylb_change = function(direction){
		 //alert(myLb.children().length);
		 
		 if(direction==='r'){
			if($(img).next()[0]==undefined){
				newimg=$(img).siblings(':first')[0];
			}else{
				newimg=$(img).next()[0];
			}
			
		}else{
			if($(img).prev()[0]==undefined){
				
				newimg=$(img).siblings(':last')[0];
			}else{
				newimg=$(img).prev()[0];
			}
		}
		
		$(img).fadeOut(700,function(){
			$(newimg).css({'opacity':0});
			$(newimg).css({'display':'block'});
			$(newimg).animate({'opacity':1.0},1000,function(){img=newimg;});
			
		});
		
		
		
	};

	 this.mylb_stop = function(){
		$(img).hide();
		$(myLb).children().remove();
		$('#mylb_layer').animate({opacity:0.0},	500,	function(){$('#mylb_layer').css( {'display':'none'});});
		$('#mylb_content_innen').css({'vertical-align':"middle",'padding-top':"0px"});
		
	};
}


function thumb_loupe(){
	
	var thumbsstripe  = null;
	var thumbscount = 0;
	var thumbswidth=0;
	var x=0;
	var move_raster;
	
	this.set_tv=function(tv,e){
		thumbsstripe = $(tv).children('.thumbviewStripe');
		thumbscount=$(thumbsstripe).children().length;
		thumbswidth=thumb_dim.width*thumbscount;
		move_raster=Math.round(thumb_dim.width/thumbscount);
		x=e.pageX;
	
	};


	this.thumb_liftup =function(e){
		//move right
		if(thumbscount==1||thumbscount==0)return;
		if(e.pageX-x>10){	
			tvtmp=$(thumbsstripe).children().eq(thumbscount-1).clone(true);
			$(thumbsstripe).children().eq(thumbscount-1).remove();
			$(thumbsstripe).children().eq(0).before($(tvtmp));
			x=e.pageX;
			
		}
		//move left
		if(e.pageX-x<-10){
			tvtmp=$(thumbsstripe).children().eq(0).clone(true);
			$(thumbsstripe).children().eq(0).remove();
			$(thumbsstripe).children().eq(thumbscount-2).after($(tvtmp));
		
			x=e.pageX;
			}
		};
	}

function page_init(){
	
	page=document.URL.split("/").pop();
	switch(page) {
	case 'photo': callPhotoPage();
		$('#main_innen').css('min-height',504);
		break;
	case "motion": callMotionPage();
		break;
	case "campaign":callPhotoPage();
		$('#main_innen').css('min-height',504);
		break;
	case "news":
		break;
	case "clients":
		$('#main').css('height',904);
		break;
	case "contact":
		break;
	default:
		break;
	}	
		
}

function callPhotoPage(){
	$(".thumbviewlook").each(
			function(i){
				load_sibling_thumbs(this);
				$(this).mouseenter(function(e){tl= new thumb_loupe();tl.set_tv(this,e);});
				$(this).mouseleave(function(){delete tl;});
				$(this).mousemove(function(event){
						tl.thumb_liftup(event);
				});
				
			});
		$('#mylb_clbutton').click(function(){lb.mylb_stop();});
		$('#mylb_larrow').click(function(){lb.mylb_change('l');});
		$('#mylb_rarrow').click(function(){lb.mylb_change('r');});

}

function callMotionPage(){
	$(".thumbviewlook").each(
			function(i){
				load_sibling_thumbs(this);
				$(this).mouseenter(function(e){tl= new thumb_loupe();tl.set_tv(this,e);});
				$(this).mouseleave(function(){delete tl;});
				$(this).mousemove(function(event){
						tl.thumb_liftup(event);
				});
				
			});
		$('#mylb_clbutton').click(function(){lb.mylb_stop();});
		$('#mylb_larrow').click(function(){lb.mylb_change('l');});
		$('#mylb_rarrow').click(function(){lb.mylb_change('r');});

}

function getDirContent(dir,target){
	
	myPage=window.location.href.split('/').pop();
	var param = {page:myPage, reqArg:dir, req:'getDirContent'};
	$(target).load(asyncUrl,param, function(){lb.mylb_showfirst();});
	
	
	
}

function load_sibling_thumbs(tv){
	thumbid=tv.id.split('_').pop();
	var stripe = $(tv).children('.thumbviewStripe');
	myPage=window.location.href.split('/').pop();
	
	var param = {page:myPage,reqArg:thumbid,req:'getAllThumbs'};
	
	$(stripe).load(asyncUrl,param, function(){
		$(stripe).children('a').click(function(){lb.mylb_start(this);return false;});
		$(stripe).children('span').click(function(){lb.mylb_start(this);return false;});}
	);
			
}


/*******************************************************/


function hcb_resize(){
	akt_ratio=$(window).width()/$(window).height();

	if(akt_ratio>=bg_img_ratio){
		$("#bg_img").height($(window).height());
		$("#bg_img").width($("#bg_img").height()*bg_img_ratio);
		$("#bg_img").css("margin-top",0);			
	}
	else{
		$("#bg_img").width($(window).width());
		$("#bg_img").height($("#bg_img").width()/bg_img_ratio);
		$("#bg_img").css("margin-top",($(window).height()-$("#bg_img").height())/2);
	}	
}

function hcb_resize2(){
	
		$("#bg_img").width($(window).width());
		$("#bg_img").height($("#bg_img").width()/1.3);
		if($("#bg_img").height()<$(window).height()){
			$("#bg_img").css("margin-top",($(window).height()-$("#bg_img").height())/2);
		}
		else{
			$("#bg_img").css("margin-top",0);
		}
	//	$("#txttmp").text($(window).height()+"  :  "+$("#bg_img").height());
}


