function toggleNews(lnk){
	
	$(lnk).blur();
	$(lnk).parent().next().toggle();
	
	if($('span', lnk).html() == '&gt;&gt;&gt;')
		$('span', lnk).html('&lt;&lt;&lt;')
	else
		$('span', lnk).html('&gt;&gt;&gt;')
}

function hideNews(cnt){
	var t = $(cnt).parent().parent().prev().children('a');
	toggleNews(t);
}

var cur_step = 0;
var cur_photo = 0;

function changeProjectPhoto(photo, title, cimg){
	$('#big_photo > img').attr('src', '/img/projects/' + photo);
	$('#photo_title').html(title);
	if(cimg){
		cur_photo = $(cimg).parent().prevAll().length;
	}
}

function showNextPhoto(dir){
	
	/* get photos num */
	var tds = $('td', $('#project_photos_cnt'));
	
	if(dir == 1 && cur_photo<tds.length-1 || dir == 0 && cur_photo > 0){
		
		cur_photo = cur_photo + (dir ? 1 : -1);
		/* cur img */
		var cur_img = $('img', $(tds[cur_photo]));
		changeProjectPhoto($(cur_img).attr('rel'), $(cur_img).attr('title'), null);
		
		var pos = $(tds[cur_photo]).position();
		var tbl_pos = $('#project_photos_cnt').position();
		var cnt_width = $('#project_photos').width();
		if(dir == 1 && pos.left-(cnt_width*cur_step) >= cnt_width || dir == 0 && pos.left < tbl_pos.left*-1){
			showNextPhotos(dir);
		}
		
	}
	
}

function showNextPhotos(dir){
	
	var container = document.getElementById('project_photos');
	var pc = document.getElementById('project_photos_cnt');
	var cw = $(container).width();
	var pw = $(pc).width();
	
	var pos = $(pc).position();
	var new_left = pc.left;
	var show_ph = null;
	if(dir == 1){		
		if(pos.left-cw+pw > 0){
			cur_step++;
			new_left = pos.left - cw;
		}
	}else{
		if(pos.left + cw <= 0){
			cur_step--;
			new_left = pos.left+cw;
		}
	}
	/*
	var pid = (cur_step*6) + 1;
	show_ph = $('#ph' + pid);
	if(show_ph){
		changeProjectPhoto($(show_ph).attr('rel'), $(show_ph).attr('title'));
	}
	*/
	pc.style.left = new_left + 'px';
	
}

function openProjectPopup(url, pid){
	
	var p = document.getElementById('project_popup');
	if(!p){
		p = document.createElement('div');
		p.setAttribute('id', 'project_popup');
		document.body.appendChild(p);
	}
	
	showSandBox();	
	
	var l = ($(window).width()-$(p).width())/2;
	$(p).css({'left': l});
	$(p).css({'top': $(document).scrollTop() + 20});	
	$(p).show();
	
	$.get(url, {'id': pid}, onProjectLoaded);
	
}

function onProjectLoaded(res){
	$('#project_popup').html(res);
}

function closeProjectPopup(){
	hideSandBox();
	$('#project_popup').html('').hide();
}

function showSandBox(){
	var s = document.getElementById('sandbox');
	if(!s){
		s = document.createElement('div');
		s.setAttribute('id', 'sandbox');
		$(s).click(function(){closeProjectPopup;});
		document.body.appendChild(s);
	}
	s.style.height = $(document).height() + 'px';
	$(s).show();
}

function hideSandBox(){
	var s = document.getElementById('sandbox');
	if(s){
		s.style.display = 'none';
	}
}
