function strip_tags(input, allowed){
	allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
 return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
		return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
 });
}

function ContestTeaserRotator(name, params){

	this.params = {};
	var self = this;
	var name = name;
	var lang = getSiteLang();	
	var timer;
	var current_index = 0;
	var max_height = 0;
	var min_height = 1000000;
	var direction = 'up';
	var num_blocks = 0;
	var num_contests = 0;
	var item_height = 0;
	
	this.initialize = function(){
		
		if (typeof(params) == 'object'){ this.params = params; }
		if (this.params.num_per_page === undefined){ this.params.num_per_page = 4; }
		if (this.params.teaser_length === undefined){ this.params.teaser_length = 120; }
		if (this.params.delay === undefined){ this.params.delay = 4000; } 
		if (this.params.box_height === undefined){ this.params.box_height = 100; } 
		if (this.params.box_width === undefined){ this.params.box_width = 300; } 
		if (this.params.mode === undefined){ this.params.mode = 'continueous'; } 
		if (this.params.move_by_one === undefined){ this.params.move_by_one = true; } 
		var css_text = '<style> \
		.affinityClubs.contestTeaser, .affinityClubs.contestTeaser .clearfix {clear: both;} \
.affinityClubs.contestTeaser .errorMode {display: block;} \
#ct_rotator_ul, #ct_rotator_ul > li { \
	margin: 0; padding: 0; \
	list-style-type: none; \
	} \
#ct_rotator_ul {width: 100%;} \
#ct_rotator_ul > li { \
	padding: 6px 6px 0; \
	*height: 1%; \
	} \
#ct_rotator_ul > li a.thumb { \
	display: block; max-width: 100%; overflow: hidden; float: left; \
	} \
	#ct_rotator_ul > li a.thumb img { \
		height: auto; padding: 0 8px 6px 0; max-width: 92%; \
		} \
		#ct_rotator_ul > li a.thumb img[src="/EI/MS/S.gif"] {display: none;} \
#ct_rotator_ul > li h4 {margin: 0 0 .2em; padding: 0;} \
#ct_rotator_ul > li .blurb {margin: 0; padding: 0;} \
#ct_rotator_ul > li .jumperlink {display: block; padding-bottom: 6px;} \
</style>';
		
		document.write(css_text + '<div ID="contest_teaser_rotator"></div>');

		this.loadXML();
	};
	
	this.loadXML = function(){		
		
		var date = new Date(); 
		$.ajax({
			type:"GET",
			url:"/_Shared/content/public/contest.ashx?cb=" + date.getTime(),
			dataType:"text",
			success:function(xml_text){
				self.draw(xml_text);
			}
		});				
	};
	
	this.draw = function(xml_text){
		
		var xml = $.fromXMLString(xml_text);
		var x = 0;
		var y = 0;
		var i = 0;
		var contest_id = '';
		var contest_image = '';
		var contest_name = '';
		var contest_text = '';
		var html_text = '<h3>contest teaser</h3>';	
		var container_width = 0;
		
		html_text = '<div id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_ContentPanel" class="control affinityClubs contestTeaser">';
					
		//html_text += '<div ID="ct_rotator_container" style="position:relative; width:100%; overflow:hidden; height:' + ((self.params.box_height + 18) * self.params.num_per_page) + 'px"><div ID="ct_rotator_content" style="position:absolute; height:2500px">';
		html_text += '<div ID="ct_rotator_container" style="position:relative; width:100%; overflow:hidden; "><div ID="ct_rotator_content" style="position:absolute; height:2500px">';
		y++;
	
		$(xml).find('Contest').each(function(){ 							 
																				 
			x++;
			if (x == 1 || ((x-1) % self.params.num_per_page) == 0){ 
				html_text += '<div id="ct_rotator_' + y + '"><div id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_NormalMode"><ul class="hasThumbs" id="ct_rotator_ul">'; 
				y++;
			}
			
			contest_id = $(this).attr('ContestID');
			contest_image = $(this).attr('ContestImageThumbnail');
			contest_name = $(this).find('ContestText').attr('ContestHeader');
			contest_text = $(this).find('ContestText').text();
			contest_teaser = strip_tags(contest_text);
			contest_teaser = html_entity_decode(contest_teaser);
			contest_teaser = contest_teaser.substr(0, self.params.teaser_length) + '...';
			
			html_text += '\
				<li class="' + (x%2 == 0?'tralt':'trdef') + '" style="height:' + self.params.box_height + 'px; overflow:hidden"> \
						<a class="thumb" href="/Contests/Register.aspx?ContestID=' + contest_id + '"> \
								<div style="height:100px; width:100px"><img id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_rptContestList_ct' + (100 + x) + '_img" \ src="' + contest_image + '" style="border-width:0px;" /></div> \
						</a> \
						<h4> \
								<a href="/Contests/Register.aspx?ContestID=' + contest_id + '"> \
										<span id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_rptContestList_ct' + (100 + x) + '_lblContestHeadline">' + contest_name + '</span> \
								</a> \
						</h4> \
						<p class="blurb"> \
								<span id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_rptContestList_ct' + (100 + x) + '_lblContestDescription" style="line-height:1.1">' + contest_teaser + ' \
						<a onclick="javascript:window.location.href = \'/Contests/Register.aspx?ContestID=' + contest_id + '\';return false;" id="ctl00_MainMidLeft_GenericControl5_uc4aaad30241ac4a94a9082bcddc521a04_rptContestList_ct' + (100 + x) + '_lnkRedirect" class="jumperlink action" href="javascript:__doPostBack(\'ctl00$MainMidLeft$GenericControl5$uc4aaad30241ac4a94a9082bcddc521a04$rptContestList$ctl01$lnkRedirect\',\'\')" style="display:inline">' + (lang=='fr'?'plus':'more') + '...</a></span></p> \
						<div class="clearfix"></div> \
				</li>';
				
				if ((x % self.params.num_per_page) == 0){ 
					html_text += '</ul></div></div>'; 
				}
				
		});
		num_contests = x;
		
		if ((x % self.params.num_per_page) != 0){ 
			html_text += '</ul></div></div>'; 
		}
	
		html_text += '</div></div></div></div>';
		if (x > 0){
			$('#contest_teaser_rotator').html(html_text);
		}
		
		if (this.params.mode == 'continueous'){
			$('#ct_rotator_content').append('<div id="ct_rotator_1">'+$('#ct_rotator_1').html()+'</div>');
		}
		
		container_width = $('#ct_rotator_container').width();
		$('#ct_rotator_content').width(container_width);
		num_blocks = y;
		for (i = 1; i < y; i++){
			if ($('#ct_rotator_' + i).height() > max_height){
				max_height = $('#ct_rotator_' + i).height();
			}
			if ($('#ct_rotator_' + i).height() < min_height){
				min_height = $('#ct_rotator_' + i).height();
			}
			$('#ct_rotator_'+i).width(container_width);
		}
		$('#ct_rotator_'+(i+1)).width(container_width);
		$('#ct_rotator_container').height(max_height);
		
		item_height = max_height / this.params.num_per_page;
		
		if (num_contests > self.params.num_per_page){
			self.start();		
		}
	};
	
	this.rotate = function(){
		var offset;
		if (this.params.mode == 'continueous'){
			if (this.params.move_by_one){
				current_index++;
				if (current_index == num_contests){
					offset = item_height;
					$('#ct_rotator_content').animate({top:'-='+offset}, 1000, function(){ self.reset(); } );
				}
				else{
					offset = item_height;
					$('#ct_rotator_content').animate({top:'-='+offset}, 1000, function(){ self.start(); } );
				}
			}
			else{
				current_index++;
				if (current_index == num_blocks -1){
					offset = min_height;
					$('#ct_rotator_content').animate({top:'-='+offset}, 1000, function(){ self.reset(); } );
				}
				else{
					offset = max_height;
					$('#ct_rotator_content').animate({top:'-='+offset}, 1000, function(){ self.start(); } );
				}
			}
		}
		else{
			if (direction == 'up'){ current_index++; }else{ current_index--; } 
			if (current_index == (num_blocks-1) && direction == 'up'){ direction = 'down'; }
			else if(current_index == 1 && direction == 'down'){ direction = 'up'; }
			if (direction == 'up'){
				$('#ct_rotator_content').animate({top:'-='+max_height}, 1000, function(){ self.start(); } );
			}
			else{
				$('#ct_rotator_content').animate({top:'+='+max_height}, 1000, function(){ self.start(); } );
			}
		}
	};
	
	this.reset = function(){
		current_index = 0;
		$('#ct_rotator_content').css('top','0px');
		self.start();
	}
	
	this.stop = function()
	{
		if (timer){
			clearTimeout(timer);
		}
	};	

	this.start = function()
	{
		timer = setTimeout(name + '.rotate()', self.params.delay);
	};	
	
	this.initialize();

}
