var page;
var pageFade;
var first_page = true;

var opened_form = new Object();

$(document).ready(function() {
	catchJSLinks();
});

function catchJSLinks(elem) {
	if(elem == null) {
		elem = document.body;
	}
	$(elem).find('a[rel=jslink]').each(function() {
		var ocf = this.onclick;
		if(ocf == null) return;
		this.onclick = null
		var str = ocf.toString();
		$(this).click(function() {
			if(str.indexOf('readAll')>=0) {
				var sstart = str.indexOf('readAll')+8;
				var sstop = str.indexOf(',');
				var sid = parseInt(str.substring(sstart, sstop));
				readAll(sid, $(this).parent().parent());
			} else {
				ocf();
			}
			return false;
		});
		//alert(this.onclick);
	});
}

function GoTo(url, callback) {
	var base_url = 'http://www.xitemanager.com';
	first_page = false;
	loading(true);
	pageFadeOut(function() {
		page.animate({
			height: 150
		}, 500, function() {
			var pars = null;
			if(url.page) {
				pars = { act: 'get_page' };
				for(var k in url) {
					pars[k] = url[k];
				}
			} else {
				pars = { act: 'get_page', page: url };
			}
			
			$.get(base_url+'/ajax.php', pars, function(html) {
				var div = $('<div style="display: none">'+html+"</div>");
				$(document.body).append(div);
				//alert(div.height());
				var hh = div.height() + 20;
				page.animate({
					height: hh
				}, 500, function() {
					page.height('auto');
					page.html(html);
					div.remove();
					$('.pagination').css('right', '17px');
					$('#storico').addClass('storico_ie');
					pageFadeIn();
					catchJSLinks(page);
					catchExternalLinks(page);
					if(callback != null) {
						callback();
					}
				});
				pageFade.animate({
					height: div.height()
				}, 500);
			});
		});
	});
	
	//Banner
	banner_page = url;
}

$(document).ready(function() {
	page = $("#page_content");
	pageFade = $("<div></div>");
	pageFade.css({
		backgroundColor: '#ffffff',
		position: 'absolute',
		width: '100%',
		//height: '100%',
		height: '0',
		top: 0,
		left: 0,
		//opacity: 0,
		zIndex: 1000
	});
	
	$(document.body).bind('ajax_ready', function() {
		//Pagination boxes
		$(".pagination .box").hover(function() {
			setPaginationBoxSelected(this, true);
		}, function() {
			setPaginationBoxSelected(this, false);
		});
		
		$(".hover").hover(function() {
			this.src = this.src.replace('.jpg', '_over.jpg');
			this.src = this.src.replace('_over_over', '_over');
		}, function() {
			this.src = this.src.replace('_over.jpg', '.jpg');
		});
		
		//Gallery
		$(".img_gallery").hover(function() {
			checkMiniGalleryBtn();
		},function() {
			$(this).find('.btn').fadeOut();
		});
		
		buildGallery();
	});
	$(document.body).trigger('ajax_ready');
});

function setPaginationBoxSelected(elem, val) {
	if($(elem).hasClass('selected')) return;
	if(val) {
		$(elem).css({
			color: '#666666',
			backgroundImage: "url('img/page_bg_over.jpg')"
		});
	} else {
		elem.style.color = '';
		$(elem).css({
			color: '',
			backgroundImage: "url('img/page_bg_box.jpg')"
		});
	}
}

function pageFadeOut(callback) {
	/*
	var offset = page.offset();
	pageFade.css({
		top: offset.top,
		left: offset.left
	});
	pageFade.width(page.width());
	pageFade.height(page.height());
	$(document.body).append(pageFade);

	pageFade.animate({
		opacity: 1
	}, 500, function() {
		//page.css('visibility', 'hidden');
		callback();
	});
	//*/
		callback();
}

function pageFadeIn() {
	$(document.body).bind('xmtr_loaded', function() {
		startBannerAnimation();
		$("#loading_dialog").fadeOut(function() {
			page.css('visibility', 'visible');
			pageFade.animate({
				opacity: 0
			}, 500, function() {
				pageFade.remove();
				loading(false);
			});
		});
	});
	$(document.body).trigger('ajax_ready');
	xmtr();
	/*
	$("#loading_dialog").fadeOut(function() {
		
	});
	*/
}

function readAll(sid, elem) {
	opened_form.sid = sid;
	opened_form.elem = elem;
	stopSlideShow();
	slideShowLocked = true;
	
	$(".hidden_detail").slideDown();
	$.get('actions/get.php', { act:'scheda_text', id: sid }, function(text) {
		text += '<div class="leggi_tutto">' +
			'	<a href="javascript:void(0)" onclick="closeBlock('+sid+', $(this).parent().parent())">Chiudi</a>' +
			'</div>' +
			'<div class="separator">&nbsp;</div>';
		elem.html(text);
		var hh = elem.height() + 50;
		/*
		var hh2 = hh * 1.2;
		if(hh2 - hh < 50) {
			hh = hh + 50;
		} else {
			hh = hh2;
		}
		*/
		var p = elem.parent().parent();
		var p2 = p.parent().parent().parent();
		p.animate({
			height: hh+"px"
		}, 500);
		if(p2.attr('id') != 'page_content') {
			p2.animate({
				height: (hh+50)+'px'
			}, 500);
		}
	});
}

function closeBlock(sid, elem) {
	opened_form.sid = null;
	opened_form.elem = null;
	$(".hidden_detail").slideUp();
	
	$.get('actions/get.php', { act:'truncated_scheda_text', id: sid }, function(text) {
		//text = text.replace(/<h3 class="text_replace" style="color:( *)(\#([a-f0-9]*)|([a-f0-9]*)|rgb\(\d+, \d+, \d+\))">[^>]+<\/h3>/i, '');
		text += '<div class="leggi_tutto" style="margin-top: 10px">' +
			'<a href="javascript:void(0)" onclick="readAll('+sid+', $(this).parent().parent())">Leggi tutto</a>' +
			'</div>'+
			'<div class="separator">&nbsp;</div>';
		elem.html(text);
		var hh = elem.height()+50;
		//var hh = 222;
		var p = elem.parent().parent();
		var p2 = p.parent().parent().parent();
		p.animate({
			height: hh+"px"
		}, 500);
		if(p2.attr('id') != 'page_content') {
			p2.animate({
				height: (hh+80)+'px'
			}, 500);
		}
	});
}

function openExpanded() {
	readAll('', $("#leggi_tutto_btn").parent());
}
