$(document).ready(function () {
	/*
		$(".photo-stream li a").click(function() {
			show_photo('.photo-stream li', $(this).attr('href'));
			return false;
		});
		$(".photos li a").click(function() {
			show_photo('.photos li', $(this).attr('href'));
			return false;
		});
	*/
	
	
		$(".photo-stream li a").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});	
		
		$(".photos li a").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
	
		$("a[rel=img_group]").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
		/*
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return currentArray[currentIndex].childNodes[0].getAttribute("alt");
				}		
		*/
		
});


	function show_photo(photos_list_class, photo_src) {
		prepare_modal_layout('<div style="position: relative; margin: 10px; overflow: hidden;"><img src="/graphics/spacer.gif" alt="" style="position: absolute; top: 0px; left: 0px; z-index: 99998; width: 60px; height: 60px; opacity: 0.0;" id="photo_prev" /><img src="/graphics/spacer.gif" alt="" id="photo" style="width: 60px; height: 60px; opacity: 0.0;" /></div>');
		$("#photo").css({'cursor':'pointer'});
		$("#photo_prev").css({'cursor':'pointer'});
		$("#photo").unbind('click');
		$("#photo_prev").unbind('click');
		$("#photo").click(function () {next_photo(photos_list_class)});
		$("#photo_prev").click(function () {next_photo(photos_list_class)});
		$("#photo_prev").css({'opacity' : '0.0', 'width' : '60px', 'height' : '60px'});
		$("#photo").css({'opacity' : '0.0', 'width' : '60px', 'height' : '60px'});
		setTimeout(function() {set_photo(photo_src)}, 300);
    };
	
	function next_photo(photos_list_class) {
		photo_src = $("#photo").attr('src');
		//alert(photo_src);
		var i = 1;
		for (i = 1; i < $(photos_list_class + " a").length; i++) {
			if ($(photos_list_class + " a").eq(i-1).attr('href') == photo_src) {
				set_photo($(photos_list_class + " a").eq(i).attr('href'));
			}
		}
	}
	
	function prev_photo(photos_list_class) {
		photo_src = $("#photo").attr('src');
		var i = 0;
		for (i = 0; i < ($(photos_list_class + " a").length - 1); i++) {
			if ($(photos_list_class + " a").eq(i+1).attr('href') == photo_src) {
				set_photo($(photos_list_class + " a").eq(i).attr('href'));
			}
		}
	}
	
	function set_photo(photo_src) {
		var photo_file = new Image;
		$(photo_file).unbind('load');
		$(photo_file).bind('load', function () {
			$("#photo_prev").attr('src', $("#photo").attr('src'));
			$("#photo_prev").css({'opacity' : '1.0'});
			$("#photo").attr('src', photo_src);
			$("#photo").css({'opacity' : '0.0'});
			$("#photo").animate({'width' : photo_file.width + 'px', 'height' : photo_file.height + 'px', 'opacity' : '1.0'}, 300);
			$("#photo_prev").animate({'width' : photo_file.width + 'px', 'height' : photo_file.height + 'px', 'opacity' : '0.0'}, 300);
			$("#modal_window").animate({'width' : (photo_file.width + 20) + 'px', 'height' : (photo_file.height + 20) + 'px', 'marginLeft' : '-' + Math.floor((photo_file.width + 20) / 2) + 'px'}, 300);
		});
		photo_file.src = photo_src;
	}

	var intval = "";

	function start_slideshow() {
		if (intval == "") {
			intval = setInterval("next_photo()", 3000);
		} else {
			stop_slideshow();
		}
	}

	function stop_slideshow () {
		if (intval != "") {
			clearInterval(intval);
			intval = "";
		}
	}

    function prepare_modal_back () {
		var modal = document.createElement('div');
        modal.id = 'modal';
        modal.style.zIndex = '9999';
        modal.style.position = 'absolute';
		modal.style.zoom = '1';
        modal.style.left = '0px';
        modal.style.top = '0px';
        modal.style.width = $(window).width() + 'px';
        modal.style.height = $(window).height() + 'px';
        modal.style.height = $(document).height() + 'px';
        modal.style.opacity = '0.85';
        modal.style.filter = 'alpha(opacity = 85)';
        modal.style.backgroundColor = '#440033';
        modal.style.display = 'none';
		
		return modal;
	}

    function prepare_modal_window (inner_html) {
		var modal_window = document.createElement('div');

        modal_window.id = 'modal_window';
        modal_window.style.zIndex = '99999';
        modal_window.style.position = 'absolute';
        modal_window.style.left = '50%';
        modal_window.style.marginLeft = '-40px';
//        modal_window.style.top = Math.floor((parseInt($(window).scrollTop())+665*2+100)) + 'px';
        modal_window.style.top = 50 + 'px';
        modal_window.style.width = '80px';
        modal_window.style.height = '80px';
        modal_window.style.backgroundColor = '#ffffff';
        modal_window.style.webkitBoxShadow = '0px 0px 40px #220011';
        modal_window.style.webkitBorderRadius = '5px';
        modal_window.style.MozBoxShadow = '0px 0px 40px #220011';
        modal_window.style.MozBorderRadius = '5px';
        modal_window.style.boxShadow = '0px 0px 40px #220011';
        modal_window.style.borderRadius = '5px';
		/*if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
        	modal_window.style.border = '10px solid #ffffff';
		}*/
        modal_window.style.display = 'none';

        modal_window.innerHTML = inner_html;

		return modal_window;
    }

    function prepare_modal_layout (inner_html) {
		var _body;
	
		var modal;
		var modal_window;
	
		_body = document.getElementsByTagName('body')[0];
		
		modal = prepare_modal_back();
		modal_window = prepare_modal_window(inner_html);

        _body.appendChild(modal_window);
        _body.appendChild(modal);
		
        $(modal).click(function(){
			stop_slideshow();
			$(modal).animate({'opacity': '0.0'}, 500);
			$(modal_window).animate({'opacity': '0.0'}, 500, function() {
	            $(modal).remove();
	            $(modal_window).remove();
			});
        });
		
/*        $(modal_window).click(function(){
            $(modal).remove();
            $(modal_window).remove();
        });*/
		
		$(modal).show();
		$(modal).css({'opacity': '0.0'});
		$(modal).animate({'opacity': '0.85'}, 300);
		$(modal_window).show();
		$(modal_window).css({'opacity': '0.0', 'top': '30px'});
		$(modal_window).animate({'opacity': '1.0', 'top': '50px'}, 300);
    }

