v_old = '';
var p_scroll = '';
$(document).ready(function(){
	
	$.post('lib/get_projects.php',{},function(data){
		if(data.status == 'ok'){
			$('#c-graphic').html(build_projects(data.projects_g));
			$('#c-interaction').html(build_projects(data.projects_i));
			//$('#c-neurostatus').html(build_projects(data.projects_n));
			//$('#c-projects').html(build_projects(data.projects_p));
			//$('#c-office').html(get_text(data.c_office));
			//$('#c-contact').html(get_text(data.c_contact));
			//$('#c-activities').html(get_text(data.c_activities));
			//alert(data.texts.c_contact.text_de)
			$('#c-office').html(get_text(data.texts.c_office.text_de));
			$('#c-home').html(get_text(data.texts.c_contact.text_de));
			$('#c-activities').html(get_text(data.texts.c_activities.text_de));
			projects = data;
			$('.item').click(function(e){
				e.stopPropagation();
				id = $(this).attr('id').split('-');
				document.location.href = '#project:'+id[1]+':'+id[2];
				nav();
				
			})
			nav();	// starts the whole thing !
			var t = setInterval("nav('x')",250); 	// always checks the url 
		}
	},'json')
	
	$(document).mousemove(function(e){
		getCursor(e);
	});
	
	$(document).click(function(e){
		if(mode == 'project'){
			if(pres_mode == 'play'){
				stop_presentation();
				//getCursor(e);
			}else{
				
				if(cursor == 'right'){
					go_img(1);
				}else if(cursor == 'left'){
					go_img(-1);
				}else if(cursor == 'up'){
					go_project(-1);
				}else if(cursor == 'down'){
					go_project(1);
				}else{
					close_project();
				}
				
			}
		}
	});
	
	$(window).scroll(function(){
		if(mode != 'project'){
			p_scroll = $(document).scrollTop();
		}
	})

	
	
})

// cb custom cursor

function getCursor(e){
	if(e){
		cursor_x = 	e.pageX;
		cursor_y = 	e.pageY;
	}	
	
	if(mode == 'project'){
		if(pres_mode == 'play'){
			setCursor('stop');
		}else{
			if(cursor_x > 1000 || cursor_y < 50){
				setCursor('close');
			}else if(cursor_y < 200){
				setCursor('up');
			}else if(cursor_y > 600){
				setCursor('down');
			}else if(cursor_x < 200){
				setCursor('left');
			}else if(cursor_x > 700){
				setCursor('right');
			}else{
				setCursor('close');
			}
		}
	}else{
		setCursor('');	
	}

}

function setCursor(c){
	//if(cursor != c){
		cursor  = c;
		$('body').removeClass();
		$('body').addClass('cursor-'+c);		
		//$('body').addClass('cursor-'+c+'2');
		//t = setTimeout("fixCursor('"+c+"')",200);
	//}
}	
function fixCursor(c){
	$('body').removeClass();
	$('body').addClass('cursor-'+c);		
}
// cb projects

function build_projects(data){
	html = '';
	$.each(data, function(key, val) {
		html += build_project(key,val);	
	});
	return html;
}

function build_project(key,data){
	//html = '<div style="borderx:1px solid #ff00ff;clear:both">';
	html= '<div id="p-'+key+'-0" class="item p-title '+data.categories+'" ><div><h2>'+get_text(data.title)+'</h2></div></div>';
	c = 0;
	for(i=1;i<=data.imgs.length;i++){
		html+='<div  id="p-'+key+'-'+i+'" class="item p-img '+data.categories+'"><img src="media/small/'+data.imgs[(i-1)].img+'" width="160"></div>';
	}
	//html+= '</div>';
	return html;
}

// navigational stuff

function show_detail(){
	$('#main').hide();
	$('#detail').show();
}

function close_project(){
	set_highlight(selection[0],selection[1]);
	stop_presentation();
	document.location.href='#'+contentx;
	nav();
}

function nav(){
	var param = getParam();
	if(!param){
		document.location.href='#home';
	}
	if(param != paramold || !paramold){
		paramold = param;
		var params = param.split(':');
		setnav(params[0],params[1],params[2]);
	}
}

function setnav(v,p,n){
	
	
	if(v == 'project'){
		mode = 'project';
		$('#main').fadeOut(100);
		set_project(p,n);
		$('#project').fadeIn(100);
	}else{
		$('.subnav').hide();
		$('#sub_'+v).show();
		$('.subnav').find('a').removeClass('on');
		if(!p){
			p = 'all';
		}
		$('#n-'+v+'-'+p).addClass('on');
		
		mode = 'home';
		$('#main').fadeIn(100);
		$('#project').fadeOut(100);
		$('.mainnav').find('a').removeClass('on');
		$('#n-'+v).addClass('on');
		
		$('#c-'+contentx).find('.item').each(function(){
			if(p == 'all' || $(this).hasClass(p)){
				//$(this).fadeIn(200);
				//$(this).slideUp(200);	
				$(this).show();	
				
			}else{
				$(this).hide();
				//$(this).slideDowm(200);	
				//$(this).fadeOut(200);	
			}												
		})
		
		if(contentx != v){
			if(contentx){
				$('#c-'+contentx).fadeOut(100);
			}
			$('#c-'+v).fadeIn(100);
			contentx = v;
		}
		$(document).scrollTop(p_scroll);
		
	}
	getCursor();
}

