
var busy = false;
var feedBusy = false;
// Clock js
/*
 *  Main function to set the clock times
 */
(function() {
  // Initialise the locale-enabled clocks
  initInternationalClocks();
  // Initialise any local time clocks
  initLocalClocks();
  // Start the seconds container moving
  moveSecondHands();
  // Set the intial minute hand container transition, and then each subsequent step
  setUpMinuteHands();

  DisplayCurrentTime();
})();

/*
 *  Set up an entry for each locale of clock we want to use
 */
 function getTimes() {
 
  var now = new Date();
  // Set the time manually for each of the clock types we're using
  var times = [
    {
      jsclass: 'js-hawaii',
      jstime: '-10'
    },
    {
      jsclass: 'js-alaska',
      jstime: '-8'
    },
    {
      jsclass: 'js-pacific',
      jstime: '-7'
    },
    {
      jsclass: 'js-central',
      jstime: '-5'
    },
    {
      jsclass: 'js-eastern',
      jstime: '-4'
    },
    {
      jsclass: 'js-mountain',
      jstime: '-6'
    }
  ];
  return times;
}

function calcTime(offset) {

    d = new Date();

    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    return new Date(utc + (3600000*offset));

}
function DisplayCurrentTime() {
	var times = getTimes();
	setInterval(function(){
	  	for (i = 0; i < times.length; ++i) {
		    var hours = calcTime(times[i].jstime).getHours();
		    var minutes = calcTime(times[i].jstime).getMinutes();
		    var seconds = calcTime(times[i].jstime).getSeconds();

		    var am_pm = hours >= 12 ? "PM" : "AM";
		    
		    hours = hours > 12 ? hours - 12 : hours;
		    hours = hours < 10 ? "0" + hours : hours;

		    minutes = minutes < 10 ? "0" + minutes : minutes;
		    
		    seconds = seconds < 10 ? "0" + seconds : seconds;
		    
		    time = "<span>"+hours +"</span><span>"+  minutes +"</span><span>"+  seconds +"</span><span>"+  am_pm+"</span>";
		    
		    var lblTime = document.getElementById(times[i].jsclass);
		    
		    lblTime.innerHTML = time;
		}
	},1000);
 };
/*
 * Set up the clocks that use moment.js
 */
function initInternationalClocks() {
  // Initialise the clock settings and the five times
  var times = getTimes();
  for (i = 0; i < times.length; ++i) {
    var hours = calcTime(times[i].jstime).getHours();
    var minutes = calcTime(times[i].jstime).getMinutes();
    var seconds = calcTime(times[i].jstime).getSeconds();

    var degrees = [
      {
        hand: 'hours',
        degree: (hours * 30) + (minutes / 2)
      },
      {
        hand: 'minutes',
        degree: (minutes * 6)
      },
      {
        hand: 'seconds',
        degree: (seconds * 6)
      }
    ];
    for (var j = 0; j < degrees.length; j++) {
      var elements = document.querySelectorAll('.active .' + times[i].jsclass + ' .' + degrees[j].hand);
      
      for (var k = 0; k < elements.length; k++) {
        	elements[k].style.webkitTransform = 'rotateZ('+ degrees[j].degree +'deg)';
          elements[k].style.transform = 'rotateZ('+ degrees[j].degree +'deg)';
          // If this is a minute hand, note the seconds position (to calculate minute position later)
          if (degrees[j].hand === 'minutes') {
            elements[k].parentNode.setAttribute('data-second-angle', degrees[j + 1].degree);
          }
      }
    }
  }
  // Add a class to the clock container to show it
  var elements = document.querySelectorAll('.clock');
  for (var l = 0; l < elements.length; l++) {
    elements[l].className = elements[l].className + ' show';
  }
}

/*
 * Starts any clocks using the user's local time
 */
function initLocalClocks() {
  // Get the local time using JS
  var date = new Date;
  var seconds = date.getSeconds();
  var minutes = date.getMinutes();
  var hours = date.getHours();

  // Create an object with each hand and it's angle in degrees
  var hands = [
    {
      hand: 'hours',
      angle: (hours * 30) + (minutes / 2)
    },
    {
      hand: 'minutes',
      angle: (minutes * 6)
    },
    {
      hand: 'seconds',
      angle: (seconds * 6)
    }
  ];
  // Loop through each of these hands to set their angle
  for (var j = 0; j < hands.length; j++) {
    var elements = document.querySelectorAll('.local .' + hands[j].hand);
    for (var k = 0; k < elements.length; k++) {
        elements[k].style.transform = 'rotateZ('+ hands[j].angle +'deg)';
        // If this is a minute hand, note the seconds position (to calculate minute position later)
        if (hands[j].hand === 'minutes') {
          elements[k].parentNode.setAttribute('data-second-angle', hands[j + 1].angle);
        }
    }
  }
}

/*
 * Move the second containers
 */
function moveSecondHands() {
  var containers = document.querySelectorAll('.bounce .seconds-container');
  setInterval(function() {
    for (var i = 0; i < containers.length; i++) {
      if (containers[i].angle === undefined) {
        containers[i].angle = 6;
      } else {
        containers[i].angle += 6;
      }
      containers[i].style.webkitTransform = 'rotateZ('+ containers[i].angle +'deg)';
      containers[i].style.transform = 'rotateZ('+ containers[i].angle +'deg)';
    }
  }, 1000);
  for (var i = 0; i < containers.length; i++) {
    // Add in a little delay to make them feel more natural
    var randomOffset = Math.floor(Math.random() * (100 - 10 + 1)) + 10;
    containers[i].style.transitionDelay = '0.0'+ randomOffset +'s';
  }
}

