/*
 * subset of imgGallery v1.0
 * @author Eirik Lillebo
 */

$(document).ready(function(){
	var sys = new imgGallery();
	sys.init(false);
});

/*
 * ImageGallery Class
 */
function imgGallery(){

	// Default settings
	var debug               = true;
	var console_maxLines    = 15;
	var console_curLines    = 0;
	var item_width		= 967;
	var item_height		= 314;
	var item_count		= 0;	
	var gallery_width	= 0;
	var viewport_size	= 1;
	var gallery_position    = 0;
	var buffer              = 20;	
	var isMoving		= false;

	this.init = function(debug_mode){

		if (!debug_mode) {
			debug = debug_mode;
			$("#Term").css("display", "none");
		}

		//getFilter();
		positionItems();
		addEventHandlers();
		cloneItems();

	}

	
	function positionItems() {

		/*
		 * Loop over li-elements in #gallery
		 */
		$("#gallery ul li").children().each(function(){

			// Reset any previous positions
			$(this).css("position", "absolute");
			$(this).css("left", "0");
			$(this).css("top", "0");

                        // Show this item
                        $(this).css("display", "block");
                        $(this).css("left", gallery_width);
                        gallery_width = gallery_width + (item_width + buffer);

                        item_count++;

		});
                if (item_count == 1) {
                    $('.btn_next').css("display", "none");
                    $('.btn_prev').css("display", "none");
                }
		logg("Total count: "+item_count);
	}

        /*
         * slide right function
         */
        function slideRight(){
            if (!isMoving && ((viewport_size * 2) < (item_count + 1))) {
                // Lock and check
                isMoving = true;
                checkClone("prev");
                gallery_position = gallery_position + (item_width + buffer);
             
                // Animate galleries
                $("#gallery ul").animate({
                        left: '+='+(item_width+buffer)
                }, 1000, function() {
                        // On finish
                        isMoving = false;                     
                });
            }
        }

        /*
         * slide left function
         */
        function slideLeft(){
            if (!isMoving && ((viewport_size * 2) < (item_count + 1))) {
                // Lock and check
                isMoving = true;
                checkClone("next");

                gallery_position = gallery_position - (item_width + buffer);
            
                // Animate galleries
                $("#gallery ul").animate({
                        left: '-='+(item_width+buffer)
                }, 1000, function() {
                        // On finish
                        isMoving = false;                     
                });
            }
        }

	/*
	 * Add handlers for user actions
	 */
	function addEventHandlers() {

		// Previous
		$(".btn_prev").bind("click", function(e) {
                    e.preventDefault();
                    slideRight();
                });
                   
		// Next
		$(".btn_next").bind("click", function(e) {
                    e.preventDefault();
                    slideLeft();
                });

                // Accessibility rules demand tab and enter possibilities:
                $('.btn_prev').keydown(function(e) {
                    if (e.which == 13) {
                        slideRight();
                    }
                    else if (e.which == 9 && e.shiftKey){
                        logg(".btn_prev shift-tab pressed");
                        $('.LastMainMenuElement a').focus();
                    }
                });
		
                $('.btn_next').keydown(function(e) {
                    if (e.which == 13) {
                        slideLeft();
                    }
                    else if (e.which == 9) {
                        // Some specific tab functionality for Hydro main page:
                        $("#PictureMenu .Header a").focus();
                        if (e.shiftKey){
                            logg("btn_next shift-tab pressed");
                            $(".btn_prev").focus();
                        }
                    }
                });

                // Some specific tab functionality for Hydro main page:
                $("#gallery-content li").live({
                    focusin: function(){
                        $(".btn_prev").focus();
                    }
                });                

                $("#PictureMenu .Header a").keydown(function(e){
                    if (e.which == 9 && e.shiftKey) {
                        $(".btn_next").focus();
                    }
                })

	}

	/*
	 * Clone gallery content for mirroring
	 */
	function cloneItems() {
		// Create clone
		var itemlist = $("#gallery-content").html();
		var clone = "<ul id=\"content-clone\">"+itemlist+"</ul>";

		// Add clone
		$("#gallery").append(clone);

		// Position clone to left of content
		$("#content-clone").css("position", "relative");
		$("#content-clone").css("left", (gallery_width - gallery_position));
		$("#content-clone").css("top", -item_height - buffer);//-((item_height * 2) + buffer));

                /* Re-bind event handlers
		$("#gallery a.btn_prev").bind("click", slideRight);
                $("#gallery a.btn_next").bind("click", slideLeft);*/

	}

	/*
	 * Check and adjust clone positioning
	 */
	function checkClone(dir){
		if (dir == "prev") {
			if (gallery_position == 0) {
				// Clone to left
				$("#content-clone").css("left", -(gallery_width));
			}
			if (gallery_position == gallery_width) {
				switchContent("prev");
			}
		}

		if (dir == "next") {

			var viewport_width = (item_width + buffer) * viewport_size;
			var tail_width = -(gallery_width - viewport_width);

			logg("Gallery pos:   "+gallery_position);
			logg("Gallery width: "+gallery_width);
			logg("Tail width:    "+tail_width);

			if (viewport_width > gallery_width) {
				tail_width = -(gallery_width - viewport_width);
			}

			if ((gallery_position == tail_width)) {
				// Clone to right
				logg("Clone to right");
				$("#content-clone").css("left", viewport_width);
			}

			if (gallery_position == -gallery_width) {
				switchContent("next");
			}

		}

	}

	/*
	 * Switch clone with original
	 */
	function switchContent(dir) {

		logg("Switching...");

		// Reset position tracker
		gallery_position = 0;

		// Apply CSS displacement
		$("#gallery-content").css("left", "0");

		if (dir == "prev") {
			$("#content-clone").css("left", -(gallery_width));
		} else {
			$("#content-clone").css("left", (gallery_width));
		}

	}

	/*
	 * Output debug info
	 */
	function logg(msg) {
		if (debug) {
			if (console_curLines == console_maxLines) {
				$("#Term").html("<strong>Output:</strong><br>");
				console_curLines = 0;
			}

			$("#Term").append("[CONSOLE] "+msg+"<br>");
			console_curLines++;
		}
	}

}

