$(document).ready(function() {
	carousel_init();
});

var max_length;
var max_diff = 155;
var curr_div = 2;
var in_animation_hover = false;
htmn2 = '';
function carousel_init()
{
	htmn = '';
	htmnl = '';
	htmnf = '';
	j = 0;
	for (i in mycarousel_itemList) {
		itmie = mycarousel_itemList[i];
		htmnn = '';
		htmnn += '<li>';
    cclass = '';
    if (itmie.highlight == 1) {
      cclass = 'div-carousal-item-highlight';
    }
    htmnn += '<div class="div-carousal-item '+cclass+'">';
    htmnn += '<div class="div-item-content">';
    htmnn += '<div class="div-item-title">';
    htmnn += '<a href="'+itmie.url+'">'+itmie.title+'</a>';
    htmnn += '</div>';
    htmnn += '<div class="div-item-date">';
    /*htmnn += itmie.date+' | '+itmie.timing;*/
	htmnn += itmie.timing;
    htmnn += '</div>';
    htmnn += '<div class="div-item-description">';
    htmnn += itmie.content;
    htmnn += '</div>';
    htmnn += '<div class="div-item-contact">';
    htmnn += 'Contact: <a href="mailto:'+itmie.contact+'">'+itmie.contact+'</a>';
    htmnn += '</div>';
    htmnn += '</div>';
    htmnn += '<div class="div-item-image">';
    htmnn += '<img src="'+itmie.image+'" />';
    htmnn += '</div>';
    htmnn += '</div>';
		htmn += htmnn;
		if (j == 0 || j == 2 || j == 3 || j == 4 || j == 1) {
			htmnl += htmnn;
		}
		if (j == (mycarousel_itemList.length - 1) || j == (mycarousel_itemList.length - 2) || j == (mycarousel_itemList.length - 3) || j == (mycarousel_itemList.length - 4) || j == (mycarousel_itemList.length - 5)) {
			htmnf += htmnn;
		}
		j++;
	}
	hgt = mycarousel_itemList.length * max_diff;
	nytop = -80 + (5 * 155) ;
	new_html = '<div class="top-nav"><div style="background:url(\''+base_path+'images/nav_top.png\') -2px 0 no-repeat;"></div></div><div class="event-carousal-inner-container" style="top: -'+nytop+'px;"><ul>'+htmnf+htmn+htmnl+'</ul></div><div class="bottom-nav"><div style="background:url(\''+base_path+'images/nav_bottom.png\') no-repeat"></div></div>';
   
	$('#mycarousel').html(new_html);
	max_length = hgt;
	hgt = hgt + (4 * 155);
  $('.top-nav').hover(show_nav, hide_nav);
  $('.top-nav').bind('click', start_nav_up);
  $('.bottom-nav').hover(show_nav, hide_nav);
  $('.bottom-nav').bind('click', start_nav_down);
	
}

function show_nav()
{
  $(this).children('div').fadeIn(250);
}

function hide_nav()
{
  $(this).children('div').fadeOut(100);
}

var stop_animation = false;

function start_nav_up()
{
  if (in_animation_hover) {
    return;
  }
  ctop = $('.event-carousal-inner-container').css('top');
  hgt = $('.event-carousal-inner-container').height();
  ctop = ctop.replace('px', '');
  new_ctop = parseInt(ctop) + 155;
	curr_div--;
  $('.event-carousal-inner-container').animate({top : new_ctop+'px'}, 1000, function () {
		if (curr_div <= 0) {
			curr_div = mycarousel_itemList.length;
			nytop = (mycarousel_itemList.length + 3) * 155 - 80;
			$('.event-carousal-inner-container').css({top : '-'+nytop+'px'});
		}
    in_animation_hover = false;
  })
}

function start_nav_down()
{
  if (in_animation_hover) {
    return;
  }
	in_animation_hover = true;
  ctop = $('.event-carousal-inner-container').css('top');
  hgt = $('.event-carousal-inner-container').height();
  ctop = ctop.replace('px', '');
  new_ctop = parseInt(ctop) - 155;
	curr_div++;
  $('.event-carousal-inner-container').animate({top : new_ctop+'px'}, 1000, function () {
		if (curr_div > mycarousel_itemList.length) {
			curr_div = 1;
			nytop = -80 + (4 * 155);
			$('.event-carousal-inner-container').css({top : '-'+nytop+'px'});
		}
    in_animation_hover = false;
  })
}

function stop_nav()
{
  if (!stop_animation) {
    stop_animation = true;
  } else {
    stop_animation = false;
  }
}