/**
 *	@ Script : gallery.js
 *
 *	@ Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *	
 */

$(function() {
	/**
	 *	@ for Menu Gallery
	 *	
	 */
	var _imgWidth = $('#mData img').width(),
		_totalSize = $('#mData img').size() - 1,
		_totalWidth =  _imgWidth * (_totalSize + 1),
		_posArray = [],
		_current = -1,
		_speed = 500, // Change Time
		_time = 5000; // Display Time
	for (var i = 0; i <= _totalSize; i++) {
		_posArray[i] = - (i * _imgWidth);
	}
	
	/* Css Settings */
	$('#mData').css('width',_totalWidth);
	
	/* autoPlay Controler */
	function autoPlay() {
		var _hidden = _current;
				
		_current++;
		if (_current > _totalSize) {
			_current = 0;
			_hidden = _totalSize;
		}

		$("#mData").animate({
			'marginLeft':_posArray[_current]
		}, _speed, function() {
			if (_hidden != -1) {
				$('#mThumb a').eq(_hidden).removeClass('current');
			}
			$('#mThumb a').eq(_current).addClass('current');
		});
	}
	
	/* Move Controler */
	function moveCtrl(_callName) {
		$('#mThumb a').unbind('click', navCtrl); // Event Disable
		clearTimeout(_timer); // autoPlay Stop
		
		var _current = _callName;
		
		$("#mData").animate({
			'marginLeft':_posArray[_current]
		}, _speed, function() {
			$('#mThumb a').bind('click', navCtrl);
		});
	}
	
	/* Timer Controler */
	function timerStart() {
		autoPlay();
		_timer = setTimeout(timerStart, _time);
	}
	
	/* Navigation Controler */
	function navCtrl() {
		var _callName = $(this).attr("rel");
		moveCtrl(_callName);

		$('#mThumb a').each(function() {
			$(this).removeClass('current');
		});
		$(this).addClass('current');
		return false;
	}
	
	/* Initilaize */
	function initilaize() {
		$('#mThumb a[rel="0"]').addClass('current'); // Default Current
		timerStart();
	}
	$('#mThumb a').bind('click', navCtrl);
	initilaize();
	
	
	/**
	 *	@ for Main Gallery
	 *	
	 */
	function slideshow() {
		// DELETE
		$('#mainGallery').css({
			'position':'relative',
			'height':410,
			'marginBottom':30
			
		});
		$('#mainGallery img').css({
			'position':'absolute',
			'top':0,
			'left':0
		});
		//
		_this = $('#mainGallery img');
			var _current = 0,
			_time = 1000,
			_delay = 4000,
			_total = _this.size() - 1;
	
		function loop() {
			getCurrent();
			_this.eq(_current).delay(_delay).fadeIn(_time, function() {
				_this.eq(_current - 1).css('display','none');
				loop();
			});
		}
		function getCurrent() {
			if (_current >= _total) _current = -1;
			_current++;
			_this.eq(_current - 1).css('z-index','0');
			_this.eq(_current).css('z-index','10');
		}
	
		_this.css("display", "none");
		_this.eq(_current).css("display", "block");
		_this.eq(_current).delay(_delay).fadeIn(_time, loop);
	}
	slideshow();

});
