$(document).ready(function(){

    if (!Array.prototype.indexOf)
    {
      Array.prototype.indexOf = function(elt /*, from*/)
      {
        var len = this.length;
    
        var from = Number(arguments[1]) || 0;
        from = (from < 0)
             ? Math.ceil(from)
             : Math.floor(from);
        if (from < 0)
          from += len;
    
        for (; from < len; from++)
        {
          if (from in this &&
              this[from] === elt)
            return from;
        }
        return -1;
      };
    }
    
    var button_names = [];
    
    jQuery("form button").each( function (index) {
        
        var nom, replaced
        
        if(jQuery(this).attr('name') == ""){
            nom = "joker_" + index;
        }else{
            nom = jQuery(this).attr('name');
            jQuery(this).removeAttr('name');            
        }
        
        if(button_names.indexOf(nom) < 0) {
            replaced = document.createElement("input");
            replaced.type = "hidden";
            replaced.name = nom
            
            jQuery(replaced).insertBefore(jQuery(this));             
            button_names.push(nom);
        }

        if(this.attributes.getNamedItem("value")){
            jQuery(this).click( function () {                
                jQuery("input[name='" + nom + "']").val(this.attributes.getNamedItem("value").nodeValue);
            });
        }
        
    });
        
    if(jQuery.browser.msie){                    
        jQuery("#body form").submit( function () {

            jQuery(this).find("button[type='submit']:visible").each(function () {
                var clases = jQuery(this).attr('class');                
                var boto = document.createElement('button');
                boto.innerHTML = jQuery(this).html();
                if(jQuery(this).attr('class') != "")  boto.className = jQuery(this).attr('class');
                jQuery(this).after(boto).remove();
            });

            return true;
        });
    }

    /* Controls de the click event on next and prev sayings */
   	if( $("#sayings") ) {
        $("#next").click( function() {        
            var sayings = $("#sayings ul li");
            var length = sayings.length;
            for( var i = 0 ; i < length ; i++ ) {
                var saying = $(sayings[i]);
                if( saying.attr("id") == "main-saying" && i < length - 1 ) {
                    selected_saying = $(sayings[i+1]);
                    if( selected_saying ) {
                        saying.slideUp(400);
                        setTimeout("selected_saying.slideDown(400);",300);
                        saying.attr("id","");
                        selected_saying.attr("id","main-saying");
                        break;
                    }
                }
            } 
        });

        $("#prev").click( function() {        
            var sayings = $("#sayings ul li");
            var length = sayings.length;
            for( var i = 0 ; i < length ; i++ ) {
                var saying = $(sayings[i]);
                if( saying.attr("id") == "main-saying" && i > 0 ) {
                    selected_saying = $(sayings[i-1]);
                    if( selected_saying ) {
                        saying.slideUp(400);
                        setTimeout("selected_saying.slideDown(400);",300);
                        saying.attr("id","");
                        selected_saying.attr("id","main-saying");
                        break;
                    }
                }
            } 
        });

        $("#first").click( function() {
            first_saying = $("#sayings ul li:first");
            if( first_saying.attr("id") != "main-saying" ) {
                main_saying = $("#main-saying");
                main_saying.slideUp(400);
                setTimeout("first_saying.slideDown(400);",300);
                main_saying.attr("id","");
                first_saying.attr("id","main-saying");
            }
        });

        $("#last").click( function() {
            last_saying = $("#sayings ul li:last");
            if( last_saying.attr("id") != "main-saying" ) {
                main_saying = $("#main-saying");
                main_saying.slideUp(400);
                setTimeout("last_saying.slideDown(400);",300);
                main_saying.attr("id","");
                last_saying.attr("id","main-saying");
            }
        });
	}
    /* -------------------------------------------------- */

    if( $("ul.all") )
        $("ul.all").hide();

    /* Controls the click event on Veure totes book detail */
    if( $("#view-all") ) {
        $("#view-all").click( function() {
            $("ul.all").show();
        });
    }
    /* --------------------------------------------------- */

    /* It sets to empty the input value of newsletter form */
    if( $('#email_field') ) {
        $('#email_field').focus( function() {
            $(this).attr("value","");
        });
    }
    /* --------------------------------------------------- */

    /* Controls the opacity for images on content right */
    var images = $('.newsletter img');
    for( var i = 0 ; i < images.length ; i++ ) {
        var img = images[i];
        $(img).fadeTo('fast',.5);
    }

    $('.newsletter img').mouseover( function() {
        $(this).fadeTo('fast',1);
    });

    $('.newsletter img').mouseout( function() {
        $(this).fadeTo('fast',.5);
    });
    /* --------------------------------------------------- */
    

    if( $("#open") ) {
    $("#open").attr("href","#");
    }


});

