/*
	galleryList
	largeIMG
*/

jQuery.fn.galleryLight = function(_options){
	
	var _options = jQuery.extend({
		galleryList: 'ul li a',
		largeIMG: 'div.img-hold img',
		btNext: 'a.next-photo',
		btPrev:'a.prev-photo'
	},_options);
	
	return this.each(function(){
		var _THIS = $(this);
		var _galleryList = jQuery(_options.galleryList, _THIS);
		var _largeIMG = jQuery(_options.largeIMG, _THIS);
		var _btNext = jQuery(_options.btNext, _THIS);
		var _btPrev = jQuery(_options.btPrev, _THIS);
		
		// preload
		var _imagesG = [];
		_galleryList.each(function(i, el){
			if ($(el).attr('href') != '') {
				_imagesG[i] = new Image();
				var _rel = $(this).attr('href');
				_imagesG[i].src = _rel;
			}
		});
		
		// click gallery
		_galleryList.click(function(){
			_galleryList.removeClass('active');
			var _rel = $(this).attr('href');
			_largeIMG.attr('src',_rel);
			var _heightParent = _largeIMG.parent().height() - 72;
			var _thisIMGheight = _largeIMG.height();
			/*_largeIMG.css('marginTop',(_heightParent-_thisIMGheight)/2);*/
			$(this).addClass('active');
			return false;
		});
		// Next/Prev
		_btNext.click(function(){
			var _indexActive = _galleryList.index(_galleryList.filter('.active')) + 1;
			_galleryList.removeClass('active');
			if (_galleryList.eq(_indexActive).length) {
				_galleryList.eq(_indexActive).addClass('active');
			} else _galleryList.eq(0).addClass('active');
			var _rel = _galleryList.filter('.active').attr('href');
			_largeIMG.attr('src',_rel);
			return false;	
		});
		_btPrev.click(function(){
			var _indexActive = _galleryList.index(_galleryList.filter('.active')) - 1;
			_galleryList.removeClass('active');
			if (_galleryList.eq(_indexActive).length) {
				_galleryList.eq(_indexActive).addClass('active');
			} else _galleryList.eq(_galleryList.length).addClass('active');
			var _rel = _galleryList.filter('.active').attr('href');
			_largeIMG.attr('src',_rel);
			return false;
		});
	});
}

	