var $ = jQuery;

function makeScrollable(wrapper, scrollable){
  // Get jQuery elements
  var wrapper = $(wrapper), scrollable = $(scrollable);

  // Hide images until they are not loaded
  scrollable.hide();
  var loading = $("<div class='loading'></div>").appendTo(wrapper);

  // Set function that will check if all images are loaded
  var interval = setInterval(function(){
    var images = scrollable.find("img");
    var completed = 0;

    // Counts number of images that are succesfully loaded
    images.each(function(){
      if (this.complete) completed++;   
    });

    if (completed == images.length){
      clearInterval(interval);
      // Timeout added to fix problem with Chrome
      setTimeout(function(){

        loading.hide();
        // Remove scrollbars    
        wrapper.css({overflow: "hidden"});

        scrollable.slideDown("slow", function(){
          enable(); 
        });                 
      }, 1000); 
    }
  }, 100);


function enable(){
  // height of area at the top at bottom, that don't respond to mousemove
  var inactiveMargin = 100;         
  // Cache for performance
  var wrapperWidth = wrapper.width();
  var wrapperHeight = wrapper.height();
  // Using outer height to include padding too
  var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
  // Do not cache wrapperOffset, because it can change when user resizes window
  // We could use onresize event, but it&#39;s just not worth doing that 
  // var wrapperOffset = wrapper.offset();

  //When user move mouse over menu          
  wrapper.mousemove(function(e){
    var wrapperOffset = wrapper.offset();
    // Scroll menu
    var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight  - inactiveMargin;

    if (top < 0){
      top = 0;
    }

    wrapper.scrollTop(top);
  });       
}




}


$(document).ready(function() {

	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}
	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url, "");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
		alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
		}
	});


	$("#slide_featured").cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 5000,
		pager:  'ul#featured_items',
		pagerAnchorBuilder: function(idx, slide) {
			return '<li class="featured_item"><a href="#">'+$(slide).find("div").attr("title")+'</a></li>'; 
		}
	});
	
	

	makeScrollable("div#featured_list", "ul#featured_items");
	
	// Menu
	$("ul#menu-main li.first").hover(
		function () {
			$(this).find("ul").show();
		},
		function () {
			$(this).find("ul").hide();
		}
	);
	
	/*$("ul#menu-main li ul li a").hover(
		function () {
			$(this).find("ul").show();
			var thumb = $(this).attr("id")+".jpg";
			if(thumb == ".jpg") { thumb = "generic_thumb.jpg" }
			$("div.menu_thumb").html('<img src="/wp-content/themes/artsetgastronomie/img/menu/'+thumb+'" style="width: 110px; height: 110px;" />');
			$("div.menu_thumb").show();
		},
		function () {
			$(this).find("ul").show();
			$("div.menu_thumb").html('<img src="/wp-content/themes/artsetgastronomie/img/menu/generic_thumb.jpg" style="width: 110px; height: 110px;" />');
		}
	);*/
	
	// Au hasard
	$(".rand a").hover(
		function () {
			//alert($(this).parent(".rand").next(".rand_title").attr("id"));
			$(this).closest(".rand_title").html($(this).attr("title"));
		},
		function () {
			$(this).closest(".rand_title").html("");
		}
	);

	$("a.retour").click( function() {
		history.back();
		return false;
	});

	$("a[title].with_qtip").qtip({
		position: {
			target: "mouse",
			adjust: { mouse: true, x: 20, y: 20 }
		},
		hide: { fixed: true }
	});
	
	$("img[title].with_qtip").qtip({
		position: {
			target: "mouse",
			adjust: { mouse: true, x: 20, y: 20 }
		},
		hide: { fixed: true }
	});
	
	
	$("a.tell-a-friend-link").click(function() {
		$('#tell-a-friend').dialog({
			width: 500,
			height: 350,
			title: 'Envoyer à un ami',
			close: function(){
				$("#tell-a-friend").dialog("destroy");
				$("div#tell-a-friend-formulaire").css("display", "block");
				$("div#tell-a-friend-message").css("display", "none");
			}
		});
		return false;
	});
	
	jQuery.validator.setDefaults({
		submitHandler: function() {
			$.post("/wp-content/themes/artsetgastronomie/ajax.php", { action: "tell_a_friend", votre_nom: $("input#votre_nom").val(), votre_email: $("input#votre_email").val(), ami_email: $("input#ami_email").val(), votre_message: $("textarea#votre_message").val(), article_url: $("input#article_url").val(), article_titre: $("input#article_titre").val()},
			function(data){
				$("div#tell-a-friend-formulaire").css("display", "none");
				$("div#tell-a-friend-message").css("display", "block").html(data);
			});
		}
	});

	$("#tell-a-friend-form").validate({
		rules: {
			votre_nom: "required",
			votre_email: {
				required: true,
				email: true
			},
			ami_email: {
				required: true,
				email: true
			}
		},
		messages: {
			votre_nom: "",
			votre_email: {
				required: "",
				email: ""
			},
			ami_email: {
				required: "",
				email: ""
			}
		}
	});
	  
	  
	$("ul#menu-main li").hover(
		function () {
			$(this).find("ul").show();
		},
		function () {
			$(this).find("ul").hide();
		}
	);
	
	$("ul#menu-main li ul.bg_img li a").each(function(){
		$(this).css("backgroundImage", "url(/wp-content/themes/artsetgastronomie/img/menu/" + $(this).attr("id") + ".jpg)");
	});
	
});
