var busy = 0

var commentary_send = function(Id, Element){
	var Commentary = document.getElementById('commentary_'+Element).value
	if(Commentary) $.post('/req/commentary_send.php',{id:Id, element:Element, commentary:Commentary},AjaxResult)
	document.getElementById('commentary_'+Element).value = ''
}

var commentary_remove = function(Id, Element){
	if(confirm('Удалить комментарий?')) $.post('/req/commentary_remove.php',{id:Id,element:Element},AjaxResult)
}

var commentary_load = function(Element, Id, Page){
	if(!Page) Page = 0
	$.post('/req/commentary_load.php', {page:Page, element:Element, id:Id}, AjaxResult)
}

var AjaxResult = function(response){
// 	alert(response)
	eval(response)
}

var shortcuts = function(){
	$('textarea.commentary').keydown(function(e){
		if(e.ctrlKey && e.keyCode == 13){
			$(this).closest('form').submit()
			return;
		}
	})
}

var hash_init = function(){
	if(location.href.indexOf('#') > 0){
		var path = location.href.substr(location.href.indexOf('#')+1);
		var path_arr = path.split('/');
		if(typeof(path_arr[0]) !== 'undefined'){
			eval('if(typeof '+path_arr[0]+'_init == "function") '+path_arr[0]+'_init("'+path+'")')
		}
		return true;
	}
	else return false;
}

var gallery_init = function(){
	photos_on_page = Math.floor(($('#body').width()-72)/136);
	pages = Math.ceil(photos.length/photos_on_page);
	var pager = '';
	for(var i = 1; i <= pages; ++i){
		pager += '<a href="javascript:void(0)" onclick="show_thumbs('+i+')" id="page'+i+'"> </a>';
	}
	document.getElementById('navi').innerHTML = pager;
	show_thumbs(page);
}

var show_thumbs = function(pg){
	page = pg;
	$('.active', 'div.navi').removeClass('active');
	$('#page'+page).addClass('active');
	var content = ''
	for(var i = photos_on_page*(page-1); i < photos_on_page*page; ++i){
		if(i > photos.length-1) break;
		content += '<img src="'+photos[i][2]+'" onclick="photo_load('+i+');" alt="" id="th'+i+'" />'
	}
	document.getElementById('gallery_thumbs').innerHTML = content
}

var load_next_photo = function(){
	var next_i = parseInt(photo)+1;
	var next_photo = typeof photos[next_i] == 'undefined' ? 0 : next_i;
	photo_load(next_photo);
	$('div.items:animated').stop();
}

var photo_init = function(path){
	path = path.split('/');
	if(typeof path[1] == 'undefined') return;
	for(var i in photos){
		if(photos[i][0] == path[1]){
			photo_load(i);
			$('div:eq('+i+')', '.items').click();
			return;
		}
	}
}

var photo_load = function(i){
	if(!photos.length){
		document.getElementById('photo').style.textAlign = 'center';
		document.getElementById('photo').innerHTML = 'Фоток всё ещё нет...';
		return;
	}
	photo = i;

	var _page = Math.floor(i/photos_on_page)+1;
	if(page !== _page) $('#page'+_page).click();

	$('img.active', '#gallery_thumbs').removeClass('active');
	$('#th'+i).addClass('active');

	location.href = '#photo/'+photos[i][0];
	document.getElementById('commentaries_4').innerHTML = '<p style="text-align:center; padding:20px"><img src="/img/progressbar.gif" alt="" /></p>'
	commentary_load(4, photos[i][0]);
	if(!parseInt(photos[i][1])){
		$('#photo_container').html('<img src="'+photos[i][3]+'" alt="" width="'+photos[i][4]+'" height="'+photos[i][5]+'" />');
	}
	else{
		var params = {
			src:'/flash/uppod/uppod.swf',
			version:[7],
			width:500,
			height:400
		}
		var flashvars = {
			st:'/flash/uppod/style.txt',
			file:photos[i][3]
		}
		$('#photo_container').flashembed(params, flashvars)
	}
	document.getElementById('commentaries_4').style.height = photos[i][5]+'px'
}

var upload_complete = function(){
	$.post('/req/gallery/upload_complete.php',{},AjaxResult)
}

var rotate = function(degree){
	document.getElementById('photo_container').innerHTML = '<br/><br/><img src="/img/progressbar.gif" alt="..." />';
	$.post('/req/gallery/rotate.php', {id:photos[photo][0],degree:degree}, AjaxResult)
}

var delete_photo = function(){
	$.post('/req/gallery/delete_photo.php', {id:photos[photo][0]}, AjaxResult)
}

var cover = function(){
	$.post('/req/gallery/cover.php', {id:photos[photo][0]}, AjaxResult)
}

var del = function(Id, El){
	if(confirm("Удалить?"))
	$.post(
		'/req/delete.php',
		{id:Id,el:El},
		AjaxResult
	)
}

var move = function(Id, Dir, El){
	if(busy) return
	busy = 1
	$.post(
		'/req/move.php',
		{id:Id,dir:Dir,el:El},
		function(response){
			busy = 0
			eval(response)
		}
	)
}

var sms_length = function(){
	setTimeout(function(){
		var len = document.getElementById('message').value.length;
		document.getElementById('message_length').innerHTML = len;
		if(parseInt(len) > 70) document.getElementById('message').value = document.getElementById('message').value.substr(0, 70);
	}, 10)
}