//////////////////////////double sponsor field	

var sponsorFieldList = null;
var lastSponsorsCount=0;
var currentSponsorsCount=0;
var targetDivName="double-sponsor-field";
var clmn1Name="clmn-1";
var psName="ps_div_id_";
var psTabl;
var lastDivHeight=0;
var heightSum=0;
var lastPageNumber=0;
var lastRowNumEnd=0;
var lastRowNumBegin=0;
var psMarginBottom=20;
var psUrlPrefix='';//'http://www.panoramapolska.pl:8000';
var psDebug=false;//true;

//klasa
function ps(href, url, id, height, width, spiderId){
	this.href=href;
	this.url=psUrlPrefix + url;
	this.id=id;
	this.height=height;
	this.width=width;
	this.spiderId=spiderId;
}
	
function processPS(){
	getDoubleSponsorField(true);//	name='resizeElement'
   	jQuery("a[name='resizeElement']").click(function () { 
      		getDoubleSponsorField(false);
		return false;
    	});
	
}

function getDoubleSponsorField(changePageNumber){
	if(!_categoryId>0)return;	
	resizePS();
	var targetDiv=jQuery("#"+targetDivName);
	if(changePageNumber==true)clearDoubleSponsorField();
	if(sponsorFieldList==null ){
		DoubleSponsorField.search(_categoryId, processDoubleSponsorFieldResults);
	}else{
		if(lastDivHeight<targetDiv.height()||changePageNumber)
			processAddingSponsorField(changePageNumber);
		else
			processRemovingSponsorField();
	}
	lastDivHeight=targetDiv.height();
}

function processDoubleSponsorFieldResults(data){
	var content = '';
	if(data == null) return;
	if(DWRUtil._isArray(data)){
		psTabl = new Array(data.length);
		for (var rowNum = 0; rowNum < data.length; rowNum++) {
			psTabl[rowNum]=new ps(data[rowNum].uri, data[rowNum].filepath, rowNum, data[rowNum].height, data[rowNum].width, data[rowNum].spiderId);
		}
	}
	if(psDebug){
		alert("processDoubleSponsorFieldResults: liczba reklam:" + psTabl.length);	
	}
	sponsorFieldList=psTabl;
	processAddingSponsorField();
}


function clearDoubleSponsorField(){
	var targetDiv=jQuery("#"+targetDivName);
	targetDiv.empty();
	heightSum=0;
}

function processRemovingSponsorField(){
	var targetDiv=jQuery("#"+targetDivName);
	if(sponsorFieldList==null){
		//targetDiv.empty();		
		clearDoubleSponsorField();
		return;
	}
	if(lastRowNumEnd<=0)return;
	var rowNum=lastRowNumEnd-1;
	var maxHeight=targetDiv.height();
        var clmn1=document.getElementById(clmn1Name);
        var dsf=document.getElementById(targetDivName);
        maxHeight=clmn1.offsetHeight - dsf.offsetTop;

	
	if(heightSum<=maxHeight)return;
	var zeroCurrentSponsorsCount=false;
	for (rowNum; rowNum >=0; rowNum--) {
		var ps=psTabl[rowNum];
		var id=psName+ps.id;
		jQuery("#"+id).remove();
		heightSum-=ps.height;
		heightSum-=psMarginBottom;
		if(heightSum<=maxHeight){
			break;
		}
	}
	currentSponsorsCount=rowNum-(lastSponsorsCount*_pageNumber);
	lastRowNumEnd=rowNum;
	resizePS();
        if(psDebug){
                alert("processRemovingSponsorField");
        }
}
	
function processAddingSponsorField(changePageNumber){
	if(changePageNumber){
// 		dla przeskokow o jedna strone oblicze dokladniej
		if(lastPageNumber-_pageNumber==1){
// 			do tylu
			currentSponsorsCount=lastRowNumBegin-lastSponsorsCount;
		}else if(lastPageNumber-_pageNumber==-1){
// 			do przodu
			currentSponsorsCount=lastRowNumEnd;
		}else{
// 			dla przeskokow wiekszych niz 1 page
			currentSponsorsCount = (lastSponsorsCount*_pageNumber);
		}
		if(_pageNumber==0)
			currentSponsorsCount=0;
	}else{
		currentSponsorsCount=lastRowNumEnd;
	}
	if(sponsorFieldList==null)return;
	var targetDiv=jQuery("#"+targetDivName);
	var rowNum=currentSponsorsCount;
	var maxHeight=targetDiv.height();
	var zeroCurrentSponsorsCount=false;
	lastRowNumBegin=rowNum;
	for (rowNum; rowNum < sponsorFieldList.length; rowNum++) {
		zeroCurrentSponsorsCount=false;
		var ps=psTabl[rowNum];
		if(ps.height>(maxHeight-heightSum))
			break;
		addPSAd(ps, targetDiv);
		heightSum+=ps.height;
		heightSum+=psMarginBottom;
		zeroCurrentSponsorsCount=true;
	}
 	if(zeroCurrentSponsorsCount)
 		currentSponsorsCount=0;
 	else
		currentSponsorsCount=rowNum-lastRowNumBegin;//(lastSponsorsCount*_pageNumber);
	lastRowNumEnd=rowNum;
        if(psDebug){
                alert("processAddingSponsorField");
        }

}
	


function addPSAd(ps, targetDiv){
	addRoiHitToDoubleSponsorField(ps);
	var id=psName+ps.id;
	var div=document.createElement("div");
	jQuery(div).appendTo(targetDiv);
	div.setAttribute("id", id);
	div.style.border=0;
	if(psDebug)
		div.style.border=1;
	div.style.margin=0;
	div.style.padding=0;
	var isImg=ps.url.indexOf(".swf")<0;
	if(isImg){
		var img=new Image();
		img.src = ps.url;
		img.style.border=0;
		img.setAttribute("title",ps.id);
		if(ps.width>0){
			img.setAttribute("width", ps.width);
			img.style.width=ps.width;
		}
		if(ps.height>0){
			img.setAttribute("height", ps.height);
			img.style.height=ps.height;
		}

		var a=document.createElement("a");
		a.setAttribute("href",ps.href);
		a.setAttribute("rel","nofollow");
		a.setAttribute("target","_blank");		
		jQuery(img).appendTo(a);
		jQuery(a).appendTo(div);
	}else{
		var w=290;
		var h=65;
		if(ps.width>0)w=ps.width;
		if(ps.height>0)h=ps.height;
		

		jQuery(div).flash({
		src: ps.url+'?click='+ps.href+'&clickTAG='+ps.href,
		width: w,
		height: h,
		quality:"high",
		loop:"true",
		play:"true"
		});
		
	}
//	jQuery("#"+id).css({"margin-bottom:50"});
	jQuery(div).css({ "margin-bottom":(psMarginBottom+"px")});
}
	
function addRoiHitToDoubleSponsorField(ps){
	var page=_pageNumber;
	try{
		page=parseInt(_pageNumber) + 1;
	}catch(err){}
	var params = "page="+(page)+",spider_id=" + ps.spiderId; 
	ROIhitp("double_sponsor_field", params);
}

function resizePS(){
	var clmn1=document.getElementById(clmn1Name);
	var dsf=document.getElementById(targetDivName);
	if(psDebug)
		jQuery("#"+targetDivName).css('background-color','gray');
	var height=clmn1.offsetHeight - dsf.offsetTop;
	if(height<0)height=0;
	jQuery("#"+targetDivName).height(height);
}

//////////////////////////end double sponsor field

