/*
Ajax.Responders.register({
	onCreate: function() {
	 if($('busy') && Ajax.activeRequestCount>0) {
	 	$('busy').removeClassName('hide');
	 }
	},
	onComplete: function() {
	 if($('busy') && Ajax.activeRequestCount==0) {
	  	$('busy').addClassName('hide');
	 }
	}
});
*/

function magazine_change_tab(tab) {
	$('magazineTab1').removeClassName('ui-tabs-selected');
	$('magazineTab2').removeClassName('ui-tabs-selected');
	$('magazineTab3').removeClassName('ui-tabs-selected');
	$('magazineDiv1').hide();
	$('magazineDiv2').hide();
	$('magazineDiv3').hide();
	switch(tab) {
		case 1:
			$('magazineTab1').addClassName('ui-tabs-selected');
			$('magazineDiv1').show();
			break;
		case 2:
			$('magazineTab2').addClassName('ui-tabs-selected');
			$('magazineDiv2').show();
			break;
		case 3:
			$('magazineTab3').addClassName('ui-tabs-selected');
			$('magazineDiv3').show();
			break;
	}
}

function magazine_comments_goto_page(id_element, page) {
	new Ajax.Updater(
		'comment-list-wrapper', 
		'/ajax/comment_list.php', {
			method:		'get',
 			parameters: { id: id_element, p: page, _: new Date().getTime() }
		}
	);
	location.hash = 'comments';
}
 
function magazine_home() {
	new Ajax.Request("/ajax/magazine_home.php",
	{
		method: 'get',
		parameters: { _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				$('left-dole').update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}
 
function magazine_navigate_footer_to_text(direction, type, id, box) {		
	var box_offset;
	var div_novosti = $('novosti');
	new Ajax.Request("/ajax/magazine_navigate_footer_to_text.php",
	{
		method: 'get',
		parameters: {direction: direction, type: type, id: id, _: new Date().getTime()},
		onCreate: function() {
 			box_offset = div_novosti.cumulativeOffset();
 			$('busy').setStyle({
 				top		: box_offset.top + 'px',
 				left	: box_offset.left + 'px',
 				width	: div_novosti.getWidth() + 'px',
 				height	: (div_novosti.getHeight() / 2 + 32)+ 'px',
 				paddingTop: ((div_novosti.getHeight() - 64) / 2) + 'px'
 			});
 			$('busy').removeClassName('hide');
		},
		onSuccess: function(transport, json) {
			if(json ? Object.inspect(json) : false) {
				//alert(json.status + json.message + json.direction);					
	 			box.update(box_old_html); 
			} else {
				box.update(transport.responseText);
			}
		},
		onComplete: function() {
  			$('busy').addClassName('hide');
		},
		onFailure: function() { alert('Error...') }
	});
}

function magazine_navigate_to_text(type, page, box) {
	new Ajax.Request("/ajax/magazine_navigate_listing.php",
	{
		method: 'get',
		parameters: {type: type, p: page, date: mdate, _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				box.update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}
function magazine_photoblog_show(_id, _part) {
	new Ajax.Request("/ajax/magazine_photoblog_show.php",
	{
		method: 'get',
		parameters: {id: _id, part: _part, _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				$('left-dole').update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}

function magazine_send_comment(_id) {
	new Ajax.Request("/ajax/magazine_send_comment_form.php",
	{
		method: 'get',
		parameters: {id: _id, _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				$('left-dole').update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}

var mdate = null;
function magazine_set_date_variable() {
	mdate = $("dYear").getValue() + "-" + $("dMonth").getValue() + "-" + $("dDay").getValue();
	magazine_change_tab(2);
	magazine_navigate_to_text('popular', '1', $('magazineDiv2'));
}

function magazine_show_comments(_id) {
	new Ajax.Request("/ajax/magazine_text_with_comments.php",
	{
		method: 'get',
		parameters: {id: _id, _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				$('left-dole').update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}

function magazine_show_text(_id, _part) {
	new Ajax.Request("/ajax/magazine_text.php",
	{
		method: 'get',
		parameters: {id: _id, part: _part, _: new Date().getTime()},
		onSuccess: function(transport, json) {
			if (json) {
				Object.inspect(json);
			} else {
				$('left-dole').update(transport.responseText);
			}
		},
		onFailure: function() { alert('Error...') }
	});
	location.hash = 'content-top';
}
