var section_position = 1;

function add_image_widths() {
  var position = 0;
  $('#collection img').each( function(index) {  
    var thumb = $('#thumbs a').get(index);
    $(thumb).attr('position', position);
    position += $(this).width();
  })
};

$(window).load( function() {
  $('#thumbs img').each( function() {
    $(this).parent().siblings('.dark_box').css('width', $(this).width());
  });
  add_image_widths();
});

var thumbs_index = $('#thumbs a').index
var next_ajax = 2;

var SliderHelper = {
  to: 0,
  collection_name: '',
  collection_length: '',
  viewing_switch: function(direction) {
    if(direction == 'prev') {
      $('#collection .viewing').prev().addClass('viewing');
      $('#collection .viewing:last').removeClass('viewing');
      $('#thumbs .viewing').prev().addClass('viewing');
      setTimeout(function() {
        $('#thumbs .viewing:last').removeClass('viewing');
      }, 200);
    } else {
      $('#collection .viewing').next().addClass('viewing');
      $('#collection .viewing:first').removeClass('viewing');
      $('#thumbs .viewing').next().addClass('viewing');
      setTimeout(function() {
        $('#thumbs .viewing:first').removeClass('viewing');
      }, 200);
    }
    SliderHelper.scrollTo('#thumbs .viewing a:first');
  },
  next_slide: function() {
    if(next_ajax <= SliderHelper.collection_length) {
      var url = '/' + SliderHelper.country_name + "/collections/" + SliderHelper.collection_name + "/" + next_ajax;
      $.ajax({ url: url, context: document.body, success: function(data) {
        $('#collection').append(data);
        next_ajax += 1;
        SliderHelper.viewing_switch('next');
      }});
    }
    else {
      SliderHelper.viewing_switch('next');
    }
  },
  scrollTo : function(obj) {
    var pos = $(obj).attr('position');
    if ( SliderHelper.to != pos ) {
      $('#collection').stop();
      $('#collection').animate({ left: '-' + pos + 'px'}, 'slow' );
      SliderHelper.to = pos;
    }
  }
};

$(document).ready( function() {
  /*
  SliderHelper.country_name = $('#hidden_country_name').val();
  SliderHelper.collection_name = $('#hidden_collection_name').val();
  SliderHelper.collection_length = $('#thumbs .section').length
  */
  // Initial Setup  
  $('ul.nav li:last').addClass('last');
  
  $('.box').each( function() {
    $(this).mouseenter( function() {
      $('.dark_box').addClass('current');
      $(this).children('.dark_box').removeClass('current');
    });
  });
  
  $(".email").placeholder({
    message: "Email"
  });
  
  $('#collection .section, #thumbs .section:first').addClass('viewing');
  /*
  $('.scrollable').scrollable();
  
  // Controls
  
  $('.next').click( function() {
    SliderHelper.next_slide()
  });
  
  $('.prev').click( function() {
    SliderHelper.viewing_switch('prev');
  });
 
  $('#thumbs a').mouseenter(function() {
    $('#thumbs a').removeClass("hoverThumb");
    $(this).addClass("hoverThumb");
    SliderHelper.scrollTo(this);
  });
  */
  
  //$("#collection a.item_image").fancybox({
	//	'overlayShow'	:	true
	//});
  	$('#collection a.item_image').lightBox({fixedNavigation:false});
  	 
});

$(window).load(function() {
  while(next_ajax <= SliderHelper.collection_length) {
    var url = '/' + SliderHelper.country_name + "/collections/" + SliderHelper.collection_name + "/" + next_ajax;
    $.ajax({ url: url, context: document.body, success: function(data) {
      $('#collection').append(data);
      SliderHelper.viewing_switch('next');
    }});
    next_ajax += 1;
  }
});
