/**
 * Ajax.Request.abort
 * extend the prototype.js Ajax.Request object so that it supports an abort method
 */
Ajax.Request.prototype.abort= function(){
    //call onAbort listener, if existent
    (this.options.onAbort || Prototype.emptyFunction)(this.transport, this.json);
    //trash existiting handlers
    this.options={};
    //now abort the request
    this.transport.abort();
  };




var oAJAX = null;
var bPickPageInProcess = false;


/*
@param callBackFunction - callback function name
@param page - number of current page to be displayed
@param displayTarget - HTML element id to render pager in
@param maxToDisplay - number of digits to by displayed in row
@param formName - HTML form id/name holding data and serving as a source form for ajax call
*/
function createPagerDigital(callBackFunction, page, displayTarget, formName, maxToDisplay, fallBackFunction){
	
	if(oAJAX) oAJAX.abort();
	
	document.forms[formName].max.value = '0';
	document.forms[formName].page.value = page;
	document.forms[formName].formName.value  = formName;
	if(displayTarget)
		document.forms[formName].displayTarget.value = displayTarget;
	if(callBackFunction)	
		document.forms[formName].callBackFunction.value  = callBackFunction;
	if(maxToDisplay)	
		document.forms[formName].maxToDisplay.value  = maxToDisplay;
	
	
	// oshri 25.8.08
	
	
	AJAX_Loading_Show(  document.forms[formName].containerTarget  );
	
	
	//TODO:
	if(formName == 'clipFloddingForm')
	{
					
		if(AsyncUpload.submitForm( document.forms[formName] , null ,  myCallBackDigital ))
		{
			document.forms[formName].submit();
		}		
	}
	else
	{
		oAJAX = new VignettePortal.AJAXClient().sendForm_inUrl(document.forms[formName], myCallBackDigital,fallBackFunction);
	}
	
	//ajaxCallSimulator(page, formName);
}

function myCallBackDigital(resp)
{
	var response = (resp.responseText)?resp.responseText:resp;
	/*
	@param max - total number of pages 
	*/
	var params = new Array();
	params[0]='page';
	params[1]='formName';
	params[2]='max';	
	
	var response2	= response;
	if(response.indexOf('mixesholder')>-1)
	{
		response2 = response.toLowerCase();
		params[1]='formname';
	}
	
	for(i=0; i<params.length; i++){
		var start = response2.indexOf('<' + params[i] + '>', 0);
		var end = response2.indexOf('</' + params[i] + '>', 0);
		params[i] = response.substring(start + params[i].length+2, end);
	}
	
	if(params[2])
	{
		document.forms[params[1]].max.value = params[2];
	}
	//call drawPager() with extracted params
	
	
	drawPagerDigital(params[0], params[1], response, null);
}

