//Base
TopListBase=function(options){this.init(options);};
TopListBase.prototype={options:null,
tableTemplate:'\<table class="table-toplist-fallback{0}">\n\<thead>{1}</thead>\n\<tbody>{2}</tbody>\n\</table>',
headingTemplate:'<tr><th colspan="2" class="poker-room-ranking no-deal-toplist-header"><span>{0}</span></th>{1}{2}{3}{4}</tr>',
headingTemplateBonus:'<th class="table-heading-toplist-fallback-bonus no-deal-toplist-header"><span>{0}</span></th>',
headingTemplateScore:'<th class="score no-deal-toplist-header"><span>{0}</span></th>',
headingTemplateReview:'<th class="review no-deal-toplist-header"><span>{0}</span></th>',
headingTemplateCTA:'<th><span>{0}</span></th>',
rowTemplate:'<tr><th scope="row">{0}.</th><td class="td-toplist-fallback-ranking"><a class="icon-logo-square icon-logo-square-{1}" href="{2}" rel="nofollow">{3}{4}</a></td>{5}{6}{7}{8}</tr>',
usFlagTemplate:'<img class="country-flag tooltip" src="{0}" alt="{1}" title="{2}"/>',
bonusTemplate:'<td class="table-cell-toplist-fallback-bonus"><a href="{0}" rel="nofollow">{1}{2}</a><span class="icon icon-tooltip" title="{3}">?</span></td>',
scoreTemplate:'<td class="score">{0}</td>',
reviewTemplate:'<td class="review"><a href="{0}">{1}</a></td>',
ctaTemplate:'<td><a href="{0}" rel="nofollow" class="button button-cta{1}"><span>{2}</span></a></td>',
init:function(options){this.options = options;},
getOptionsForCurrentCountry:function(){
var options=this.options;this.options.tableExtraCss=this.options['default'].tableExtraCss;this.options.design=this.options['default'].design;
options.rows=options['default'].rows;options.translations=options['default'].translations;options.showBonus=options['default'].showBonus;options.showCTA=options['default'].showCTA;options.showReview=options['default'].showReview;options.showScore=options['default'].showScore;
if(options[options.currentCountry]!==undefined){
if(options[options.currentCountry].rows !== undefined){options.rows = options[ options.currentCountry ].rows;}
if(options[options.currentCountry].translations!==undefined){options.translations = options[ options.currentCountry ].translations;}
}return options;},
render:function(){
var options=this.getOptionsForCurrentCountry();
options.container.innerHTML='';
var rowHTML='',rows = options.rows, len = rows.length;


for(i=0;i<len;i++){
var row=rows[i],usFlagTemplate='';
if(row.Review.CurrentLocale==='en_US'){if(row.Partner.AllowedInUSA=='1')usFlagTemplate=this.sprintf(this.usFlagTemplate,pl2.cdnPath+'images/flag-us.jpg','USA',this.options.translations['Tooltip.TOOLTIP_ACCEPTS_US_PLAYERS']);}
else{if(row.Review.IsLocal=='1')usFlagTemplate=this.sprintf(this.usFlagTemplate,this.options.translations['ToplistFlag.PATH'],this.options.translations['ToplistFlag.ALT'],this.options.translations['ToplistFlag.TOOLTIP_ACCEPTS_US_PLAYERS']);}
rowHTML+=this.sprintf(
this.rowTemplate,i + 1,row.Partner.CSSName,row.Review.OutClick,row.Partner.Name,usFlagTemplate,
options.showBonus?
this.sprintf(
this.bonusTemplate,row.Review.OutClick,row.Partner.Bonuses.MaxBonus.Symbol,row.Partner.Bonuses.MaxBonus.AmountFormated,
row.Partner.Bonuses.BonusMatchPercent?
	row.translations['Tooltip.TOOLTIP_DEPOSIT_PERCENT']:row.translations['Tooltip.TOOLTIP_DEPOSIT_FLAT']
):'',
options.showScore?this.sprintf(this.scoreTemplate,row.Partner.Rating):'',
options.showReview?this.sprintf(this.reviewTemplate,row.Review.URLSegment,options.translations['TopListBase.REVIEW']):'',
options.showCTA?this.sprintf(this.ctaTemplate,row.Review.OutClick,options.design,options.translations['TopListBase.PLAY_NOW']):'');
}



var headingName;
if(typeof PL!='undefined'){headingName=PL.Partner.PartnerType=="Casino"?'TopListBase.CASINO_SITE_RANKING':'TopListBase.POKER_SITE_RANKING';}
else{headingName=options.rows[0].Partner.PartnerType=="Casino"?'TopListBase.CASINO_SITE_RANKING':'TopListBase.POKER_SITE_RANKING';}
var html = this.sprintf(
this.tableTemplate,this.options.tableExtraCss,
this.sprintf(
this.headingTemplate,options.translations[headingName],
options.showBonus?this.sprintf(this.headingTemplateBonus,options.translations['TopListBase.BONUS']):'',
options.showScore?this.sprintf(this.headingTemplateScore,options.translations['TopListBase.SCORE']):'',
options.showReview?this.sprintf(this.headingTemplateReview,options.translations['TopListBase.REVIEW']):'',
options.showCTA?this.sprintf(this.headingTemplateCTA,options.translations['TopListBase.PLAY_NOW']):''
),
rowHTML
);
options.container.innerHTML = html;
},
sprintf:function(){ // @todo This needs to be placed in a separate util file
var num = arguments.length,oStr = arguments[0],i,pattern,re;
for(i=1;i<num;i++){pattern = "\\{" + (i-1) + "\\}",re = new RegExp(pattern, "g");oStr = oStr.replace(re, arguments[i]);}
return oStr;
}
};