/*
 * Set a timeout for the first minute hand movement (less than 1 minute), then rotate it every minute after that
 */
function setUpMinuteHands() {
  // More tricky, this needs to move the minute hand when the second hand hits zero
  var containers = document.querySelectorAll('.minutes-container');
  var secondAngle = containers[containers.length - 1].getAttribute('data-second-angle');
  
  if (secondAngle > 0) {
    // Set a timeout until the end of the current minute, to move the hand
    var delay = (((360 - secondAngle) / 6) + 0.1) * 1000;
    
    setTimeout(function() {
      moveMinuteHands(containers);
    }, delay);
  }
}

/*
 * Do the first minute's rotation, then move every 60 seconds after
 */
function moveMinuteHands(containers) {
  for (var i = 0; i < containers.length; i++) {
    containers[i].style.webkitTransform = 'rotateZ(6deg)';
    containers[i].style.transform = 'rotateZ(6deg)';
  }
  // Then continue with a 60 second interval
  setInterval(function() {
    for (var i = 0; i < containers.length; i++) {
      if (containers[i].angle === undefined) {
        containers[i].angle = 12;
      } else {
        containers[i].angle += 6;
      }
      containers[i].style.webkitTransform = 'rotateZ('+ containers[i].angle +'deg)';
      containers[i].style.transform = 'rotateZ('+ containers[i].angle +'deg)';
    }
  }, 60000);
}




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


	$.ajax({
    	dataType: "json",
    	url: '?menu=home&mapData=',
    	success: function(data){

	    	for(var state in data){
	        	simplemaps_usmap_mapdata.state_specific[state] = {description: data[state], color: "#d93549"};
	      	}
	      	
	      	simplemaps_usmap.load();
    	}
  	});	
	
 });

$(document).ready(function() {
 
	$(".feed_box").scroll(function() {
		// make sure u give the container id of the data to be loaded in.
		var feed_wrapper = $("#feed_wrapper");
		if ($(".feed_box").scrollTop() + $(".feed_box").height() >= feed_wrapper.height()) {

			var limit = Number(feed_wrapper.attr("data-limit"));
			var offset = Number(feed_wrapper.attr("data-offset"));
			
			offset = limit + offset;

			feed_wrapper.attr("data-offset",offset);

			if($("#feed_wrapper .feed_section").length >= offset)
				displayRecords(limit, offset);

		}
	});


	$(".moments_box").scroll(function() {
		// make sure u give the container id of the data to be loaded in.
		var moments_wrap = $(".moments_wrapper");

		if ($(".moments_box").scrollTop() + $(".moments_box").height() >= moments_wrap.height()) {

			var limit = Number(moments_wrap.attr("data-limit"));
			var offset = Number(moments_wrap.attr("data-offset"));

			offset = limit + offset;
			
			if($(".moments_wrapper .feed_section").length >= offset)
				loadData(limit, offset, moments_wrap, "moments");
		}
	});

});

function displayRecords(lim, off, append = true) {
	if(!feedBusy){
		feedBusy = true;
		$("#feedLoader").show();
	    $.ajax({
	      type: "GET",
	      url: "?menu=home&recordLimit="+lim+"&offset="+off+"&section=feed",
	      success: function(html) {
	      	feedBusy = false;

	      	if(append)
	        	$("#feed_wrapper").append(html);
	        else{
	        	$("#feed_wrapper").html(html);
	        	$(".feed_box").scrollTop(0);
	        }

	        $("#feed_wrapper").attr("data-offset",off);
	        $("#feedLoader").hide();
	      },
	      error: function(){
	      	feedBusy = false;
	      	$("#feedLoader").hide();
	      }
	    });
	}
}
 
function loadData(lim, off, div, current_tab, append = true) {
    if(!busy){
    	busy = true;
    	$("#momentsLoader").show();
	    $.ajax({
	      type: "GET",
	      url: "?menu=home&recordLimit="+lim+"&offset="+off+"&section=moments",
	      success: function(html) {
	      	busy = false;
	      	if(html){
	      		if(append)
	        		div.append(html);
	        	else{
	        		div.html(html);
	        		div.parent().scrollTop(0);
	        	}
	        	
	        	div.attr("data-offset",off);
	      	}
	      	$("#momentsLoader").hide();
	      },
	      error: function(){
	      	busy = false;
	      	$("#momentsLoader").hide();
	      }
	    });
	}
}
 

 // $(".tags_box").scroll(function() {
	// 	// make sure u give the container id of the data to be loaded in.
	// 	var current_tab = $(".tags_box .tab-content .tab-pane.active").attr("id");
	// 	var tags_wrap = $("#"+current_tab);

	// 	if ($(".tags_box").scrollTop() + $(".tags_box").height() >= tags_wrap.height()) {
			
	// 		var limit = Number(tags_wrap.attr("data-limit"));
	// 		var offset = Number(tags_wrap.attr("data-offset"));

	// 		offset = limit + offset;

	// 		if($(".tags_content").length >= offset)
	// 			loadData(limit, offset, tags_wrap, current_tab);
	// 	}
	// });

	 /*If browser resized, check width again */
  //   if ($(window).width() < 980) {
		// // alert("small");
  //    	a(".main").click(function(){
		// a(".main").animate({"height":"auto"},function(){
			
		// 	a(".apple_dwn").slideToggle(500)
		// 	});
		
		
		// })
		
		// a(".database_headding").addClass("collapsed");
		// a(".colapse_cus").removeClass("in");
		
		// a(".open1").removeClass("collapsed");
		// a(".open2").addClass("in");
		
  //   }
  //   else {}
