var newest = 1;
var query;
var hours = 6;

function search_text() {
	if(query) {
		$("div#hero h3").html("<strong class='live'>Real-time</strong> search for: <strong>"+query+"</strong>");
	} else {
		$("div#hero h3").load("load.php?t=hero");
	}
}

function edition() {
	var now = new Date();
	var hour = now.getHours();
	
	if (hour < 6) {
		return "<strong>early edition</strong> 12am &ndash; 6am et";
	} else if (hour < 12) {
		return "<strong>morning edition</strong> 6am &ndash; 12pm et";
	} else if (hour < 18) {
		return "<strong>afternoon edition</strong> 12pm &ndash; 6pm et";
	} else {
		return "<strong>late edition</strong> 6pm &ndash; 12am et";
	}
}

function update_all() {
	$("#updating").fadeIn();
	$(".loader-small").fadeIn();
	fetch_data("media");
	fetch_data("blogs");
	fetch_data("pundits");
	get_video();
	$("#updating").fadeOut();
	$(".loader-small").fadeOut();
	
	//search_text();
}

function fetch_data(elem) {

    /*var list = $("#tweets-" + elem);
    var last = $("#tweets-" + elem + ' li:last-child');
    // grab first child in list to know newest ID
    var first = $("#tweets-" + elem + ' li:first-child');
    // grab last child to drop off
    var id = first.attr("id");
    id = id.substring(3);
    // remove prefix from LI id to get status_id
    $.get("load.php", {
        type: elem,
        since: id
    },
    function(data) {
        list.prepend(data);
        add_source_filters(elem);
        desc();
        $("#tweets-" + elem + " li.loader").remove();
    },
    'html'
    );*/
	
	if(query) {
		$("#tweets-" + elem).load("load.php?t="+elem+"&h="+hours+"&q="+escape(query));
	} else {
		$("#tweets-" + elem).load("load.php?t="+elem+"&h="+hours);
	}
	
    return (false);
}

function animate(img) {	

}

function get_video() {
	
	if(query) {
		$("div#video ul").load("load.php?t=video&q="+escape(query),function(){
			$("div#video ul a").click(function() {
				video_player($(this).attr("id").substr(2));
		    });
		
			jQuery('.yt').quickpaginate( { perpage: 7, showcounter: false } ); 
		});
	} else {
		$("div#video ul").load("load.php?t=video",function(){
			$("div#video ul a").click(function() {
				video_player($(this).attr("id").substr(2));
		    });
		
			$("div#video ul a").hover(function() {
				var img = $(this).css("background-image");
				animate(img);
			});
			
			jQuery('.yt').quickpaginate( { perpage: 7, showcounter: false } );
		
		});
	}
	
	return (false);
}

function video_player(v) {
	$("div#player-container").load("load.php?t=player&v="+v, function(){ $("a#player-close").click(function() { $("div#player-container").slideUp("slow"); }) });
	$("div#player-container").slideDown("slow");
}

function more_link(type) {
	
	$("#more-"+type).bind("click", function(e){	
		$("#tweets-"+type).load("load.php?type="+type+"&rows="+$("#more-"+type).attr("name"));
		var next = parseInt($("#more-"+type).attr("name")) + 20;
		$("#more-"+type).attr({ 
			name: next
		})
	});
	
}

function check_filters(div, elem) {
	var value = elem.val();
    //stringValue = elem+' ul li.' + value;
	if (elem.is(':checked')) {
		//$(stringValue).fadeIn('fast');
		$(div+" ul.filterThis").addClass(value);
	} else {
		//$(stringValue).fadeOut('fast');
		$(div+" ul.filterThis").removeClass(value);
	}
}

$(document).ready(function() {

    // Initial AJAX loaders
    fetch_data("media");
    fetch_data("pundits");
    fetch_data("blogs");
	get_video();
    $("div#breaking").load("load.php?t=breaking");
    $("div#hero h3 span").load("load.php?t=hero");
    $("div#headlines ul").load("load.php?t=big",
    function() {
        $("div#headlines").jCarouselLite({
            visible: 5,
            auto: 4000,
            speed: 2000
        });
    });
    $("#updating").fadeOut();

    // AJAX Polling every 60s
    setInterval(function() {
        $("#updating").fadeIn();
        fetch_data("media");
        fetch_data("pundits");
        fetch_data("blogs");
        $("#big").load("load.php?t=big");
	    $("div#breaking").load("load.php?t=breaking");
        $("#updating").fadeOut();
    },
    60000);

    // adjust filters on click
/*
    $('#blogs input').click(function() {
		check_filters("#blogs", $(this));
    });

	$('#pundits input').click(function() {
		check_filters("#pundits", $(this));
    });

	$('#media input').click(function() {
		check_filters("#media", $(this));
    });

	// check filters on page load

	$("#blogs input").each(function (i) {
		check_filters("#blogs", $(this));
	});

	$("#pundits input").each(function (i) {
		check_filters("#pundits", $(this));
	});
	
	$("#media input").each(function (i) {
		check_filters("#media", $(this));
	});*/
	
    // Clock
/*
    $(function($) {
        var options = {
            format: '%I:%M%p',
            utc: true,
            utcOffset: -5
        }
        $('#jclock').jclock(options);
    });*/

	// Search

	swap_val = [];  
	$("#search-text").each(function(i){  
	    swap_val[i] = $(this).val();  
	    $(this).focusin(function(){  
	        if ($(this).val() == swap_val[i]) {  
	            $(this).val("");  
	        }  
	    }).focusout(function(){  
	        if ($.trim($(this).val()) == "") {  
	            $(this).val(swap_val[i]);  
	        }  
	    });  
	});
	
	$("#search-field").submit(function() {
		
		query = $("#search-text").val();
		
		if (query == '') {
			$("#search-result").text('politics');
		} else {
			$("#search-result").text(query);
		}
			
		$("div#player-container").slideUp("slow"); // close video if open
		update_all();
	});
	
	// Time Filters
	
	$("#time-filters a").click(function() {
		hours = $(this).attr("title");
		$("#time-filters a").removeClass("selected");
		$(this).addClass("selected");
		update_all();
    });

	// Party Filters
	
	$('#global-left').click(function() {
	  $("body").toggleClass("left");
	});
	
	$('#global-right').click(function() {
	  $("body").toggleClass("right");
	});
	
	$('#global-center').click(function() {
	  $("body").toggleClass("center");
	});
	
});