$(document).ready(function () {
  remove_focus_borders();
  setup_colors();
  display_colors();
});

$(window).scroll(function () { 
  display_colors();
});

$(window).resize(function () { 
  display_colors();
});

// ----------------------------------------
// remove focus borders in Firefox
function remove_focus_borders() {
	$("a").each( function() {
		$(this).bind("focus", function() { $(this).blur(); });
	});
}

// ----------------------------------------
// Search Bar

 $(document).ready(function(){
 $("#edit-search-theme-form-1").val("Search...");

 $("#edit-search-theme-form-1").blur(function(){
 if(!$(this).val()) $(this).val("Search...");
 });
 $("#edit-search-theme-form-1").focus(function(){
 if($(this).val() == "Search...") $(this).val("");
 });
 }); 


// ----------------------------------------
// Playlist Menu


function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}
$(document).ready(function() {initMenus();});


// ----------------------------------------
// Adapted from the work of Jerome Rigaud for http://freetoair.org

function setup_colors() {
  $(".postWrapper div.background").css({
  'position': 'fixed',
  'top': '0', 
  'left': '0',
  'width': '100%',
  'height': '100%',
  'z-index': '-1'
  });
}

// ----------------------------------------
function display_colors() {
  var sectionArray = $.makeArray($(".postWrapper"));
  
  var window_scroll_top = $(window).scrollTop();
  var window_center = $(window).height()/2;
  window_center = 100;
  
  // sort sections by their relative distance to the center of the window
  sectionArray.sort( function(a, b) { 
    return getDiff(a, window_scroll_top, window_center) > getDiff(b, window_scroll_top, window_center) ? 1 : -1; 
  });
  
  $(".postWrapper div.background:visible").hide();
  $(sectionArray[0]).children("div").show();
  
  $(".active").removeClass("active");
  $(sectionArray[0]).addClass("active");
}

// ----------------------------------------
function getDiff(item, window_scroll_top, window_center) {
  var item_viewportoffset_top = $(item).offset().top - window_scroll_top;

  var dist_of_top = Math.abs(item_viewportoffset_top - window_center);
  var dist_of_bottom = Math.abs(item_viewportoffset_top + $(item).height() - window_center);

  // return minimum of distances of top and bottom of an element
  // to center of the window
  return Math.min( dist_of_top, dist_of_bottom );
}

// ----------------------------------------
// N Glitch Infite Random Loop

var timer_is_on=0;

function timedCount(){

t=setTimeout("timedCount()",10000);

var now=new Date();
var number = now.getSeconds();
var milisecNum = number * 500;

console.log(milisecNum)
console.log(t)
  $.doTimeout( '#s1', milisecNum, function(){
        $('#n1').hide();
        $('#s1').show();
		$('#s1').cycle({
		    timeout: 1, 
    		speed:   50,
    		continuous: 1
		});
	
      	  
	  $.doTimeout( '#s1', 900, function(){				
			$('#s1').cycle('pause');
		    $('#s1').cycle(0);		
      
      var now=new Date();
	  var number = now.getSeconds();
	  var milisecNum = number * 500;
      
      console.log(milisecNum)
      
         $.doTimeout( '#s1', milisecNum, function(){		

		    $('#s1').cycle('resume');			

            $.doTimeout( '#s1', 900, function(){		

		       $('#s1').cycle('pause');			

	        });

	     });
      
      });		          
   
  });

}

    $(document).ready(function doTimer(){

	if (!timer_is_on)
	  {
	  timer_is_on=1;
	  timedCount();
	  }
	});