function drawPagerDigital(page, formName, response, startPage)
{
	//max is maximum possible page number for example 25 (filled by callback )
	//page is number of page that was clicked for example 5
	var max = document.forms[formName].max.value;
	//casting to Number
	+page;
	+max;
	//alert("page is " + page + "max is " + max);
	//-render start
	var stream = '';
	stream = "<table class='generic_paging_table' cellpadding='0' cellspacing='0' border='0'>";
	stream += "<tr><td class='generic_numeric_paging'>";

	//----left ruler arrow
	if(max > 1){//else pager doesnt needed

		//if(renderStart  == 0){//we are in the first page
		if(page == 1){
			stream += "<img src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif' alt='prev' width='18' height='18' />";
		}else{
			stream += "<img class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif' alt='prev' width='18' height='18'"
				+ " onClick='pickPageDigital(" + (page - 1) + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'"
					+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_left_on.gif" + String.fromCharCode(34) + ");'" + 
					"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif" + String.fromCharCode(34) + ");"  
					 + "'>" + "</img>";
		}			 
		//---inner table start
		stream += "<table class='generic_paging_results' cellpadding='0' cellspacing='0' border='0'><tr>";
		
		if( (page >= 4 && max > 5) || (page >= 3 && max == 6  )){ 
			stream += "<td class='generic_href' onClick='pickPageDigital(" + 1 + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + 1 +"</td>";	
		}
		if (page >= 5 && max > 6){ 								
			stream +="<td>...</td>";
		}
		
		//page numbers
		
		var x = 0;
		if (page < 3){
		    x = 3 - page;
		}
		var y = 0;
		if (max - page < 3){
			y = 2 - (max*1 - page*1);
		}
		//alert(" page=" + page +  "  max=" + max + "   x=" + x  + "   y=" + y); 
		var start = page*1 - 2 - y;
		if (start < 1){
			start = 1;
		}else if(start==(max-5) && page==(max-3) ){
			start++;
		}
		var end = page*1 + 2 + x;
		if (end > max){
			end = max;
		}else if( end==6 && page==4){
			end--;
		}
		
		//alert ("start=" + start + "end=" + end);            
		for( i = start; i <= end; i++){
			if(i == page){
			stream += "<td><div class='numeric_paging_fill'><div class='numeric_paging_left'><div class='numeric_paging_right numeric_paging_text'>" + i + "</div></div></div></td>";	
			}else{
			stream += "<td  class='generic_href' onClick='pickPageDigital(" + i + "," +
				String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + i + "</td>";
			}
	    }
		//---right ruler arrow			
		if (page == max){
			stream += "</tr></table>";
			stream += "<img src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'></img>";
		}else if ( (page <= max - 4)  || (page==4 && max==6) ){
			if(max > 6){stream +="<td>...</td>";}
			if (max > 5){
				//stream +="<td>...</td>";
			
				stream += "<td  class='generic_href' onClick='pickPageDigital(" + max + "," +
						  String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + max +"</td>";
			}
			stream += "</tr></table>";
			stream += "<img  class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'"
				+ " onClick='pickPageDigital(" + (page*1 + 1) + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'"
					+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right_on.gif" + String.fromCharCode(34) + ");'" + 
					"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif" + String.fromCharCode(34) + ");" 						  
					 + "'>" + "</img>";
		}else{ //we are not in the last page
			/*
		 	if (page == max - 4){
			stream += "<td  class='generic_href' onClick='pickPageDigital(" + (max - 1) + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + (max - 1) +"</td>";
			stream += "<td  class='generic_href' onClick='pickPageDigital(" + max + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + max +"</td>";		
			}
			*/ 		  
			if (page == max - 3 && max > 5){
			stream += "<td  class='generic_href' onClick='pickPageDigital(" + max + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'>" + max +"</td>";
			}
			stream += "</tr></table>";
			stream += "<img  class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'"
				+ " onClick='pickPageDigital(" + (page*1 + 1) + "," +
					String.fromCharCode(34) + formName + String.fromCharCode(34) + ")'"
					+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right_on.gif" + String.fromCharCode(34) + ");'" + 
					"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
					+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif" + String.fromCharCode(34) + ");"  
					 + "'>" + "</img>";
		}
		 
	}
	stream += '</td></tr></table>';
	
	$(document.forms[formName].displayTarget.value).update(stream);

	// hide AJAX indicator
	AJAX_Loading_Hide();
	
	if(response)
		eval(document.forms[formName].callBackFunction.value)(response);	

	bPickPageInProcess = false;		
}

function highlight(object, isOn){
	var suffix = 'HighLight';
	if(isOn==1){
		object.className = object.className + suffix;
	}
	else{
		if(isOn==0){
			object.className = object.className.substr(0, object.className.indexOf(suffix));		
		}
		else{
		object.src = isOn;
		}
	}
	
}

function pickPageDigital(page, formName){

if(bPickPageInProcess)
  return false;
else
  bPickPageInProcess = true;

KPlayer_stopAllPlayers();
	//callBackFunction, page, displayTarget, formName
	
	// if came from forum
	if (formName == "forumPagerFRM")
	{			
		messageClose();
	}
	

	var oForm = document.getElementById(formName);
	if(oForm){ 
		oForm.scrollIntoView(true);
		if( oForm.containerTarget) AJAX_Loading_Show(  oForm.containerTarget  );
	}
	
	if(oAJAX) {oAJAX.abort();}

	createPagerDigital(null, page, null, formName, null);		
	
}

var ajaxLoading = $('ajaxLoading');

function AJAX_Loading_Show(formField){
	if(!formField) return;
	var elm = $(formField.value);
	var ajaxLoading = $('ajaxLoading');
	if(elm) elm.innerHTML = '';
	if(ajaxLoading) ajaxLoading.show();
	}
	
function AJAX_Loading_Hide()
{
	var ajaxLoading = $('ajaxLoading');
	if(ajaxLoading) ajaxLoading.hide();
}	
	

