var time, iterator = 0;
var ShopArr = new Array(); 
var ShopActualArr = new Array();
var right_pres = false;  
var left_pres = false;	
var name, newName = '';

function leftStep() {
	
	$('#shop_presenter_left_arrow_unclick').show();
	$('#shop_presenter_right_arrow_unclick').show();
	$('#shop_presenter_left_arrow').hide();
	$('#shop_presenter_right_arrow').hide();
	$('.presenter_cat_type').fadeOut('slow');
	$('#shop_presenter').fadeOut('slow', function() {

		setTimeout( function() { 
			$('#shop_presenter_left_arrow_unclick').hide();
			$('#shop_presenter_right_arrow_unclick').hide();
			$('#shop_presenter_left_arrow').show();
			$('#shop_presenter_right_arrow').show();
		}, 300 );
		
		if(iterator - 16 < 0) {
			iterator = ShopArr.length - 16;
		} else {
			iterator -= 8;
		}		

		$('#shop_presenter').empty();
		var postpresstr = "nextShops=";
		for(var i=iterator; i < (iterator + 8); i++) {
			postpresstr += ShopArr[i]+'|';	
		}			

		$('#shop_presenter').empty();
		$('#shop_presenter').load("req_resp/pres_req_resp.php?"+postpresstr, function() {
			$('#shop_presenter').fadeIn('slow');
		});	
	});
}	

function rightStep() {
	
	$('#shop_presenter_left_arrow_unclick').show();
	$('#shop_presenter_right_arrow_unclick').show();
	$('#shop_presenter_left_arrow').hide();
	$('#shop_presenter_right_arrow').hide();
	$('.presenter_cat_type').fadeOut('slow');
	$('#shop_presenter').fadeOut('slow', function() {
		
		setTimeout( function() { 
			$('#shop_presenter_left_arrow_unclick').hide();
			$('#shop_presenter_right_arrow_unclick').hide();
			$('#shop_presenter_left_arrow').show();
			$('#shop_presenter_right_arrow').show();
		}, 300 );
		
		if(iterator + 16 > ShopArr.length) {
			iterator = 0;
		} else {
			iterator += 8;
		}		
		
		$('#shop_presenter').empty();
		var postpresstr = "nextShops=";
		for(var i=iterator ; i < (iterator + 8); i++) {
			postpresstr += ShopArr[i]+'|';	
		}
		
		$('#shop_presenter').load("req_resp/pres_req_resp.php?"+postpresstr, function() {
			$('#shop_presenter').fadeIn('slow');
		});	
	});											  
}

function callbackShopArrIsReady(){
	
	var temp_math = (Math.floor(ShopArr.length/2)-4);
	iterator = (temp_math < 0) ? 0 : temp_math;
	var actual_shops_arr = $('#actual_shops').html().split("|");
	var temp_splice_pos = iterator-1;
	for(var i=0;i<actual_shops_arr.length;i++) {
		temp_splice_pos++;
		ShopArr.splice(temp_splice_pos,0,actual_shops_arr[i]);
	}
	if(right_pres) { rightStep(); }
	if(left_pres) { leftStep(); }
	
}	

Array.prototype.findInArray = function(searchStr) {
	var returnArray = false;
	for (i=0; i<this.length; i++) {
		if (typeof(searchStr) == 'function') {
		  if (searchStr.test(this[i])) {
			if (!returnArray) { returnArray = [] }
			returnArray.push(i);
		  }
		} else {
		  if (this[i]===searchStr) {
			if (!returnArray) { returnArray = [] }
			returnArray.push(i);
		  }
		}
	}
	return returnArray;
}	

String.prototype.replaceAll = function(strTarget, strSubString ) {

	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	
	while (intIndexOfMatch != -1){
		strText = strText.replace( strTarget, strSubString )
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	
	return( strText );
}

function shop_presenter_firstCall(type) {
	
	if(ShopArr.length < 1) {
		$.ajax({
		type: "POST",
		url: "req_resp/pres_req_resp.php",
		data: "firstCall=ok&actualShops="+urlencode($('#actual_shops').html()),
		success: function(result){
				ShopArr = result.split("|");
				callbackShopArrIsReady();
			}
		});				
	} else {
		if(type=="left") leftStep();
		if(type=="right") rightStep();			
	} 
	if(type=="left") {
		right_pres = false;
		left_pres = true;
	} else if(type=="right") {
		right_pres = true;
		left_pres = false;
	}
}

$(document).ready(function () {	

	/*********balra lépés********/	
	$('#shop_presenter_left_arrow').click(function(e) {	
		shop_presenter_firstCall("left");
	});	
	
	/*********jobbra lépés********/
	$('#shop_presenter_right_arrow').click(function(e) {
		shop_presenter_firstCall("right");
	});		

// doc ready end	
});

