/*
 * jQuery Vuligal v0.1.0 - vuliad@gmail.com
 *
 * Copyright (c) 2010 Remin Vladislav 16.04.2010 22:22.
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
 

 
 
jQuery.fn.vuligal = function(options){

	// настройки по умолчанию
	var options = jQuery.extend({
		BigBoxH: 200, // 
        BigBoxW: 300, // 
		itemshow:5,
		ImBoxW:100,
		spacing:10,//i
		ImBoxH:80,
		delay:600, // 
		start:3
	},options);

	return this.each(function() {

		var CurGal=this;
  		var imageboxes;
		var bigboxes;
		//var cgal
		$(CurGal).wrap('<div class="vuligallery"></div>');
		var GalCont = $(CurGal).parent();
		var BigBox = $('<div class="BigBox"></div>');
		var MinBox = $('<div class="MinBox"></div>');
		var LongBox= $('<div class="LongBox"></div>');
		$(MinBox).append(LongBox);
		var ArrowBox= $('<div class="ArrowBox"></div>');
		var ArrowPrev = $('<div class="ArrowPrev"></div>');
		var ArrowNext = $('<div class="ArrowNext"></div>');
		$(ArrowBox).append(ArrowPrev,ArrowNext);


		$(GalCont).append(BigBox,MinBox,ArrowBox,$('<div style="clear:both;"></div>'));

//	    console.log(GalCont);

		$(CurGal).children().each( function () {
		//				console.log($(this).find('a').attr('href'));
						//	console.log($(this).find('img'));
							var bigimagebox = $('<div class="bigimagebox"></div>').css('display','none');
							var imagebox = $('<div class="imagebox"></div>');
					
							//console.log($(LongBox).children().length);
					
							$(imagebox).data('BigBox',$(LongBox).children().length);
							$(bigimagebox).append('<img src="'+$(this).find('a').attr('href')+'"/>');
							$(imagebox).append($(this).find('img'));
							$(imagebox).css('margin-right',options.spacing);
							
			//image()
			$(BigBox).append(bigimagebox);
			$(LongBox).append(imagebox);
			});
		$(CurGal).remove();
		if(options.start>$(LongBox).children().length-1) { options.start=$(MinBox).children().length-1;}
        GetImageBoxes();
		var currentImage =$(imageboxes).eq(options.start-1);
		var currentBox = $(bigboxes).eq(options.start-1);
		var leftelem;
		$(currentImage).addClass("currentImage");
        $(currentBox).css("display","block");
        SetBigBox();
		if(options.BigBoxW!=0) { $(BigBox).css('width',options.BigBoxW);}
		if(options.BigBoxH!=0) { $(BigBox).css('height',options.BigBoxH);}
		if(options.ImBoxW!=0) { $(imageboxes).css('width',options.ImBoxW);}
		if(options.ImBoxH!=0) { $(imageboxes).css('height',options.ImBoxH);}

$(ArrowBox).css('width',(TotalWidth($(currentImage)))*options.itemshow - options.spacing);
		
$(MinBox).css('width',(TotalWidth($(currentImage)))*options.itemshow - options.spacing);

$(MinBox).css('height',TotalHeight($(currentImage)));
		SetNewPos();
		//$(LongBox).css('left',-$(currentImage).position().left);

		//console.log()

		//$LongBox()

		function TotalWidth(theDiv)
		{
///////////var theDiv = $("#theDiv");
			var tWidth = theDiv.width();
			tWidth += parseInt(theDiv.css("padding-left"), 10) + parseInt(theDiv.css("padding-right"), 10); //Total Padding Width
			tWidth += parseInt(theDiv.css("margin-left"), 10) + parseInt(theDiv.css("margin-right"), 10); //Total Margin Width
			tWidth += parseInt(theDiv.css("borderLeftWidth"), 10) + parseInt(theDiv.css("borderRightWidth"), 10); //Total Border Width
			return tWidth;
		}


		function TotalHeight(theDiv)
		{
///////////var theDiv = $("#theDiv");
			var tHeight = theDiv.height();
			tHeight += parseInt(theDiv.css("padding-top"), 10) + parseInt(theDiv.css("padding-bottom"), 10); //Total Padding Width
			tHeight += parseInt(theDiv.css("margin-top"), 10) + parseInt(theDiv.css("margin-bottom"), 10); //Total Margin Width
			tHeight += parseInt(theDiv.css("borderTopWidth"), 10) + parseInt(theDiv.css("borderBottomWidth"), 10); //Total Border Width
			return tHeight;
		}



    	function SetBigBox()
    	{
    			//$(BigBox).html("<img src=\""+$(currentImage).data('BigBox')+"\" />");
				//$(BigBox).find('img').attr('src',$(currentImage).data('BigBox'));
				$(currentBox).fadeOut(options.delay / 2, function () 
						{
							currentBox = $(bigboxes).eq($(currentImage).data('BigBox')); 				
							$(currentBox).fadeIn(options.delay / 2);
						});
				//console.log($(currentImage).data('BigBox'));			
				//$(currentImage).data('BigBox');
    		}


		function SetNewPos()
		{
			//if($(currentImage).position().left+$(MinBox).css('width') )
			//console.log($(currentImage).position().left+' '+$(MinBox).css('width')+' ' );
			//var newpos=0;
			
		if($(imageboxes).length-$(imageboxes).index(currentImage)<options.itemshow -1)
			{
			leftelem=$(imageboxes).eq($(imageboxes).length-options.itemshow);
		//	console.log(leftelem);
			}
//		else if($(imageboxes).index(currentImage))
	//	{
//			leftelem=$(imageboxes).eq($(imageboxes).length-options.itemshow);
			
	//	}
		else  if($(imageboxes).index(currentImage)>=options.itemshow / 2 - options.itemshow % 2 )
			{
			//	console.log($(imageboxes).index(currentImage))
				leftelem=$(imageboxes).eq($(imageboxes).index(currentImage)- (options.itemshow / 2 - options.itemshow % 2 ));//    $(currentImage).prev().prev();
			}
			else
			{
				leftelem=$(imageboxes).first();
			}
			
//	if($(imageboxes).index(currentImage)==leftelem)
	//{
		//leftelem=$(currentImage.prev());
		
	//}
	
			
		
		//	else
		//	{
			$(LongBox).stop(0,0);
				$(LongBox).animate({'left':-$(leftelem).position().left},options.delay,'swing');
		//	}
		}


		function GetImageBoxes()
    	{
			bigboxes=$(BigBox).children();
    	return imageboxes=$(LongBox).children();
    		}

  		//function ScrollToCurrent()
  		//{
  		//$(LongBox).css('left',-$(currentImage).position().left);
  		//	}

		$(ArrowNext).bind('click', function () {
			
		//		if($(imageboxes).length-$(imageboxes).index(currentImage)==options.itemshow)
		//			{
		// Задумки для реализации бесконечного прокручивания.
		//			$(LongBox).append($(imageboxes).first());
		//			GetImageBoxes();
		//			ScrollToCurrent();
		//			}
		// если вдруг сопрете и доделаете, пришлите автору)	
					
				if($(imageboxes).index(currentImage)<$(imageboxes).length-1)
					{
						$(currentImage).removeClass('currentImage');
						currentImage=$(currentImage).next();
						$(currentImage).addClass('currentImage');
						SetNewPos();
						SetBigBox();
					}
		});

			$(imageboxes).bind('click', function () {
							if(this!=currentImage)
									{
									$(currentImage).removeClass('currentImage');	
									currentImage=this;
									$(currentImage).addClass('currentImage');	
									
									SetNewPos();
									SetBigBox();
									}
												  });

			$(ArrowPrev).bind('click', function () {
		
					if($(imageboxes).index(currentImage)==0)
					{
					
				// Задумки для реализации бесконечного прокручивания.
				//			$(LongBox).prepend($(imageboxes).last()); GetImageBoxes();
				//			ScrollToCurrent();
					}
					else
					{
												$(currentImage).removeClass('currentImage');
						currentImage=$(currentImage).prev();
						$(currentImage).addClass('currentImage');						
						SetNewPos();
						SetBigBox();

					}

});



	});

};
