var calJson = [{
  'id': 1,
  'title': 'Midsummers Night Dream',
  'date': new Date()
  },
  {
  'id': 2,
  'title': 'Childrens Acting Workshop',
  'date': getFutureDate(3)
  },
  {
  'id': 3,
  'title': 'एक हिंदी नाटक',
  'date': getPastDate(5)
  },
  {
  'id': 4,
  'title': 'Workshop Series by Jehan Maneckshaw',
  'start': getPastDate(8),
  'end': getPastDate(4),
  },
  {
  'id': 5,
  'title': 'Lorem Ipsum Dolor Amet',
  'date': getFutureDate(5)
  }
  ]

function getFutureDate(no) {
  var now = new Date();
  now.setDate(now.getDate() + no);
//  console.log(now.getDate());
  return now;
  }

function getPastDate(no) {
  var now = new Date();
  now.setDate(now.getDate() - no);
  return now;
  }

$(document).ready(function() { 
//  $('#mycarousel').jcarousel();
//  initMap(); 
  //Behaviour of top drop-down dialog
  //TODO: remove global var currentlyDisplayedDialog
  currentlyDisplayedDialog = '';
  $('.dialogLink').live("click", function() {
    if (currentlyDisplayedDialog != '') {
      $('#' + currentlyDisplayedDialog).slideUp();
      }
    var target = $(this).attr("data-target");
    var targetSelector = '#' + target;
    var t = $(targetSelector);
    if (target == currentlyDisplayedDialog) {
      t.slideUp();
      currentlyDisplayedDialog = '';
      return true;
      }
    //Adds header elem from title attr and adds close button.
    if (t.children(".dialogTitle").length === 0) {
      var titleElem = $('<div />');
      titleElem.addClass('dialogTitle');
      titleElem.text(t.attr("title"));
      var closeBtn = $('<div />');
      closeBtn.addClass('closeBtn');
      closeBtn.attr("data-target", target);
      closeBtn.text("X");
      closeBtn.click(function() {
        var tempTarg = $(this).attr("data-target");
        $('#' + tempTarg).slideUp();
        currentlyDisplayedDialog = '';
        });
      titleElem.append(closeBtn);
      t.prepend(titleElem);
      }
    t.slideDown();
    currentlyDisplayedDialog = target;
    return true;
    });

  //top button hover states.
  //TODO use addClass / removeClass
  addTopHovers();
  $('.toplink').hover(function() {
    $(this).css({'font-weight': 'bold', 'color': '#fff'});
    }, function() {
    $(this).css({'font-weight': 'normal', 'color': '#eee'});
    });

  //center buttons hover states
  $('.centerBtn').hover(function() {
    $(this).addClass('centerBtnHover');
    }, function() {
    $(this).removeClass('centerBtnHover');
    });  

  //Handles center tabs: displays the div specified in data-target and if data-source is set, loads data from url specified.
  //TODO: remove use of global currentlyDisplayedTab
  currentlyDisplayedTab = '';
  $('.tabBtn').live("click", function() {
    var that = $(this);
    var target = $(this).attr('data-target');
    var tElem = $('#' + target);
    if (currentlyDisplayedTab == target) {
      return true;
      } else if (currentlyDisplayedTab == '') {
      tElem.show();
      currentlyDisplayedTab = target;
      } else {
      var cElem = $('#' + currentlyDisplayedTab);
      cElem.hide();
      tElem.show();
      $('.tabBtn').filter("div[data-target=" + currentlyDisplayedTab + "]").removeClass("centerBtnSelected");
      currentlyDisplayedTab = target;
      if (that.attr('data-source')) {
        if (!tElem.attr("data-loaded")) {
          var dataSource = that.attr('data-source');
          tElem.load(dataSource);
          tElem.attr("data-loaded", "true");
          }
        }
      }
      that.addClass('centerBtnSelected');
      return true;
    });

  //toggle function for left menu collapsibles  
  $('.leftTab').toggle(function() {
    $(this).next().slideDown();
//    $(this).css('text-decoration', 'underline');
    }, function() {
    $(this).next().slideUp();
//    $(this).css('text-decoration', 'none');
    });



/*
  isAnimating = [];
  $('.basicLink').each(function() {
    var innerText = $(this).children('.basicLinkInner').text();
    $(this).attr('data-id', innerText);
    isAnimating[innerText] = false;
    });
  $('.basicLink').hover(function() {
    var id = $(this).attr('data-id');
    isAnimating[id] = true;
    $(this).children('.basicLinkInner').animate({'right': '150px'}, 500, function() {
      isAnimating[id] = false;
      });
    }, function() {
    $(this).children('.basicLinkInner').stop();
    $(this).children('.basicLinkInner').css("right", "0px");
    });
*/
/*
  $('#basicLinks').mouseout(function() {
    $('.basicLinkInner').stop();
    $('.basicLinkInner').each(function() {
      var currRight = $(this).css("right");
      if (currRight != '0px') {
        $(this).animate({'right': '0px'}, 250);
        }
      });
    });
*/
/*.mouseout(function() {
    $(this).stop();
    $(this).children('.basicLinkInner').animate({'right': '0px'}, 250);
    });*/

  //silly function to add a 'See All' to the end of the left tab contents, might be evil.
  $('.leftTabContent').each(function() {
    var e = $('<div />');
    e.addClass('seeAll');
    e.text('See All');
    $(this).append(e);
    });


  //also a bit silly, simulates a click on the calendar button to make it the default displayed.
  $('#calendarBtn').click();
  $('#tabCalendar').fullCalendar({
    'events': calJson
    });
  $('#newsBtn').click();

  //onclick handler to close admin message box
  $('#adminMessageBoxClose').click(function() {
    $('#adminMessageBox').slideUp();
    });

  $('#loginSubmit').click(function() {
    $('#loginDialog').slideUp();
    $('#adminMessage').slideUp(500, function() {
      $(this).html("You are now logged in.");
      $(this).show()
      $('#adminMessageBox').slideDown();
      var html = getLoggedInHtml();
      $('#loginRegister').html(html);
      addTopHovers();
      addLoggedInClickHandlers();
      $('#profileButton').click();
      });
    });
  });

