// doing this because modal not working in IE with latest of jQuery
var contentDialogOpen = 0;

function loadContent(content) {
	if(contentDialogOpen == 1)
		return;
	
	var content = content;
	$('<div id="contentDialog">').dialog({
		show: 'fade',
		hide: 'fade',
		position: [$(window).width() / 6, 50],
		width: $(window).width() * 0.75,
		modal: false,
		buttons: { 'Close': function() {
			$(this).dialog('close');
		}},
		open: function() {
			contentDialogOpen = 1
			$(this).load(content, pageSetup).height('auto').siblings('div.ui-dialog-titlebar').remove();
			$('#contentDialog').bind('dialogclose', function(event, ui) {
				$('#contentDialog').remove();
				contentDialogOpen = 0;
			});
		}
	});
}

var currentContent = 'content-default';
var clicks = 0;
function animateContent(newId) {
	var newId = newId;
	var dynamicDirection = (clicks++ % 2 == 0 ? 'right' : 'left');
	$('#' + currentContent).hide('slide', { direction: dynamicDirection }, 1000, function() {
		$('.copyright').remove();
		$('#' + newId).fadeIn(500, function() {
			$(this).load('/currentVersion/static/' + newId.split('-')[1] + '.html', function() {
				initControls();
				resetCopyright();
			});
		});
		currentContent = newId;
	});
}

function initControls() {
	$('.menuitem').mouseover(function() {
	    $(this).css('cursor', 'pointer');
	});

	$('a.topmenu').button();
	$('#tabs').tabs();
	$('#accordion').accordion({autoHeight:true});		
	
	var delay = function() { initDataviz(); };
	window.setTimeout(delay, 1000);	
}

function buildNewsData() {
	var data = new Array();
	$('#news-listing').children().each(function() {
		var item = new Array();
		item.push(this.id, $(this).attr('class'));
		data.push(item);
	});
	return data;
}

function initDataviz() {
	if(currentContent != 'content-news')
		return;
	
	/*var data = [['11-1-2011', 'EVENT'],
	            ['7-1-2008', 'MILESTONE']];*/
	var data = buildNewsData();
	
	var id = 'news-temporalview';
	$('#'+id).html('').addClass('dataviz-plot');	
	$.jqplot(id, [data], {
		axes: { 
			xaxis: { 
				renderer: $.jqplot.DateAxisRenderer, 
				tickInterval: '1 years', 
				autoscale: true,
				min: '06-01-2007',
				tickOptions: {
					formatString: '%#m-%#d-%Y'						
				} 
			},
			yaxis: { 
				renderer: $.jqplot.CategoryAxisRenderer
			}
		},
		highlighter: { 
			show: true,
			showTooltip: true,
			tooltipLocation: 's',
			useAxesFormatters: true,
			formatString: '<table class="jqplot-highlighter" style="width:200px"><tr><td>%s</td></tr></table>'
		},
		series:[{color:'#C02828', showLine: false}]
	});
	
	resetCopyright();
}

function decodeNewsEvent(eventDate) {
	return eventDate + ' : ' + $('#news-listing #' + eventDate).html();
}

function resetCopyright() {
	/*$('.copyright').remove();
	$('<div class="copyright">&copy;Tactical Trust Inc, 2011 ; All Rights Reserved</div>').appendTo('body');
	*/
}

function pageSetup() {
	initControls();
	
	initMenu();
	$('#js-news').ticker();
	$('.content').hide();
	$('#content-default').show();
}

function initMenu() {
	$('#mainmenu .kwicks').kwicks({
		max: 205,
		isVertical: false,
		spacing:  5
	});	
}

