(function($) {
	/**
	 * $ is an alias to jQuery object
	 *
	 */
	$.fn.scroller = function(options) {
// настройки по умолчанию
		options =  jQuery.extend({
		element : '',
		cat : '',
		category : '',
		count : 3,
		method : '',
		cname :'',
		counter : 0,
		lang : 'am',
		speed : 700,
		steparray : new Array(),
		size : 0,
		finish : 0,
		width : 0,
		height : 0,
		nextBn : '',
		prevBn : '',
		pfix : '0',
		act:'',
		xp : ''
},options);
this.init = function()
{
	//this.counter = 0;
	//$("#"+this.prevBn).click = this.prev;
	//$("#"+this.prevBn).click = this.prev;
	$("#"+options.prevBn).click(function () { 
      prev(); 
    });
	$("#"+options.nextBn).click(function () { 
		next(); 
	});
	$("#start_"+options.element).click(function () { 
		gotofirst(); 
	});
	//$("#"+this.nextBn).click = this.next;
}
function next()
{
	options.counter++;
	options.counter=(options.counter<0 || options.counter== undefined )?0:options.counter;
	if (options.steparray[options.counter] == undefined && options.counter!=0)
	{
		$("#"+options.nextBn+options.pfix).removeClass("hidden");
		var margin = options.size*options.counter*(-1);
		options.steparray[options.counter] = margin;
		if (options.method == 'margin-left')
			$("#"+options.element).animate({"marginLeft": (margin)+"px"},options.speed,"",insertnewgen());
		else 
			$("#"+options.element).animate({"left": (margin)+"px"},options.speed,"",insertnewgen());
	}
	else
	{
		if(options.finish!=0 && (options.counter+1)==options.finish)
			$("#"+options.nextBn+options.pfix).removeClass("hidden");
		if (options.method == 'margin-left')
			$("#"+options.element).animate({"marginLeft": options.steparray[options.counter]+"px"},options.speed);
		else 
			$("#"+options.element).animate({"left": options.steparray[options.counter]+"px"},options.speed);
		
	}
	$("#"+options.prevBn+options.pfix).addClass("hidden");
	$("#start_"+options.element+options.pfix).addClass("hidden");
	return false;
}
function prev()
{
	options.counter--;
	options.counter=(options.counter<0)?0:options.counter;
	options.steparray[options.counter]=(options.counter==0)?0:options.steparray[options.counter];
	if (options.method == 'margin-left')
		$("#"+options.element).animate({"marginLeft": options.steparray[options.counter]+"px"},options.speed);
	else
		$("#"+options.element).animate({"left": options.steparray[options.counter]+"px"},options.speed);
	if(options.counter==0)
	{
		$("#"+options.prevBn+options.pfix).removeClass("hidden");
		$("#start_"+options.element+options.pfix).removeClass("hidden");
	}
	$("#"+options.nextBn+options.pfix).addClass("hidden");
	return false;	
}
function gotofirst()
{
	options.counter=0;
	if (options.method == 'margin-left')
		$("#"+options.element).animate({"marginLeft": "0px"},options.speed);
	else
		$("#"+options.element).animate({"left": "0px"},options.speed);
	if(options.counter==0)
	{
		$("#"+options.prevBn+options.pfix).removeClass("hidden");
		$("#start_"+options.element+options.pfix).removeClass("hidden");
	}
	$("#"+options.nextBn+options.pfix).addClass("hidden");
	return false;	
}
function insertnewgen()
{
	$.post("engine.php", {'act':options.act,'page':options.counter+1,'cnt':options.count,'xp':options.xp},
		function(data)
		{
			var newsg = jQuery(document.createElement('div'));
			if (options.width)
				newsg.css('width',options.width+"px");
			if (options.height)
				newsg.css('height',options.height+"px");
			if (options.cname != '')
				newsg.attr("class",options.cname);
			//newsg.setAttribute("class", options.cname);
			newsg.html(data);
			//alert("#last_"+options.element);
			$("#last_"+options.element).before(data);
			//newsg.insertBefore($("#last_"+options.element));
			$("#"+options.prevBn+options.pfix).addClass("hidden");
			$("#"+options.nextBn+options.pfix).addClass("hidden");
			if (data==null || data=='')
			{	
				options.finish = options.counter+1;
				$("#"+options.nextBn+options.pfix).removeClass("hidden");
			}
		},"json");
		
		return false;
}
return this.init();
};
})(jQuery); 
