/*================================================
  infogrid.js
  ================================================*/

var $el, $parentWrap, $otherWrap, $allTitles, $allCells, $iComportament;
var nowMoving = false;

function evCarouselInit() {
	$allTitles = $("div.dt").css({
		//padding: 5, // setting the padding here prevents a weird situation, where it would start animating at 0 padding instead of 5
		"cursor": "pointer" // make it seem clickable
	});

	$allCells = $("div.dd").css({
		position: "relative",
		top: 0,
		left: 0
		// display: "none" // info cells are just kicked off the page with CSS (for accessibility)
	});

	// clicking image of inactive column just opens column, doesn't go to link   
	$("#page-wrap div.info-col ul.evenimente").delegate("a","click", function(e) { 
		if ( !$(this).parents("li.current").parents(".info-col").hasClass("curCol") ) {  
			//alert('parent:' + $(this).parent().parent().html() );
			_gaq.push(['_trackEvent', 'Comportamente', 'Homepage', 'Expandare box acordeon']);
			e.preventDefault(); 
			$(this).next().find('dt:first').click(); 
 		}
	}); 

	if( typeof(Cufon) !== 'undefined' ) {
		Cufon.replace('p.ora_locatie', { fontFamily: 'proxima nova cn lt' }, { hover: true });
		Cufon.refresh();
	}
	
	$('div.info-col h2').click(function() {
		if ( !$(this).parent().hasClass("curCol") ) {  
			$(this).parent().find("h3 a:eq(0)").click();
		}
	});
	
	$('div.info-col h4').click(function() {
		if ( !$(this).parent().hasClass("curCol") ) {  
			$(this).parent().find("h3 a:eq(0)").click();
		}
	});
		
	$('div.info-col > h3').click(function() {
		if ( !$(this).parent().hasClass("curCol") ) {  
			$(this).parent().find("h3 a:eq(0)").click();
		}
	});
	
	getCurrentColumn();
}

function getCurrentColumn() {
	$iComportament = $('#page-wrap').find("div.curCol").index();// $parentWrap.index();
	return $iComportament;
}

$(function() {
	// Set up variables
	evCarouselInit();

	// clicking on titles does stuff
	$("#page-wrap").delegate("div.dt", "click", function() {
		// cache this, as always, is good form
		$el = $(this);
                     
		// if this is already the active cell, don't do anything
		if (!$el.hasClass("current")) {
			if( $("#page-wrap").children().length > 5 ) {
				// one too many...
				$parentWrap = $el.parent().parent().parent();
				$otherWraps = $(".info-col").not($parentWrap);
				
				if( getCurrentColumn() < 0 ) {
					if( $parentWrap.index() > 0 ) {
						$("#page-wrap div.info-col:first-child").remove();
					} else {
						$("#page-wrap div.info-col:last-child").remove();	
					}					
				}
			}
			$parentWrap = $el.parent().parent().parent();
			$otherWraps = $(".info-col").not($parentWrap);
			
			// remove current cell from selection of all cells
			$allTitles = $("div.dt").not(this);

			// close all info cells
			$allCells.slideUp();

			// return all titles (except current one) to normal size
			$allTitles.animate({
					fontSize: "18px"
				});

			// animate current title to larger size            
			$el.animate({
					fontSize: "22px"
				}).prev().slideDown(400);

			$el.animate({
					fontSize: "22px"
				}).next().slideDown(400);

			// make the current column the large size
			$parentWrap.animate({
				width: 300
			}).addClass("curCol");

			// make other columns the small size
			$otherWraps.animate({
				width: 140
			}).removeClass("curCol");

			// make sure the correct column is current
			$allTitles.removeClass("current");
			$el.addClass("current");  
			
			$("div.dt").parent().not(this).removeClass("current");
			$el.parent().addClass("current");
			
			getCurrentColumn();
			
			if( typeof(Cufon) !== 'undefined' ) 
				Cufon.refresh();
			
			//document.location.hash = $parentWrap.attr('rel') + "-" + ($el.parent().index() + 1);
		} else {
			_gaq.push(['_trackEvent', 'Comportamente', 'Homepage', 'Accesare post din acordeon']);
			// going to our destination
			window.location = $el.find("a").attr('href');
		}
	});
	
	$("#starter").trigger("click");	
	/*posTag = parseInt( document.location.hash.substring( 12 ) );
	if( posTag ) {
		dayTag = document.location.hash.substring( 1,11 );
		$("#page-wrap div.info-col[rel='" + dayTag + "'] ul li:nth-child(" + posTag + ")").hide();
	} else {
		$("#starter").trigger("click");	
	}*/		
});

