//Some variables we need to manage the archive timeline

var monthArray = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

var timePos = 0; // Controls current view

/** 
 * Description: Rewritten Archive construction function.
 * Creates the entire archive for the length of the duration from archiveStart to archiveEnd
 * 
*/
function initArchive() {

	var startMonth = archiveEnd.getMonth();
	var mCnt = 0;
	var yCnt = 0;
	var right = 54;
	
	var curr = 0;
	while (right < archiveLengthPx) {
		var bgDiv = $('<div></div>').addClass('gridBg');
		bgDiv.css('right', right + 'px');
		$('#tagHeap').append(bgDiv);
		bgDiv.addClass('archiveScroll');
		if (right == 54)  //tag the first and last elements for containment on scrolling
		{
			bgDiv.attr("id","firstBg");
		}else if (right + 250 >= archiveLength) {
			bgDiv.attr("id","lastBg");
		}
	
		//format the monthStr
		var monthStr =  monthArray[startMonth - mCnt] + " " + (archiveEnd.getFullYear() - yCnt).toString();
		//append to the timeline
		var monthDiv = $('<div>' + monthStr + '</div>').addClass('month').addClass('archiveScroll');
		monthDiv.css('right', right + 'px');
		$('#timeline').append(monthDiv);

		//check if we have reached previous year
		if (startMonth - mCnt == 0)
		{
			yCnt++;
			mCnt = 0;
			startMonth = 11;
		}else {
			mCnt++;
		}
		//and the bg
		right += 250;
	}
	// set the tag heap position
	$('#tagHeap').css({
		'right' : "0px",
		'width' : archiveLengthPx + "px"
	});
	$('#timeline').css({
		'right' : "0px",
		'width' : archiveLengthPx + "px"
	});
}
/** 
 * Description:
 * 
 * @param 
 * @return 
*/
function scrollLeft(){
	if (reachedEnd("left"))
	{
		$('#scrollLeft').addClass('disabled');
		clearInterval(this);
		return;
	}
	
	//advance the tags
	$("#tagHeap").css({
		'right': parseInt($('#tagHeap').css('right')) - 2  + "px"
	});
	$('#timeline').css({
		'right': parseInt($('#timeline').css('right')) - 2  + "px"
	});
}
/** 
 * Description:
 * 
 * @param 
 * @return 
*/
function scrollRight(){
	if (reachedEnd("right"))
	{
		$('#scrollRight').addClass('disabled');
		clearInterval(this);
		return;
	}


	//advance the tags
	$("#tagHeap").css({
		'right': parseInt($('#tagHeap').css('right')) + 2 + "px"
	});
	$('#timeline').css({
			'right': parseInt($('#timeline').css('right')) + 2 + "px"
	});
}
/** 
 * Description:
 * 
 * @param 
 * @return 
*/
function reachedEnd(dir){
	if ((dir == "right" && parseInt($('#tagHeap').css('right')) >= 0) || 
		(dir == "left" && archiveLengthPx + parseInt($('#tagHeap').css('right')) < $('#archiveWrapper').width() - 108))
	{
		return true;
	}else{
		return false;
	}
}
/**
 * 
 * @var 
 */
var archiveState = false;

