// Thickbox defulat width and height
TB_DEFAULT_W = 600;
TB_DEFAULT_H = 600;

function ajax_submitPollVote () {
	var key = this.id.split("_")[2];
	var formId = 'poll_' + key;
	var answer = $('#' + formId).find("input[@type=radio]:checked");
	
	
	if (answer.size() == 1) {		
		var pollId = $('#poll_' + key + '_id').attr('value');
		var dataString = 'task=castVote&pollId=' + pollId + '&answerId='+ answer.attr('id').split('_')[1];
		$.ajax({
			type: "POST",
			url: $('#' + formId).attr('action'),
			data: dataString,
			success: function(data,status) {
				$('#poll_container_' + key).before(data).remove();
				
				$('.indicator img').each(function() {
					var width = $(this).attr('alt');
					$(this).animate({ 
				        width: width
				      }, 1000 );
				});				
			}
		});		
	}
	
	return false;
}

function ajax_subscribeNewsletter () {
	var emailMinLength = 6;
	var email = $('#newsletter_email').attr('value');
	
	if (email.length >= emailMinLength && email.indexOf('@') > 0 && email.indexOf('.') > 0) {
		var dataString = 'task=subscribeNewsletter&email='+email;
		
		$.ajax({
			type: "POST",
			url: $('#news_subscription').attr('action'),
			data: dataString,
			success: function(data,status) {
				$('#newsletter_form_container').html(data);
				
			},
			error: function(XMLHttpRequest, status, errorThrown) {
				$('#newsletter_error').html(invalid_email).removeClass("hidden");
			}
		});
	} else {
		$('#newsletter_error').html(invalid_email).removeClass("hidden");
	}
	
	return false;
}

function showDetails (id) {
	$('.obstacle_content').addClass('hidden');
	$('.obstacle_more').removeClass('hidden');
	$('.obstacle_more').addClass('more');
	$('#link_'+id).addClass('hidden');
	$('#link_'+id).removeClass('more');
	$('#obstacle_'+id).removeClass('hidden');	
	
	return false;
}

function switchTopPromotion (id, autoRotate) {
	
	if (autoRotate === true) {
		id = $('div.slide_ctrl li.active').attr('id').split('_');
		id = id[2];
		id++;
		if (id > maxJs) {
			id = 1;
		}
	} else {
		clearInterval(intervalId);
		intervalId = '';
	}
	
	$('div.slide_ctrl li.active').removeClass('active');
	$('#slide_ctrl_' + id).addClass('active');
	
	$('div.slide_content:visible').queue(function () {
		$(this).fadeOut('250', function () {
			$('#slide_content_'+ id).queue(function () {
//				$('div.slide_content:visible').each(function () {
//					$(this).hide();
//				});
				$(this).fadeIn('250');
				$(this).dequeue();
			});
		});
		$(this).dequeue();		
	});	
	return false;
}

var intervalId = '';
var interval = 10000;

function autoRotate (modifier) {		
	if (modifier == true) {
		intervalId = setInterval ( "switchTopPromotion('0',true)", interval );
	} else if (modifier == false ) {
		if (intervalId != '') {
			clearInterval(intervalId);
			intervalId = '';
		} 
	}	
}

