var sliderPosition = 1;
var tabActive = true;
var slidingTimer = '';

$(window).blur(function(){
	tabActive = false;
});
$(window).focus(function(){
	tabActive = true;
});

function slideCarousel(){
	if (tabActive){
		var newLeft = sliderPosition * 21 + 14;
		$("#featured_selected").animate({
	    left: newLeft+"px"
	  }, 500);
	  
	  var newLeft = sliderPosition * 726;
		$("#featured ul").animate({
	    marginLeft: "-"+newLeft+"px"
	  }, 500);
	  	
	  sliderPosition = sliderPosition + 1;
	  if (sliderPosition == 5)
	  	sliderPosition = 0;
  }
}

$(document).ready(function(){
	slidingTimer = setInterval('slideCarousel()', 5000);
});

$('input[type="text"],textarea').live('focus', function(){
	if ($(this).attr('title') == $(this).val())
		$(this).val('');
});

$('input[type="text"],textarea').live('blur', function(){
	if ('' == $(this).val())
		$(this).val($(this).attr('title'));
});

function adjustAddThis(){
	$('.addthis_button_facebook_like iframe').width(83);
	$('.addthis_button_tweet iframe').width(100);
	$('.addthis_button_google_plusone div').width(65);
}

$(document).ready(function(){
	setInterval('adjustAddThis()', 500);
});

$('#password_title').live('click', function(){
	$(this).hide();
	$('#sidebar_password').focus();
});
$('#sidebar_password').live('focus', function(){
	$('#password_title').hide();
});
$('#sidebar_password').live('blur', function(){
	if ($('#sidebar_password').val() == '')
		$('#password_title').show();
});

function checkPassword(){
	if ($('#sidebar_password').val() != '')
		$('#password_title').hide();
}
setInterval('checkPassword', 1000);

function pollVote(e, currentVotes, option_id){
  $.get('/ajax.php?action=poll_vote&v2&option_id='+option_id, function(data) {
  	if (data == 'error'){
  		alert('You can vote only once');
  	} else {
  		$(e).css('color', '#FFF601');
  		$(e).parent().children('span').html(currentVotes + 1);
  	}
	});
}

$('#slide_selector li').live('click', function(){
	sliderPosition = parseInt($(this).attr('class')) - 1;
	slideCarousel();
	clearInterval(slidingTimer);
});

$('#btn_benefits').live({mouseenter: function(){
		var offset = $('#btn_benefits').offset();
		$('#what_are_the_benefits').css('display', 'block');
		$('#what_are_the_benefits').css('left', (offset.left - $('#what_are_the_benefits').outerWidth()) + 'px');
		$('#what_are_the_benefits').css('top', offset.top - 6 +'px');
	}, mouseleave: function(){
		$('#what_are_the_benefits').css('display', 'none');
	}
});

$('.clkDownload').live('click', function(e){
	$('#downloadLightbox').lightbox_me({
    centered: true
  });
  href = $(this).attr('href');
  $('#invisibleIframe').attr('src', href);
  return false;
});

if (showControlBox){
	$(document).ready(function(){
		theWidth = $(document).width();
		rightOffset = Math.round((theWidth - 1000) / 2 - 78);
		$('#arrow_control').css('right', rightOffset+'px');
		$('#arrow_control').show();
	});
	$('#arrow_control a.top, #arrow_control a.bottom').click(function(){
		return false;
	});
	$('#arrow_control .top').click(function(){
		$(document).scrollTop(0);
	});
	$('#arrow_control .bottom').click(function(){
		$(document).scrollTop($('#content').height());
	});
}
