/**
 *  Clear if value match
 */
function clearif(object, value)
{
	if(object.value == value) object.value = '';
}

/**
 *  Count down timer
 */
function countdown(id, time)
{
	var current = new Date();
	var end = new Date(time);
	count_event = Math.floor((end - current.getTime()) / 1000);
	if(count_event > 0)
	{
		ss_event = to_time_string(count_event % 60);
		count_event = Math.floor(count_event / 60);
		mm_event = to_time_string(count_event % 60);
		count_event = Math.floor(count_event / 60);
		hh_event = to_time_string(count_event % 24);
		count_event = Math.floor(count_event / 24);
		dd_event = count_event;

		document.getElementById(id + '_days').innerHTML = dd_event;
		document.getElementById(id + '_hours').innerHTML = hh_event;
		document.getElementById(id + '_minutes').innerHTML = mm_event;
		document.getElementById(id + '_seconds').innerHTML = ss_event;
	}
	setTimeout("countdown('"+id+"', "+time+");", 1000);
}

/**
 *  Draw statistics graph
 */
function draw_graph(id, options, request)
{
	var default_options =
	{
		colors: [ '#2a8eca', '#8ce58c', '#ff9c00' ],
		grid: { backgroundColor: '#ffffff', color: '#999999', hoverable: true },
		legend: { backgroundColor: '#ffffff', labelBoxBorderColor: '#636363', noColumns: 1, position: 'ne', show: true },
		lines: { show: true },
		points: { show: true },
		yaxis: { color: '#636363', min: 0 },
		xaxis: { color: '#636363', mode: 'time', ticks: null, timeformat: '%y-%0m-%0d' }
	};
	$.extend(default_options, options);

	$.ajax(
	{
		data: { profile_id: request.profile_id },
		dataType: 'json',
		success: function(stats)
		{
			default_options.xaxis.ticks = stats.settings.ticks;

			var data = [];
			$('.' + id + '_toggles').find('input:checked').each(function()
			{
				var key = $(this).attr('name');
				if(key && stats.data[key]) data.push(stats.data[key]);
			});
			$.plot($('#' + id), data, default_options);

			var date_format = new Date(), label = '', previous = null;
			$('#' + id).bind('plothover', function(event, pos, item)
			{
				if(item)
				{
					if(previous != item.dataIndex)
					{
						$('#' + id + '_tooltip').remove();
						previous = item.dataIndex;
						date_format.setTime(item.datapoint[0]);
						label = date_format.getFullYear() + '-' + to_time_string(date_format.getMonth() + 1) +
						'-' + to_time_string(date_format.getDate());

						$('<div id="'+ id + '_tooltip"><span class="'+ id + '_tooltip_x">' + label + '</span>' +
						'<span class="'+ id + '_tooltip_y">' + item.datapoint[1] + '</span></div>').css(
						{
							left: item.pageX + 5,
							top: item.pageY + 5
						}).appendTo('body').fadeIn(100);
					}
				}
				else
				{
					$('#' + id + '_tooltip').remove();
					previous = null;            
				}
			});

			$('.' + id + '_toggle_serie').die('click');
			$('.' + id + '_toggle_serie').live('click', function()
			{
        var data = [];
        $('.' + id + '_toggles').find('input:checked').each(function()
				{
					var key = $(this).attr('name');
					if(key && stats.data[key]) data.push(stats.data[key]);
        });
        if(data.length > 0) $.plot($('#' + id), data, default_options);
			});
		},
		type: 'post',
		url: '/' + request.language + '/ajax/statistics'
	});
}

/**
 *  Hover
 */
function hover()
{
	$('.hover_button').die('hover');
	$('.hover_button').live({
		mouseenter: function()
		{
			$(this).find('.hover_dropdown').show();
		},
		mouseleave: function()
		{
			$(this).find('.hover_dropdown').hide();
		}
	});
	return true;
}

/**
 *  Facebook connect
 */
function init_facebook(app_id, signin)
{
	if(app_id)
	{
		FB.init({appId: app_id, status: false, cookie: true, xfbml: true});
		FB.Event.subscribe('auth.sessionChange', function(response) { document.location.replace(signin); });
	}
	return true;
}

/**
 *  Share on facebook
 */
function link(href, target)
{
	if(target = '_top') top.location.href = href;
	else document.location.href = href;
}

/**
 *  OpenX
 */
function openx(zoneid, single)
{
	if(single) zoneid = zoneid + '&amp;block=1';
	var m3_u = (location.protocol=='https:'?'https://r.famebloggers.com/www/delivery/ajs.php':'http://r.famebloggers.com/www/delivery/ajs.php');
	var m3_r = Math.floor(Math.random()*99999999999);
	if (!document.MAX_used) document.MAX_used = ',';
	document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
	document.write ("?zoneid=" + zoneid);
	document.write ('&amp;cb=' + m3_r);
	if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
	document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
	document.write ("&amp;loc=" + escape(window.location));
	if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
	if (document.context) document.write ("&context=" + escape(document.context));
	if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
	document.write ("'><\/scr"+"ipt>");
}

/**
 *  Create a popup window
 */
function popup_window(url, width, height)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '"+id+"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');");
}

/**
 *  Share on facebook
 */
function share_facebook(u)
{
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

/**
 *  Share on MySpace
 */
function share_myspace(T, C, U, L)
{
	var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(T)
	+ '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
	window.open(targetUrl);
}

/**
 *  Convert time to string
 */
function to_time_string(n)
{
	var s = '';
	if(n < 10) s += '0';
	return s + n.toString();
}
