
$(document).ready(function(){

  var curday = $('#selecteddate').attr('rel');

  $('div.row').each(function(){
    $(this).children('.dayrow:not(.first)').hide();
    $(this).children('.dayrow:not(.'+curday+')').hide();
  });

  hideCinemasWithNoTimes(curday);
  showCinemasWithTimes(curday);

  $('.dayrow').removeClass('hidden');

  //if the calendar icon is clicked then show the list of days
  $('#dayall').click(function(){
     if($('#showdays').is(':hidden')) {
       $('#showdays').css('z-index',6);
       $('#showdays').slideDown();
       $('#showdays').css('position','absolute');
       $('#showdays').children().css('z-index',5);
     }else{
      $('#showdays').slideUp();
     }
    return false;
  });

  $('#showdays').click(function(e) {
    e.stopPropagation();
  });

  $(document).click(function() {
    $('#showdays:visible').slideUp();
  });


  //Selecting a day from the list
  $('#showdays li a').click(function(){
    // change the selecteddate
    $('#selecteddate').html($(this).html());
    //close the showdays list
    //$('#showdays').slideToggle();
    $('#showdays').hide();
    //need to show the film times for this day
    //date is the a rel attribute
    var dateid = $(this).attr('rel');
    $('#selecteddate').attr('rel',dateid);
    //hide all dayrows that do not have this class
    //show those that do have this class
    //$('.'+dateid).slideDown().siblings(':not(.rowheader)').slideUp();
    $('.dayrow:not(.rowheader)').hide();
    $('.'+dateid).show()

    hideCinemasWithNoTimes(dateid);
    showCinemasWithTimes(dateid);

    return false;
  });

  // Previous or next button clicked
  $('#daynext').click(function(){

    //first what is the current day?
    var curclass = $('div.row').children('.dayrow:visible').attr('class');
    curclass = curclass.replace('dayrow','');
    curclass = curclass.replace('first','');
    curclass = curclass.replace(' ','');

    // curclass= $('#selecteddate').attr('rel');

    //FIRST MAKE SURE THERE IS A NEXT SIBLING of class dayrow
    var maxdateid = $('#showdays li:last a').attr('rel') ;

    //alert(maxdateid + ' - ' + curclass);
    if (maxdateid>curclass) {
      //ok we have a max date
      if  ($('.'+curclass).next().html()) {
        var nextclass = $('.'+curclass).next().attr('class');
           nextclass = nextclass.replace('dayrow','');
           nextclass = nextclass.replace('first','');
           nextclass = nextclass.replace(' ','');

        var nextclass = $('ul#showdays li a[rel=\''+trim(curclass)+'\']').parent().next().find('a').attr('rel');

        //change the selecteddate item
        var newday = $('a#li-'+nextclass).html();
        $('#selecteddate').html(newday);
        $('#selecteddate').attr('rel',nextclass);

        //$('.'+curclass).slideUp().next().slideDown();
        $('.'+curclass).hide();
        $('.'+nextclass).show();
        hideCinemasWithNoTimes(nextclass);
        showCinemasWithTimes(nextclass);

        // NEW SECTION
      } else if ( $('.'+curclass).parent('.row').next().children('.dayrow').html() ) {

       var nextclass = $('.'+curclass).parent('.row').next().children('.dayrow').attr('class');
           nextclass = nextclass.replace('dayrow','');
           nextclass = nextclass.replace('first','');
           nextclass = nextclass.replace(' ','');

        var nextclass = $('ul#showdays li a[rel=\''+trim(curclass)+'\']').parent().next().find('a').attr('rel');

        //change the selecteddate item
        var newday = $('a#li-'+nextclass).html();
        $('#selecteddate').html(newday);
        $('#selecteddate').attr('rel',nextclass);

        //$('.'+curclass).slideUp().next().slideDown();
        $('.'+curclass).hide();
        $('.'+nextclass).show();
        hideCinemasWithNoTimes(nextclass);
        showCinemasWithTimes(nextclass);


      } else {
        var sErr = 'Sorry that\'s as far as you can go.';
         $.prompt(sErr,{ prefix: 'mh',
                      show: 'fadeIn',
                      promptspeed: 'fast' ,
                      bgiframe:'true' });
      }

    } else {
      //alert('Sorry that\'s as far as you can go.');
      //replace old school alert with nice fancybox?
      var sErr = 'Sorry that\'s as far as you can go.';
       $.prompt(sErr,{ prefix: 'mh',
                    show: 'fadeIn',
                    promptspeed: 'fast' ,
                    bgiframe:'true' });
    }

    return false;
  });


  $('#dayprev').click(function(){

    //first what is the current day?
    var curclass = $('div.row').children('.dayrow:visible').attr('class');
    curclass = curclass.replace('dayrow ','');
    curclass = curclass.replace(' first','');
    curclass = curclass.replace(' ','');

    // curclass = $('#selecteddate').attr('rel');
    // alert(curclass)
    //FIRST MAKE SURE THERE IS A PREV SIBLING of class dayrow

    var mindateid = $('#showdays li:first a').attr('rel') ;

    curclass= trim(curclass);
    mindateid= trim(mindateid);

    if (mindateid<curclass ) {
      //ok we have a min date

      var prevclass = $('ul#showdays li a[rel=\''+trim(curclass)+'\']').parent().prev().find('a').attr('rel');

      //change the selecteddate item
      var newday = $('a#li-'+prevclass).html();

      $('#selecteddate').html(newday);
      $('#selecteddate').attr('rel',prevclass);

       //$('.'+curclass).slideUp().prev().slideDown();
      $('.'+curclass).hide();
      $('.'+prevclass).show();

      hideCinemasWithNoTimes(prevclass);
      showCinemasWithTimes(prevclass);

    } else {
      //alert('Sorry that\'s as far as you can go.');
      //replace old school alert with nice fancybox?
      var sErr = 'Sorry that\'s as far as you can go.';
       $.prompt(sErr,{ prefix: 'mh',
                    show: 'fadeIn',
                    promptspeed: 'fast',
                    bgiframe:true });


    }

    return false;
  });

  $('#mhcontainer').livequery(function(){
    $(this).bgiframe();
  });



/*
  // Make the BBFC info open up on mouse over.
  $('p.bbfcinfo').mouseover(function(){
     $(this).css({position:'absolute',top:0,right:'6px'});
     $(this).animate({height:'65px',opacity:0.95,fontWeight:'900',backgroundColor:'#000000'},1000)
                                     }
                            )
                  .mouseout(function(){
     $(this).animate({height:'30px',opacity:1,fontWeight:'100',backgroundColor:'#333333',queue:true},1000,'linear',function(){$(this).css({background:'none'});} ) ;


                                      }
                            );
  */

});


function JumpToADate(sDate) {

  //change the selecteddate item
  var newday = $('a#li-'+sDate).html();
  $('#selecteddate').html(newday);
  $('#selecteddate').attr('rel',sDate);

  //$('.'+curclass).slideUp().next().slideDown();
  $('div.dayrow').hide();
  $('.'+sDate).show();
  hideCinemasWithNoTimes(sDate);
  showCinemasWithTimes(sDate);

}

function hideCinemasWithNoTimes(sClass) {
  $("#showtimeslist div.row").each(function(){
    if($(this).find("div."+sClass).length == 0) {
      $(this).slideUp();
    }
  })
}
function showCinemasWithTimes(sClass) {
  $("#showtimeslist div.row").each(function(){
    if($(this).find("div."+sClass).length > 0) {
      $(this).slideDown();
    }
  })
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
