Event.observe(window, 'load', catInit);

function loadstart () {
	Element.show('loadindicator');
}

function loadend () {
	Element.hide('loadindicator');
}


function catInit () {
	cat = new cat();
	
}


function cat () {
	
}


//обновляет фото одной кошки
cat.prototype.changePhoto = function (id) {
	//alert('12');
	if ($('update_imgclass_'+id).hasClassName('activeqqq')) return;
	//проверяем
	
	
	new Ajax.Request("changephoto.php?id="+id, {
  			method: 'get',
  			onCreate: loadstart,
		 	onSuccess: cat.changePhotoOnSuccess,
		 	onFailure: function(transport) {alert("Ошибка"+transport.responseText);}
 	});
}

cat.prototype.changePhotoOnSuccess = function(transport) {
	eval(transport.responseText);
	el=$('update_photo_now_url');
	//Try.these(function(){el.style.backgroundImage="url("+photo_img.url+")";});
	Try.these(function(){$('update_img_height').src=photo_img.url;});
	
	
	el.title=photo_img.opisanie;

	//изменяем высоту распорки
	//$('update_img_height').height=photo_img.height;
	
	//Try.these(function(){$('update_img_height').src=photo_img.url;});
	
	//у всех у кого div.preview_photo1 img удалем класс active 
	el=$$('#update_cat_now_photo img');
	for (var i=0; i<el.length;i++)   el[i].removeClassName('activeqqq');
	//ставим класс active риссунку с id=update_imgclass_photo_img.id
	$('update_imgclass_'+photo_img.id).addClassName('activeqqq');
	//$('update_photo_now_rating').innerHTML=photo_img[0].rating;
	Try.these(function(){$('update_photo_now_rating').innerHTML=photo_img.rating;});
	
	
	
	loadend();
	
}

//изменяет текущую кошку
cat.prototype.load = function (id) {
	//alert(id);
	new Ajax.Request("catload.php?id="+id, {
  			method: 'get',
  			onCreate: loadstart,
		 	onSuccess: cat.loadOnSuccess,
		 	onFailure: function(transport) {alert("Ошибка"+transport.responseText);}
 	});
 	//alert('ok');
}


cat.prototype.loadOnSuccess = function(transport) {
	eval(transport.responseText);
	
	
	//photo - объект с описанием кошки photo_img - объект с массивами фотографий кошки
	$('update_cat_now_opisanie').innerHTML=photo.opisanie;
	
	Try.these(function(){$('update_cat_now_title').innerHTML=photo.title;});
	//$('update_cat_now_title').innerHTML=photo.title;
	
	//у всех у кого div.preview_photo1 img удалем класс active 
	el=$$('#update_cat_all_photo img','#update_cat_all_photo a');
	for (var i=0; i<el.length;i++)   el[i].removeClassName('activeqqq');
	//ставим класс active риссунку с id=update_imgclass_photo_img.id
	$('update_imgclass_all_'+photo.id).addClassName('activeqqq');
	

	Try.these(function(){$('update_cat_now_date').innerHTML=photo.date;});
	//обновление списка фоток выбранной кошки

	
	//alert(photo_img.length=='undefined');
	if (photo_img==0) { //если фоток нет
		Element.hide('block_anotherfoto');
		Try.these(function(){$('update_photo_now_rating').innerHTML=''});
		el=$('update_photo_now_url');
		el.style.backgroundImage="";
		el.title='';
		Element.hide('update_img_height');
		//$('update_img_height').height=0;
		
	} else {

		//обновление видимой фотки. Нулевая фотка в массиве фото_имж с максимальным рейтингом
		Try.these(function(){$('update_photo_now_rating').innerHTML=photo_img[0].rating;});
		el=$('update_photo_now_url');
		//el.style.backgroundImage="url("+photo_img[0].url+")";
		Element.show('update_img_height');
		Try.these(function(){$('update_img_height').src=photo_img[0].url;});
		//Try.these(function(){$('update_img_height').src=photo_img[0].url;});
		el.title=photo_img[0].opisanie;
		//$('update_img_height').height=photo_img[0].height;
		
		
		Element.show('block_anotherfoto');
		el=$('update_cat_now_photo');
		str='';
		for (var i=0; i<photo_img.length; i++) {
			if (i==0) 	active1='class="activeqqq"';
			else 		active1='';
			str+='<div class="preview_photo1">';
			str+='<a href="javascript:;" onClick="cat.changePhoto(\''+photo_img[i].id+'\')" title="'+photo_img[i].opisanie+'">';
			str+='<img '+active1+' id="update_imgclass_'+photo_img[i].id+'" src="'+photo_img[i].url_mini+'" width="70" height="70"></a></div>';
		}

		el.innerHTML=str;
		enableTooltips("tooltip_content1");
		
	}
	
		loadend();

}
