function get_obj(id_name) {
	if (document.getElementById) {
		return document.getElementById(id_name);
	} else if (document.all) {
		return document.all[id_name];
	} else {
		return null;
	}
}

function callToServer(URL) {
	var IFrameDoc;

	IFrameObj = get_obj('RSIFrame');

	if (IFrameObj.contentDocument) { // For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) { // For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) { // For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	
	IFrameDoc.location.replace(URL);
	return false;
}

function noF5(URL) {
	callToServer('');
	setTimeout ("location.href='"+URL+"'", 1000);
}

var agt = navigator.userAgent.toLowerCase();
var isIE = (agt.indexOf("msie") != -1);

function doshow (objname,inline) {
	obj_fl = get_obj(objname); if (!obj_fl) return false;
	if (isIE || inline==1) obj_fl.style.display = "inline"; else obj_fl.style.display = "table-row";

}
function dohide (objname) {
	obj_fl = get_obj(objname); if (!obj_fl) return false;
	obj_fl.style.display = "none";
}

function doshowhide (objname) {
	obj = get_obj(objname);
	if (obj.style.display == 'table-row' || obj.style.display == 'inline') {
		dohide(objname);
	} else {
		doshow(objname);
	}
}

function imgUpload() {
	$('f').submit();
	dohide('uplinput'); doshow('loading', 1);
}
function imgClear() {
	$('uplinput').innerHTML = '<input type="file" name="userfile" id="userfile" onchange="imgUpload();">';
	dohide('loading'); doshow('uplinput', 1);
}

function imgTop(id) {
	first = $('image_gallery').getElement('div').get('id');
	doshow('u'+first, 1);
	$('image_gallery').grab($('p'+id),'top');
	dohide('up'+id);
}

function imgAdd(id, src, from, fav) {
	if (!id) id = '0';
	var wdiv = new Element('div', {
		'id': 'p'+src,
		'html': '<div style="float:left;">'+(!fav?'<img id="i'+src+'" src="img/data/'+id+'/_'+src+'.jpg" class="pic">':'<a href="?'+from+'&edit='+id+'&imgfav='+src+'" target="RSIFrame" onclick="this.blur();" class="pic"><img id="i'+src+'" src="img/data/'+id+'/_'+src+'.jpg"'+(!f[src]?'':' class="pict"')+'></a>')+'</div><div class="pt5 pr10 pl5" style="float:left;"><div class="pb10"><a href="#" onclick="Mediabox.open(\'?'+from+'&edit='+id+'&imgdescr='+src+'\', \'Введите краткое описание изображения\', \'400 40\'); return false;"><img src="i/miniedit.gif"></a></div><a href="?'+from+'&edit='+id+'&imgdel='+src+'" target="RSIFrame" onclick="return confirm(\'Удалить это изображение?\');"><img src="i/minidel.gif"></a><div class="pt15"><a href="?'+from+'&edit='+id+'&imgtop='+src+'" target="RSIFrame" id="up'+src+'"><img src="i/up.gif"></a></div></div>',
		'styles': {
			'float': 'left',
			'height': '100px'
		}
	});
	wdiv.inject($('image_gallery'));
}

function buildGallery(id, srcarr, from, fav) {
	srcarr.each(function(src){
		imgAdd(id, src, from, fav);
	});
	dohide('u'+$('image_gallery').getElement('div').get('id'));
}

function delobj(obj){
	var myFx = new Fx.Tween($(obj), {
	    onComplete: function(){
			$(obj).dispose();
	    }
	});
	myFx.start('opacity','1','0');
}

function imgFav(obj) {
	$(obj).toggleClass('pict');
}

function imgFavOut(obj) {
	
	$(obj).removeClass('pict');
}

function imgFavIn(obj) {
	$(obj).addClass('pict');
}

function fadeInOut(objin, objout){
	var myFx = new Fx.Tween($(objout), {
	    onComplete: function(){
			$(objout).hide();
			$(objin).show();
			$(objin).fade('in');
	    }
	});
	myFx.start('opacity','1','0');
}

function rotate(inout) {
	tmp = ci;
	ci = ci + 3; if (ci>11) ci = 0;
	var w = $(document.body).getDimensions().width;
	
	if (!inout) {
		var Fx1 = new Fx.Tween($('ti4'), {	onComplete: function(){ $('ti1').set('styles', { 'background': 'url(\'img.php?id='+images[ci+1-1][0]+'&pic='+images[ci+1-1][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti1').set('href', '?works&id='+images[tmp+1-1][0]); $('tt1').innerHTML='<a href="?works&id='+images[tmp+1-1][0]+'">'+images[tmp+1-1][2]+'</a>'; Fx2.start('opacity','0','1'); } });
		var Fx2 = new Fx.Tween($('ti5'), {	onComplete: function(){ $('ti2').set('styles', { 'background': 'url(\'img.php?id='+images[ci+1][0]+'&pic='+images[ci+1][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti2').set('href', '?works&id='+images[tmp+1][0]); $('tt2').innerHTML='<a href="?works&id='+images[tmp+1][0]+'">'+images[tmp+1][2]+'</a>'; Fx3.start('opacity','0','1'); } });
		var Fx3 = new Fx.Tween($('ti6'), {	onComplete: function(){ $('ti3').set('styles', { 'background': 'url(\'img.php?id='+images[ci+2][0]+'&pic='+images[ci+2][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti3').set('href', '?works&id='+images[tmp+2][0]); $('tt3').innerHTML='<a href="?works&id='+images[tmp+2][0]+'">'+images[tmp+2][2]+'</a>'; setTimeout('rotate(1);', 5000); } });
		Fx1.start('opacity','0','1');
	} else {
		var Fx1 = new Fx.Tween($('ti4'), {	onComplete: function(){ $('ti4').set('styles', { 'background': 'url(\'img.php?id='+images[ci+1-1][0]+'&pic='+images[ci+1-1][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti1').set('href', '?works&id='+images[tmp+1-1][0]); $('tt1').innerHTML='<a href="?works&id='+images[tmp+1-1][0]+'">'+images[tmp+1-1][2]+'</a>'; Fx2.start('opacity','1','0'); } });
		var Fx2 = new Fx.Tween($('ti5'), {	onComplete: function(){ $('ti5').set('styles', { 'background': 'url(\'img.php?id='+images[ci+1][0]+'&pic='+images[ci+1][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti2').set('href', '?works&id='+images[tmp+1][0]); $('tt2').innerHTML='<a href="?works&id='+images[tmp+1][0]+'">'+images[tmp+1][2]+'</a>'; Fx3.start('opacity','1','0'); } });
		var Fx3 = new Fx.Tween($('ti6'), {	onComplete: function(){ $('ti6').set('styles', { 'background': 'url(\'img.php?id='+images[ci+2][0]+'&pic='+images[ci+2][1]+'&res='+w+'\') 50% 50% no-repeat' }); $('ti3').set('href', '?works&id='+images[tmp+2][0]); $('tt3').innerHTML='<a href="?works&id='+images[tmp+2][0]+'">'+images[tmp+2][2]+'</a>'; setTimeout('rotate(0);', 5000); } });
		Fx1.start('opacity','1','0');
	}
}