function appendRightColumn( data, do_remove, count ) {
	if( strpos( data, 'DOCTYPE', 1 ) != false ) {
		alert('Nu mai exista alte evenimente introduse dupa aceasta data. Va rugam reveniti.');
		return false;
	}
	if( do_remove ) {
		sColDate = $("#page-wrap div.info-col:first-child").attr('title');
		$('span.prevextradays').prepend( '<span class="someclass">' + Base64.encode( '<div class="info-col" title="'+ sColDate +'">' + $("#page-wrap div.info-col:first-child").html() + '</div>' ) + '</span>' );
		/* if( count == 2 ) {
			sColDate = $("#page-wrap div.info-col:first-child").attr('rel');
			$('span.prevextradays').prepend( '<span class="someclass">' + Base64.encode( '<div class="info-col" rel="'+ sColDate +'">' + $("#page-wrap div.info-col:first-child").html() + '</div>' ) + '</span>' );
		} */
		$("#page-wrap div.info-col:first-child").hide("slow", function() {
				$(this).empty().remove();
		});
	}
	$(data).hide().appendTo('#page-wrap').css("max-height","300px").show("slow", function(){ nowMoving = false; }).css("max-height","1300px");
	evCarouselInit();
	$("#page-wrap div.info-col:last-child div.dd").css({
		display: "none"
	});
	if( count == 2 ){
		$("#page-wrap div.info-col:nth-child(6) div.dd").css({
			display: "none"
		});
	}
	
}

function appendLeftColumn( data, do_remove, count ) {
	if( strpos( data, 'DOCTYPE', 1 ) != false ) {
		alert('Pentru a vedea evenimentele mai vechi de 30 zile, va rugam accesati arhiva de evenimente.');
		return false;
	}
	
	if( do_remove ) {
		sColDate = $("#page-wrap div.info-col:last-child").attr('title');
		$('span.nextextradays').prepend( '<span class="someotherclass">' + Base64.encode( '<div class="info-col" title="'+ sColDate +'">' + $("#page-wrap div.info-col:last-child").html() + '</div>' ) + '</span>' );
		/* if( count == 2 ) {
			sColDate = $("#page-wrap div.info-col:last-child").attr('rel');
			$('span.nextextradays').prepend( '<span class="someotherclass">' + Base64.encode( '<div class="info-col" rel="'+ sColDate +'">' + $("#page-wrap div.info-col:last-child").html() + '</div>' ) + '</span>' );
		} */
		$("#page-wrap div.info-col:last-child").hide("slow", function() {
			$(this).remove();
		});
	}
	$(data).hide().prependTo('#page-wrap').css("max-height","300px").show("slow").css("max-height","1300px");
	evCarouselInit();
	$("#page-wrap div.info-col:first-child div.dd").css({
		display: "none" // info cells are just kicked off the page with CSS (for accessibility)
	});
	if( count == 2 ){
		$("#page-wrap div.info-col:nth-child(2) div.dd").css({
			display: "none" // info cells are just kicked off the page with CSS (for accessibility)
		});
	}
}

$(document).ready(function() {
			
	$("#next_one").click(function() {
		if( nowMoving ) return true;
		nowMoving = true;
		colCount = 1;
		if( getCurrentColumn() == 0 ) {
			colCount = 2;
		}
		
		if( $('span.nextextradays span:first-child').length != 0 ) {
			cachedCount = colCount;
			data = '';
			while( cachedCount > 0 ) {
				data = data + Base64.decode( $('span.nextextradays span:first-child').html() );
				$('span.nextextradays span:first-child').remove();
				cachedCount = cachedCount - 1;
			}
			appendRightColumn( data, true, colCount );
		} else {
			var data = {	action: 'ajax_acordeon_obtine_zi',	date: $("#page-wrap div.info-col:last-child").attr('title'),	offset: 1, count: colCount	};
			$.post('/index.php', data, function(data) {
				appendRightColumn( data, true, colCount );
			});
		}
		return false;
	});

	$("#previous_one").click(function() {
		colCount = 1;	
		if( getCurrentColumn() == 4 ) {
			colCount = 2;
		}		
		
		if( $('span.prevextradays span:first-child').length != 0 ) {
			cachedCount = colCount;
			data = '';
			while( cachedCount > 0 ) {
				data =  Base64.decode( $('span.prevextradays span:first-child').html() );
				$('span.prevextradays span:first-child').remove();
				cachedCount = cachedCount - 1;
			}
			appendLeftColumn( data, true, colCount );
		} else {
			var data = {	action: 'ajax_acordeon_obtine_zi',	date: $("#page-wrap div.info-col:first-child").attr('title'), offset: -1, count: colCount	}; 
			$.post('/index.php', data, function(data) {
				appendLeftColumn( data, true, colCount );
			});
		}
		return false;
	});
	
});




