/*
 * Recipe/food subsite javascript enhancements.
 *
 * Depends on default.js and other global scripts.
 *
 */



/* enhance html for visual purposes, based on jQuery {{{ */

function applyFillet() {
	// paging buttons
	$('#primary div.paging ul li').append('<span class="lt"></span><span class="rt"></span><span class="lb"></span><span class="rb"></span>').css('position','relative');
	// recipe_info
	$('#recipe_info').append('<div class="corner corner_bl"></div><div class="corner corner_tr"></div>');
	$('.recipe_detail_page .fillet').append('<div class="corner corner_tl"></div><div class="corner corner_tr"></div><div class="corner corner_bl"></div><div class="corner corner_br"></div>');
}

function enhanceRefineOptions() {

	var maxOptions = 4;

	$('#primary #refine ul').each(function(){
		$('li:gt('+maxOptions+')', this).hide().addClass('hidden').addClass('expandable');
		if ($('li', this).length > maxOptions + 1) {
			$(this).append('<li><a href="#" class="more">Meer...</a></li>');
		}
	});
	// attach functions to more/less links
	$('#primary #refine ul a.more').click(function(){
		this.toggle = (!this.toggle || this.toggle == 'more') ? 'less' : 'more'
		this.className = this.toggle;
		if (this.toggle == 'less') {
			this.innerHTML = 'Minder...';
			$(this).parents('ul').find('li.hidden').slideDown().removeClass('hidden');
		}
		else {
			this.innerHTML = 'Meer...';
			$(this).parents('ul').find('li.expandable').slideUp().addClass('hidden');
		}
	});
}

/* }}} */

/* Recipe rating {{{ */


// Rate a recipe on a five-star scale:
function recipesAttachRatingFunctions() {


    // if voted, give feedback
    function voted() {
        var votedspan = document.getElementById('recipe_rating_voted');
        if (votedspan) {

        	$(votedspan).hover(function(){
        		 var span = document.getElementById('rating_feedback');
                 if (span) span.innerHTML = 'U hebt al gestemd';
        	},function(){
        		var span = document.getElementById('rating_feedback');
        		if (span) span.innerHTML = 'Waardering';

        	});
        }
    }
    voted();

    var aID = -1;
    $('#recipe_info .recipe_rating span span').each(function() {
        // Setup event handlers on the five stars:
        var a = this;
		aID++;

        var ratingbox = a.parentNode.parentNode;
        var initbox = a.parentNode;
        var position = aID + 1;
        a.position = aID+1;

        function on_rating_failure (response) {
            initbox.className = 'recipe_rating_stars initstars_';
            // TODO better feedback.
        }

        $(a).hover(function(){
        	$(this.parentNode).addClass('stars_' + this.position);
            var span = document.getElementById('rating_feedback');
            if (span) span.innerHTML = 'Geef uw beoordeling: ' + this.position + ((this.position==1)?' ster':' sterren');
        },function(){
        	$(this.parentNode).removeClass('stars_' + this.position);
            var span = document.getElementById('rating_feedback');
            if (span) span.innerHTML = '&nbsp;';
        }).click(function(){
			// Set form and run submit:

			var initbox = this.parentNode;
            var rbutton = document.getElementById('rate_form_rate' + this.position);
            if ( ! rbutton ) {
                return;
            }
            rbutton.checked = 'checked';

            /**
         	* Post this rate
         	*/

            var ajax_rate = $.ajax({
            	type: "POST",
				url: "/recepten/raterecept",
   				data: $('#rate_form').serialize(),
   				success: function(response){

   					 /**
         			 * Callback for successful rate post.
			         *
			         * The response is a working plain HTML for non-ajax browser;
			         * The new data is stuck on the response as X-JSON as well.
			         */

   					var json_response = eval('(' + ajax_rate.getResponseHeader('X-JSON') + ')');


   					if (json_response == null) {
			             throw 'Missing JSON data in response header.';
			         }

		            var span = document.getElementById('rating_feedback');
		            if (span) {
		                span.innerHTML = 'Bedankt voor uw beoordeling';
		            }
		            span = document.getElementById('recipe_rating_number');
		            if (span) {
		                span.innerHTML = new String(parseInt(json_response.noOfRates));
		            }
		            initbox.className = 'recipe_rating_stars initstars_'
		                                + parseInt(json_response.newrate);
		            initbox.innerHTML = parseInt(json_response.newrate)
		                + ((json_response.newrate == 1) ? ' ster' : ' sterren');
		            initbox.id = 'recipe_rating_voted';


   				},
   				error: function() {
   					/**
         			* Callback for failed rate post.
         			*/

   					 initbox.className = 'recipe_rating_stars initstars_';
   				}

            });

            return false;

        });

    });
}

/* }}} */



/*
  DOM enhancements only applicable for recipepages
*/


function initRecipesFunctions() {
	applyFillet();
	enhanceRefineOptions();
	recipesAttachRatingFunctions();
	// recipeAjaxSearcher();
}

initRecipesFunctions();

// vim: fdm=marker ts=4 sts=4 sw=4 noet