function addLoggedInClickHandlers() {
  

  }

function getLoggedInHtml() {
  var html = '';
  html += '<div id="loggedInButtons">';
  html += '<div class="topBtn loggedIn tabBtn" data-target="tabProfile" data-source="/x0profile" id="profileButton" data-target="">Profile &nbsp;</div>';
  html += '<div class="topBtn loggedIn" id="messagesButton" data-target=""> &nbsp;Messages &nbsp;</div>';
  html += '<div class="topBtn loggedIn" id="connectionsButton" data-target=""> &nbsp;Connections</div>';
  html += '</div>';
  return html;
  }

function addTopHovers() {
  $('.topBtn').hover(function() {
    $(this).css("color", "#FCB281");
    }, function() {
    $(this).css("color", "#fff");
    });
  }

function initCarousel() {
  $('#mycarousel').jcarousel({start: 3});
  }

function initMap() {
  var latlng = new google.maps.LatLng(19.13307, 72.91374);
  var myOptions = {
    zoom: 11,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map"), myOptions);
  var lonlat = [];
  lonlat[0] = new google.maps.LatLng(19.063152313232, 72.829391479492);
  lonlat[1] = new google.maps.LatLng(19.106038, 72.828798);
  for (l in lonlat) {
    if (lonlat.hasOwnProperty(l)) {
      var m = new google.maps.Marker(lonlat[l]);
      map.addOverlay(m);
/*      google.maps.Event.addListener(m, 'click', function() {
        m.openInfoWindowHtml("Details of the event would go here ... ");
        }); */
      }
    }
  }
