/* Setup preview carousel */
function setupPreview() {
	$('.preview').each(function(){
		setupCarousel($(this));
	});
};

function setupCarousel(elm, scrollVal, verticalVal, visibleVal, wrapVal, fader) {
  var scrollVal = typeof(scrollVal) == 'undefined' ? 2 : scrollVal,
	  verticalVal = typeof(verticalVal) == 'undefined' ? false : verticalVal,
	  visibleVal = typeof(visibleVal) == 'undefined' ? 2 : visibleVal,
	  wrapVal = typeof(wrapVal) == 'undefined' ? null : wrapVal,
	  fader  = typeof(fader) == 'undefined' ? false : fader,
      container = null;

  container = elm.find('ul');
  
  container.children('li:contains(EMPTY-ITEM)').remove();
  
  container.parents('.preview').addClass('preview-carousel-loaded');

  container.jcarousel({
    wrap: wrapVal,
    visible: visibleVal,
    scroll: scrollVal,
    vertical: verticalVal
  });

  if (fader) {
	container.after('<div class="jcarousel-fade"></div>');	
  };

}


/* Setup custom select */
function setupCustomSelect() {
  $('.field-select select').each(function(){
    $(this).parent().addClass('field-select-custom');
    $(this).selectmenu({
      width: 305,
      style: 'dropdown'
    });
  });
}


/* Setup form elements placeholder */
function setupFormElementsPlaceholder() {
  $('.field').find('input[type=text]').each(function(){

    var el = $(this),
    placeholder = el.prev('label').text();
    //remove '*' from element name  	
    element = placeholder.split('*');
    placeholder = element[0];
    placeholder = placeholder.replace(/:/, "");
    //end remove
    el.attr('placeholder', placeholder);
    el.val(placeholder);

    el.focus(function(){
        if ($(this).val() == $(this).attr('placeholder')) {
          $(this).val('');
        }
      })
      .blur(function(){
        if ($(this).val() == '') {
          $(this).val($(this).attr('placeholder'));
        }
      });
  });
  
  if ($('#edit-submitted-message').val() == ''){
    $('#edit-submitted-message').val('Message');
  }
  
  // hacks:
  // Hide body message
  $('#edit-submitted-message').focus(function(){
    if ($(this).val() == 'Message'){
      $(this).val('');
    }
  })
  .blur(function(){
    if ($(this).val() == ''){
      $(this).val('Message');
    }
  });
  
  // remove default text before process submit
  $("form").submit(function() {
    $('.field', this).find('input[type=text]').each(function(){
      var el = $(this);
      if (el.val() == el.attr('placeholder')) {
        $(this).val('');
      }
    });
    // hack
    var el = $('#edit-submitted-message', this);
    if (el.val() == 'Message'){
      el.val('');
    }
  });
  
}


function setupLightboxCarousel(){
  var elm = $('#colorbox .items');
  if (elm.size() == 0) return;

  setupCarousel(elm, 5, false, 5);
  
  elm.find('a').click(function(e){
    e.preventDefault();
    var preview = $(this).parents('#preview').find('.item');
    preview.find('img').attr('src', $(this).attr('href'));
    preview.find('p').html($(this).closest('li').find('p.caption').text());
  })
}

$(document).bind('cbox_complete', function(){
  setupLightboxCarousel();
});

$(document).ready(function(){
  $('a.ajax').colorbox({
    opacity: 0.8
  });

  setupPreview();
  setupCustomSelect();
  setupFormElementsPlaceholder();

  setupCarousel($('#container .events'), 1, true, 5);
  setupCarousel($('#container .news'), 1, true, 5, null, true);

  coverFlow.init();
  
});