function set_project(p,n){
	type = p.substr(0,1);
	
	if(mode == 'project'){
		if(type == 'g' || type=='i' || type=='n' || type=='p'){
			pro = projects['projects_'+type][p];
			t = get_text(pro.title);
			img_id = p+'-'+n;
			$('#project-title').html(t);
			if(n != 0){
				img = pro.imgs[(n-1)];
				// VIDEO
				if(img.video){
					html = '<div id="v'+img_id+'">'+get_video(img.video,img.img,img_id,840,630)+'</div>';
					obj = $('#v'+img_id);
					if(!obj.attr('id')){
						$('#video').append(html);
						
						jwplayer('v'+img_id).setup({
						
							'flashplayer': 'js/jw/player.swf',
							'width': '840',
							'height': '630',
							'height': '630',
							'file': 'media/videos/'+img.video,
							'image': 'media/upload/'+img.img,
							'modes': [
								{type: 'html5'},
								{type: 'flash', src: 'js/jw/player.swf'},
								{type: 'download'}
							]
						});
						
						
					}
					$('#'+previous_img).remove();
				}else{
					// IMAGES	
					obj = $('#'+img_id);
					if(!obj.attr('id')){	
						html = '<div id="'+img_id+'"class="img"><img src="media/upload/'+img.img+'" width="840"></div>';
						$('#container').append(html);
					}
					if(img_id != previous_img){
						$('#container').find('#'+img_id).onImagesLoaded(function(_this){
							$(_this).fadeIn(250,function(){
								if(previous_img){
									$('#'+previous_img).remove();
								}
								previous_img = img_id;
							});
						});
					}
				
				}
			
			}else{
				// TITLE
				html = '<div id="'+img_id+'" class="project-page"><div class="text">'+get_text(pro.text_de)+'</div></div>';
				$('#container').append(html);
				if(img_id != previous_img){
					$('#container').find('#'+img_id).onImagesLoaded(function(_this){
						$(_this).fadeIn(250,function(){
							if(previous_img){
								$('#'+previous_img).remove();
							}
							previous_img = img_id;
						});
					});
				}
			
			}
		}
	}else{
		if(contentx == 'graphic' || contentx == 'interaction'){
			set_highlight(p,n);
			
		}
	}
	selection = new Array(p,n);
}

function get_video(src,img_src,id,w,h){
	v = '';
	v+= '<video ';
	v+= 'src="media/video/'+src+'" ';
	v+= 'id="'+id+'" ';
	v+= 'poster="media/video/'+img_src+' ';
	v+= 'width="840"> ';
	v+= 'height="630"';
	v+= '>';
	v+='</video>';
	
	return v;
}

function get_text(t){
	return urldecode(t);
}

function urldecode (str) {
	return decodeURIComponent(str.replace(/\+/g, '%20'));
}

function set_highlight(p,n){
	$('.item').removeClass('hi');
	$('#p-'+p+"-"+n).addClass('hi');
}

function go_img(dir){
	num = selection[1];
	type = selection[0].substr(0,1);
	//i = projects['projects_'+type][selection[0]].imgs;
	//alert(i.length)
	total_img = projects['projects_'+type][selection[0]].imgs.length;
	
	num = eval(num) + eval(dir);
	if(num <= total_img && num >= 0){
		//set_project(selection[0],num);
		document.location.href='#project:'+selection[0]+':'+num;
	}else{	
		go_project(dir,'img');
	}
}

function go_project(dir,t){
	type = selection[0].substr(0,1);
	id = get_next_prev_project(type,selection[0],dir);
	if(id){
		if(t == 'img'){
			if(dir > 0){
				//set_project(id,0);
				document.location.href='#project:'+id+':'+0;
			}else{
				//set_project(id,projects['projects_'+type][id].imgs.length);
				document.location.href='#project:'+id+':'+projects['projects_'+type][id].imgs.length;
			}
		}else{
			//set_project(id,0);
			document.location.href='#project:'+id+':'+0;
		}
	}
}

function get_next_prev_project(type,id,dir){
	c = 0;
	check= '';
	last_id = '';
	rid = '';
	$.each(projects['projects_'+type], function(key, val) {
		if(!rid){
			if(check == 'ok' && dir > 0){
				rid = key;
			}
			if(key == id){
				if(dir < 0){
					rid = last_id;
				}
				check = 'ok';
			}
			last_id = key;
		}
	});
	return rid;
}

function playstop(){			
	if(pres_mode == 'play'){
		stop_presentation()
	}else{
		play_presentation()
	}
}
function stop_presentation(){
	pres_mode = 'stop';
	$('#playstop').text('Play');	
	window.clearInterval(presentation);
}
function play_presentation(){
	presentation = window.setInterval("go_img(1)",2500);
	pres_mode = 'play';
	$('#playstop').html('Stop');
}

// keyboards navigation

$(document).keyup(function(evt){

	var KeyCode = evt.keyCode;
	
	if(KeyCode == 37){
		$('.help').fadeOut(200);
		go_img(-1)
	}
	if(KeyCode == 39){
		$('.help').fadeOut(200);
		go_img(1)
	}
	if(KeyCode == 40){
		$('.help').fadeOut(200);
		go_project(1)
	}
	if(KeyCode == 38){
		$('.help').fadeOut(200);
		go_project(-1)
	}
	if(KeyCode == 32){
		playstop();
	}
	if(KeyCode == 13){
		key_enter();
	}
	if(KeyCode == 16){
		//key_project();
	}
	return evt.preventDefault();
	
});

function key_enter(){
	if(mode == 'project'){
		close_project();
	}else{
		//document.location.href='#project:'+selection[0]+':'+selection[1];
	}
}


// the hash getter

function getParam(){
	url = window.location.href;
	if(url.indexOf('#') > -1) {
		var url_elements = url.split('#');
		return url_elements[url_elements.length-1];
	}else{
		return '';
	}
}