/** 
 * Description: Toggles the view of the archive
 * 
*/
function archiveToggle() {
	if (!archiveState && $(window).height() < 640)
	{
		$("#footerWrapper").css('position','absolute')
	}
	$('body').css('overflow','hidden');
	if (!archiveState) //not currently in archive state.  move to archive state
	{
		
		$('#archiveWrapper').css('display','block');
		
		$('#footerWrapper').animate({bottom: '400px'},'slow', 
			function() {
				$('#archiveToggle').attr('src',"themes/keboola/images/x.gif");
				archiveState = true;
				//Disable the cloudMap
				$("<div></div>").attr('id','cloudBlanket').appendTo('#cloudMap');
				$('.cloudTag:not(.archive)').each(function(){
					if ($.browser.msie){
						if ($(this).children(':first').css('background-color') != '#ffffff' && $(this).children('first:').css('background-color') != 'transparent'){
							$(this).grayscaleToggle();
						}
					}else{
						if ($(this).css('background-color') != '#ffffff' && $(this).css('background-color') != 'transparent'){
							$(this).grayscaleToggle();
						}
					}
				});
				$('body').css('overflow','auto');
			});
		//if ($.browser.msie){
		//	window.setTimeout("$('.cloudTag:not(.archive)').each(function(){$(this).grayscaleToggle();});",100);
		//	var t = setTimeout("$('.cloudTag:not(.archive)').each(function(){if($(this).css('color') != '#4c4c4c' && $(this).css('color') != '#cccccc') $(this).addClass('ieGrey');});",600);
		//}else {
		//	window.setTimeout("});",600);
		//}
	}else { //currently in archive state, move to hidden
		$('#footerWrapper').animate({bottom: '0px'},'slow',
			function(){
				$('#archiveToggle').attr('src',"themes/keboola/images/downArrow.gif");
				archiveState = false;
				//remove the blanket
				
			});
		$('#cloudBlanket').remove();
		$('.cloudTag:not(.archive)').each(function(){
				if ($.browser.msie){
				//	$(this).removeClass('ieGrey');
					if ($(this).children(':first').css('background-color') != '#ffffff' && $(this).children('first:').css('background-color') != 'transparent'){
						$(this).grayscaleToggle();
					}
				}else {
					if ($(this).css('background-color') != '#ffffff' && $(this).css('background-color') != 'transparent'){
						$(this).grayscaleToggle();
					}
				}
		});
		//if (!$.browser.msie){
		//	$('.cloudTag:not(.archive)').toggleClass('unfocus');
		//}
		//$('.cloudTag:not(.archive)').toggleClass('unfocus');
		var a = setTimeout("$('#archiveWrapper').css('display','none');$('body').css('overflow','auto');",500);
	}
}

