$(function () {
  //Navbar
  $('#main_menu ul li:odd').addClass('odd');
  $('#main_menu ul li:even').addClass('even');
  $("#main_menu").superfish({pathClass:'current',pathLevels:0, autoArrows: false });
  $('ul li:first-child').addClass('first');
  $('ul li:last-child').addClass('last');

  //Slideshow
  $('#browsable').scrollable({circular: true}).navigator({indexed: true}).autoscroll( 8000, { autopause: true });
  $('#browsable').scrollableAddClones();
  $('.navi a:nth-child(1)').addClass('navi-1');
  $('.navi a:nth-child(2)').addClass('navi-2');


  //Services
  $('.logo-grid li:nth-child(4n)').addClass('last');
  $('.logo-grid li').hover(function(){
    $(this).find('span').fadeIn(400);
  },function() {
    $(this).find('span').fadeOut(200);
  });

  //Client List
  $('.client-list-text li:nth-child(3n)').addClass('last');

  //Contact Form Dropdown
  $('#contact-dropdown .directions-link').click(function(){ $('#directions').closeBtn().fadeIn(); showLatLngHybrid('map',32.726100681912925, -117.22625967358589); });

  $('a[href$="contact"]').drawer();

  //Sidebar Accordion
   $('#accordion h3').click(function() {
    $(this).toggleClass('top-only active ')
   	$(this).next().slideToggle();
   	return false;
   });
});

function showLatLngHybrid(e, la, lo)
{
  var e = document.getElementById(e);
  if(e != null)
  {
    var latlong = new google.maps.LatLng(la, lo);
    var map = new google.maps.Map(e,{ zoom:18, center:latlong, mapTypeId:google.maps.MapTypeId.HYBRID });
    new google.maps.Marker({position: latlong,map: map});
  }
}

$.fn.drawer = function () {
  $(this).toggle(function(){
    var parent = $('#contact-dropdown').closeBtn('.container')
    $('#slideshow').slideUp();
    scrollTo('html');
    parent.slideDown();
    return false;
  },
  function(){
    var parent = $('#contact-dropdown');
    $(parent).slideUp();
    $(parent).find('.close').remove();
    $('#slideshow').slideDown();
    
  });
  return this;
};

$.fn.closeBtn = function (html){
    var parent = $(this);
    if($(parent).find(".close").length == 0)
    {
      var button = $('<a />').attr({href:'#'}).html('x').addClass('close').click(function(){
        parent.slideUp();
        if(!$('.image-container').is(":empty"))
        {
          $("#slideshow").slideDown();
        }
      });
      if(html != undefined){
        parent.find(html).prepend(button);
      } else {
        parent.prepend(button);
      }
    }
    return this;
};


function showMe(category)
{
  $('.btn-list li a.active').removeClass('active');
  $('#'+category).addClass('active');
  $('.logo-grid li').fadeOut(200);
  $('.logo-grid').find('li.last').removeClass('last');
  $('.logo-grid li:last').addClass('last');
  if ( category == 'all' ) {
  $('.logo-grid li').fadeIn(400);
  $('.logo-grid li:nth-child(4n)').addClass('last');
  } else {
  $('.logo-grid li.'+category).fadeIn(400);
  var i = 0;
  $('.logo-grid li.'+category).each(function() {
  if(i==3 || i==7 || i==11 || i==15 || i==19 || i==23 || i==27) {
    $(this).addClass('last');
  }
  i++;
  });
  }
}

function scrollTo(selector) {
	var targetOffset = $(selector).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500);
}

function dragTimelineToo() {
	var timelineContainerWidth = $('#timeline-container').width();
	var timelineWidth = $('#timeline').width();
	var miniTimelineWidth = $('#mini-timeline').width();
	var handleWidth = $('#timeline-handle').width();
	
	var maxTimelineMove = ( timelineWidth - timelineContainerWidth );
	var maxScrollerMove = ( miniTimelineWidth - handleWidth );
	
	var handleX = $('#timeline-handle').css('left');
	var handlePos = handleX.substring( 0, handleX.length-2 );
	var handlePerc = Math.round(( handlePos / maxScrollerMove )*100);
	
	var moveto = (((handlePerc/100)*maxTimelineMove)*-1);
	$('#timeline').css('left',moveto);
}
$(document).ready(function() {
	if($('#timeline').length) {
		$('#timeline-handle').draggable({ axis:'x', containment:'#mini-timeline', drag:function(){dragTimelineToo();} });
		
		var timelineContainerWidth = $('#timeline-container').width();
		var timelineWidth = $('#timeline').width();
		var miniTimelineWidth = $('#mini-timeline').width();
		var handleWidth = $('#timeline-handle').width();
		
		$("#mini-timeline").click(function(e){
			var xPos = e.pageX - this.offsetLeft;
			if (xPos < (miniTimelineWidth-(handleWidth/2))) {
				$('#timeline-handle').css('left',(xPos-(handleWidth/2)));
				dragTimelineToo();
			} if (xPos >= (miniTimelineWidth-(handleWidth/2))) {
				$('#timeline-handle').css('left',(miniTimelineWidth-handleWidth));
				dragTimelineToo();
			} else if (xPos < handleWidth) {
				$('#timeline-handle').css('left',0);
				dragTimelineToo();
			}
		});
		$('#mini-timeline').hover(function() { $('#timeline-handle').stop(); $('#timeline').stop(); });
		$('#timeline').hover(function() { $('#timeline-handle').stop(); $('#timeline').stop(); });
		$('#timeline-handle').animate({left:(miniTimelineWidth-handleWidth)}, 30000 );
		$('#timeline').animate({left:(timelineContainerWidth-timelineWidth)}, 30000 );
	}
});
