// -----------------------------------------------------------------------------------
// author Eugene Sarjinki, sarjick@gmail.com
// JavaScript Quick Gallery is a free plugin based on jQuery (jquery.com), that helps you create animated photo gallery very fast
// Last updated: 2011-20-01
//***********************************************

(function( $ ){
	jQuery.fn.sarjickGallery = function(options){
		return this.each(function() {
				var settings = {
				'space' : 5, 										/* space between photos */
				'current' : 0, 										/* number of active photo at start */
				'photoOpacity' : 1, 								/* on mouse action set opacity */
				'step' : 3, 										/* count of photos in scrolling */
				'arrowLeftJumpPath' : '../_common/img/gallery_nav/arrowBlackJump.gif', 
				'arrowLeftPath' : '../_common/img/gallery_nav/arrowBlack.gif',
				'arrowRightJumpPath' : '../_common/img/gallery_nav/arrowBlack2Jump.gif',
				'arrowRightPath' : '../_common/img/gallery_nav/arrowBlack2.gif',
				'bigPhotoClick' : 'LR', 							/* LR=RL, R, L  -  set which photo show when click on big*/
				'data' : 'gallery.php', 							/* server file, which back you info about photo, also can be used 			 																   	JavaScript Object, like:
																		var data = {
																			'id1' : "Text 1",
																			'id2' : "Text 2"
																		};
																		where id1, id2 - attributes 'rel' of photos */
				'activeClass' : 'active' 							/* any css class for active photo */
			};
			if (options) { 
				$.extend(settings, options);
			}
			var space = settings.space;
			var current = settings.current;
			var photoOpacity = settings.photoOpacity;
			var step = settings.step;
			var arrowLeftJumpPath = settings.arrowLeftJumpPath;
			var arrowLeftPath = settings.arrowLeftPath;
			var arrowRightJumpPath = settings.arrowRightJumpPath;
			var arrowRightPath = settings.arrowRightPath;
			var bigPhotoClick = settings.bigPhotoClick;
			var data = settings.data;
			var activeClass = settings.activeClass;
			
			var element = this;  
			var id = $(element).attr("id");
			var allPhotos = $("#" + id).html();
			var miniPhotos;
			var txtBlock;
			var count;
			var visiblePhotos = 0;
			var stepItem;
			var lightboxSettings = { containerResizeSpeed: 350, isPlayButton: false};
			var specNavPos = 0;
			
			function init(){		
				$("#" + id).html('<div class="arrowLeft"><img src="' + arrowLeftJumpPath + '" class="arrowPrevJump" /><img src="' + arrowLeftPath + '" class="arrowPrev" /></div><div class="arrowRight"><img src="' + arrowRightPath + '" class="arrowNext" /><img src="' + arrowRightJumpPath + '" class="arrowNextJump" /></div><div class="galleryPhotos"><div class="inner">' + allPhotos + '</div></div><a class="bigPhoto"></a><div class="text"></div>');
				
				miniPhotos = $("#" + id + " .galleryPhotos a");
				txtBlock = $("#" + id + " .text");
				count = miniPhotos.size();
				stepItem = miniPhotos.eq(0).width() + space + 2;/*2 - link's gray border left and right*/
				
				
				$("#" + id + " .arrowLeft img").hover(function () {$(this).css("opacity", 1);}, function () {$(this).css("opacity", photoOpacity);});
				$("#" + id + " .arrowRight img").hover(function () {$(this).css("opacity", 1);}, function () {$(this).css("opacity", photoOpacity);});

				$(".specialNavPrev").hover(function () {$(this).css("opacity", 1);}, function () {$(this).css("opacity", 0.5);});
				$(".specialNavNext").hover(function () {$(this).css("opacity", 1);}, function () {$(this).css("opacity", 0.5);});
				
				miniPhotos.css("margin-right", space + "px");		
				//$("#" + id + " .galleryPhotos .inner").css("width", count * stepItem);
				
				miniPhotos.each(function(index) {
					$(this).hover(function () {$(this).css("opacity", photoOpacity);}, function () {$(this).not(".active").css("opacity", 0.5);});
					
					$(this).click(function() {
						current = index;
						initPhoto();
						return false;				
					});
					
				});
				
				checkHash();
				initPhoto();
				initPhotosPosition();
				initScroll();		
			}
				
			function initPhoto(){
				miniPhotos.attr("class", "");
				miniPhotos.css("opacity", 0.5);
				var bigSrc = "";
				
				$("#" + id + " .bigPhoto").animate({
					opacity: 0
				}, 500, function() {
					bigSrc = miniPhotos.eq(current).attr("href").replace("medium", "original");
					//$("#" + id + " .bigPhoto").attr("href", bigSrc);
					$("#" + id + " .bigPhoto").html('<img class="bigPhotoImg" src="' + miniPhotos.eq(current).attr("href") + '" />');
					
					specNavPos = ($(".bigPhotoImg").height())/ 2 + 62;
					if (specNavPos < 80){
						setTimeout(function(){
							specNavPos = ($(".bigPhotoImg").height())/ 2 + 62;
							addSpecNavPos();
						},500);
						//alert(specNavPos);
					}else{
						addSpecNavPos();
					}
					
					$("#" + id + " .bigPhoto").animate({
						opacity: 1							   
					});
					
					$("#" + id + " .bigPhoto").click(function(){
						$(".galleryPhotos a").lightBox(lightboxSettings);
						miniPhotos.eq(current).trigger("click");
					});
					
					//
					//$("#" + id + " .bigPhoto").lightBox(lightboxSettings);
					//addSpecNavPos();
				});
				
				initInfo(miniPhotos.eq(current).attr("rel"));
				
				miniPhotos.eq(current).css("opacity", photoOpacity);
				miniPhotos.eq(current).attr("class", activeClass);
				
				visiblePhotos = Math.floor(current/step);
				initPhotosPosition();
				addHash();
				//addSpecNavPos();
			}
			function addSpecNavPos(){
				/*var qqq = ($(".bigPhotoImg").height())/ 2 + 62;
				$(".specialNav").css("margin-top",qqq + "px");*/
				$(".specialNav").animate({
					marginTop: specNavPos							   
				});
			}
			function addHash(){
				window.location.hash = miniPhotos.eq(current).attr("rel");
			}
			function checkHash(){
				if(window.location.hash.length > 0){
					miniPhotos.each(function(index) {
						if("#" + $(this).attr("rel") == window.location.hash){
							current = index;
							return false;
						}					
					});
				}
			}
			function initPhotosPosition(){
				$("#" + id + " .galleryPhotos .inner").animate({
					left:-visiblePhotos*step*stepItem + "px"
				});
			}
			function initScroll(){
				$("#" + id + " .arrowLeft img.arrowPrev").click(function(){
					element.gotoPrevPhoto();	
				});
				$("#" + id + " .arrowRight img.arrowNext").click(function(){
					element.gotoNextPhoto();		
				});
				
				$(".specialNavPrev").click(function(){
					element.gotoPrevPhoto();	
				});
				$(".specialNavNext").click(function(){
					element.gotoNextPhoto();		
				});
				
				$("#" + id + " .arrowLeft img.arrowPrevJump").click(function(){
					element.gotoPrevSlide();
				});
				$("#" + id + " .arrowRight img.arrowNextJump").click(function(){
					element.gotoNextSlide();
				});
				/*
				$("#" + id + " .bigPhoto").click(function(e){
					var position = $(this).position();
					if(bigPhotoClick == "LR" || bigPhotoClick == "RL"){					
						if(e.pageX - position.left < $(this).width()/2){
							element.gotoPrevPhoto();
						}
						else{
							element.gotoNextPhoto();
						}
					}
					else if(bigPhotoClick == "R"){
						element.gotoNextPhoto();
					}
					else if(bigPhotoClick == "L"){
						element.gotoPrevPhoto();
					}
					else{
						element.gotoNextPhoto();
					}				
				});
				*/
			}
			this.gotoNextPhoto = function(){
				if(current < count - 1){
					current	++;			
				}
				else{
					current	= 0;
				}
				initPhoto();
			}
			this.gotoPrevPhoto = function(){
				if(current > 0){
					current	--;			
				}
				else{
					current = count - 1;
				}
				initPhoto();
			}
			this.gotoNextSlide = function(){
				var add = 0;
				if(count%step == 0){
					add = 1;
				}
				
				if(visiblePhotos < Math.floor(count/step) - add){
					visiblePhotos++;			
				}
				else{
					visiblePhotos = 0;
				}
				initPhotosPosition();
			}
			this.gotoPrevSlide = function(){
				var add = 0;
				if(count%step == 0){
					add = 1;
				}
				
				if(visiblePhotos > 0){
					visiblePhotos--;			
				}
				else{
					visiblePhotos = Math.floor(count/step) - add;
				}
				initPhotosPosition();
			}
			function initInfo(idPhoto){
				/*
				if(typeof data == "string"){
					$.ajax({
						type: "POST",
						url: data,
						data: "id=" + idPhoto,
						success: function(msg){
							txtBlock.html(msg);
						}
					});		
				}
				else{
					txtBlock.html(data[idPhoto]);
				}
				*/
			}
			
			init();
		});		
	};
})( jQuery );

