/*
	Requires:
	- jquery-1.3.2.min.js
*/


/* Initialize GalleryHandler when page load has finished
-------------------------------------------------------------*/
$(document).ready(function(){GalleryHandler.init()});



/* GalleryConfiguration object
-------------------------------------------------------------*/
GalleryConfiguration = {
	GALLERY_CLASS:"gallery",
	GALLERY_ACTIVE_CLASS:"gallery-active",
	GALLERY_BACKGROUND_CLASS:"gallery-background",
	GALLERY_OPEN_TEXT:"open",
	GALLERY_CLOSE_TEXT:"close",
	GALLERY_EXTERNAL_IMAGES_TEXT:'',
	GALLERY_EXTERNAL_IMAGES_LINK:''
}


/* GalleryHandler object
-------------------------------------------------------------*/
GalleryHandler = {
	
	galleries:[],
	
	background:null,
	
	pageWidth:0,
	pageHeight:0,
	
	init:function() {
		
		GalleryHandler.background = $("<div class=\"" + GalleryConfiguration.GALLERY_BACKGROUND_CLASS + "\">");
		GalleryHandler.background.click(GalleryHandler.closeGallery);
		
		GalleryHandler.instantHideBackground();
		
		$("body").append(GalleryHandler.background);
		
		GalleryHandler.updatePageSize();
		
		$(window).resize(GalleryHandler.updatePageSize);
		
		$("." + GalleryConfiguration.GALLERY_CLASS).each(function() {
			GalleryHandler.galleries.push(new Gallery(this));		  
		});
	},
	
	updatePageSize:function() {
		GalleryHandler.pageHeight = $(document).height();
		GalleryHandler.setBackgroundSize();
		GalleryHandler.centerGalleries();
	},
	
	centerGalleries:function() {
		for(var i=0;GalleryHandler.galleries[i]!==undefined;i++) {
			GalleryHandler.galleries[i].center();
		}
	},
	
	setBackgroundSize:function() {
		if (GalleryHandler.isIE6()) {
			this.background.css("height",GalleryHandler.pageHeight + "px");
		}
	},
	
	showBackground:function() {
		GalleryHandler.doIE6Trick();
		GalleryHandler.background.show();
		GalleryHandler.background.fadeTo(250,.75);
	},
	
	hideBackground:function() {
		GalleryHandler.background.fadeTo(150,0,GalleryHandler.instantHideBackground);
	},
	
	instantHideBackground:function() {
		GalleryHandler.undoIE6Trick();
		GalleryHandler.background.fadeTo(0,0);
		GalleryHandler.background.hide();
	},
	
	doIE6Trick:function() {
		if (GalleryHandler.isIE6()) {
			$('select').css('visibility','hidden');
		}
	},
	
	undoIE6Trick:function() {
		if (GalleryHandler.isIE6()) {
			$('select').css('visibility','');
		}
	},
	
	isIE6:function() {
		return typeof($('body').get(0).style.maxHeight)=='undefined';
	},
	
	closeGallery:function() {
		
		GalleryHandler.hideBackground();
		
		for (var i=0;GalleryHandler.galleries[i]!=undefined;i++) {
			GalleryHandler.galleries[i].close();
		}
	}
}


function Gallery(element) {
	
	// set self reference
	var self = this;
	
	// get resource
	this.resource = $(element);
	
	// define items to be included in gallery
	this.images = $('<ul>');
	
	var index = 0;
	
	$('li',this.resource).each(function() {
		
		var thumbnail = $(this).clone();
		$('a',thumbnail).bind('click',self,self.onThumbClick);
		
		if (index > 2) {
			$(this).hide();
		}
		
		self.images.append(thumbnail);
		
		index++;
	});
	
	// define open button
	this.openButton = $('<a class="open" href="#">' + GalleryConfiguration.GALLERY_OPEN_TEXT + '</a>');
	this.openButton.bind('click',this,this.onOpen);
	this.resource.append(this.openButton);
	
	// define special image
	var mark = $('<img class="mark" src="../images/tom-tom-mark.png"/>');
	
	// define images link
	var moreLink = $('<a class="external" href="' + GalleryConfiguration.GALLERY_EXTERNAL_IMAGES_LINK + '">' + GalleryConfiguration.GALLERY_EXTERNAL_IMAGES_TEXT + '</a>');
	
	// replace with gif if ie6
	if (GalleryHandler.isIE6()) {
		mark = $('<img class="mark" src="../images/tom-tom-mark.gif"/>');
	}
	
	// define close button
	this.closeButton = $('<a href="#" class="close">' + GalleryConfiguration.GALLERY_CLOSE_TEXT + '</a>');
	this.closeButton.bind('click',this,this.onClose);
	
	// define full image holder
	this.image = $('<span/>');
	
	// define container
	this.container = $('<div/>');
	this.container.append($('<div/>'));
	this.container.append(this.closeButton);
	this.container.append(this.images);
	this.container.append(this.image);
	this.container.append(moreLink);
	//this.container.append(mark);
	
	//this.container.hide();
	this.close();
	
	// bind gallery open events
	$('a',this.resource).each(function(){
		$(this).bind('click',self,self.onOpen);
	});
	
	$("body").append(this.container);
}

Gallery.prototype.onThumbClick = function(e) {
	e.data.updateImageSource($(e.currentTarget).attr('href'));
	e.preventDefault();
	return false;
}

Gallery.prototype.updateImageSource = function(src) {
	this.image.empty();
	this.image.append($('<img src="' + src + '"/>'));
	
	// set active image
	$('li a',this.images).each(function() {
		if (src == $(this).attr('href')) {
			$(this).addClass('selected');
		}
		else {
			$(this).removeClass('selected');
		}
	});
}

Gallery.prototype.center = function() {
	if (this.container.hasClass(GalleryConfiguration.GALLERY_ACTIVE_CLASS)) {
		if (GalleryHandler.isIE6()) {
			this.container.css("top",($(document).scrollTop() + "px"));
		}
		else {
			this.container.css("margin-top",-(this.container.height() / 2) + "px");
		}
	}
}

Gallery.prototype.open = function() {
	GalleryHandler.showBackground();
	this.container.addClass(GalleryConfiguration.GALLERY_ACTIVE_CLASS);
	this.center();
	
	// if opacity is supported do cool container fade in
	if ($.support.opacity) {
		this.container.animate({opacity:'+=0'},500).fadeTo(250,1);
	}
	
	this.container.show();
}

Gallery.prototype.close = function() {
	GalleryHandler.hideBackground();
	this.container.removeClass(GalleryConfiguration.GALLERY_ACTIVE_CLASS);
	this.container.hide();
	
	// if opacity is supported set opacity to 0
	if ($.support.opacity) {
		this.container.fadeTo(0,0);
	}
}

Gallery.prototype.onOpen = function(e) {
	
	var src = $(e.currentTarget).attr('href');
	
	if (src.lastIndexOf('#') != -1) {
		src = $('li a',this.resource).attr('href');
	}
	
	e.data.updateImageSource(src);
	e.data.open();
	e.preventDefault();
	return false;
}

Gallery.prototype.onClose = function(e) {
	e.data.close();
	e.preventDefault();
	return false;
}