function checkMiniGalleryBtn() {
	 if(miniGalleryHasNext()) {
		$("#next_btn_small").fadeIn();
	} else {
		$("#next_btn_small").fadeOut();
	}
	
	if(miniGalleryHasPrev()) {
		$("#prev_btn_small").fadeIn();
	} else {
		$("#prev_btn_small").fadeOut();
	}
}

function miniGalleryHasNext() {
	var img = $('.selected_slide').find(".selected_img").parent().next().find("img");
	return img.hasClass("img_slide");
}

function miniGalleryHasPrev() {
	var img = $('.selected_slide').find(".selected_img").parent().prev().find("img");
	return img.hasClass("img_slide");
}

function nextImage() {
	var selimg = $('.selected_slide').find(".selected_img");
	var img = selimg.parent().next().find("img");
	if(img.hasClass("img_slide")) {
		selimg.fadeOut('slow', function() {
			selimg.removeClass('selected_img');
			img.fadeIn();
			img.addClass('selected_img');
			checkMiniGalleryBtn()
		});
	}
}

function prevImage() {
	var selimg = $('.selected_slide').find(".selected_img");
	var img = selimg.parent().next().find("img");
	if(img.hasClass("img_slide")) {
		selimg.fadeOut('slow', function() {
			selimg.removeClass('selected_img');
			img.fadeIn();
			img.addClass('selected_img');
			checkMiniGalleryBtn()
		});
	}
}

function changeTitle(text) {
	$("#gallery_img_title").text(text);
}

function showGallery(link) {
	stopAllAnimations();
	gallery = $('<div style="position:relative; width: 95%; height:100%; border: 1px #D3D1D1 solid;">' +
				'<img src="img/loading_img.gif" style="position: absolute; left: 49%; top:49%; z-index:-1; cursor: pointer;" />' +
				'<div id="this_img_box"><img src="'+link.href+'" style="display: none; margin-top: 8px; border: 1px #D3D1D1 solid;" class="this_img" />' +
				'<div id="prev_btn_big" style="cursor: pointer; position: absolute; left: 0; top:39%; display: none"><img src="img/prev.gif" onclick="prevImageInGallery()" /></div>' +
				'<div id="next_btn_big" style="cursor: pointer; position: absolute; right: 0; top:39%; display: none"><img src="img/next.gif" onclick="nextImageInGallery()" /></div></div>' +
				'<div id="gallery_img_title" style="position: absolute; left: 20px; bottom:10px; font-size: 11px; color: #666666; font-weight:bold">'+link.title+'</div>' +
				'<img style="cursor: pointer; position: absolute; right: 20px; bottom:5px;" src="img/close.gif" alt="Close" onclick="closeGallery()" />' +
			'</div>');
	gallery_selected = link;
	var img = gallery.find(".this_img");
	gallery.css('width', '685');
	var ww = $(window).width();
	var bleft = (ww - 685) / 2;
	$.blockUI({
		css: {
			background: '#fff',
			border: 0,
			width: 685,
			height: 520,
			top: '3%',
			//left: '25%',
			left: bleft,
			cursor: 'normal'
		},
		message: gallery
	});
	$(window).resize(function() {
		var ww = $(window).width();
		var bleft = (ww - gallery.width()) / 2;
		$('.blockMsg').css('left', bleft);
	});
	
	img.load(function() {
		$(this).fadeIn();
		
		var ps = ___getPageSize();
		var iw = parseInt($(this).width());
		var l = (ps[0] - (iw + 20)) / 2;
		var ih = parseInt($(this).height());
		var hdiff = 58;
		//var t = (ps[1] - (ih+hdiff*2)) / 3;
		gallery.width(iw + 20);
		$(".blockPage").css({
			'left': l,
			'width': (iw+20),
			'top': '3%',
			'height': (ih+hdiff)
		});
	});
	
	$("#this_img_box").hover(function() {
		checkGalleryBtn();
	}, function() {
		$("#next_btn_big").fadeOut();
		$("#prev_btn_big").fadeOut();
	});
}

function checkGalleryBtn() {
	if(galleryHasNext()) {
		$("#next_btn_big").fadeIn();
	} else {
		$("#next_btn_big").fadeOut();
	}
	
	if(galleryHasPrev()) {
		$("#prev_btn_big").fadeIn();
	} else {
		$("#prev_btn_big").fadeOut();
	}
}

function galleryHasNext() {
	var next = $(gallery_selected).next();
	return next.hasClass("original_img");
}

function nextImageInGallery() {
	var next = $(gallery_selected).next();
	if(next.hasClass("original_img")) {
		var img = gallery.find(".this_img");
		img.fadeOut(function() {
			img.load(function() {
				$(this).fadeIn();
				changeTitle(next[0].title);
			});
			img.attr('src', next[0].href);
			gallery_selected = next[0];
			checkGalleryBtn();
		});
	}
}

function galleryHasPrev() {
	var prev = $(gallery_selected).prev();
	return prev.hasClass("original_img");
}

function prevImageInGallery() {
	var prev = $(gallery_selected).prev();
	if(prev.hasClass("original_img")) {
		var img = gallery.find(".this_img");
		img.fadeOut(function() {
			img.load(function() {
				$(this).fadeIn();
				changeTitle(prev[0].title);
			});
			img.attr('src', prev[0].href);
			gallery_selected = prev[0];
			checkGalleryBtn();
		});
	}
}

function buildGallery() {
	$(".original_img").click(function() {
		showGallery(this);
		return false;
	});
}

function closeGallery() {
	$.unblockUI();
	$(window).unbind('resize');
	restartAllAnimation()
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

