// -----------------------------------------------------------------------------------
//
//	Gallerybox 1.0
//	Author: Dinesk Kumar V
//  Created: 01-02-09
//
//	For more information, visit:
//	http://sam-sys.com/gallerybox.html
//
// -----------------------------------------------------------------------------------

(function($)
{
	$.fn.galleryBox = function(settings) {
		
		settings = jQuery.extend({		// Settings to configure the jQuery galleryBox plugin
			olayBgColor: 		'#000',
			olayOpacity:		0.75,
			imgResizeSpeed:		600,
			initContentWidth:	200,
			initContentHeight:	200,
			contentWidth:		300,
			contentHeight:		400,
			timeoutTimer:		false,
			keyToClose:			'c',		// (string) (c = close) Letter to close the jQuery galleryBox interface.
			keyToPrev:			'p',		// (string) (p = previous) Letter to show the previous image
			keyToNext:			'n',		// (string) (n = next) Letter to show the next image.
			imgArray:			[],
			galArray:			[],
			imgActive:			0,
			galActive:			0
		},settings);
		var jQueryObj = this;
		
		function _initialize() {
			_start(this,jQueryObj);
			return false;
		}
		
		function _start(objClicked,jQueryObj) {
			$('embed, object, select').css({ 'visibility' : 'hidden' });
			settings.imgArray.length = 0;
			settings.imgActive = 0;
			var thumpath ='';
			
			for ( var i = 0; i < jQueryObj.length; i++ ) {
				var fpath = jQueryObj[i].getAttribute('href');
				var tpath = jQueryObj[i].getAttribute('href').replace('lg', 'sm');
				settings.imgArray.push(new Array(fpath, tpath));
				
				if ( objClicked.getAttribute('href') == jQueryObj[i].getAttribute('href') ) {
					settings.imgActive = settings.imgArray.length-1;
				}
			}
			
			_buildBox();
			_buildEvents();
			_buildContentArea();
			_LoadImage();
		}
		
		function _buildBox() {
			
			$('body').append('<div id="gallerybox-overlay"></div><div id="gallerybox-outter"><div id="gallerybox-content-left"></div><div id="gallerybox-content-right"></div><div id="gallerybox-content"><img id="gallerybox-image"/><div id="gallerybox-content-description"></div><div id="gallerybox-image-overlay"></div></div></div>');
					
			if($.browser.msie){
				$('#gallerybox-overlay').css({
					backgroundColor: settings.olayBgColor, opacity: settings.olayOpacity, width: $(document.body).width(), height: $(document.body).height()
				}).fadeIn();				
			}else{
				$('#gallerybox-overlay').css({
					backgroundColor: settings.olayBgColor, opacity: settings.olayOpacity, width: $(document).width(), height: $(document).height()
				}).fadeIn();
			}
			
			$('#gallerybox-outter').css({
				width: $(window).width(), height: $(window).height(), top: $(window).scrollTop(), left: $(window).scrollLeft()
			});
						
			settings.contentWidth =	$('#gallerybox-outter').width()-380;
			settings.contentHeight = $('#gallerybox-outter').height() + 70;			
		}
		
		function _buildEvents(){
			$('#gallerybox-overlay').click(function() {	_closeBox();	});
			$('#gallerybox-outter').click(function(event) {	if(event.target.id=='gallerybox-outter') _closeBox(); else return false; });
			$('#gallerybox-image').load(function (event) {_trasformBox();	});
			
			$(window).resize(function() {
				clearTimeout(settings.timeoutTimer);
				if($.browser.msie)
					$('#gallerybox-overlay').css({width: $(document.body).width(), height: $(document.body).height()});
				else
					$('#gallerybox-overlay').css({width: $(document).width(), height: $(document).height()});
					
				$('#gallerybox-outter').css({width: $(window).width(), height: $(window).height()});
				settings.contentWidth =	$('#gallerybox-outter').width()-380;
				settings.contentHeight = $('#gallerybox-outter').height() + 70;
				settings.timeoutTimer=setTimeout(function(){_LoadImage();},200);
			});	
			
			$(window).scroll(function() {
				$('#gallerybox-outter').css({top: $(window).scrollTop(), left: $(window).scrollLeft()});
			});
						
			$('#gallerybox-content-left').click(function() { 
				if(settings.imgActive>0) {
					settings.imgActive--;
					prv = $('a.gallerybox_b_img_s').prev();
					$('a.gallerybox_b_img_s').removeClass('gallerybox_b_img_s').addClass('gallerybox_b_img');
					prv.addClass('gallerybox_b_img_s');
					_LoadImage();
				}
			});
			
			$('#gallerybox-content-right').click(function() { 
				if(settings.imgActive < settings.imgArray.length-1) {
					settings.imgActive++;
					nxt = $('a.gallerybox_b_img_s').next();
					$('a.gallerybox_b_img_s').removeClass('gallerybox_b_img_s').addClass('gallerybox_b_img');
					nxt.addClass('gallerybox_b_img_s');
					_LoadImage();
				}
			});
		}
		
		function _buildContentArea(){
			topc = ($('#gallerybox-outter').height()-(settings.initContentHeight+ $('#gallerybox-bottombox').height()))/2;
			leftc =  ($('#gallerybox-outter').width()-settings.initContentWidth)/2
			
			pstop = ((settings.initContentHeight - 150)/2) + topc;
			
			$('#gallerybox-content').css({	width: settings.initContentWidth, height: settings.initContentHeight, top: topc, left: leftc });
			$('#gallerybox-content-left').css({width: 150, height: 100, top:  pstop, left: leftc, opacity:1});			
			$('#gallerybox-content-right').css({width: 150, height: 100, top:  pstop,left: (settings.initContentWidth + leftc-150), opacity:1 });
		}
		
		
		function _loadGallery(obj){			
			settings.galActive = parseInt(obj.getAttribute('rel'));
			settings.imgActive = 0;
			_LoadImage();		
		}
		
		function _LoadImage(){
			// show meta data div
			//$('#gallerybox-content-description').slideUp(200);
			
			$('#gallerybox-content,#gallerybox-image,#gallerybox-content-left,#gallerybox-content-right').stop();
			$('#gallerybox-image-overlay').fadeIn('slow', function() { 
				$('#gallerybox-image').css({width:'',height:''}); 
				$('#gallerybox-image').attr('src', settings.imgArray[settings.imgActive][0]);
			});
			var position = $('#gallerybox-content').position();
			pstop = (($('#gallerybox-content').height()- 150)/2) + position.top;
			psleft = ($('#gallerybox-content').width()/2) + position.left;
			$('#gallerybox-content-left').animate({top:pstop, left: psleft-75},500,function(){$('#gallerybox-content-left').hide()});			
			$('#gallerybox-content-right').animate({top:pstop, left: psleft-75},500,function(){$('#gallerybox-content-right').hide()});			
			
			// add corner rounding
			$('#gallerybox-content-left, #gallerybox-content-right, #gallerybox-content').corners();
		}
		
		function _trasformBox()
		{			
			var wd = $('#gallerybox-image').width();
			var hd = $('#gallerybox-image').height();
			var ratio = 100;
			if(settings.contentWidth<wd && settings.contentHeight<hd){
					ratio1 = (100/wd) * settings.contentWidth;
					ratio2 = (100/hd) * settings.contentHeight;
					if(ratio1<ratio2) ratio = ratio1; else ratio = ratio2;
					
			}
			else if(settings.contentHeight<hd) ratio = (100/hd) * settings.contentHeight;
			else if(settings.contentWidth<wd) ratio = (100/wd) * settings.contentWidth;
			
			wd   = ( wd / 100 ) * ratio;
			hd   = ( hd / 100 ) * ratio;
			xtop  = ( $('#gallerybox-outter').height() - hd ) / 2;
			yleft = ( $('#gallerybox-outter').width() - wd ) / 2;
			pstop = ( ( hd - 150 ) / 2 ) + xtop;
			if ( wd < 0 ) wd = 10; 
			if ( hd < 0 ) hd = 10;
			$('#gallerybox-content').animate({width: wd, height: hd, top: xtop-10,left: yleft },settings.imgResizeSpeed, function(){
				$('#gallerybox-image-overlay').fadeOut(function(){ _trasformSideBox(); });
			});
			
			if ( ratio < 100 ) $('#gallerybox-image').css({ width: wd, height: hd});
		}
		
		function _trasformSideBox(){				
			var position = $('#gallerybox-content').position();
			yleft = $('#gallerybox-content').width() + position.left + 20;
			
			pstop = (($('#gallerybox-content').height()- 150)/2) + position.top;
			psleft = ($('#gallerybox-content').width()/2) + position.left;
			
			if ( settings.imgActive > 0 ){
				$('#gallerybox-content-left').css({
					top:pstop,
					left: psleft-75,
					backgroundImage : "url("+settings.imgArray[settings.imgActive-1][1]+")"
				}).show();
				$('#gallerybox-content-left').animate({top:pstop, left: position.left -170 },settings.imgResizeSpeed);
			}
			
			if ( settings.imgActive < settings.imgArray.length - 1 ){
				$('#gallerybox-content-right').css({
					top:pstop,
					left: psleft-75,
					backgroundImage : "url("+settings.imgArray[settings.imgActive+1][1]+")"
				}).show();
				$('#gallerybox-content-right').animate({top:pstop, left:yleft+10},settings.imgResizeSpeed);
			}
			
			/* Show meta data div
			$('#gallerybox-content-description').css({
				backgroundColor: '#fff', opacity: 0.5
			}).slideDown(500);
			*/
		}
		
		function _closeBox() {
			$('#gallerybox-content,#gallerybox-image,#gallerybox-content-left,#gallerybox-content-right').stop();
			$('#gallerybox-outter').remove();
			$('#gallerybox-overlay').fadeOut(function() { $('#gallerybox-overlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		return this.unbind('click').click(_initialize);
	};
})(jQuery);

$(document).ready(function () {							
    $("a[rel^='gallerybox']").galleryBox();
});