// JavaScript Document

function setPictureHeight($img)
{
	//set picture height:
	var documentHeight = $('body').height();
	var navigationHeight = 90;
	$img.css('max-height', documentHeight - navigationHeight - 50); //documentHeight - navigationHeight);
}
$(window).bind('resize', function() {
	setPictureHeight($('#main_image').find('img'));
});

var globalHiddenGallery='';

function prepareFilesWhichAreNotInGalleries()
{
	//var photos = array();
	//$('p:not(.galeria)').click(function() { alert('paragraf'); } );
		
	var regexp = /(jpg|gif|png)$/i

	//$('p:not(.galeria)').find('a img').each(addItemToGallery);
	$('#content_psla a img').each(function(i,item) {
		$itemParent=$(item).parent();
		var url = $itemParent.attr('href');
		if(regexp.test(url)) {
			if(! $itemParent.parent().is('.galeria'))
				addItemToGallery(i, item);
		}
	});
	//globalHiddenGallery = '<ul>' + globalHiddenGallery + '</ul>';
}

function getFullAddress(address)
{
	if(address.indexOf('http://') >= 0)
		return address;
	return 'http://'+document.domain+address;
}

function addItemToGallery(i, item)
{
$item = $(item).parent();
globalHiddenGallery += getGalleryLi($item.attr('href'),$item.html(), $item.attr('title'));
$item.click(	function(event) { 
	var link = $(this).attr('href');
	window.location.hash='#'+getFullAddress(link);
	showGalleryFromText(globalHiddenGallery);
	$.historyLoad(link);
	//alert($item.parent().attr('href'));
	event.preventDefault();
	} );
}

function getGalleryLi(link, pictureImg, title)
{
	return '<li><a href="'+ link +'" title="'+title+'" >'+ pictureImg +'</a></li>';
}

var galleryObject = "#fullPageGallery";
//turn each old gallery (div.galeria with images) into <ul><li></li></ul> structure
$(document).ready(function() {
	prepareFilesWhichAreNotInGalleries();
	//ka¿d¹ galeriê przygotuj
	$('p.galeria').each(function() {
		var galleryUl='';
		$(this).find('a').each(function() {
			//$(this).wrap('<li></li>');
			galleryUl += getGalleryLi($(this).attr('href'),$(this).html(), $(this).attr('title'));
		});
		//galleryUl = '<ul>'+$(this).html()+'</ul>';
		galleryUl = '<ul>'+galleryUl+'</ul>';
		$(this).html(galleryUl);
		$(this).find('ul').click(function(event) { 
			if( event.target.tagName == "IMG" )
			{
				$clicked = $(event.target);
				$parent = $clicked.parent().parent();
				$parent.addClass("active");
				$clicked.addClass("selected"); 
				window.location.hash='#'+$clicked.parent().attr('href');
				showGallery($(this)); 
				$.historyLoad($clicked.parent().attr('href'));
				//$.galleria.activate($clicked.parent().attr('href'));
				$clicked.removeClass("selected");
				$parent.removeClass("active");
				event.preventDefault(); 
			}
		});
	});
//	$("body").append('<div id="fullPageGallery"><div id="fullPagePhoto"></div></div>');
	$('<div id="fullPageGallery" style="display: none"></div>').appendTo('body');
	$(galleryObject).click(function(event) {
										//alert(event.target.tagName);
										 if(event.target.tagName == "DIV")
											hideFullPageGallery($(this));
										 });
										 
	if(window.location.hash.length>1 && window.location.hash.substring(window.location.hash.length-3).toLowerCase()=='jpg')
	{
		var link = window.location.hash.substring(1, window.location.hash.length);
		var $item = $('#content_psla a[href='+link+']:first');
		var $itemParent = $item.parent().parent();
		if($itemParent.parent().is(".galeria")) 
			showGallery($itemParent);
		else
			showGalleryFromText(globalHiddenGallery);
	}
	//showFullPageGallery($(galleryObject));
	//showGallery($this);
});

function showGallery($gallery, picture)
{
	showGalleryFromText($gallery.html(), picture);
}
function showGalleryFromText(galleryText, picture)
{
//<div id="picture_title_gallery"></div>
	$(galleryObject).html('<div class="gallery_layer"><div id="main_image" class="galleria_container"></div><div id="gallery_fullnav"><div class="prev_images" /><div id="gallery_nav"><ul class="gallery_demo_unstyled mtp_gallery galleria">'+galleryText+'</ul></div><div class="next_images" /></div><p class="nav"><a href="#" onclick="$.galleria.prev(); return false;">&laquo;</a> | <a href="#" onclick="$.galleria.next(); return false;">&raquo;</a></p><img id="closeGallery" src="http://static.mtp.pl/libs/gallery/close.png" alt="Close gallery / Zamknij"></img></div>');
	$galleryUl =$(galleryObject).find('ul');
	$galleryUl.html(galleryText);
	//$('#gallery_nav').css('width', $(galleryObject).width() - 120);
	showFullPageGallery($(galleryObject));
	
	$('#closeGallery').click(function(event)
	{
		hideFullPageGallery($(this).parent().parent());
	});
		
	$('.next_images').click(function(event) {
		$galleryUl.animate({left: $galleryUl.position().left- $('#gallery_nav').width() }, 500);
		event.preventDefault();
	});
	
	//scroll galery forward
	$('.next_images').click(function(event) {
		$galleryUl.animate({left: $galleryUl.position().left- $('#gallery_nav').width() }, 500);
		event.preventDefault();
	});
	$('.prev_images').click(function(event) {
		//alert($galleryUl.css('left'));
		//alert($galleryUl.css('left')-100);
		$galleryUl.animate({left: $galleryUl.position().left + $('#gallery_nav').width() }, 500);
		event.preventDefault();
	});
	//return false;
	$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
	
		//$($(galleryObject)).find('ul').galleria({
		$('ul.mtp_gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				setPictureHeight(image);
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				//scroll to active thumbnail
				var thumbPosition = thumb.position();
				//alert(thumbPosition);
				if(thumbPosition!=null)
					$galleryUl.animate({left: ($('#gallery_nav').width() / 2) - thumbPosition.left - (thumb.width() / 2)  }, 500);
				//$("#picture_title_gallery").text(thumb.attr('title'));
				// add a title for the clickable image
				image.attr('title','Next image >>');
				//alert('active');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
}

function hideFullPageGallery($obj) {
	enableFlash();
	window.location.hash='#';
	$.historyLoad('');
	//$(galleryObject).html('');
	$obj.css('display', 'none');
}
function showFullPageGallery($obj) {
	disableFlash();
	$obj.css('display', 'block');
}
	

function disableFlash() {
	$("embed").css("display", "none");
	//$("embed").hide();
	//$("object").hide();
	//$("object").css("display", "none");
}

function enableFlash() {
	$("embed").css("display", "block");
}