$(document).ready(function(){
	//add nice corners
	//$('.cloudTag').corner();
	$(window).resize(function() {
		if ($(window).height() < 680 && archiveState || $(window).height() < 300 && !archiveState)
		{
			if (archiveState)
			{
					$("#footerWrapper").css('position','absolute');
			}else {
					$("#footerWrapper").css('bottom',$(window).height() - 300);
			
			}
		}
		else 
		{
		}
	});
	

	// nobody likes broken image tags, although this should never get called
	$('img').error(function(){
 		$(this).remove();
	});

	

	/* UNCOMMENT THIS TO ENABLE TAG FOCUSING 
	$("div.cloudTag").mouseenter(function() {
		$(this).siblings().addClass("unfocus");
	}).mouseleave(function() {
		$(this).siblings().removeClass("unfocus");
	});
	*/
	
	$('#archiveTrigger').click(function(){	archiveToggle();	});
	
	//initialise the timeline
	initArchive();
	
	/**
	 * @var Interval, mouse enter sets the scrollLeft function to run at this interval
	 */
	var scrollLeftInterval;
	/**
	 * Start scrolling on mouse enter the scroll bar
	 */
	$('#scrollLeft').bind("mouseenter",function(){
		//alert($("#archiveWrapper").width() + " " + archiveLengthPx);
		clearInterval(scrollLeftInterval);
		if ($('#scrollRight').hasClass('disabled'))
		{
			$('#scrollRight').removeClass('disabled')
		}
		scrollLeftInterval = setInterval(scrollLeft,10);
	});
	/** 
	 * Description: The booster going left
	 * 
	*/
	$('#scrollLeft').bind("mousedown",function(){
		var distance = 996;
		if (archiveLengthPx + parseInt($('#tagHeap').css('right')) < $('#archiveWrapper').width() + 996 - 108)
		{
			if (reachedEnd("left"))
			{
				return;
			}else {
				clearInterval(scrollLeftInterval);
				distance = 	archiveLengthPx + parseInt($('#tagHeap').css('right')) - $('#archiveWrapper').width() + 108;
			}
		}

		clearInterval(scrollLeftInterval);
		
		$("#tagHeap").animate({
			right: parseInt($("#tagHeap").css('right')) - distance + "px"
		});
		$('#timeline').animate({
			right: parseInt($("#timeline").css('right')) - distance + "px"
		});

		scrollLeftInterval = setInterval(scrollLeft,10);
	});
	//stop the scrolling on mouse leave
	$('#scrollLeft').bind("mouseleave",function(){
		clearInterval(scrollLeftInterval);
	});
	/**
	 * @var Interval, mouse enter sets the scrollLeft function to run at this interval
	 */
	var scrollRightInterval;
	/** 
	 * Description:
	 * 
	 * @param 
	 * @return 
	*/
	$('#scrollRight').bind("mouseenter",function(){
		clearInterval(scrollRightInterval);
		if ($('#scrollLeft').hasClass('disabled'))
		{
			$('#scrollLeft').removeClass('disabled')
		}

		scrollRightInterval = setInterval(scrollRight,10);
	});
	/** 
	 * Description:
	 * 
	 * @param 
	 * @return 
	*/
	$('#scrollRight').bind("mousedown",function(){
		var distance = 996;
		if (parseInt($('#tagHeap').css('right')) > - 996)
		{
			if (reachedEnd("right"))
			{
				return;
			}else {
				clearInterval(scrollRightInterval);
				distance = 	0 - parseInt($('#tagHeap').css('right'));
			}
		}

		clearInterval(scrollRightInterval);
		
		$("#tagHeap").animate({
			right: parseInt($("#tagHeap").css('right')) + distance + "px"
		});
		$('#timeline').animate({
			right: parseInt($("#timeline").css('right')) + distance + "px"
		});

		scrollRightInterval = setInterval(scrollRight,10);
	});
	/** 
	 * Description:
	 * 
	*/
	$('#scrollRight').bind("mouseleave",function(){
		clearInterval(scrollRightInterval);
	});
	
	//stop propagation for mousedown on tags
	$('.cloudTag').bind("mousedown",function(event){event.stopPropagation(); return false;});
	/** 
	 * Description: iPhone style scrolling
	 * 
	*/
	var startX;
	var mouseDown = false;
	$('#tagHeap').bind("mousedown",function(event){
		startX = event.pageX;
		mouseDown = true;
		//$(this).css('cursor','move');
		//alert("tag heap mousedown");
	});
	$('#tagHeap').bind("mouseup",function(event){
		mouseDown = false;
		//$(this).css('cursor','default');
	});

	/** 
	 * Description: Iphone style scrolling on mousemove if mouseDown
	 * 
	*/
	$('#tagHeap').bind("mousemove",function(event){
		if (mouseDown)
		{
			//how much the mouse has moved
			var distance = startX - event.pageX;
			//check if reached the end on the right side
			if (reachedEnd("right") && distance > 0)
			{
				$('#scrollRight').addClass('disabled');
				$('#tagHeap').mouseup();
				return;
			}else if(parseInt($('#tagHeap').css('right')) + distance > 0){ //check if the added distance will go passed the right edge
				distance = 	0 - parseInt($('#tagHeap').css('right')); // move to the end
				$('#scrollRight').addClass('disabled');
				$('#tagHeap').mouseup();
			}else if ($('#scrollRight').hasClass('disabled')) //remove the disabled class if found
			{
				$('#scrollRight').removeClass('disabled');
			}
				
			if (reachedEnd("left") && distance < 0)
			{
				$('#scrollLeft').addClass('disabled');
				$('#tagHeap').mouseup();
				return;
			}else if(archiveLengthPx + parseInt($('#tagHeap').css('right')) + distance < $('#archiveWrapper').width() - 108){
				distance = $('#archiveWrapper').width() - 108 - archiveLengthPx - parseInt($('#tagHeap').css('right'));
				$('#scrollLeft').addClass('disabled');
				$('#tagHeap').mouseup();
			}
			else if ($('#scrollLeft').hasClass('disabled'))
			{
				$('#scrollLeft').removeClass('disabled');
			}
			
			$("#tagHeap").css({
				'right': parseInt($("#tagHeap").css('right')) + distance + "px"
			});
			$('#timeline').css({
				'right': parseInt($("#timeline").css('right')) + distance + "px"
			});
			startX = event.pageX;
		}
	});
		
});
function deferredCorners(){
	$('.cloudTag.archive').each(function(){
		if ($(this).hasClass('cornerMe')) {
			if ($(this).hasClass('ui-draggable')) {
				setIEcorners($(this));
			}else {
				if ($.browser.msie || ($.browser.mozilla && parseFloat($.browser.version.split(0,3)) > 1.9) ){
					setIEcorners($(this));
				}else {
					$(this).corner({
						tl: { radius: 3 },
						tr: { radius: 3 },
						bl: { radius: 3 },
						br: { radius: 3 }});
				}
			}
		}
		if ($(this).parent().attr('href')) {
			$(this).parent().appendTo("#tagHeap");
		}else {
			$(this).appendTo("#tagHeap");
		}
		$('.archive').css('visibility','visible');
	});
}
//prepare the greyscaling
//this caches for improved speed
$(document).ready(function(){
	//deferredGrayscale();
	//setTimeout("deferredCorners()",100);
	deferredCorners();
});		


