if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

$.fn.paginateRender = function() {
		start = this.options.size*page_current;
		stop = start + this.options.size;


		$('div.product', this).hide(); 
		$('div.product', this).slice(start, stop).show(); 
		if(page_counter-1==page_current) $('.next', this).hide(); else $('.next', this).show();
		if(page_current==0) $('.prev', this).hide(); else $('.prev', this).show();

		pagination = '';
		for(xx=1; xx<=page_counter; xx++)
		{
			if(xx==page_current+1)
			pagination += ' <strong>'+xx+'</strong> ';
			else
			pagination += ' <a href="#">'+xx+'</a> ';
		}

		$('.pagination .pages', this).html(pagination);
		
		$('.pagination .pages a', this).click(function() {
			page_current= $(this).text()-1;
			container.paginateRender();
			return false;
		});
			
}

$.fn.paginate = function(size) {
	container = this;
	container.options = new Array();
	container.options.size = size;
	counter = $('div.product', this).length;
	page_counter = Math.ceil(counter/size);
	page_current = 0;
	
	if(page_counter>1)
	{
		$(this).append('<div class="hr"></div><div class="paginate"></div><div class="pagination"><div class="pages"></div><a href="#" class="prev">&laquo; prev</a><a href="#" class="next">next &raquo;</a></div>');
		$('.prev', this).click(function() {
			page_current--; 
			container.paginateRender();
			return false;
		});
		$('.next', this).click(function() {
			page_current++; 
			container.paginateRender();
			return false;
		});
	}
	container.paginateRender();
};

function ac_item(row, i, max) {myclass=''; if(i==1) myclass='ac_first'; if(i==max) {myclass='ac_last'}; return '<span class="'+myclass+'">'+row[0]+': '+row[1]+'</span>';}

function ac_match(row, i, max) {return row[0]}
sub_list = new Array;
function acm_result(event, row, formatted) {
	$(this).val('');

	if(sub_list.indexOf(row[0])!='-1')
	return false;
	
	$sub = $('.register_list .sub:eq(0)').clone().show();
	sub_list.push(row[0]);
	$('img', $sub).attr('src', 'upload/products_images/'+row[0]+'.jpg');
	$('.sku', $sub).text(row[0]);
	$('.name div p', $sub).text(row[1].substr(0,32));
	$('select', $sub).attr('name', 'online['+row[2]+']');
	$('input[type=text]', $sub).attr('name', 'store['+row[2]+']');
	$('input, select', $sub).addClass('req');


	$('.register_list').append($sub);

	$('.product', $sub).click(function() {
		$(this).prev().remove();
		$(this).remove();
	});


}


$(document).ready(function() {

	if($('.products').length>0)
	{
		$('.products').paginate(12);

		$('.product').click(function() {
			window.location = $('a', this).attr('href');
		});

		$('.product').mouseover(function() {
			$('.img_bigger', this).show();
		});
		$('.product').mouseout(function() {
			$('.img_bigger', this).hide();
		});

		$('.product_img').mouseover(function() {
			$('.img_big', this).show();
		});
		$('.product_img').mouseout(function() {
			$('.img_big', this).hide();
		});
	}

	$('#search a').css('opacity', '0.6');
	
	$('#search a').mouseover(function() {
		$(this).stop().fadeTo("fast", '1');
	});
	
	$('#search a').mouseout(function() {
		$(this).stop().fadeTo("fast", '0.6');
	});

	if($("input[name=model]").length>0)
	{
		$("input[name=model]").autocomplete("/_extras/search_sku.php", {
			width: 400, selectFirst: false, delay: 0, minChars: 1, max: 15, matchContains: true, autoFill: false, formatItem: ac_item, formatMatch: ac_match
		});
		$("input[name=model]").result(acm_result);

		$('form.accept_js').submit(function() {

			if($('input[name^=store]').length<2)
			{
				error = true;
				text = 'You have to choose model';
			}
		});
	}


});



