var POPULAR_ARTICLE_LOADED = 1;
var TWITTER_POST_LOADED = 1;
var WINDOW_FOCUS = 1;
var TIME_ACCELERATOR = 10;

var marker;
var map;
document.twitter_post = [];

$(function(){
	after_dom();

	var map = new GMap2(document.getElementById('twitter_live'), {mapTypes: [G_PHYSICAL_MAP] });
	var yogyakarta = new GLatLng(-7.797224,110.368797);
	map.setCenter(yogyakarta, 3);
	map.addControl(new GSmallMapControl());	/**/

	$('#article-popular-refresh a').bind('click', function(){
		$('#salsil-pop').html(document.popular_articles);
		document.popular_articles = '';
		$(this).parent('div').hide();
		return false;
	});
	
	$(window).bind('blur', function() {
	   WINDOW_FOCUS = 0;
		TIME_ACCELERATOR = 1;
	});

	$(window).bind('focus', function() {
	   WINDOW_FOCUS = 1;
		TIME_ACCELERATOR = 10;
	});
	
	/* 
		* Popular Article 
		* 1 minutes
	*/
	$("#salsil-pop").everyTime((60000/TIME_ACCELERATOR),function() {
		if(POPULAR_ARTICLE_LOADED){
			getPopularArticles();
		}
	});
	/* 
		* Live Tweet 
		* 6 seconds
	*/
	if(TWITTER_POST_LOADED){
		getLatestLocalTweetsMap();
	}
	$('#twitter_live').everyTime(6000, function(){
		if(document.twitter_post.length > 0){
			var sekarang = document.twitter_post.pop();
			var info = '<div class="tweet">'+sekarang[3]+'</div>';
			map.closeInfoWindow();
			if(marker) map.removeOverlay(marker);
			marker = new GMarker(new GLatLng(sekarang[0],sekarang[1]));
			map.addOverlay(marker);
			map.openInfoWindow(marker.getLatLng(), info, {maxWidth:200, noCloseOnClick : true, pixelOffset : new GSize(0,-25) } );
		}else{
			if(TWITTER_POST_LOADED){
				getLatestLocalTweetsMap();
			}
		}
	});
	
	/* 
		* Reset Home  
		* 30 minutes
	*/
	setInterval("resetHome()", 1800000);
	
	$("ul.tabs_dir li.tab_dir").click(function(){
		dirName = $(this).children("a").attr("href").substr(4);
		if($("#dir" + dirName + " ul").children().size() == 0) {
			getBlogPostFromCategory(dirName);
		}
	});
});

function resetHome(){
	EDITOR_ARTICLE_LOADED = 1;
	EDITOR_PICKED_LOADED = 1;
	POPULAR_ARTICLE_LOADED = 1;
	LATEST_BLOGPOST_LOADED = 1;
	TWITTER_POST_LOADED = 1;
	return false;
}

function getPopularArticles(){
	var headline  = '';
	var pop_list = '';
	POPULAR_ARTICLE_LOADED = 0;
	$.getJSON(document.base_url + "ajax/getPopularArticles/0/today/",  function(json){
		POPULAR_ARTICLE_LOADED = 1;
		if(json.length > 0){
			for(i=0; i<json.length; i++) {
				if(i>0)
					var c = i%2 ? 'odd' : 'even';
				else
					var c = 'featured';
										
				var retweet = 'RT @salingsilang: '+ json[i].article_title + ' '+json[i].article_link;
				var img = '';
				if(json[i].picture_url || json[i].picture_filename){
					//img = '<img src="'+(json[i].picture_url ? json[i].picture_url : json[i].picture_filename)+'" alt="" class="img-left" />';
					img = '<div class="ilustrasi" style="background:url('+(json[i].picture_url ? json[i].picture_url : json[i].picture_filename)+') center no-repeat;">&nbsp;</div>';

				}
				
				headline += '<li class="'+c+'" id="article-pop-'+json[i].application_id+'-'+json[i].article_id+'"><img class="webicon" src="'+document.base_url+'images/favicons/'+json[i].application_slug+'.png"/><h4><a href="'+json[i].article_link+'" target="_blank">'+json[i].article_title_limit+'</a><a target="_blank" href="'+json[i].article_link+'" class="comment-count">'+json[i].article_comments+'</a></h4><div class="clearfix"><ul class="sh_fav">	<li class="fbshare"> <a target="_blank" href="http://www.facebook.com/sharer.php?u='+urlencode(json[i].article_link)+'" class="b-fbshare">Share</a> </li> <li class="retweet"> <a target="_blank" href="http://twitter.com/home/?status='+urlencode(retweet)+'" class="b-retweet">Retweet</a> </li><li class="asik"><a href="javascript:void(0);" onclick="favePost(this,3,'+json[i].application_id+','+json[i].article_id+');" class="b-asik">Asik</a></li></ul></div><div class="clearfix excerpt">'+img+'<p>'+json[i].article_excerpt+'</p><p class="clearfix notes"><img width="16" src="'+json[i].application_url+'images/users/'+json[i].user_id+'-16.jpg"/> '+json[i].user_alias+', <span class="timelapse" title="'+json[i].article_time_utc+'">'+json[i].article_time+'</span></p></div></li>';
				pop_list += ''+json[i].application_id+'-'+json[i].article_id+'-';
			}
			if(document.popular_articles_list != pop_list){
				$('#article-popular-refresh').show();
				document.popular_articles = headline;
				document.popular_articles_list = pop_list;
			}
			after_dom();
		}
	});
}

function getLatestLocalTweetsMap(){
	TWITTER_POST_LOADED = 0;
	var a = [];
	document.twitter_post = [];
	$.getJSON(document.base_url + "ajax/getLatestLocalTweetsMap/",  function(json){
		TWITTER_POST_LOADED = 1;
		if(json.length > 0){
			for(i=0; i<json.length; i++) {
				a = [ json[i].latitude , json[i].longitude, json[i].source, json[i].text];
				document.twitter_post.push(a);
			}
		}
	});
}
function load_topsy(){
	// <script type="text/javascript">topsyWidgetPreload({ "url": "", "title": "", "theme": "jade", "nick": "salingsilang" });</script>
}
function getBlogPostFromCategory(category){
	var blogPostUl = $("#dir" + category + " ul");
	if($(blogPostUl).children().size() == 0) {
		var safe_dir = (category+'').toLowerCase().replace(/\s/, '_');
		$.getJSON(document.base_url + "ajax/getBlogPostsByTag/" + safe_dir,  function(json){
			if(json.length > 0){
				$(blogPostUl).html('<li class="loading"></li>');
				var blogPosts = "";
				for(i=0; i<json.length; i++) {
					var blogPostState = "even";
					if(i%2) blogPostState ="odd";
					
					blogPosts += '<li class="clearfix ' + blogPostState + '"><img class="thumb" src="http://open.thumbshots.org/image.aspx?url=' + json[i].uri + '" width="120" height="90" /><a class="post-title" href="' + json[i].uri + '">' + utf8_decode(json[i].title) + '</a><span class="timelapse" title="' + json[i].time_utc + '">' + json[i].time + '</span><p>' + json[i].tag + ' - <a href="' + json[i].uri + '" target="_blank">' + json[i].uri + '</a> <br />Relevansi : <span class="relevansi">100%</span></p></li>';
				}
				$(blogPostUl).html(blogPosts);
			}
		});
	}
}
