/*

	myGallery 		jQuery Content Gallery Plugin
	Author: 		Marek Żeglarski
	Version:		1.0 (January 19, 2011)
	Documentation: 	buhahaha!
	
*/
(function($){
	
	var id;
	var rel, big_photo;
	var j_gallery, j_panels;
	
	var tmp, overlay, wrap, outer, inner, close, nav_up, nav_down, next_page, prev_page, main_photo, strip_photos, photo_title, photo_description, left_col, right_col;

	$.fn.myGallery = function(options) {		
		
		$('.myGallery_item').unbind('click.mg').bind('click.mg', function(e) {
			j_gallery = $(this).parent().parent();
			j_panels = $('.panel', j_gallery);		
			id = j_gallery.attr('id');
			myGallery_init();
			
			rel = $(this).attr('rel') || '';
			big_photo = $(this).attr('href') || '';			
			myGallery_start();	
			
			strip_photos.scrollTo(strip_photos.find('a[rel="'+rel+'"]'),600);		
			return false;
		});
		
		return this;
	};
	
	
	$.myGallery = function(obj, opts) {
		myGallery_start();
	};
	
	
	$.myGallery.close = function() {		
		function _cleanup() {
			overlay.fadeOut('fast');
			wrap.hide();
		}
		wrap.fadeOut( 'fast', _cleanup);	
	};
	
	
	$.myGallery.next_page = function() {
		//strip_photos.scrollTo({top:'+=500px',left:'+=0'},600);
		strip_photos.scrollTo({top:'+=560px',left:'+=0'},600);
	};	
	
	$.myGallery.prev_page = function() {
		//strip_photos.scrollTo({top:'-=500px',left:'+=0'},600);
		strip_photos.scrollTo({top:'-=560px',left:'+=0'},600);
	};
	
	/*
	$.myGallery.next = function(p_rel) {
		var next_item_index = strip_photos.find('a[rel="'+p_rel+'"]').index()+1;
		var next_item = strip_photos.find('a:eq('+next_item_index+')');
		var next_item_rel = next_item.attr('rel');
		var next_item_photo = next_item.attr('href');
		return $.myGallery.pos( next_item_rel, next_item_photo );
	};
	
	$.myGallery.prev = function(p_rel) {
		var prev_item_index = strip_photos.find('a[rel="'+p_rel+'"]').index()-1;
		var prev_item = strip_photos.find('a:eq('+prev_item_index+')');
		var prev_item_rel = prev_item.attr('rel');
		var prev_item_photo = prev_item.attr('href');
		return $.myGallery.pos( prev_item_rel, prev_item_photo );
	};

	$.myGallery.pos = function(t_rel, t_photo) {
		rel = t_rel;
		big_photo = t_photo;
		myGallery_start();		
	};
	*/
	
	
	function myGallery_start() {	
		myGallery_show();
	};
	
	
	function myGallery_show() {			
		overlay.css({
				'background-color'	: '#000',
				'opacity'			: '0.8'
			}).unbind().show();				
		overlay.one('click',	$.myGallery.close);	

		j_panels.each(function(i){
			$(this).find('a.myGallery_item').clone().appendTo(tmp);			   
		});			
		
		strip_photos.html( tmp.contents() );
		myGallery_enableStripClicking();		

		myGallery_showItem(rel,big_photo);
		
		var view	= get_viewport();
		var to		= {};

		to.top	= view[3] + ((view[1] - (wrap.height())) * 0.5);
		to.left	= view[2] + ((view[0] - (wrap.width())) * 0.5);
		to.top	= Math.max(view[3], to.top);
		to.left	= Math.max(view[2], to.left);

		wrap.css(to);		
		wrap.fadeIn('fast', _finish);		
	};
	
	
	function _finish() {
		$(document).unbind('keydown.mg').bind('keydown.mg', function(e) {
			if ( e.keyCode == 27 ) {
				$.myGallery.close();
			} 
		});	
	};
	
	
	function myGallery_showItem(i_rel, photo_url) {
		main_photo.empty().append( '<img src="'+photo_url+'" alt="" />' ); // width="720"
		var title = $('.panel_item_'+i_rel+' .panel-overlay h2').text();
		var description = $('.panel_item_'+i_rel+' .panel-overlay p').text();
		photo_title.empty().append( '<h2>'+title+'</h2>' );
		photo_description.empty().append( '<p>'+description+'</p>' );
		$('a', strip_photos).removeClass('current');
		strip_photos.find('a[rel="'+i_rel+'"]').addClass('current');
	};
	
	
	function myGallery_enableStripClicking() {
		strip_photos.children().each(function(i){
			$(this).click(function(){
				rel = $(this).attr('rel');
				big_photo = $(this).attr('href');
				myGallery_showItem( rel, big_photo);
				return false;
			});
		});
	};
	
	
	function get_viewport() {
		return [ $(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];
	};
	
	
	function myGallery_init() {

		$('body').append(
			tmp			= $('<div id="myGallery-tmp"></div>'),
			overlay		= $('<div id="myGallery-overlay"></div>'),
			wrap		= $('<div id="myGallery-wrap"></div>')
		);

		outer = $('<div id="myGallery-outer"></div>')
			.appendTo( wrap );

		outer.append(
			inner		= $('<div id="myGallery-inner"></div>'),
			close		= $('<a id="myGallery-close"></a>')			
		);
			
		inner.append(
			left_col 			= $('<div id="myGallery-leftcol"></div>'),			
			right_col 			= $('<div id="myGallery-rightcol"></div>')			
		);	
		
		left_col.append(
			main_photo			= $('<div id="myGallery-mainphoto"></div>'),
			photo_title 		= $('<div id="myGallery-phototitle"></div>'),
			photo_description	= $('<div id="myGallery-photodescription"></div>')
		);
		
		right_col.append(
			prev_page			= $('<a href="javascript:;" id="myGallery-up"></a>'),
			strip_photos		= $('<div id="myGallery-stripphotos"></div>'),			
			next_page			= $('<a href="javascript:;" id="myGallery-down"></a>')	
		);
		
		/*
		nav_up.click(function(e) {
			$.myGallery.prev(rel);
		});

		nav_down.click(function(e) {
			$.myGallery.next(rel);
		});*/
		
		prev_page.click(function(e) {
			$.myGallery.prev_page();
		});

		next_page.click(function(e) {
			$.myGallery.next_page();
		});
		
	};
	
	
	$.fn.myGallery.defaults = { };


})(jQuery);