var ordPager = 
{
	replaceQueryString : function(param,value) 
	{
	    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
	    if (this.url.match(re))
	        return this.url.replace(re,'$1' + param + "=" + value + '$2');
	    else
	        return this.url + '&' + param + "=" + value;
	},
	
	gotoPage: function(page)
	{
		var newURL = (page==1)? this.url.split('&page=')[0] :this.urlTemplate.interpolate({pageNum:page});
		window.location.href = newURL;
	},	
	
	draw: function(page, formName)
	{
		this.url = window.location.href;
		this.urlTemplate = this.replaceQueryString('page', '#{pageNum}');
		
		alerT(this.urlTemplate );
		//page is number of page that was clicked for example 5
		var max = document.forms[formName].max.value;
		//casting to Number
		+page;
		+max;
		//-render start
		var stream = '';
		stream = "<table class='generic_paging_table' cellpadding='0' cellspacing='0' border='0'>";
		stream += "<tr><td class='generic_numeric_paging'>";
	
		//----left ruler arrow
		if(max > 1){//else pager doesnt needed
	
			//if(renderStart  == 0){//we are in the first page
			if(page == 1){
				stream += "<img src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif' alt='prev' width='18' height='18' />";
			}else{
				stream += "<img class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif' alt='prev' width='18' height='18'"
					+ " onclick='ordPager.gotoPage("+ (page - 1) + ")'"
						+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_left_on.gif" + String.fromCharCode(34) + ");'" + 
						"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_left.gif" + String.fromCharCode(34) + ");"  
						 + "'>" + "</img>";
			}			 
			//---inner table start
			stream += "<table class='generic_paging_results' cellpadding='0' cellspacing='0' border='0'><tr>";
			
			if( (page >= 4 && max > 5) || (page >= 3 && max == 6  )){ 
				stream += "<td class='generic_href' onclick='ordPager.gotoPage(1)'>1</td>";	
			}
			if (page >= 5 && max > 6){ 								
				stream +="<td>...</td>";
			}
			
			//page numbers
			
			var x = 0;
			if (page < 3){
			    x = 3 - page;
			}
			var y = 0;
			if (max - page < 3){
				y = 2 - (max*1 - page*1);
			}
			//alert(" page=" + page +  "  max=" + max + "   x=" + x  + "   y=" + y); 
			var start = page*1 - 2 - y;
			if (start < 1){
				start = 1;
			}else if(start==(max-5) && page==(max-3) ){
				start++;
			}
			var end = page*1 + 2 + x;
			if (end > max){
				end = max;
			}else if( end==6 && page==4){
				end--;
			}
			
			//alert ("start=" + start + "end=" + end);            
			for( i = start; i <= end; i++){
				if(i == page){
				stream += "<td><div class='numeric_paging_fill'><div class='numeric_paging_left'><div class='numeric_paging_right numeric_paging_text'>" + i + "</div></div></div></td>";	
				}else{
				stream += "<td  class='generic_href' onclick='ordPager.gotoPage("+i+ ")'>" + i + "</td>";
				}
		    }
			//---right ruler arrow			
			if (page == max){
				stream += "</tr></table>";
				stream += "<img src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'></img>";
			}else if ( (page <= max - 4)  || (page==4 && max==6) ){
				if(max > 6){stream +="<td>...</td>";}
				if (max > 5){
					//stream +="<td>...</td>";
				
					stream += "<td  class='generic_href' onclick='ordPager.gotoPage("+ max + ")'>" + max +"</td>";
				}
				stream += "</tr></table>";
				stream += "<img  class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'"
					+ " onclick='ordPager.gotoPage("+ (page*1 + 1) + ")'"
						+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right_on.gif" + String.fromCharCode(34) + ");'" + 
						"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif" + String.fromCharCode(34) + ");" 						  
						 + "'>" + "</img>";
			}
			else
			{ 
				
				//we are not in the last page  
				if (page == max - 3 && max > 5){
				stream += "<td  class='generic_href' onclick='ordPager.gotoPage("+ max + ")'>" + max +"</td>";
				}
				stream += "</tr></table>";
				stream += "<img  class='generic_href' src='" + staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif' alt='next' width='18' height='18'"
					+ " onclick='ordPager.gotoPage("+ (page*1+1) + ")'"
						+ " onmouseover='numeric_paging_swapImage(this," + String.fromCharCode(34) 
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right_on.gif" + String.fromCharCode(34) + ");'" + 
						"onmouseout='numeric_paging_swapImage(this," + String.fromCharCode(34)
						+ staticResourcesUriPrefix + "/images/common/search_results_arrow_right.gif" + String.fromCharCode(34) + ");"  
						 + "'>" + "</img>";
			} 
		}
		stream += '</td></tr></table>';
		
		$(document.forms[formName].displayTarget.value).update(stream);
	}
	
}
