// JavaScript Document

(function($) {
	$.fn.jQuerySlider = function(options) {
		var defaults = {
			'width' : 400,
			'height' : 150,
			'speed' : 500,
			'part' : 1,
			'pagination' : true,
			'full_slide' : false,
			'auto_slider' : [true, 5000],
			'fade_effect' : false,
			'vertical' : false
		};
		return this.each(function() {
			if(options) $.extend(defaults, options);
			var auto_slider = defaults['auto_slider'];
			var pagination = defaults['pagination'];
			var part = defaults['part'];
			var width = defaults['width'];
			var height = defaults['height'];
			var speed = defaults['speed'];
			var full_slide = defaults['full_slide'];
			var fade_effect = defaults['fade_effect'];
			var vertical = defaults['vertical'];
			var jQueryGallery = $(this).attr('class') ? "." + $(this).attr('class') : "#" + $(this).attr('id');
			var gallery_bx = $(this).children('div').attr('class') ? "." + $(this).children('div').attr('class') : "#" + $(this).children('div').attr('id');
			var full_width = $(this).children('div').children('div').attr('class') ? "." + $(this).children('div').children('div').attr('class') : "#" + $(this).children('div').children('div').attr('id');
			var slider = $(this).children('div').children('div').children('div').attr('class').split(' ')[0];
			var count = $('.'+slider+'').length;
			var maxcount = Math.ceil(count/part);
			var i = 0;
			var full_wid = count * width;
			if(vertical) {
				$(full_width).width(width*part).height(height);
			} else {
				$(full_width).width(full_wid).height(height);
			}
			if(part > 1) {
				$(jQueryGallery+' '+gallery_bx).width(width*part).height(height);
			} else {
				$(jQueryGallery+' '+gallery_bx).width(width).height(height);
			}
			$(jQueryGallery+' .'+slider+'').width(width).height(height);
			var target = jQueryGallery+' .key';
			if(pagination) {
				target = jQueryGallery+' .key, '+jQueryGallery+' .pagination a';
				var html = "<div class='pagination'>";
				if(vertical) {
					var final_count = maxcount + 1;
				} else {
					var final_count = full_slide==true ?  maxcount+1 : count-(part-2);
				}
				for(var k = 1; k < final_count; k++){
					html += "<a ";
					if(k == 1) html += "class='active' ";
					html += "href='javascript:void(0)' rel='"+k+"'>"+k+"</a>";
					}
				html += "</div>";
				$(jQueryGallery+'').append(html);
			}
			$(target).click(function() {
				if(auto_slider[0]==true) clearInterval($timeout);
				var cur_class = $(this).attr('class') == undefined ? "" :  $(this).attr('class').split(' ')[1];
				SlideBox(cur_class, $(this), "");
				if(auto_slider[0]==true) intervalID();
			});
			if(auto_slider[0]==true) intervalID();
			function intervalID() {
				$timeout = setInterval(function() {
					SlideBox("right", "", "auto");
				}, auto_slider[1]);
			}
			function SlideBox(action, element, auto) {
				if(action == 'right') {
					i++;
					var righthidden = full_slide==true ?  maxcount-1 : count-part;
					if(i>righthidden && auto == 'auto') {
						i = 0;
					}
				} else if(action == 'left') {
					i = i - 1;
				} else {
					i = $(element).attr('rel') - 1;
				}
				if(vertical) {
					var final_slide = height * i;
					var limit = (height * (maxcount-1));
				} else {
					var final_slide = full_slide==true ?  (width*i)*part : width*i;
					var limit = full_slide==true ? ((maxcount*part)*width)-(part*width) : (count*width)-(part*width);
				}
					if((final_slide >= 0) && (final_slide <= limit )) {
						if(fade_effect) {
							$(full_width).animate({
								opacity : 0
							}, speed);
						}
						if(vertical) {
							$(full_width).animate({
								top : -(final_slide)
							}, speed );	
						} else {
							$(full_width).animate({
								left : -(final_slide)
							}, speed );
						}
						if(fade_effect) {
							$(full_width).animate({
								opacity : 1
							}, speed);
						}
					$(jQueryGallery+' .pagination a').removeClass('active').eq(i).addClass('active');
					}
				if(final_slide > 0) $(jQueryGallery+' .left').show('fast');
				if(final_slide == 0)$(jQueryGallery+' .left').hide('fast');
				if(final_slide < limit)$(jQueryGallery+' .right').show('fast');
				if(final_slide == limit)$(jQueryGallery+' .right').hide('fast');
			}
		});
	};
})(jQuery);
