var totalTweets=0;
function refreshTweet()
{
	totalTweets++;
	if (totalTweets<=20) {
		$.getJSON('/index/tweet',function(data){
			if (data) {
				$('.tweeter_footer').html(data.content);
			}
		});
		setTimeout(refreshTweet,300000);
	}
}
$(function(){
	$('table.bus_tour tr').hover(function(){
		$(this).addClass('over');
	},function(){
		$(this).removeClass('over');
	});
	//refreshTweet();
	$('#right_first_name,#first_name_main').bind('click',function(){
		if ($(this).attr('value')=='First Name') {
			$(this).attr('value','');
		}
	});
	$('#right_location,#location_main').bind('click',function(){
		if ($(this).attr('value')=='Location') {
			$(this).attr('value','');
		}
	});
    $('#right_location,#category_main').bind('click',function(){
		if ($(this).attr('value')=='Category') {
			$(this).attr('value','');
		}
	});
    $('#idea_content').bind('click',function(){
		if (!$(this).attr('clicked')) {
			$(this).attr('value','');
            $(this).attr('clicked','1');
		}
	}).bind('focus',function(){
		if (!$(this).attr('clicked')) {
			$(this).attr('value','');
            $(this).attr('clicked','1');
		}
	}).bind('blur',function(){
		if (!$(this).attr('clicked')) {
			$(this).attr('value','');
            $(this).attr('clicked','1');
		}
	}).bind('keypress',function(){
		if (!$(this).attr('clicked')) {
			$(this).attr('value','');
            $(this).attr('clicked','1');
		}
	});

	$('.video_bar_container .video').hover(function(){$(this).addClass('over');},function(){$(this).removeClass('over');});
	
	$('.tab_user input').attr('value','');
	//tabs
	$('.comment_tabs .new_reader a').bind('click',function(e){
		e.preventDefault();
		$('.tab_user').hide();
		$('.tab_new_user').show();
		$('.current_tab').removeClass('current_tab');
		$(this).parent().addClass('current_tab');
		$('#new_user_name').focus();
		
		//clear previous
		$('#returning_email').attr('value','');
		$('#returning_password').attr('value','');
		$('#twitter_username').attr('value','');
		$('#twitter_password').attr('value','');
		return false;
	});
	$('.comment_tabs .returning a').bind('click',function(e){
		e.preventDefault();
		$('.tab_user').hide();
		$('.tab_returning_user').show();
		$('.current_tab').removeClass('current_tab');
		$(this).parent().addClass('current_tab');
		$('#returning_email').focus();
		
		//clear previous
		$('#new_user_name').attr('value','');
		$('#new_user_email').attr('value','');
		$('#new_user_password').attr('value','');
		$('#new_user_confirm_password').attr('value','');
		$('#twitter_username').attr('value','');
		$('#twitter_password').attr('value','');
		
		return false;
	});
	$('.comment_tabs .twitter a').bind('click',function(e){
		e.preventDefault();
		$('.tab_user').hide();
		$('.tab_twitter_user').show();
		$('.current_tab').removeClass('current_tab');
		$(this).parent().addClass('current_tab');
		$('#twitter_username').focus();
		//clear previous
		$('#new_user_name').attr('value','');
		$('#new_user_email').attr('value','');
		$('#new_user_password').attr('value','');
		$('#new_user_confirm_password').attr('value','');
		$('#returning_email').attr('value','');
		$('#returning_password').attr('value','');
		return false;
	});
	$('.comment_tabs .logout a').bind('click',function(e){
		e.preventDefault();
		logout();
		return false;
	});
	
	check_login();
 
    $('#comment_form').ajaxForm({ 
        dataType:  'json',
        beforeSubmit:function(){
            $('#comment_form input[type="submit"]').attr('disabled',true).attr('value','Posting...');
        },
        success:function(data){
            $('#comment_form input[type="submit"]').removeAttr('disabled').attr('value','Add Comment');
    		if (data.error) {
    			alert(data.error);
    		}
            try {
                if (data.new_comment) {
                    $('.last_comment').removeClass('last_comment');//remove any previous comments that were marked as "last"
                    var build_html='<div class="comment last_comment">';

                        build_html=build_html+'<div class="picture">';
                            build_html=build_html+'<img src="'+(data.new_comment.picture?data.new_comment.picture:'/html/images/general_comment.gif')+'" alt="'+data.new_comment.name+'">';
                        build_html=build_html+'</div>';
                        build_html=build_html+'<div class="user_name your_post">your post</div><p>'+data.new_comment.comment+'</p>';
                    build_html=build_html+'</div><div class="spacer_left comment_spacer"></div>';
                    $('.comment_tabs').addClass('logged_in');
                    if (build_html) {
                        $('#actual_comments').append(build_html);
                    }
                    $('#message').attr('value','');
                    $('.tab_returning_user').hide();
                    $.scrollTo($('#actual_comments').next(),800);
                    check_login();
                }
            }catch(e) {
                
            }
    	}
    }); 
    
    if ($('#actual_comments').length>0) {
    	
	    	get_page(document.location.href);
    
    }
    bind_pagination();
    
    //captcha
    $('#captcha').css('cursor','pointer').attr('title','Click to refresh').bind('click',function(){
    	var ntime=new Date();
    	$('#captcha').attr('src','/video/captcha?_='+ntime.getTime());
    });
});
function bind_pagination()
{
	$('#pagination a').bind('click',function(){
		get_page($(this).attr('href'));
	});
}
function get_page(page_string)
{
	var page=page_string.split('#p');

	if (page.length<=1) {
		page[1]=0;
	}
	$.get('/comments/refresh/page/'+(page[1]+'/video_id/'+$('#actual_comments').attr('class').replace('video','')),function(data){
    	$('#actual_comments').html(data);
    	bind_pagination();
	});
	
}

function check_login()
{
	if ($('.logged_in').length>0) {
		$('.tab_user').addClass('on');
		$('.comment_tabs > li:not(.logout)').hide();
		$('.logout').show();
	}else{
		$('.tab_user').removeClass('on');
		$('.comment_tabs > li:not(.logout)').show();
		$('.logout').hide();
	}
}
function logout()
{
	$('.comment_tabs').removeClass('logged_in');
	$.getJSON('/comments/logout',function(data){
		if (data.logged_out) {
			check_login();
		}
	},'json');
}
function vote(vote_id)
{
	var video_id=$('#actual_comments').attr('class').replace('video','');
	var email=$('#email_address').attr('value');
	if (!email) {
		alert('Please enter your email');
		$("#email_address").focus();
		return false;
	}

	$.getJSON('/video/vote/video_id/'+video_id+'/email/'+email+'/fkey/'+vote_id+'/captcha/'+$('#captcha_button').attr('value'),function(data){
        
		var ntime=new Date();
    	$('#captcha').attr('src','/video/captcha?_='+ntime.getTime()+'&refresh=1');
		if (data.error) {
			alert(data.error);
			$("#email_address").focus();
			return;
		}
		if (data.voted) {
			alert('Thank you for voting. A confirmation email has been dispatched to the specified email.');
			$("#email_address").attr('value','').focus();
    	}
        
	});
}
