//var ajaxScrollerSelectBegin = 0;
var ajaxScrollerSelectBeginArray = new Array();

var ajaxScrollerDirection = 1;

jQuery(window).load(function(){
	// click on next/prev link
	jQuery(".ajaxscroller_prev").click(function(){
		getItemAndPlace(this, "prev");
	});
	jQuery(".ajaxscroller_next").click(function(){
		getItemAndPlace(this, "next");
	});
	
	// scroll automatic every x seconds
	if (ajaxScrollerAutoScroll > 0) {
		autoScroll();
	}
	
	if (ajaxScrollerStopAtLastElement > 0) {
		jQuery(".ajaxscroller_prev").hide();
	}
	jQuery( ".ajaxscroller_next").each(function(i){
		getItemAndPlace(this, "begin");
	}); 
});

/**
 * Basic function for AJAX request
 *
 * @param object cObj: clicked element (prev or next link)
 * @param string nextOrPrev: Link clicked ("next" or "prev")
 */
function getItemAndPlace(cObj, nextOrPrev) {
	var c_id = jQuery(cObj).attr('id');	
	var id_arr = c_id.split('_');
	
	if(id_arr[2] !== undefined && id_arr[2] !== ''){
		c_Pid=id_arr[2];
		b_Pid=id_arr[2];
		ajaxScrollerPid = id_arr[2];
	} else {
		c_Pid='';
		b_Pid=0;
	}
	if (ajaxScrollerSelectBeginArray[b_Pid] == undefined) {
		ajaxScrollerSelectBeginArray[b_Pid] = 0;
	}
	
	var as_pages = jQuery("#ajaxscroller_pid_"+ c_Pid).val();
	
	if (ajaxScrollerStopAtLastElement > 0) {
		//jQuery(".ajaxscroller_prev, .ajaxscroller_next").fadeOut();
		jQuery("ajaxscroller_prev_"+c_Pid+", ajaxscroller_next_"+c_Pid).fadeOut();
	}

	// stop timeout
	if (ajaxScrollerAutoScroll > 0) {
		window.clearTimeout(autoScroller);
	}
	// calculate next record
	if (nextOrPrev == "prev") {
		ajaxScrollerSelectBeginArray[b_Pid] = ajaxScrollerSelectBeginArray[b_Pid] - ajaxScrollerMax;
	} else if (nextOrPrev == "next") {
		ajaxScrollerSelectBeginArray[b_Pid] = ajaxScrollerSelectBeginArray[b_Pid] + ajaxScrollerMax;
	} else ajaxScrollerSelectBeginArray[b_Pid] = 0;
	
	if (ajaxScrollerSelectBeginArray[b_Pid] >= ajaxScrollerMaxRowCount[b_Pid]) {
		ajaxScrollerSelectBeginArray[b_Pid] = 0;
	}
	if (ajaxScrollerSelectBeginArray[b_Pid] < 0) {
		ajaxScrollerSelectBeginArray[b_Pid] = ajaxScrollerMaxRowCount[b_Pid] - ajaxScrollerMax;
	}

	// hook for effects
	hookGetItemAndPlaceStart(cObj, nextOrPrev,ajaxScrollerDirection,b_Pid);
	
	// AJAX request
	jQuery.ajax({
		url: ajaxScrollerURL[c_Pid],
		//data: ({id : ajaxScrollerPid, type : ajaxScrollerTypeNum, begin : ajaxScrollerSelectBeginArray[b_Pid], no_cache : 1, as_pages: as_pages, lang: ajaxScrollerLang}),
		data: ({begin : ajaxScrollerSelectBeginArray[b_Pid], as_pages: as_pages, lang: ajaxScrollerLang, startpage:ajaxScrollerStartpage}),
		async : false,
		dataType: "json",
		success: function(ret){
		
		//alert(ret.content);	

		// place new content
			//jQuery(cObj).closest(".tx-conajaxscroller-pi1").find(".ajaxscroller_content").html(ret.content);
			jQuery("#ajaxscroller_content_" + c_Pid).html(ret.content);
			
			jQuery(document).ready(function() {
				jQuery("a[rel^='lightbox']").slimbox({  }, null, function(el) {
                        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
                });
				initializePrettyPhotoGalleries();
			}); 
			
			// scroll automatic every x seconds
			if (ajaxScrollerAutoScroll > 0) {
				autoScroll();
			}
			
			// hide / show next and prev buttons
			if (ajaxScrollerStopAtLastElement > 0) {
				if (ajaxScrollerSelectBeginArray[b_Pid] > 0) {
					jQuery("#ajaxscroller_prev_" + c_Pid).fadeIn();
				} else {
					jQuery("#ajaxscroller_prev_" + c_Pid).fadeOut();
				}
				if (ajaxScrollerSelectBeginArray[b_Pid] >= ajaxScrollerMaxRowCount[b_Pid] - 1) {
					jQuery("#ajaxscroller_next_" + c_Pid).fadeOut();
				} else {
					jQuery("#ajaxscroller_next_" + c_Pid).fadeIn();
				}
			}
			
			// hook for effects
			hookGetItemAndPlaceSuccess(cObj, nextOrPrev,ajaxScrollerDirection,b_Pid);
		}
	});
}

/**
 * Timeout for next automatic scrolling
 */
function autoScroll() {
	autoScroller = window.setTimeout('getItemAndPlace(jQuery(".ajaxscroller_next"))', ajaxScrollerAutoScroll * 1000);
}
