var defFontSize = 13;
var defFont = 0;
var toolOptions = null;
var toolButtons = null;
var toolButtonsDetails = null;
var toolChildren = null;
var arToolOptionsSize = new Array(50, 75, 25, 50, 1, 1);
var arFonts = new Array('Verdana', 'Arial', 'Times New Roman', 'Georgia');
$(document).ready(function(){
	curFontSize = parseInt($.cookie('curFontSize'));
	curFont = getFontIndex($.cookie('curFont'));
	toolOptions = $('#toolOptions');
	toolButtons = $('#toolButtons a');
	toolButtonsDetails = $('#toolButtons a.haveDetail');
	toolOptions.css({opacity:0,height:'1px',display:'block'});
	toolChildren = toolOptions.children();//.wrap('<div><\/div>').parent();
	toolChildren.css('display', 'none');
	var code = '';
	for(var i=0,len=arFonts.length; i<len; ++i ){
		code += '<label for="optFont' + i + '" style="font-family:'+arFonts[i]+';"><input name="optFontGroup" type="radio" id="optFont' + i + '" value="' + arFonts[i] + '"\/>'+ arFonts[i]+'<\/label>';
	}
	$('#optionFont').html(code);
	$(":input[id^=optFont]").bind("click", setFont).eq(curFont).click();
	setFontSize();
	setFont();
	toolButtons.click(function(){
		$this = $(this);
		if($this.hasClass('hover') || $this.hasClass('disabled'))return;
		$('#toolButtons a').removeClass('hover');
		$('#toolBar').contents().stop();
		if($this.hasClass('haveDetail')){
			var idx = toolButtonsDetails.index(this);
			var newHeight = toolChildren.eq(idx).outerHeight();
			newHeight = newHeight ? newHeight : 1;
			toolOptions.animate({height : newHeight + 'px', opacity : 0});
			toolChildren.fadeOut('fast');
			//toolChildren.eq(idx).css('display', 'block');
			toolChildren.eq(idx).fadeIn();//animate({opacity : 1});
			$(this).css('opacity', 0).addClass('hover').animate({opacity:1});
			toolOptions.animate({opacity : 1}, {queue:false});
		}else{
			toolChildren.css('display', 'none');
			toolOptions.animate({height : 0 + 'px', opacity : 0});
		}
	});
	toolButtons.eq(1).click();
    $('#btnIncFontSize').mousedown(function(){curFontSize+=2;setFontSize();});
    $('#btnDecFontSize').mousedown(function(){curFontSize-=2;setFontSize();});
	code = '<ul>';
	var tmp;
	var title = document.title;
	var url = document.location.href;
	var arSocialBookmarks = new Array(
			['Digg', 'digg', 'http://digg.com/submit?phase=2&url='+url+'&title='+title],
			['Propeller', 'propeller', 'http://www.propeller.com/submit/?U='+url+'&T='+title],
			['Stumbleupon', 'stumbleupon', 'http://www.stumbleupon.com/submit?url='+url+'&title='+title],
			['Reddit', 'reddit', 'http://reddit.com/submit?url='+url+'&title='+title],
			['Del.ico.us', 'delicious', 'http://del.icio.us/post?url='+url+'&title='+title],
			['Newsvine', 'newsvine', 'http://www.newsvine.com/_tools/seed&save?u='+url+'&h='+title],
			['Google', 'google', 'http://www.google.com/bookmarks/mark?op=edit&bkmk='+url+'&title='+title],
			['Yahoo! Buzz', 'yahoo', 'http://buzz.yahoo.com/submit/?submitUrl='+url+'&submitHeadline='+title],
			['Ma.gnolia', 'magnolia', 'http://ma.gnolia.com/bookmarklet/add?url='+url+'&title='+title],
			['Furl', 'furl', 'http://www.furl.net/storeIt.jsp?u='+url+'&t='+title],
			['Facebook', 'facebook', 'http://www.facebook.com/share.php?u='+url],
			['Spurl', 'spurl', 'http://www.spurl.net/spurl.php?url='+url+'&title='+title]
		);
	code = '';
	for(var i=0,len=arSocialBookmarks.length; i<len; ++i ){
		var tmp = arSocialBookmarks[i];
		code += '<li><a id="'+tmp[1]+'" href="'+tmp[2]+'">'+tmp[0]+'</a></li>';
	}
	$('#optionShare').html(code);

});
function setFontSize(){
	curFontSize = isNaN(curFontSize) ? defFontSize : curFontSize;
	if(curFontSize > 17)curFontSize =  17;
	else if(curFontSize < 11)curFontSize = 11;
	$('.newsContainer').css('fontSize', curFontSize + 'px');
	$('.newsContainer').css('lineHeight', (curFontSize + 4) + 'px');
	$.cookie('curFontSize', curFontSize);
}
function setFont(){
	idx = $(":input[id^=optFont]").index($(this));
	if(idx >= 0 && idx < 4){
		$('.newsContainer').css('fontFamily', arFonts[idx]);
		$.cookie('curFont', arFonts[idx]);
	}
}
function getFontIndex(font){
	for(i in arFonts){
		if(arFonts[i] == font)return i;
	}
	return defFont;
}


var offset,serverTime,uTime,sunrise,sunset,zoneDiff;
var flag = 0;
var weekDays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT	');
var months = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'NOV', 'DEC');
function showTime(){
	cTime = (new Date()).getTime();
	var insTime = cTime - offset;
	t = new Date(insTime);
	var day = weekDays[t.getDay()];
	var date = months[t.getMonth()] + ' ' + t.getDate() + ', ' + t.getFullYear();
	apm = 'AM';
	hr = t.getHours();
	if(hr > 12){
		hr -= 12;
		apm = 'PM';
	}else if(hr == 12)apm = 'PM';
	min = ((min = t.getMinutes()) < 10) ? '0' + min : min;
	$('#timeDay').html(hr + ':' + min + ' ' + apm);
	$('#fullDate').html(date + ' ' + day);
	//sec = ((sec = t.getSeconds()) < 10) ? '0' + sec : sec;
	//$('#time').html(hr + ':' + min + ':' + sec + ' ' + apm);
	var diff = (cTime - uTime)/1000;
	if(diff < 0 || diff > 120){
		$('#status').html('Syncing with server');
		updateTime();
		uTime += 10000;
	}else{
		//$('#status').html('Next sync with server in ' + (120 - Math.floor(diff)) + ' seconds. <'+'a href="javascript:updateTime();">Sync now</'+'a>.');
	}
}