/**
 * jQuery CMS Plugin.
 *
 * @link http://www.mtel-solutions.com/
 * @copyright Copyright &copy; 2011 MTel solution
 */

;(function($) {	
	$.fn.galleryTabs = function(){
		var $obj = $(this);
		$obj.find('img:eq(0)').show();
		$obj.find('ul.tabs a:eq(0)').css('color','#00CCFF');
		var total = $obj.find('div.photo a').size();
		var test = 760/total;
		//#gallery ul.tabs li
		$obj.find('ul.tabs li').width(test);
		//alert(test);

		$(this).find('ul.tabs a').click(function(){
			clearInterval(galleryTimer);
			var index = $obj.find('ul.tabs a').index(this);

			$obj.find('ul.tabs a:not('+index+')').css('color','#444444');
			$(this).css('color','#00CCFF');
					
			var loading = $('<div class="gallery_loading"><img border="0" src="/public/javascripts/gallery_tabs/loading.png"></div>');
			$obj.append(loading);
			$obj.find('div.photo').find('img:visible').fadeOut('fast', function(){
				$obj.find('div.photo img:eq('+index+')').fadeIn('fast', function(){
					loading.remove();
				});						
			});		
			galleryTimer = setInterval(loopGallery, 5000);
		});
		
		$obj.append('<div class="transparency"></div><div class="photo_click"></div>').find('div.photo_click').click(function(){
			var currentPhoto = $('#gallery div.photo img:visible');
			var index = $('#gallery div.photo img').index(currentPhoto);
			window.location.href = $('#gallery div.photo a:eq('+index+')').attr('href');
		});
	

		function loopGallery(){
			var currentPhoto = $obj.find('div.photo img:visible');
			var index = $obj.find('div.photo img').index(currentPhoto);
			var nextIndex = (parseInt(index)+1) >= $obj.find('div.photo a').size() ? 0 : parseInt(index)+1;
			$obj.find('ul.tabs a:eq('+nextIndex+')').trigger('click');
		}
			
		var galleryTimer = setInterval(loopGallery, 5000);
	}
})(jQuery);
