// JavaScript Document
function te_timeline_next_day(day) {
	    jQuery.ajax({
		    type: "post",
        url: 'wp-admin/admin-ajax.php',
//        url: "wp-content/plugins/tiny-events/tiny-events-ajax-response.php",
        data: { 
          action: 'te_timeline_next',
          _ajax_nonce: '95ddc7d620',
          day: day,
          city: jQuery("input[name='te_city']:checked").val(),
          theme: jQuery("input[name='te_theme']:checked").val() 
        },
        dataType: "json",
        beforeSend: function() {
          jQuery("#te_timeline_next span").unbind();
          jQuery("#te_timeline_previous span").unbind();
        },
        success: function(json){ //so, if data is retrieved, store it in html
          te_timeline_date = json.back_1;
          jQuery("#te_timeline_"+json.back+", "+"#te_timeline_"+json.back+"_date, #te_timeline_"+json.back+"_bottom").hide("normal",function () {jQuery(this).remove();});
          jQuery("#te_timeline_"+json.yesterday+"_date").after('<div id="te_timeline_'+json.today+'_date" class="te_day grid_1 omega"></div>');
          jQuery("#te_timeline_"+json.yesterday+"_bottom").after('<div id="te_timeline_'+json.today+'_bottom" class="te_bottom rounded grid_1 omega"></div>');
           
          jQuery("#te_timeline_"+json.today+"_date").hide();
          jQuery("#te_timeline_"+json.today+"_bottom").hide();
          jQuery("#te_timeline_"+json.yesterday).after('<div id="te_timeline_'+json.today+'" class="te_day grid_1 omega"></div>');
          jQuery("#te_timeline_"+json.today).hide();
          jQuery("#te_timeline_"+json.back_1+"_date").addClass('alpha');
          jQuery("#te_timeline_"+json.back_1+"_bottom").addClass('alpha');
          jQuery("#te_timeline_"+json.back_1).addClass('alpha');
          jQuery("#te_timeline_"+json.today+"_date").html(json.html_date);
          jQuery("#te_timeline_"+json.today+"_bottom").html(json.html_bottom);
          jQuery("#te_timeline_"+json.today).html(json.html);
          jQuery("#te_timeline_"+json.yesterday+"_date").removeClass('omega');
          jQuery("#te_timeline_"+json.yesterday+"_bottom").removeClass('omega');
          jQuery("#te_timeline_"+json.yesterday).removeClass('omega');
          jQuery("#te_timeline_"+json.today+"_date").show("normal"); //animation
          jQuery("#te_timeline_"+json.today).show("normal"); //animation
          jQuery("#te_timeline_"+json.today+"_bottom").show("normal"); //animation
          jQuery("#te_timeline_next span").bind('click',function(){te_timeline_next_day(json.forward)});
          if (json.back != '')
            jQuery("#te_timeline_previous span").bind('click',function(){te_timeline_previous_day(json.back)});
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          alert(textStatus+': '+errorThrown);
        }
	    }); //close jQuery.ajax(
//	    return false;
}

function te_timeline_previous_day(day) {
	    jQuery.ajax({
		    type: "post",
        url: 'wp-admin/admin-ajax.php',
//        url: "wp-content/plugins/tiny-events/tiny-events-ajax-response.php",
        data: { 
          action: 'te_timeline_previous',
          _ajax_nonce: '95ddc7d620',
          day: day, 
          city: jQuery("input[name='te_city']:checked").val(),
          theme: jQuery("input[name='te_theme']:checked").val() 
        },
        dataType: "json",
        beforeSend: function() {
          jQuery("#te_timeline_next span").unbind();
          jQuery("#te_timeline_previous span").unbind();
        },
        success: function(json){ //so, if data is retrieved, store it in html
          //alert(json.yesterday);
          te_timeline_date = json.today;
          jQuery("#te_timeline_"+json.forward+", #te_timeline_"+json.forward+"_date, #te_timeline_"+json.forward+"_bottom").hide("normal",function () {jQuery(this).remove();});
          jQuery("#te_timeline_"+json.forward_1+", #te_timeline_"+json.forward_1+"_date, #te_timeline_"+json.forward_1+"_bottom").addClass('omega');
          jQuery("#te_timeline_"+json.yesterday+"_date").before('<div id="te_timeline_'+json.today+'_date" class="te_day grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.yesterday+"_bottom").before('<div id="te_timeline_'+json.today+'_bottom" class="te_bottom rounded grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.yesterday).before('<div id="te_timeline_'+json.today+'" class="te_day grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.today+", #te_timeline_"+json.today+"_date").hide();
          jQuery("#te_timeline_"+json.today+", #te_timeline_"+json.today+"_bottom").hide();
          jQuery("#te_timeline_"+json.today+"_date").html(json.html_date);
          jQuery("#te_timeline_"+json.today).html(json.html);
          jQuery("#te_timeline_"+json.today+"_bottom").html(json.html_bottom);
          jQuery("#te_timeline_"+json.yesterday+", #te_timeline_"+json.yesterday+"_date, #te_timeline_"+json.yesterday+"_bottom").removeClass('alpha');
          jQuery("#te_timeline_"+json.today+", #te_timeline_"+json.today+"_date, #te_timeline_"+json.today+"_bottom").show("normal"); //animation
          jQuery("#te_timeline_next span").bind('click',function(){te_timeline_next_day(json.forward)});
          if (json.back != '')
            jQuery("#te_timeline_previous span").bind('click',function(){te_timeline_previous_day(json.back)});  
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          alert(textStatus+': '+errorThrown);
        }
	    }); //close jQuery.ajax(
	    return false;
}

