/*!
 * Select Replacement jQuery Plugin v1.0.1
 * http://mbe.ro/
 *
 * Licensed under FreeBSD License
 * Redistributions of files must retain the above copyright notice and this extensions of CakePHP cannot be used without written
 * authorization from the author. By using it you agree to the terms.
 *
 * @copyright     Copyright 2011, mbe (http://mbe.ro)
 * @link          http://mbe.ro mbeCake Project
 * @version       1.0.1
 * @author        mbe (http://mbe.ro)
 * @license       FreeBSD License (http://www.freebsd.org/copyright/freebsd-license.html)
 */
(function ($) {
    $.fn.replaceSelects = function (options) {
        var defaults = {
            cssClass: 'optionbox',
            zIndex: 2,
            hideOnBodyClick: true,
            replaceForIphone: false,
            onElementSelect: function (element) {
                return true;
            },
            hideListEffect: function (element) {
                $(element).slideUp('fast');
            },
            toggleListEffect: function (element) {
                $(element).slideToggle('fast');
                return true;
            }
        };
        var options = jQuery.extend(defaults, options);
        var is_select_opened = null;

        return this.each(function () {
            if (!options.replaceForIphone && (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i))) {
                return false;
            }
            var opts = new Array;
            var selected = new Object();
            var width = $(this).width();
            if (parseInt($(this).css('width')) > 0) {
                width = parseInt($(this).css('width'));
            }
            var hasFloat = $(this).css('float');
            width += 40;
            $(this).children().each(function () {
                var o = new Object();
                o.value = $(this).attr('value');
                if ($(this).attr('html')) {
                    o.html = $(this).attr('html');
                } else {
                    o.html = $(this).html();
                }
                o.label = $(this).html();
                o.selected = $(this).attr('selected');
                o.disabled = $(this).attr('disabled');
                if (o.selected) {
                    selected = o;
                }
                opts.push(o);
            });
            $(this).hide(); //hide me
            //html = ''; 
            _title = '';
           
            if (!$(this).parent().hasClass('select-container')){
                $(this).wrap('<div class="select-replacement ' + options.cssClass + '" ><div class="select-container" style="display:none"></div><div class="selected" ><div class="selected-inner"><div class="selected-inner2">' + selected.label + '</div></div></div><div class="options" style="display:none;"><div class="top-shadow"><div class="left-shadow"></div></div></div></div>');
                for (x in opts) {

                    var aTag = $("<a/>");
                    aTag.attr("href", "#" + escape(opts[x].html));
                    aTag.attr("class", "option" + (opts[x].selected ? "-selected" : "") + (opts[x].disabled ? "-disabled" : ""));
                    aTag.attr("rel", opts[x].value);
                    aTag.attr("title", escape(opts[x].label));
                    aTag.html(opts[x].html);

                    $(".select-replacement .options").append(aTag);
                    //FIX ALESSIO
                    //html += '<a href="#'+escape(opts[x].html)+'" class="option'+(opts[x].selected?'-selected':'')+(opts[x].disabled?'-disabled':'')+'" rel="'+opts[x].value+'" title="'+escape(opts[x].label)+'">'+opts[x].html+'</a>';
                }
            }
            $('.select-replacement .selected').unbind('click');
            $('.select-replacement .selected').click(function () {
                if (options.toggleListEffect($(this).parent().children('.options')[0])) {
                    options.zIndex++;
                    $(this).parent().css('z-index', options.zIndex);
                    $(this).parent().children('.options').css('z-index', options.zIndex);
                    if ($.fn.replaceSelects.is_select_opened && $.fn.replaceSelects.is_select_opened.parent().children('.selected')[0] != this) {
                        options.hideListEffect($.fn.replaceSelects.is_select_opened[0]);
                    }
                    $.fn.replaceSelects.is_select_opened = $(this).parent().children('.options');
                }
            });
            $('.select-replacement .options .option, .select-replacement .options .option-selected, .select-replacement .options .option-disabled, .select-replacement .options .option-selected-disabled').unbind('click');
            $('.select-replacement .options .option, .select-replacement .options .option-selected, .select-replacement .options .option-disabled, .select-replacement .options .option-selected-disabled').click(function () {
                if (options.onElementSelect(this)) {
                    if (!$(this).is('.option-disabled') && !$(this).is('.option-selected-disabled')) {
                        var optionbox = $(this).parent().parent();
                        var select = optionbox.find('.select-container select');
                        optionbox.find('.selected .selected-inner2').html(unescape($(this).attr('title')));
                        select.val($(this).attr('rel'));
                        var f = select.attr('onchange');
                        if (f) {
                            f.call(select.val());
                        }
                        $(this).parent().children('.option, .option-selected').each(function () {
                            $(this).removeClass('option-selected');
                            $(this).addClass('option');
                        });
                        options.hideListEffect($(this).parent()[0]);

                        $(this).addClass('option-selected');
                        $(this).removeClass('option');
                    }
                }
                return false;
            });
            if (options.hideOnBodyClick) {
                $(document).ready(function () {
                    $('html').click(function (event) {
                        if (!$(event.target).is('.select-replacement,.select-replacement *,.select-replacement .selected *')) {
                            options.hideListEffect('.select-replacement .options');
                        }
                    });
                });
            }
        });
    };
})(jQuery);
