// <![CDATA[

var Mamafoo = new function(){}

/*
* Image gallery
*/
Mamafoo.Gallery = function(){
	
	this.file_pane = false; // Div wrap pane  for displaying images/videos (crop)
	this.file_pane_inner = false; // Div pane for displaying images/videos
	this.left_arrow = false; // Div pane for left arrow
	this.right_arrow = false; // Div pane for right arrow
	this.arrow_negative_offset = 0; // The amount of pixels subtracted before we calculate the y center
	
	this.files = new Array(); // Files in gallery (url, width, height, mime)
	this.file_objects = new Array(); // Loaded image/movie objects
	
	this.current = 0; // Which image is showing now?
	this.animating = false; // Are we currently animating?
	
	// Allowed image mime types
	this.image_mime_types = new Array('image/gif', 'image/jpeg', 'image/jpe', 'image/jpg', 'image/png');
	
	this.init = function() {
		this.preload();
		this.initFilePane();
	}
	
	/*
	* Show next picture
	*/
	this.next = function() {		
		if (!this.animating && this.current < this.file_objects.length-1) {
			this.animating = true;
			
			// Move left by amount of current image
			// Note that gallery var i used in afterFinish, uargh !
			var new_x = this.files[this.current][1];
			
			Effect.MoveBy(this.file_pane_inner, 0, -new_x , {
				duration: 1.0,
				transition: Effect.Transitions.slowstop,
				afterFinish: function() { gallery.animating = false; }
			});
			
			// Show left arrow
			this.left_arrow.style.display = "block";
			
			// Set image counter
			this.current++;
			
			// Hide right arrow when viewing last image
			if (this.current == this.file_objects.length-1) {
				this.right_arrow.style.display = "none";
			}
		}
	}
	
	this.prev = function() {
		if (!this.animating && this.current > 0) {
			this.animating = true;
			
			// Move right by amount of previous image
			// Note that gallery var i used in afterFinish, uargh !
			var new_x = this.files[this.current-1][1];
			Effect.MoveBy(this.file_pane_inner, 0, new_x , {
				duration: 1.0,
				transition: Effect.Transitions.slowstop,
				afterFinish: function() { gallery.animating = false; }
			});
			
			// Show right arrow
			this.right_arrow.style.display = "block";
			
			// Set image counter
			this.current--;
			
			// Hide left arrow when viewing last image
			if (this.current == 0) {
				this.left_arrow.style.display = "none";
			}
		}
	}
	
	this.initFilePane = function() {
		if (this.file_pane_inner) {
			var file_pane_width = 0;
			var arrow_top_position = 0;
			var arrow_height = 0;
		
			// Get the window size
			var dim = Position.GetWindowSize();
		
			// Create array of image heights
			var heights = new Array();
		
			for(var i = 0; i < this.files.length; i++) {
				heights[i] = this.files[i][2];
			}
				
			// Find the tallest image
			var max_height = heights.max();
		
			// Always make room for the scrollbar
			// Other elements might interfeer (text_window)
			file_pane_width = dim[0] -20;
		
			// If image is taller than window we have a scrollbar
			if (max_height >= dim[1]) {
				// Arrows height is calculated by browser height
				arrow_height = dim[1] - this.arrow_negative_offset;
			}
			else {				
				// Arrows height is calculated by image height
				arrow_height = max_height;
			}

			// Set height of arrows
			this.left_arrow.style.height = arrow_height + 'px';
			this.right_arrow.style.height = arrow_height + 'px';
			
			// Also set height of first a-tag.. (IE)
			this.left_arrow.getElementsByTagName("A")[0].style.height = arrow_height + 'px';
			this.right_arrow.getElementsByTagName("A")[0].style.height = arrow_height + 'px';
			
						
			// Hide left arrow when viewing first image
			if (this.current == 0) {
				this.left_arrow.style.display = "none";
			}
			
			// Hide right arrow when we only have one image
			if (this.files.length == 1) {
				this.right_arrow.style.display = "none";
			}
			
			// Set width and height of wrapper
			this.file_pane.style.width = file_pane_width + 'px';
			this.file_pane.style.height = max_height + 'px';
			this.file_pane.style.overflow = 'hidden';
	
			// Fill inner pane with images
			var images = '<table border="0" cellpadding="0" cellspacing="0"><tr>';
			for(var i = 0; i < this.files.length; i++) {
				images += ('<td>' + this.file_objects[i] + '</td>');
			}
			images += '</tr></table>';
			this.file_pane_inner.update(images);
			
			
			//document.getElementById("gallery_wrap").appendChild(this.file_objects[0]);
			//this.file_pane.appendChild(this.file_objects[0]);
		}
	}
	
	this.setArrowNegativeOffset = function(amount) {
		this.arrow_negative_offset = amount;
	}
	
	/*
	* Set the div pane for the navigation
	*
	* @param string pane Html id of div pane
	*/
	this.setNavigationPane = function(pane) {
		if (document.getElementById(pane)) {
			this.navigation_pane = document.getElementById(pane);
		}
	}
	
	/*
	* Set the div pane for the navigation
	*
	* @param string pane Html id of div pane
	*/
	this.setFilePane = function(pane, inner, left, right) {
		if ($(pane) && $(inner) && $(left) && $(right)) {
			this.file_pane = $(pane);
			this.file_pane_inner = $(inner);
			this.left_arrow = $(left);
			this.right_arrow = $(right);
		}
	}
	
	/*
	* Create the file object for the gallery
	*/
	this.setFiles = function(array_from_php) {
		this.files = array_from_php;
		this.init();
	}
	
	/*
	* Preload files
	*/
	this.preload = function() {
		for (var i = 0; i < this.files.length; i++) {
			
			// Mime type of current file
			var mime = this.files[i][3];
	
			// Should we create an image object? (also movies in the future)
			for (var j = 0; j < this.image_mime_types.length; j++) {
				if (this.image_mime_types[j] == mime) {
					this.file_objects[i] = new Mamafoo.GalleryImage();
					this.file_objects[i].init(this.files[i][0]);
					break;
				}
			}
		}
	}
}
/*
* Image gallery end
*/

