/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'arr-left',
			prevText: 		'Previous',
			nextId: 		'arr-right',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this);
			var s = $("li", obj).length; // total items in list;
			var w = 247; // one item width;
			var h = obj.height(); // one item height;
			var ts = s-4; // stop item;
			var t = 0; // current position;
			var tot = 0; // total items in DB, will be filled after first AJAX request;
			
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);
			//if(!vertical) $("li", obj).css('float','left');
			$('#hd').before('<div class="arrow" id="'+ options.prevId +'"><a href=\"javascript:void(0);\"><span>right</span></a></div><div class="arrow" id="'+ options.nextId +'"><a href=\"javascript:void(0);\"><span>right</span></a></div>');
			
			$("div#"+options.prevId).hide();
			$("div#"+options.nextId).hide();
			
			$("div#"+options.nextId).click(function(){
				animate("next");
				if((t>=ts) && (s!=tot)){
					fetchNext();
				}else{
					if (t>=ts) $(this).fadeOut();
				}
				$("div#"+options.prevId).fadeIn();
				
			});
			$("div#"+options.prevId).click(function(){		
				animate("prev");
				if (t<=0) $(this).fadeOut();
				$("div#"+options.nextId).fadeIn();
			});	
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+4;
				} else {
					t = (t<=0) ? 0 : t-4;
				};
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);
                    		
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			function fetchNext(){
				var lang = window.location.href.split('/'); //  ["http:", "", "www.mediaparks.eu", "ru", ""];
				$.get('/ajax.php', {act: 'loadNextProjects', offset: t+4, lang: lang[3]}, _processNext, 'xml');
			}
			function _processNext(response){
				tot = $('totalAmount', response).text();
				$('ul', obj).append($('items', response).text());
				s = $("li", obj).length; // recalculate width;
				ts = s-4; // recalculate stop item;
				$("ul", obj).css('width',s*w); // as item amount has changed reset total width;
				if(t>=ts){
					$("div#"+options.nextId).fadeOut();
				}
			}
			if(s>4) $("div#"+options.nextId).fadeIn();	
                    
		});
	  
	};

})(jQuery);