jQuery(document).ready(function() {
		var selection = jQuery("#te_timeline_city label");
    selection.click(function(event) {
      selection.removeClass('active');
      jQuery(event.target).addClass('active');
      if(jQuery.browser.msie||jQuery.browser.opera) {
        jQuery("#"+jQuery(event.target).attr('for')).attr('checked', 'checked');
        jQuery("#"+jQuery(event.target).attr('for')).trigger('click');
      }
    });			
    jQuery("#te_timeline_city input").click(function(event) {
	    jQuery.ajax({
		    type: "post",
        url: 'wp-admin/admin-ajax.php',
        data: { 
          action: 'te_timeline_reload',
          _ajax_nonce: '95ddc7d620',
          day: te_timeline_date,
          city: jQuery("input[name='te_city']:checked").val(), 
          theme: jQuery("input[name='te_theme']:checked").val() 
        },
        dataType: "json",
        beforeSend: function() {
        },
        success: function(json){ //so, if data is retrieved, store it in html
          //alert(json.html_0);
          jQuery("#te_timeline_"+json.day_0).hide('slow');
          jQuery("#te_timeline_"+json.day_0).html(json.html_0);
          jQuery("#te_timeline_"+json.day_0).show('normal');

          jQuery("#te_timeline_"+json.day_1).hide('slow');
          jQuery("#te_timeline_"+json.day_1).html(json.html_1);
          jQuery("#te_timeline_"+json.day_1).show('normal');

          jQuery("#te_timeline_"+json.day_2).hide('slow');
          jQuery("#te_timeline_"+json.day_2).html(json.html_2);
          jQuery("#te_timeline_"+json.day_2).show('normal');

          jQuery("#te_timeline_"+json.day_3).hide('slow');
          jQuery("#te_timeline_"+json.day_3).html(json.html_3);
          jQuery("#te_timeline_"+json.day_3).show('normal');

          jQuery("#te_timeline_"+json.day_4).hide('slow');
          jQuery("#te_timeline_"+json.day_4).html(json.html_4);
          jQuery("#te_timeline_"+json.day_4).show('normal');
          
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          alert(textStatus+': '+errorThrown);
        }
	    }); //close jQuery.ajax(


    });


		var selection2 = jQuery("#te_timeline_theme label");
    selection2.click(function(event) {
      selection2.removeClass('active');
      jQuery(event.target).addClass('active');
      if(jQuery.browser.msie||jQuery.browser.opera) {
        jQuery("#"+jQuery(event.target).attr('for')).attr('checked', 'checked');
        jQuery("#"+jQuery(event.target).attr('for')).trigger('click');
      }
    });			
//    if(jQuery.browser.msie||jQuery.browser.opera) {
//      jQuery("#te_timeline_theme input").change(function(event) {
//        jQuery("#"+jQuery(event.target).attr('for')).trigger('click');
//      });
//    }
    jQuery("#te_timeline_theme input").click(function(event) {
	    jQuery.ajax({
		    type: "post",
        url: 'wp-admin/admin-ajax.php',
        data: { 
          action: 'te_timeline_reload',
          _ajax_nonce: '95ddc7d620',
          day: te_timeline_date,
          city: jQuery("input[name='te_city']:checked").val(), 
          theme: jQuery("input[name='te_theme']:checked").val() 
        },
        dataType: "json",
        beforeSend: function() {
        },
        success: function(json){ //so, if data is retrieved, store it in html
          //alert(json.html_0);
          jQuery("#te_timeline_"+json.day_0).hide('slow');
          jQuery("#te_timeline_"+json.day_0).html(json.html_0);
          jQuery("#te_timeline_"+json.day_0).show('normal');

          jQuery("#te_timeline_"+json.day_1).hide('slow');
          jQuery("#te_timeline_"+json.day_1).html(json.html_1);
          jQuery("#te_timeline_"+json.day_1).show('normal');

          jQuery("#te_timeline_"+json.day_2).hide('slow');
          jQuery("#te_timeline_"+json.day_2).html(json.html_2);
          jQuery("#te_timeline_"+json.day_2).show('normal');

          jQuery("#te_timeline_"+json.day_3).hide('slow');
          jQuery("#te_timeline_"+json.day_3).html(json.html_3);
          jQuery("#te_timeline_"+json.day_3).show('normal');

          jQuery("#te_timeline_"+json.day_4).hide('slow');
          jQuery("#te_timeline_"+json.day_4).html(json.html_4);
          jQuery("#te_timeline_"+json.day_4).show('normal');
          
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          alert(textStatus+': '+errorThrown);
        }
	    }); //close jQuery.ajax(
    });


jQuery("#te_timeline_next span").bind('click',function(){te_timeline_next_day('0000-00-00')});
jQuery("#te_timeline_next span").click(function (event) { event.preventDefault(); });
jQuery("#te_timeline_previous span").click(function (event) { event.preventDefault(); });

jQuery('document').ready(function() {
  if(!jQuery.browser.msie) {
    jQuery('#te_timeline_body').jScrollPane()
  }
});

jQuery('#te_date').datepicker(jQuery.datepicker.regional['lt']);

jQuery('#te_date').datepicker('option', 'minDate', -0);
jQuery('#te_date').datepicker('option', 'dateFormat', 'yy-mm-dd');
jQuery('#te_date').datepicker('option', 'onClose', function(dateText, inst) {
     if (dateText!=te_timeline_date) {
      te_timeline_date=dateText;
	    jQuery.ajax({
		    type: "post",
        url: 'wp-admin/admin-ajax.php',
        data: { 
          action: 'te_timeline_reload2',
          _ajax_nonce: '95ddc7d620',
          day: te_timeline_date,
          city: jQuery("input[name='te_city']:checked").val(), 
          theme: jQuery("input[name='te_theme']:checked").val() 
        },
        dataType: "json",
        beforeSend: function() {
          jQuery("#te_timeline_next span").unbind();
          jQuery("#te_timeline_previous span").unbind();
        },
        success: function(json){ //so, if data is retrieved, store it in html
          //alert(json.html_0);
          jQuery('.te_day, .te_bottom').hide('slow');
          jQuery('.te_day, .te_bottom').remove();
          
          
          jQuery("#te_timeline_body_date").append('<div id="te_timeline_'+json.day_0+'_date" class="te_day grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.day_0+"_date").hide();
          jQuery("#te_timeline_body_date").append('<div id="te_timeline_'+json.day_1+'_date" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_1+"_date").hide();
          jQuery("#te_timeline_body_date").append('<div id="te_timeline_'+json.day_2+'_date" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_2+"_date").hide();
          jQuery("#te_timeline_body_date").append('<div id="te_timeline_'+json.day_3+'_date" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_3+"_date").hide();
          jQuery("#te_timeline_body_date").append('<div id="te_timeline_'+json.day_4+'_date" class="te_day grid_1 omega"></div>');
          jQuery("#te_timeline_"+json.day_4+"_date").hide();

          jQuery("#te_timeline_"+json.day_0+"_date").html(json.html_date_0);
          jQuery("#te_timeline_"+json.day_1+"_date").html(json.html_date_1);
          jQuery("#te_timeline_"+json.day_2+"_date").html(json.html_date_2);
          jQuery("#te_timeline_"+json.day_3+"_date").html(json.html_date_3);
          jQuery("#te_timeline_"+json.day_4+"_date").html(json.html_date_4);

          jQuery("#te_timeline_"+json.day_0+"_date").show('normal');
          jQuery("#te_timeline_"+json.day_1+"_date").show('normal');
          jQuery("#te_timeline_"+json.day_2+"_date").show('normal');
          jQuery("#te_timeline_"+json.day_3+"_date").show('normal');
          jQuery("#te_timeline_"+json.day_4+"_date").show('normal');

          
          jQuery("#te_timeline_body").append('<div id="te_timeline_'+json.day_0+'" class="te_day grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.day_0).hide();
          jQuery("#te_timeline_"+json.day_0).html(json.html_0);
          jQuery("#te_timeline_"+json.day_0).show('normal');

          jQuery("#te_timeline_body").append('<div id="te_timeline_'+json.day_1+'" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_1).hide();
          jQuery("#te_timeline_"+json.day_1).html(json.html_1);
          jQuery("#te_timeline_"+json.day_1).show('normal');

          jQuery("#te_timeline_body").append('<div id="te_timeline_'+json.day_2+'" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_2).hide();
          jQuery("#te_timeline_"+json.day_2).html(json.html_2);
          jQuery("#te_timeline_"+json.day_2).show('normal');

          jQuery("#te_timeline_body").append('<div id="te_timeline_'+json.day_3+'" class="te_day grid_1"></div>');
          jQuery("#te_timeline_"+json.day_3).hide();
          jQuery("#te_timeline_"+json.day_3).html(json.html_3);
          jQuery("#te_timeline_"+json.day_3).show('normal');

          jQuery("#te_timeline_body").append('<div id="te_timeline_'+json.day_4+'" class="te_day grid_1 omega"></div>');
          jQuery("#te_timeline_"+json.day_4).hide();
          jQuery("#te_timeline_"+json.day_4).html(json.html_4);
          jQuery("#te_timeline_"+json.day_4).show('normal');

          jQuery("#te_timeline_body_bottom").append('<div id="te_timeline_'+json.day_0+'_bottom" class="te_bottom rounded grid_1 alpha"></div>');
          jQuery("#te_timeline_"+json.day_0+"_bottom").hide();
          jQuery("#te_timeline_body_bottom").append('<div id="te_timeline_'+json.day_1+'_bottom" class="te_bottom rounded grid_1"></div>');
          jQuery("#te_timeline_"+json.day_1+"_bottom").hide();
          jQuery("#te_timeline_body_bottom").append('<div id="te_timeline_'+json.day_2+'_bottom" class="te_bottom rounded grid_1"></div>');
          jQuery("#te_timeline_"+json.day_2+"_bottom").hide();
          jQuery("#te_timeline_body_bottom").append('<div id="te_timeline_'+json.day_3+'_bottom" class="te_bottom rounded grid_1"></div>');
          jQuery("#te_timeline_"+json.day_3+"_bottom").hide();
          jQuery("#te_timeline_body_bottom").append('<div id="te_timeline_'+json.day_4+'_bottom" class="te_bottom rounded grid_1 omega"></div>');
          jQuery("#te_timeline_"+json.day_4+"_bottom").hide();

          jQuery("#te_timeline_"+json.day_0+"_bottom").html(json.html_bottom_0);
          jQuery("#te_timeline_"+json.day_1+"_bottom").html(json.html_bottom_1);
          jQuery("#te_timeline_"+json.day_2+"_bottom").html(json.html_bottom_2);
          jQuery("#te_timeline_"+json.day_3+"_bottom").html(json.html_bottom_3);
          jQuery("#te_timeline_"+json.day_4+"_bottom").html(json.html_bottom_4);

          jQuery("#te_timeline_"+json.day_0+"_bottom").show('normal');
          jQuery("#te_timeline_"+json.day_1+"_bottom").show('normal');
          jQuery("#te_timeline_"+json.day_2+"_bottom").show('normal');
          jQuery("#te_timeline_"+json.day_3+"_bottom").show('normal');
          jQuery("#te_timeline_"+json.day_4+"_bottom").show('normal');

          jQuery("#te_timeline_next span").bind('click',function(){te_timeline_next_day(json.forward)});
          if (json.back != '')
            jQuery("#te_timeline_previous span").bind('click',function(){te_timeline_previous_day(json.back)});

        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          alert(textStatus+': '+errorThrown);
        }
	    }); //close jQuery.ajax(
       
     }
   });
});
