/* $Id: tools3.js,v 1.14 2010-09-29 12:09:04 mmajer Exp $ */
var PPTools = {
	WP2_ID:155102,
		
	getJSUrl: function(settings,ajax)
	{
		var server=settings.imgServerName?settings.imgServerName:settings.serverName;
		// fwo, nie wylaczac server name !
		return (ajax?"":server) + "/rf/pt"
			+ "?widget-type=" + settings.widgetType
			+ "&pid=" + settings.partnerId
			+ (settings.direction?"&direction="+settings.direction:"")
			+ (settings.categoryId?"&category="+settings.categoryId:"")
			+ (settings.productList?"&productList="+settings.productList:"")
			+ (settings.type?"&type="+settings.type:"");
	},
	getImgUrlBase: function(settings)
	{
		return settings.imgServerName?settings.imgServerName:settings.serverName;
	},
	write:	function(settings)
	{
		var s = this.getHTML();
		if (s) document.write(s);
	},
	getStyleAttr: function(s)
	{
		if (s==null) return "";
		if (s.length==0) return "";
		return " style=\"" + s +"\" ";
	},

	// ---------------------------------------------------------------------------------
	// NEWS - PRODUCTS LIST
	// ---------------------------------------------------------------------------------

	getHTML_news: function()
	{
		var image_sizes = new Array();
		image_sizes[1] = {w:59,h:78};
		image_sizes[2] = {w:72,h:97};
		image_sizes[3] = {w:85,h:115};
		image_sizes[4] = {w:100,h:136};
		image_sizes[5] = {w:115,h:155};
		image_sizes[6] = {w:130,h:170};
		image_sizes[7] = {w:170,h:222};
		image_sizes[8] = {w:200,h:262};
		image_sizes[9] = {w:250,h:327};
		image_sizes[10] = {w:370,h:484};
		image_sizes[11] = {w:455,h:593};

		//backward compatibility:
		if (!this.image_size)     this.image_size     = 3;
		if (!this.w2_h_color)     this.w2_h_color     = "#ffffff";
		if (!this.w2_h_bgcolor)   this.w2_h_bgcolor   = "#5586A5";
		if (!this.w2_box_color)   this.w2_box_color   = "#999999";
		if (!this.w2_box_bgcolor) this.w2_box_bgcolor = "#ffffff";
		if (!this.w2_plr_color)   this.w2_plr_color   = "#5382A5";
		if (!this.w2_h_fontsize)  this.w2_h_fontsize  = "14px;";

		if (!this.products) return "";
		if (this.products.length == 0) return "";

		var title_style = this.title_style;
		var img_style = this.img_style;

		var count = this.products.length;
		if (this.productsCount > 0) count = Math.min(count,this.productsCount);

		// szerokosc obrazka
		if (this.image_size < 1) this.image_size = 1;
		if (this.image_size > 11) this.image_size = 11;

		this.height = this.height ? this.height : 150;
		this.imgWidth = Math.max(image_sizes[this.image_size].w+10,140);
		this.width =  this.width ? this.width : ((this.layout==0)?this.imgWidth + 15:count*(this.imgWidth + 10));

		//wrapper
		var s = '<table cellspacing="0" cellpadding="0" class="netpp_news_container"';
		if (this.width) s+=' width="' + this.width + '" ';
		if (this.height) s+=' height="' + this.height + '"';
		s += '>';

		//title
		s += '<tr>'
			+ '<td class="netpp_title" align="center" colspan="10" style="height:1%;padding:5px 3px;background-color:' + this.w2_box_bgcolor +'">'
			+ '<h3 style="margin:0;padding:5px;text-align:center;line-height:1em;font-size:13px;font-family:Arial,Helvetica,sans-serif;font-size:'+ this.w2_h_fontsize + ';color:'+ this.w2_h_color + ';background-color:'+ this.w2_h_bgcolor+'">'
			+ (this.title.length>0?this.title:'Sprawd\u017A ile mo\u017Cesz <strong style="line-height:1em;font-size:20px;display:block;">zaoszcz\u0119dzi\u0107</strong> <span style="font-weight:normal;line-height:1em;font-size:11px;display:block;"> kupuj\u0105c cyfrowe wydania</span>')
			+ '</h3>'
			+ '</td>'
			+ '</tr>';

		//logo
		//T#13950, zmiana dla WP2
		if (this.partnerId != PPTools.WP2_ID){
			s += '<tr><td class="netpp_logo" align="center" colspan="10" style="height:1%;text-align:center;padding:1px 3px 5px 3px;background-color:' + this.w2_box_bgcolor +'">';
			if (this.direction == 1 && this.msServerName!=''){
				s += '<a href="http://'+this.msServerName+'/siec_nextranet.xml"><img style="border:none;display:block;margin:0 auto;" src="'+(this.imgServerName?this.imgServerName:this.serverName)+'/images/nlayout/pp_box_logo_nextranet.png" alt="NEXTRANET.PL"></a>';
			} else {
				s += '<a href="http://www.nexto.pl?pid=' + this.partnerId + '"><img style="border:none;display:block;margin:0 auto;" src="'+(this.imgServerName?this.imgServerName:this.serverName)+'/images/nlayout/pp_box_logo_nexto.png" alt="NEXTO.PL"></a>';
			}
			s += '</td></tr>';
		}
		
		//products
		s += '<tr>'
			+ '<td style="padding:3px;background-color:' + this.w2_box_bgcolor +'">';

		s += '<table cellspacing="0" cellpadding="0" class="netpp_news" style="table-layout:fixed;height:100%;width:100%;border:1px solid red;"';
		s += '>';

//		if (this.productsCount > 0) count = Math.min(count,this.productsCount);

		this.getHTML_news_cell = PPTools.getHTML_news_cell;
		this.getHTML_news_cell_price = PPTools.getHTML_news_cell_price;
		this.getHTML_news_cell_notes = PPTools.getHTML_news_cell_notes;
		this.getHTML_news_cell_mp3player = PPTools.getHTML_news_cell_mp3player;
		this.getHTML_news_cell_buynow = PPTools.getHTML_news_cell_buynow;

		if (this.layout==1) s+="<tr>";
		var beforeTD = (this.layout==1)?"":"<tr>";
		var afterTD = (this.layout==1)?"":"</tr>";

		if (this.layout == 1)
		{
			if (this.issueLayout!=2){
				for (var i = 0; i < count; i++)
					s += this.getHTML_news_cell(i,3,count);
				s+="</tr><tr>";
			}

			if (this.issueLayout!=3){
				for (var i = 0; i < count; i++)
					s += this.getHTML_news_cell(i,2,count);
				s+="</tr><tr>";
			}

			for (var i = 0; i < count; i++)
				s += this.getHTML_news_cell_price(i,count);
			s+="</tr><tr>";

			for (var i = 0; i < count; i++)
				s += this.getHTML_news_cell_notes(i,count);
			s+="</tr><tr>";

			for (var i = 0; i < count; i++)
				s += this.getHTML_news_cell_mp3player(i,count);
			s+="</tr><tr>";

			for (var i = 0; i < count; i++)
				s += this.getHTML_news_cell_buynow(i,count);
		}
		else
		{
			for (var i = 0; i < count; i++)
				s += ((this.issueLayout!=2)?beforeTD + this.getHTML_news_cell(i,3) + afterTD:'')
					+ ((this.issueLayout!=3)?beforeTD + this.getHTML_news_cell(i,2) + afterTD:'')
					+ beforeTD
						+ this.getHTML_news_cell_price(i)
						+ this.getHTML_news_cell_notes(i)
						+ this.getHTML_news_cell_mp3player(i)
					+ afterTD
					+ beforeTD + this.getHTML_news_cell_buynow(i) + afterTD;
		}

		if (this.layout == 1) s+='</tr>';
		s+='</table>';

		//wrapper
		s+='</td></tr></table>';
		return s;
	},

	getHTML_news_cell: function(index,what,count)
	{
		var p = this.products[index];
		var s ="";

		s+='<td class="netpp_product_name" align="center" valign="middle" style="padding:3px"';
		if (this.layout==1) s+=' width="' + Math.floor(100 / count) + '%"';
		s+='>';

		s+= '<a href="' + this.serverName +
				'/rf/pr?p=' + p.id
				+ '&widget=' + this.widgetType
				+ '&pid=' + this.partnerId
				+ (this.direction==1?'&direction=' + this.direction:'')
				+ '" target="_blank"'
				+ ' style="color:'+ this.w2_box_color + ';text-decoration:none;display:block;margin:0 auto;font-weight:bold;"'
				+ '>';

		if (what==3){
			s+='<span style="' + this.title_style + '">' + p.title + '</span>';
		} else if (p.cover)
		{
			s+='<img style="' + this.img_style + '" src="'
			+ (this.imgServerName?this.imgServerName:this.serverName)
			+ '/converter?t='+this.image_size+'&img='+escape(p.cover)+'" border="0"></a>';
		}
		else
		{
			/* old method */
			s+='<img style="' + this.img_style + '" src="' + this.serverName + '/oimg?p='+p.id+'&t='+this.image_size+'" border="0"></a>';
		}

		s+="</a>";

		s+="</td>";
		return s;
	},

	getHTML_news_cell_price: function(index,count)
	{
		var p = this.products[index];
		var s ="";

		s+='<td class="netpp_product_info" align="center" valign="top" style="color:'+ this.w2_box_color + ';"';
		if (this.layout==1) s+=' width="' + Math.floor(100 / count) + '%"';
		s+='>';

		s+="<div>";
		s+='Cena: ';
		if (p.price_print != '' && parseFloat(p.price_print) > parseFloat(p.price))
			s+='<strike>' + p.price_print.replace(".",",") + '</strike> ';
		s+='<strong style="color:#E34A20;"> ' + p.price.replace(".",",") + '&nbsp;z\u0142</strong>';
		s+="</div>";
		if (this.layout == 1) s+="</td>";
		return s;
	},

	getHTML_news_cell_notes: function(index,count)
	{
		var p = this.products[index];
		var s ="";

		if (this.layout == 1) {
			s+='<td class="netpp_product_info" align="center" valign="top" style="color:'+ this.w2_box_color + ';"';
			if (this.layout==1) s+=' width="' + Math.floor(100 / count) + '%"';
			s+='>';
		}

		//T#13950, zmiana dla WP2
		//console.debug(settings.partnerId);
		if (this.partnerId != PPTools.WP2_ID){
			var note = parseInt(p.review_note)
			if (note > 0){
				//ograniczamy zakres: 1-5
				if (note>5) note=5;
				s+="<div>";
				s+='Ocena klient\u00F3w: ';
				s+='<img src="'+(this.imgServerName?this.imgServerName:this.serverName)+'/images/nlayout/pp_box_note_' + note + '.png" alt="' + note + '">';
				s+="</div>";
			}
		}

		if (this.layout == 1) s+="</td>";
		return s;
	},

	getHTML_news_cell_mp3player: function(index,count)
	{
		var p = this.products[index];
		var count = this.products.length;
		if (this.productsCount > 0) count = Math.min(count,this.productsCount);

		var flashWidth = (this.width < count*(this.imgWidth + 10))?'90%':'140';

		var s ="";

		if (this.layout == 1) {
			s+='<td class="netpp_product_info" align="center" valign="top" style="color:'+ this.w2_box_color + ';"';
			if (this.layout==1) s+=' width="' + Math.floor(100 / count) + '%"';
			s+='>';
		}

		var mp3ExtensionPattern = new RegExp(".+\.mp3$", "i");
		if (p.sample && mp3ExtensionPattern.test(p.sample)){
			var swfColor = '0x' + this.w2_plr_color.substr(1,6);
			s+="<div>";
			s+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + flashWidth + '" height="20" id="nexto_mp3player" align="middle">'
				+'	<param name="allowScriptAccess" value="sameDomain">'
				+'	<param name="allowFullScreen" value="false">'
				+'	<param name="FlashVars" value="themeColor='+swfColor+'&trackPath=' + (this.serverName?this.serverName:this.imgServerName) + p.sample + '&isStream=true">'
				+'	<param name="movie" value="'+(this.imgServerName?this.imgServerName:this.serverName)+'/swf/nexto_mp3player.swf">'
				+'	<param name="quality" value="high">'
				+'	<param name="wmode" value="transparent">'
				+'	<param name="bgcolor" value="#ffffff">'
				+'	<embed src="'+(this.imgServerName?this.imgServerName:this.serverName)+'/swf/nexto_mp3player.swf" quality="high" bgcolor="#ffffff" wmode="transparent" width="' + flashWidth + '" height="20" name="nexto_mp3player" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="themeColor='+swfColor+'&trackPath=' + (this.serverName?this.serverName:this.imgServerName) + p.sample + '&isStream=true"></embed>'
				+'</object>';
			s+="</div>";
		}
		s+="</td>";
		return s;
	},

	getHTML_news_cell_buynow: function(index,count)
	{
		var p = this.products[index];
		var s ="";

		s+='<td class="netpp_product_buynow" align="center" valign="middle"';
		if (this.layout==1) s+=' width="' + Math.floor(100 / count) + '%"';
		s+=' style="padding:2px;"';
		s+='>';

		s+= '<a href="' + this.serverName +
				'/rf/pr?p=' + p.id
				+ '&widget=' + this.widgetType
				+ '&pid=' + this.partnerId
				+ (this.direction==1?'&direction=' + this.direction:'')
				+ '" target="_blank"'
				+ ' style="display:block;padding:5px;text-decoration:none;font-size:11px;font-weight:bold;font-family:Arial,Helvetica,sans-serif;color:'+ this.w2_h_color + ';background-color:' + this.w2_h_bgcolor + ';"'
				+ '"'
				+ '>';

		s+='Zobacz wi\u0119cej';

		s+="</a>";
		s+="</td>";
		return s;
	},

	// ---------------------------------------------------------------------------------
	// SEARCH
	// ---------------------------------------------------------------------------------

	getHTML_search: function()
	{
		var s ="";
		if (this.width) s+="width:" + this.width +";";
		if (this.height) s+="; height:" + this.height+";";
		this.box_style = (this.box_style?s+this.box_style:s);

		var html = '<div class="netpp_search" '
			+ PPTools.getStyleAttr(this.box_style) + ">";
		if (this.title) html+='<h3' +  PPTools.getStyleAttr(this.hdr_style) + '>' + this.title + '</h3>';
		html +=
			'<p' +  PPTools.getStyleAttr(this.title_style) + '>' + this.prompt + '</p>'
			+ '<form action="' + this.serverName + '/rf/pr" method="post" target="_blank" ';
		if (this.layout == 0) html += 'style="text-align: center;"';
		html += 'onSubmit="if (this.sc.value == null || this.sc.value == \'\') { alert(\'Prosze wpisa\u0107 szukan\u0105 fraz\u0119.\'); this.sc.focus(); return false; } return true;"';
		html += '>';
		html += '<input type="hidden" name="widget" value="search">';
		html += '<input type="hidden" name="direction" value="' + this.direction + '">';
		html += '<input type="hidden" name="pid" value="' + this.partnerId + '">';
		html += '<input type="text" name="sc" style="' + this.style_input + '">';
		html += '<input type="submit" class="netpress_searchform_submit" value="Wyszukaj">';
		html += '<input type="hidden" name="encoding" value="\u017b">';
		html += '</form></div>';
		return html;
	},

	// ---------------------------------------------------------------------------------
	// PROMO
	// ---------------------------------------------------------------------------------

	getHTML_promo: function()
	{
		if (this.filter==1)
		{
			// selected promotions
			var a = new Array();
			this.promotion = ','+this.promotion+',';
			for (var i=0; i<this.data.length; i++)
			{
				if (this.promotion.indexOf(','+this.data[i].id+',')<0) continue;
				a[a.length]=this.data[i];
			}
			this.data=a;
		}

		if (this.data.length==0) return "";

		// math random -> po kolei T#5137
		if (this.data.length>1)
		{
			//this.data = this.data[Math.floor(Math.random()*this.data.length)];
			var v = PPTools.getCookie('tools.js');
			var o = null;
			for (var i=0; i<this.data.length; i++)
				if (this.data[i].id==v)
				{
					if (i<this.data.length-1) o=this.data[i+1];
					break;
				}
			if (o==null) o = this.data[0];
			this.data = o;
			v = this.data.id;
			PPTools.setCookie('tools.js',v,'/');
		}
		else if (this.data.length==1) this.data = this.data[0];

		var link = this.serverName + "/rf/pr"
			+ "?pid=" + this.partnerId
			+ "&widget=promo"
			+ (this.direction==1?'&direction=' + this.direction:'')
			+ "&u=" + escape(this.data.link);

		// html
		if (this.data.mega_banner_type==1 && this.type==5)
			return this.data.mega_banner_html.replace(/#link#/g,escape(link));

		var s ="<a href='"+link+"'>";

		s += "<img src='" + this.serverName;
		switch (this.type * 1)
		{
			case 0:
				s += this.data.full_banner + "' width='468' height='60' ";
				break;
			case 1:
				s += this.data.half_banner + "' width='234' height='60' ";
				break;
			case 2:
				s += this.data.button_1 + "' width='120' height='90' ";
				break;
			case 3:
				s += this.data.button_2 + "' width='120' height='60' ";
				break;
			case 4:
				s += this.data.vertical_banner + "' width='120' height='240' ";
				break;
			case 5:
				s += this.data.mega_banner + "' width='750' height='100' ";
				break;
			case 6:
				s += this.data.banner125 + "' width='125' height='125' ";
				break;
		}
		s += " border='0' alt='" + this.data.name + "'/>";
		s += "</a>"
		return s;
	},

	// -----------------------------------------------------------
	// Cookies
	// -----------------------------------------------------------

	getCookie: function(name)
	{
		var arg	= name + '=';
		var alen = arg.length;
		var	clen =document.cookie.length;
		var i = 0;
		while (i<clen)
		{
			var j = i + alen;
			if (document.cookie.substring(i,j)==arg) return PPTools.getCookieVal(j);
			i = document.cookie.indexOf(" ",i) + 1;
			if (i==0) break;
		};
		return null;
	},

	getCookieVal: function(offset)
	{
		var endstr = document.cookie.indexOf(";", offset);
		if (endstr==-1) endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset,endstr));
	},

	setCookie: function(name,value,path,expires)
	{
		var str = name + "=" + value;
		if (expires) str += " ;expires="+expires;
        if (path) str += " ;path="+path;
		document.cookie = str;
	}
}

if (typeof(settings)=="object")
{
	// stara metoda - kod partnera - niektórzy partnerzy tak mają
	if (typeof(settings.partnerId)=="undefined") settings.partnerId = settings.partnerCode;
	if (typeof(settings.title)=="undefined") settings.title = "";

	eval("settings.getHTML = PPTools.getHTML_" + settings.widgetType);
	settings.write = PPTools.write;
	document.write("<script type='text/javascript' src='"+PPTools.getJSUrl(settings)+"'></script>");
	//document.write("<script type='text/javascript'>if (settings && settings.write) settings.write();</script>");
}
