/* Author:

*/

var pebbleroad = {};

pebbleroad.init = function(){
  
  pebbleroad.flicker();
  
  pebbleroad.scrollshadow();
  
  pebbleroad.misc();
  
  jQuery('#Categories a').bind('click', pebbleroad.loadPerspectives);
  jQuery('.catLink').bind('click', pebbleroad.loadPerspectives);
  
  
  // Highlight Category in View Page
  
  var $categories = $('#Perspectives').find('.catLink'),
      $sidebarcategories = $('#Categories');
  
  $categories.each(function(){
    var id = $(this).data("id");
    $sidebarcategories.find('.category-'+id).addClass("active");
  })    
  
  
}

// Scroll

pebbleroad.scrollshadow = function(){
  jQuery.browser.mobile = /mobile/i.test(navigator.userAgent);
  
  //if(!jQuery.browser.mobile){
    checkscroll();
    $(window).scroll(function(){
      checkscroll();
    }); 
  //}  
  
  function checkscroll(){
    var _st = $('html').scrollTop() || $('body').scrollTop();
    _st > 0 ? $('#FixedHeader').addClass("headerShadow") : $('#FixedHeader').removeClass("headerShadow");
  }
  
}

// Comment

pebbleroad.misc = function(){
  
  $('#comment').focus(function(){
    $('.slideOut').slideDown();
  });
}

// TagLine Flicker

pebbleroad.flicker = function(){
  
  var $tagline = jQuery('#TagLine'),
      $span = $tagline.find('span'),
      words = $span.text().split(','),
      i=1, $li='';
      
     
  if($span.length){
    $span.remove();
    for(i=0; i < words.length; i++){
      $li+='<li>'+words[i]+'</li>';
    }
    
    $tagline.append($('<ul />').html($li));
    
    $tagline.find('ul').list_ticker({
			speed:2000,
			effect:'slide'
		});
  }  
  
}

pebbleroad.loadPerspectives = function(e){
  
  var $holder = $('#Perspectives'),
      href = this.href,
      category = this.href.split('/').pop();
      
  $('#Categories').find('li').removeClass("active").filter('.category-'+category).addClass("active");
  
  $holder.find('section').fadeOut().end().load(this.href, function(){
    $holder.find('article').hide().each(function(i,ele){
      clearTimeout(loadTimer);
      var loadTimer = setTimeout(function(){
        $(ele).fadeIn();
      },i*200);
      
    });
  });
  
  $('html,body').animate({
    scrollTop: $holder.position().top - $('#FixedHeader').height()
  },500);
  
  e.preventDefault();
  
  
  
};


// Rewritten version: IOS Scaling Bug : https://gist.github.com/1334402
// By @mathias, @cheeaun and @jdalton

;(function(doc) {

	var addEvent = 'addEventListener',
	    type = 'gesturestart',
	    qsa = 'querySelectorAll',
	    scales = [1, 1],
	    meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];

	function fix() {
		meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
		doc.removeEventListener(type, fix, true);
	}

	if ((meta = meta[meta.length - 1]) && addEvent in doc) {
		fix();
		scales = [.25, 1.6];
		doc[addEvent](type, fix, true);
	}

}(document));



$(window).load(function(){
  
  pebbleroad.init();
  
});
