var Magazin = Class.create();

Magazin.prototype = 
{
	initialize: function (target, ob_name, magazin)
	{
		this.ob_name=ob_name; //Название объекта
		this.id=magazin.id; //id журнала
		if (magazin.number) //номер журнала
			this.number=magazin.number;
		else
			this.number=0;
		if (magazin.page) //Начальная страница показа
			this.page=magazin.page;
		else
			this.page=1;
		if (magazin.view_arrows) //Отображать ли стрелки
			this.view_arrows=magazin.view_arrows;
		else
			this.view_arrows=false;
		if (magazin.closable==undefined) //Отображать ли стрелки
			this.closable=true;
		else
			this.closable=magazin.closable;
		if (magazin.pages) //Число страниц
			this.pages=magazin.pages;
		else
			this.pages=0;
		if (magazin.archive) //отображать архив
			this.archive=magazin.archive;
		else
			this.archive=false;
		this.target=target; //Место отображения
		this.wait=true;
		new Ajax.Updater('musorka', '/modules/magazin/ajax/get_data.php?ob_name='+this.ob_name+'&magazin='+this.id+'&number='+this.number+'&page='+this.page, {method:'post', evalScripts:true});
	},
	loadPages: function (page)
	{
		if (parseInt(page)+parseInt(this.pages_view)>parseInt(this.pages))
			page=parseInt(this.pages)-parseInt(this.pages_view)+1;
		if (parseInt(page)<1)
			page=1;

		dataPages=$(this.target).childNodes;
		if (dataPages[0])
			for (i=0; i<dataPages.length;i++)
				dataPages[i].remove();

		$(this.target).innerHTML = "<center><div id='magazin_info' classname='magazin_info' style='position: relative; "+
		"width:30%'><img id='first_arrow' style='cursor: pointer;position:absolute; top:0px; left: 0px;"+
		(this.view_arrows?"":"display:none;")+"' onclick='"+this.ob_name+".prev_page(1)' src='/modules/magazin/img/arrow_first.gif'><img id='left_arrow' style='cursor: pointer;position:absolute; top:0px; left: 15px;"+
		(this.view_arrows?"":"display:none;")+"' onclick='"+this.ob_name+".prev_page("+(page-this.pages_view)+
		")' src='/modules/magazin/img/arrow_prev.gif'>Журнал: "+this.name+"<br>Номер: "+this.date+"<br>Число страниц: "+
		this.pages+"<form onsubmit='"+this.ob_name+".next_page(parseInt($(\"what_page\").value)); return false;'>Переход на страницу: <input name='what_page' id='what_page' style='width:30px'></form><img id='right_arrow' style='cursor: pointer;position:absolute; top:0px; right: 15px;"+
		(this.view_arrows?"":"display:none;")+"' onclick='"+this.ob_name+".next_page("+
		(parseInt(page)+parseInt(this.pages_view))+")' src='/modules/magazin/img/arrow_next.gif'><img id='last_arrow' style='cursor: pointer;position:absolute; top:0px; right: 0px;"+
		(this.view_arrows?"":"display:none;")+"' onclick='"+this.ob_name+".next_page("+
		(parseInt(this.pages))+")' src='/modules/magazin/img/arrow_last.gif'></div></center>"+(!this.closable?"":"<img "+
		"src='/img/x2.gif' onclick='" + this.ob_name + ".close_win ();' style='cursor: pointer; position:absolute; top:0px; right: 10px;'>")+"<div id='pages_draw_area' "+
		"style='position:relative;top: +25px;left:0px;background: url(http://smi.aziopia.ru/img"+this.back+
		");overflow:hidden; overflow-x:auto; overflow-y:auto; width:" + $(this.target).offsetWidth + "px; height:" +
		$(this.target).offsetHeight + "px;'/>";

		$('pages_draw_area').style.height=($('pages_draw_area').offsetHeight - $('magazin_info').offsetHeight - 25) + 'px';
		new Ajax.Updater('pages_draw_area', '/modules/magazin/ajax/get_data.php?ob_name='+this.ob_name+'&page='+page+'&pages_view='+this.pages_view+'&number='+this.number+'&maxpage='+this.pages, {method:'post', evalScripts:true, insertion: Insertion.Bottom});
	},
	close_win: function ()
	{
		$(this.target).remove ();
	},
	prev_page: function (page)
	{
		this.loadPages(page);
	},
	next_page: function (page)
	{
		this.loadPages(page);
	},
	drawPage: function (page)
	{
	if (page<=this.pages)
	{
		var left;
		
		divelement=document.createElement('DIV');
		divelement.id='page_'+page;
		divelement.style.position='absolute';
		already_pages=$(this.target).childNodes;
		if (already_pages[0])
			if (already_pages.length==3)
				left=already_pages[0].offsetWidth;
			else
			{
				left=already_pages[0].offsetWidth;
				for (i=3;i<already_pages.length;i++)
					left+=already_pages[i].offsetWidth+10;
			}
		//alert(left+' '+already_pages.length);
		divelement.style.left=left+'px';
		divelement.style.top=$('magazin_info').offsetHeight+35+'px';
		divelement.style.border='1px solid black';
		divelement.style.width=this.width+'px';
		divelement.style.height=this.height+'px';
		divelement.style.background='url(/img'+this.paper+')';
		divelement.innerHTML='<div id="page_number_'+page+'" classname="page_number" style="text-align:center;position:absolute;border:1px solid black; top: -25px;width:20px;left:'+(this.width/2-10)+'px">'+page+'</div>';
		$(this.target).appendChild(divelement);
		for (i=0; i<this.area.length;i++)
		{
			if (this.area[i].page==page)
			{
				divelementArea=document.createElement('DIV');
				divelementArea.id=this.area[i].name;
				divelementArea.style.position='absolute';
				divelementArea.style.left=this.area[i].left;
				divelementArea.style.top=this.area[i].top;
				divelementArea.style.width=this.area[i].width;
				divelementArea.style.height=this.area[i].height;
				divelementArea.innerHTML=this.area[i].text;
				$('page_'+page).appendChild(divelementArea);
			}
		}
	}
	},
	getData: function (id,name, date, info, circulation, pages, pages_view, publisher, width, height, paper, back)
	{
		this.number=id;
		this.name=name;
		this.date=date;
		this.info=info;
		this.circulation=circulation;
		if (this.pages==0 || (this.pages>pages && this.pages!=0))
			this.pages=pages;
		this.pages_view=pages_view;
		this.width=width;
		this.height=height;
		this.paper=paper;
		this.back=back;
		//$(this.target).style.width=this.pages_view*(parseInt(this.width)+10)+30+'px';
		//document.body.style.background='url(/img'+this.back+')';
	},
	getPage: function (name, section, top, left, width, height, page, text, page_numb)
	{
		this.area = new Array();
		for (i=0; i<name.length;i++)
		{
			var massLength=this.area.length;
			this.area[massLength] = new Object();
			this.area[massLength].name=name[i];
			this.area[massLength].section=section[i];
			this.area[massLength].top=top[i];
			this.area[massLength].left=left[i];
			this.area[massLength].width=width[i];
			this.area[massLength].height=height[i];
			this.area[massLength].page=page[i];
			this.area[massLength].text=text[i];
		}
	this.drawPage(page_numb);
	}
}