var tbFrontend = {
	youTubeLink: 'http://www.youtube.com/v/@&hl=pl_PL&fs=1&rel=0&color1=0x2D3463&color2=0xA77D8B&border=0',
	sideVideos: 3,
	currentVideo: null,
	totalVideos: null,
	
	cb: null,
	
	startup: function() {
		var main = $('#main');
		switch (main.attr('class')) {
			case 'oakcji':
			case 'konkurs2':
				tbFrontend.totalVideos = videoList.length;
				tbFrontend.currentVideo = 0;
				
				if (window.location.hash != '') {
					var id = window.location.hash.substr(1);
					var ind = $.inArray(id, videoList);
					if (ind >= 0) {
						tbFrontend.currentVideo = ind;
					}
				}
					
				tbFrontend.playVideo();
				
				$('A#video_prev').click(tbFrontend.prevVideo);
				$('A#video_next').click(tbFrontend.nextVideo);
				
				if (main.attr('class') == 'konkurs2') {
					$('A#vote_form').click(tbFrontend.voteForm);
				}
				break;
			case 'konkurs':
			case 'zapiszsie':
				tbFrontend.cb = $('#main FIELDSET DL DT.agreement INPUT');
				tbFrontend.cb.hide();
				$('#main FIELDSET DL DT.agreement').append('<span></span>');
				
				tbFrontend.cb.change(function() {
					$('#main FIELDSET DL DT.agreement SPAN').toggleClass('on', ($(this).attr('checked') != ''));
				});
				tbFrontend.cb.change();
				
				$('#main FIELDSET DL DT.agreement SPAN').click(function() {
					tbFrontend.cb.click();
					tbFrontend.cb.change();
				});

				$('#main.konkurs FIELDSET UL LI INPUT, #main.zapiszsie FIELDSET UL LI INPUT, #video #voteForm INPUT.submit').val('');
				
				break;
		}
		
		if ($('#main FORM P.error')) {
			setTimeout(tbFrontend.fadeError, 3000);
		}
		
		$('A[rel=external]').attr('target', '_blank');
		
		$('A.mail').each(function() {
			$(this).text($(this).text().replace('(małpa)', '@')).attr('href', 'mailto:' + $(this).text());			
		});
	},
	
	fadeError: function() {
		$('#main FORM P.error').fadeOut(1000);
	},
	
	voteForm: function() {
		if ($('#voteForm').css('display') == 'none') {
			$('A#video_prev').unbind('click', tbFrontend.prevVideo);
			$('A#video_next').unbind('click', tbFrontend.nextVideo);
			$('#voteForm').fadeIn(500);
			$('#video UL').hide();
		} else {
			$('A#video_prev').bind('click', tbFrontend.prevVideo);
			$('A#video_next').bind('click', tbFrontend.nextVideo);
			$('#voteForm').fadeOut(300);
			$('#video UL').show();
		}
		return false;
	},
	
	disableVote: function() {
		$('A#video_prev').unbind('click', tbFrontend.prevVideo);
		$('A#video_next').unbind('click', tbFrontend.nextVideo);
		$('#video UL').hide();
	},

	prevVideo: function() {
		tbFrontend.currentVideo--;
		if (tbFrontend.currentVideo < 0) {
			tbFrontend.currentVideo = tbFrontend.totalVideos - 1;
		}
		
		tbFrontend.playVideo();
		return false;
	},
	
	nextVideo: function() {
		tbFrontend.currentVideo++;
		if (tbFrontend.currentVideo >= tbFrontend.totalVideos) {
			tbFrontend.currentVideo = 0;
		}
		
		tbFrontend.playVideo();
		return false;
	},
	
	selectVideo: function() {
		var num = $(this).text();
		tbFrontend.currentVideo = num - 1;
		
		tbFrontend.playVideo();
		return false;
	},

	playVideo: function() {
		var id = videoList[tbFrontend.currentVideo];
		var link =  tbFrontend.youTubeLink.replace('@', id);
		
		$('#player').html('').flash({
			swf: link,
			params: {
				allowFullScreen: 'true',
				allowscriptaccess: 'always'
			},
			height: 287,
			width: 362
		});
		
		window.location.hash = '#' + id;
		
		if ($('#voteForm')) {
			$('#voteForm INPUT[name=videoId]').val(id);
		}
		
		//fill list
		var ul = $('#video UL');
		ul.html('');
		var from = tbFrontend.currentVideo - tbFrontend.sideVideos;
		var to = tbFrontend.currentVideo + tbFrontend.sideVideos;
		if (from < 0) {
			to -= from;
			from = 0;
		}
		
		if (to >= tbFrontend.totalVideos) {
			from -= (to - tbFrontend.totalVideos + 1);
			to = tbFrontend.totalVideos - 1;
		}
		
		if (from < 0) {
			from = 0;
		}
		
		if (from > 0) {
			ul.append('<li>...</li>');
		}

		for(var i = from; i <= to; i++) {
			if (i == tbFrontend.currentVideo) {
				ul.append('<li class="selected">' + (i + 1) + '</li>');
			} else {
				ul.append('<li><a href="#">' + (i + 1) + '</a></li>');
				ul.find('A').last().click(tbFrontend.selectVideo);
			}
		}

		if (to < (tbFrontend.totalVideos - 1)) {
			ul.append('<li>...</li>');
		}
		
	}
};

$(tbFrontend.startup);