/*
* Image object for Image gallery
*/
Mamafoo.GalleryImage = function() {
	
	this.image = new Image(); // Image object
	
	this.init = function(src) {		
		this.image.src = src;
	}
	
	this.toString = function() {
		return '<img src="'+this.image.src+'" />';
	}
}
/*
* Image object for Image gallery end
*/

Position.GetWindowSize = function(w) {
	w = w ? w : window;
	var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
	var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
	return [width, height]
}

/*
	Javascript paging for navitation start
*/
Mamafoo.NavigationPagination = function() {
	this.current_page = 1;
	this.items_per_page = 12;
	this.no_of_pages = 1;
	this.temp_items = new Array();
	this.items = new Array();
	this.sel_index = 0;
		
	this.init = function() {
		// We need to copy the array, not reference it
		this.temp_items = $('sub_nav_inner').getElementsByTagName("li");
		for (var i = 0; i < this.temp_items.length; i++) {
			if (this.temp_items[i].className) {
				this.sel_index = i;
			}
			this.items[this.items.length] = this.temp_items[i];
		}
		
		this.no_of_pages = Math.ceil(this.items.length / this.items_per_page);
		this.setSelectedPage();
		this.writeNav();
	}
	
	this.writeNav = function() {
		var start = (this.items_per_page * this.current_page) - this.items_per_page;
		var end = (this.items_per_page * this.current_page) - 1;
				
		var h = '<ul>';
			// Previous link
			if (this.current_page > 1) {
				h += '<li><a href="#" onclick="pagination.previous();"><span>&laquo; show previous</span></a></li>';
			}
			
			// Posts
			for (var i = start; i <= end; i++) {
				if (i < this.items.length) {
					if (this.items[i].className) {
						h += '<li class="sel">' + this.items[i].innerHTML + '</li>';
					}
					else {
						h += '<li>' + this.items[i].innerHTML + '</li>';
					}
				}
			}
			
			// Next link
			if (this.current_page < this.no_of_pages) {
				h += '<li><a href="#" onclick="pagination.next();"><span>show next &raquo;</span></a></li>';
			}	
		h += '</ul>';
		
		$('sub_nav_inner').update(h);
	}
	
	this.setSelectedPage = function() {
		var page = Math.ceil((this.sel_index + 1) / this.items_per_page);
		this.current_page = page;
	}
	
	this.previous = function() {
		this.current_page--;
		this.writeNav();
	}
	
	this.next = function() {
		this.current_page++;
		this.writeNav();
	}
}

/*
	Javascript paging for navitation end
*/

// ]]>