//Tabed
TopListTabed = function(options){
	this.init(options);
}

TopListTabed.prototype = {
	defaults : {
		maxDoubleSlots: 5,
		maxSlots: 10
	},
	options : null,
	template : '{0}<div class="line ui-tabs ui-tabs-alt ui-tabs-alt-toplist">{1}<div class="unit-last">{2}</div></div>',
	headingTemplate : '<div class="block-heading"><span class="text-pokersite">{0}</span><span class="text-bonus">{1}</span><span class="text-quickreview">{2}</span></div>',
	tabListTemplate : '<div class="unit tab-container"><ol class="ui-tabs-nav">{0}</ol><div id="best-online-poker-sites-extra-content" class="extra-content"></div></div>',
	tabTemplate : '<li class="{0}"><a href="{1}"><span class="text-number">{2}.</span><span class="icon-logo-square icon-logo-square-{3}">{4}</span><span class="text-bonus">{5}{6}</span><span class="bg"></span></a></li>',
	tabItemTemplate : '<div id="toplist-{0}" class="block-toplist-minireview">{1}{2}{3}{4}{5}</div>',
	ratingTemplate: '<div class="bd"><span class="{0}">{1}<span class="meter-container"><span class="meter" style="width:{2}%"></span></span><span class="numbers">{3}</span></span></div>',
	exclusiveOfferTemplate : '<div class="exclusive-offer"><a class="link-logo-square" href="{0}"><img src="{1}" alt="{2}"/></a>{3}{4}</div>',
	bonusListTemplate : '<dl class="list-bonus"><dt>{0}</dt>{1}{2}</dl>',
	bonusTemplatePercent : '<dd class="list-item-bonus-bonus-pokerlistings"><a href="{0}" rel="nofollow">{1}%</a> <span class="text-description">{2}</span> <a href="{3}" rel="nofollow">{4}{5}</a><span class="icon icon-tooltip" title="{6}">?</span></dd>',
	bonusTemplateFlat : '<dd class="list-item-bonus-bonus-pokerlistings"><a href="{0}" rel="nofollow">{1} {2}{3}</a><span class="icon icon-tooltip" title="{4}">?</span></dd>',
	standardBonusTemplatePercent : '<dd class="list-item-bonus-bonus-regular">{0} {1}% / {2}{3}</dd>',
	standardBonusTemplateFlat : '<dd class="list-item-bonus-bonus-regular">{0} {1} {2}{3}</dd>',
	usPlayersWelcome : '<img class="image-flag image-tooltip" src="'+pl2.cdnPath+'images/flag-us.jpg" alt="USA" title="{0}"/>',
	listAttributeTemplate : '<dl class="list-attribute"><dt>{0}</dt>{1}</dl>',
	listItemAttributeTemplate : '<dd class="list-item-help"><span class="icon icon-bullet"></span><a href="{0}">{1}</a><span class="icon icon-tooltip" title="{2}">?</span></dd>',
	readReviewTemplate : '<a href="{0}" class="link-review">{1}</a>',
	standardModule : '\
			<div class="{0}">\n\
				<div class="{1}">\n\
					<div class="t"><div class="tl"></div><div class="tr"></div></div>\n\
					{2}\n\
					<div class="bg"></div>\n\
					<div class="b"><div class="bl"></div><div class="br"></div></div>\n\
				</div>\n\
			</div>',
	actionsTemplate : '<div class="bd"><ul class="list-actions"><li><a href="{0}" rel="nofollow" class="button button-download-alt"><span>{1}</span></a></li><li><a href="{2}" rel="nofollow" class="link-visit">{3}</a></li></ul></div>',
	init : function(options) {
		this.options = options;
		this.options.maxDoubleSlots = options.maxDoubleSlots !== undefined ? options.maxDoubleSlots : this.defaults.maxDoubleSlots; //@Todo add generic object merge for defaults and options
		this.options.maxSlots = options.maxSlots !== undefined ? options.maxSlots : this.defaults.maxSlots; //@Todo add generic object merge for defaults and options
	},
	getOptionsForCurrentCountry : function(){
		var options = this.options;

		options.rows = options['default'].rows;
		options.translations = options['default'].translations;

		if( options[options.currentCountry] !== undefined){
			if( options[options.currentCountry].rows !== undefined ) {
				options.rows = options[ options.currentCountry ].rows;
			}
			if( options[options.currentCountry].translations !== undefined ) {
				options.translations = options[ options.currentCountry ].translations;
			}
		}


		return options;
	},
	render : function() {
		var options = this.getOptionsForCurrentCountry();

		options.container.innerHTML = ''; //@todo fix this for IE

		var tabHTML = '';

		var tabItemHTML = '';

		var rows = options.rows;


		var numberOfRows = rows.length < this.options.maxSlots ? rows.length : this.options.maxSlots;
		var numberOfSingleSlots = 2 * ( numberOfRows - this.options.maxDoubleSlots );
		var numberOfDoubleSlots = numberOfRows - numberOfSingleSlots;

		for(i = 0; i < rows.length; i++ ){
			var row = rows[ i ];

			var ratingWidth = row.Partner.Rating / 10 * 100;
			var ratingNumber = '' + (Math.floor(row.Partner.Rating)) + '/10';

			var tabClass = 'ui-tabs';
			if( i == 0 ) {
				tabClass += ' ui-tabs-selected'; //@todo is this always correct?
				tabClass += ' ui-tabs-first';
			}
			if( i == rows.length - 1 ) {
				tabClass += ' ui-tabs-last';
			}

			tabClass += i < numberOfDoubleSlots ? ' ui-tabs-alt-toplist-double' : ' ui-tabs-alt-toplist-single';

			tabHTML += this.sprintf( this.tabTemplate,
				tabClass,
				'#toplist-' + row.Partner.CSSName, //@todo fix this, will not work for multiple lists on same page
				i + 1,
				row.Partner.CSSName,
				row.Partner.Name,
				row.Partner.Bonuses.MaxBonus.Symbol,
				row.Partner.Bonuses.MaxBonus.AmountFormated
			);

			var exclusiveOffersHTML = '';
			if( row.Review.ExclusiveOffers.length ) {
				for( j = 0; j < row.Review.ExclusiveOffers.length && j < 2; j++ ){
					offer = row.Review.ExclusiveOffers[ j ];
					exclusiveOffersHTML += this.sprintf( this.listItemAttributeTemplate,
						row.Review.OutClick,
						offer.Title,
						offer.Text
					);
				}
			}

			tabItemHTML += this.sprintf( this.tabItemTemplate,
				row.Partner.CSSName,
				this.sprintf( this.standardModule,
					'mod mod-additional mod-contour-internal mod-rating',
					'inner',
					this.sprintf( this.ratingTemplate,
						'rating rating-compact rating-toplist-minireview',
						'',
						ratingWidth,
						ratingNumber
					)
				),
				this.sprintf( this.exclusiveOfferTemplate,
					row.Review.OutClick,
					row.Partner.Logo.URL,
					row.Partner.Name,
					this.sprintf( this.bonusListTemplate,
						options.translations[ 'TopListTabed.EXCLUSIVE_BONUS' ],
						row.Partner.Bonuses.BonusMatchPercent ?
							this.sprintf( this.bonusTemplatePercent,
								row.Review.OutClick,
								row.Partner.Bonuses.BonusMatchPercent,
								options.translations[ 'TopListTabed.UP_TO' ],
								row.Review.OutClick,
								row.Partner.Bonuses.MaxBonus.Symbol,
								row.Partner.Bonuses.MaxBonus.AmountFormated,
								row.translations[ 'Tooltip.TOOLTIP_DEPOSIT_PERCENT' ]
							) :
							this.sprintf( this.bonusTemplateFlat,
								row.Review.OutClick,
								options.translations[ 'TopListTabed.FLAT' ],
								row.Partner.Bonuses.MaxBonus.Symbol,
								row.Partner.Bonuses.MaxBonus.AmountFormated,
								row.translations[ 'Tooltip.TOOLTIP_DEPOSIT_FLAT' ]
							),
						row.Partner.Bonuses.StandardBonusMatchPercent ?
							this.sprintf( this.standardBonusTemplatePercent,
								options.translations[ 'TopListTabed.REGULAR' ],
								row.Partner.Bonuses.StandardBonusMatchPercent,
								row.Partner.Bonuses.StandardMaxBonus.Symbol,
								row.Partner.Bonuses.StandardMaxBonus.AmountFormated
							) :
							this.sprintf( this.standardBonusTemplateFlat,
								options.translations[ 'TopListTabed.REGULAR' ],
								options.translations[ 'TopListTabed.FLAT' ],
								row.Partner.Bonuses.MaxBonus.Symbol,
								row.Partner.Bonuses.MaxBonus.AmountFormated
							)
					),
					row.Partner.AllowedInUSA ?
						this.sprintf( this.usPlayersWelcome,
							options.translations[ 'Tooltip.TOOLTIP_ACCEPTS_US_PLAYERS' ]
						) : ''
				),
				row.Review.ExclusiveOffers.length ?
					this.sprintf( this.listAttributeTemplate,
						options.translations[ 'TopListTabed.SPECIAL_OFFER' ],
						exclusiveOffersHTML
					) : '',
				this.sprintf( this.readReviewTemplate,
					'/' + row.Review.URLSegment,
					options.translations[ 'TopListTabed.LINK_READ_REVIEW' ]
				),
				this.sprintf( this.standardModule,
					'mod mod-additional mod-contour-internal',
					'inner',
					this.sprintf( this.actionsTemplate,
						row.Review.OutClick,
						options.translations[ 'TopListTabed.BUTTON_PLAY_NOW' ],
						row.Review.OutClickDownload,
						row.translations[ 'TopListTabed.LINK_FREE_DOWNLOAD' ]
					)
				)
			);
		}

		var html = this.sprintf( this.template,
			this.sprintf( this.headingTemplate,
				options.translations[ 'TopListTabed.POKER_SITE' ],
				options.translations[ 'TopListTabed.BONUS' ],
				options.translations[ 'TopListTabed.QUICK_REVIEW' ]
			),
			this.sprintf(  this.tabListTemplate,
				tabHTML
			),
			tabItemHTML
		);

		options.container.innerHTML = html;
	},
	sprintf : function() { // @todo This needs to be placed in a separate util file
		var num = arguments.length;
		var oStr = arguments[0];
		for (var i = 1; i < num; i++) {
			var pattern = "\\{" + (i-1) + "\\}";
			var re = new RegExp(pattern, "g");
			oStr = oStr.replace(re, arguments[i]);
		}
		return oStr;
	}
}


/**
 * Displays which toplist layout to display
 */
TopListChooser = function(options) {
	this.init(options);
}

TopListChooser.prototype = {
    toplist: null,
    init: function(options) {
		var template = 'Base';

		if (options[options.currentCountry] !== undefined && options[options.currentCountry].template !== undefined) {
			template=options[options.currentCountry].template;
		}else if(options['default'] !== undefined && options['default'].template !== undefined) {
			template=options['default'].template;
		}

		switch(template) {
			case 'Tabed':
				this.toplist = new TopListTabed(options);
				break;
			case 'Base':
				this.toplist = new TopListBase(options);
				break;
			default: 
				this.toplist = new TopListBase( options );
		}
    },
    render: function() {
		this.toplist.render();
	}
};


