// Input element hints
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

// Preloading of images
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);


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

  // nieuwsbrief related stuff
	$('#nieuwsbrief_name').hint();
	$('#nieuwsbrief_email').hint();

	$('#nieuwsbrief form:first input[type=submit]').click(function (e) {
		var form_data = $('#nieuwsbrief form:first').serialize();
		jQuery.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} });
		$.ajax({
			type: "POST",
 			dataType: "script",
			url: '/newsletter_signups',
			data: form_data,
			async: false
			// beforeSend: function() {
			// 	// disable submit button
			// 	// show ajax spinner
			// },
			// error: function(e) {
			// 	// alert('error:'+ e);
			// },
			// success: function(data) {
			// 	// alert('success '+ data);
			// },
			// complete: function(data) {
			// 	alert('complete ' + data);
			// }
		});
		e.preventDefault();
	});	
  // EOF nieuwsbrief related stuff

	var use_animation = false;

	if(use_animation==true) {
		$('#navigation li').each(function(index) {
			$(this).hide();
		});
	    $('#footer').hide();
	    $('#textcontainer').hide();
	    $('#closer').hide();

		$('#navigation li').each(function(index) {
		    $(this).fadeIn(index*650);
		});
	    $('#closer').fadeIn(1500);
	    $('#textcontainer').fadeIn(2500);
	    $('#footer').fadeIn(3500);
	}
	
	var slide_array_prefix = '/img/';
	var slide_array = ['bg_home.jpg', 'bg_restaurant.jpg', 'bg_beach.jpg', 'bg_events.jpg', 'bg_media-news.jpg', 'bg_gallery.jpg', 'bg_vacatures.jpg', 'bg_contact.jpg'];
	var slide_idx = 0;
	var find_slide_idx = function() {
		var bgurl = $('#container').css('background-image');
		for(var i=0; i<slide_array.length; i++) {
			if(bgurl.indexOf(slide_array[i]) != -1) return i;
		}
		return 0;
	}
	var slide_next = function(e) {
		slide_idx++;
		if(slide_idx > slide_array.length - 1) slide_idx = 0;
		slide_change();
		e.preventDefault();		
	}
	var slide_prev = function(e) {
		slide_idx--;
		if(slide_idx < 0) slide_idx = slide_array.length - 1;
		slide_change();
		e.preventDefault();		
	}
	var slide_change = function() {
		$('#container').css('background-image', 'url('+ slide_array_prefix + slide_array[slide_idx] +')');
	}

	var closer_close_anim = function(e) {
		$('#container').data('original-bg', $('#container').css('background-image'));
		slide_idx = find_slide_idx();

		if(use_animation) {
		    $('#textcontainer').fadeOut(2500);
		    $('#footer').fadeOut(1500);
			$('#navigation li').each(function(index) {
			    $(this).fadeOut(3000-(index*400));
			});
			$('#closer').animate({ left: [123, 'easeOutQuad']}, 500);
		} 
		else {
			$('#textcontainer').hide();
		    $('#footer').hide();
			$('#navigation li').each(function(index) {
			    $(this).hide();
			});
			$('#closer').css({ left: 123});
		}
		$('#closer>a:first').text('O');
	
		tid = window.setTimeout(function() {
				var left = $('#closer').clone(false),
					right = $('#closer').clone(false);
				left.attr('id','go_left').css({left: 90, fontSize: '1.8em'}).insertBefore('#closer').hide();
				right.attr('id','go_right').css({left: 156, fontSize: '1.8em'}).insertAfter('#closer').hide();
				if(use_animation) {
					left.fadeIn(500); right.fadeIn(500);
				}
				else {
					left.show(); right.show();
				}
				$('#go_left>a:first').html('&laquo;').click(slide_prev);
				$('#go_right>a:first').html('&raquo;').click(slide_next);
			
			}, use_animation == true ? 750 : 100);				
		e.preventDefault();
	}
	var closer_open_anim = function(e) {
		if(use_animation) {
			$('#go_left').fadeOut(500);
			$('#go_right').fadeOut(500);
		}
		else {
			$('#go_left').hide();
			$('#go_right').hide();			
		}
		
		tid = window.setTimeout(function() {
			$('#go_left').remove();
			$('#go_right').remove();			
			$('#closer>a:first').text('X');
			
			if(use_animation) {
				$('#closer').animate({ left: [900, 'easeOutQuad']}, 500);

				$('#navigation li').each(function(index) {
				    $(this).fadeIn(index*650);
				});
			    $('#textcontainer').fadeIn(2500);
			    $('#footer').fadeIn(3500);
			}
			else {
				$('#closer').css({ left: 900});

				$('#navigation li').each(function(index) {
				    $(this).show();
				});
			    $('#textcontainer').show();
			    $('#footer').show();				
			}

			$('#container').css('background-image', $('#container').data('original-bg'));
		}, use_animation == true ? 500 : 100);

		e.preventDefault();
	}
	$('#closer').toggle(closer_close_anim, closer_open_anim); 

	var _adjustContent = function(callback) {
		var wWidth = $(window).width(),
			wHeight = $(window).height(),
			bgWidth = 1600,
			bgHeight = 1115,
			margin = 32,
			newLogoX = 0;
		// horizontal fixes
		if(bgWidth+(margin*2) < wWidth) { 
			newLogoX = ((wWidth - bgWidth) / 2)-margin;
			if(newLogoX < -margin) newLogoX = 0;
			$("#content").css({ marginLeft: newLogoX });
		}
		else {
			$("#content").css({ marginLeft: 0 });
		}
		// vertical fixes
		if(bgHeight+(margin*2) < wHeight) {
			// $("#content").css({border: '1px solid red'});

			// store original background alignment in data
			// set background alignment to center center
		}
		else {
			// $("#content").css({border: '0'});
			
			// if background alignment is not the one in data
			// restore
		}
    }

	$(window).resize(_adjustContent);
	_adjustContent();	
	
	// jQuery.preLoadImages( slide_array.map(function(el){ return slide_array_prefix + el }) );
});

