/* 
*	
*	@author: 	Federico pizzutto - http://www.pngised.net
*	@date: 		2010-05-16
*
* 	LICENSE
*	Quest'opera è stata rilasciata sotto la licenza Creative Commons Attribuzione-Condividi allo stesso modo 2.5 Italia.
*	Per leggere una copia della licenza visita il sito web http://creativecommons.org/licenses/by-sa/2.5/it/
*	o spedisci una lettera a Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*/

(function($) {
    $.fn.extend({

        customStyle: function(options) {
            if (!$.browser.msie || ($.browser.msie && $.browser.version > 6)) {
                return this.each(function() {

                    var esto = $(this);
					
                    var currentSelected = $(this).find('li.selected');
                    $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">' + currentSelected.text() + '</span></span>').css({ position: 'absolute', visibility: 'hidden', fontSize: $(this).next().css('font-size') });
                    var selectBoxSpan = $(this).next();
                    var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) - parseInt(selectBoxSpan.css('padding-right'));
                    var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                    selectBoxSpan.css({ display: 'block' });
                    selectBoxSpanInner.css({ width: selectBoxWidth, display: 'block' });
                    var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
                    selectBoxSpan.click(function() {
                        //selectBoxSpan.css('display', 'none');
                        //currentSelected.removeClass('selected');

						if (esto.css("visibility") == 'hidden') {
						
							esto.css({
								visibility: 'visible',
								top: '20px'
							});
							
							
							esto.children('li').addClass('opens').slideDown().click(function(e){
								var target = $(e.target);
								if (target.parent().hasClass('selected')) {
									esto.css({
										visibility: 'hidden',
										top: '0px'
									});
									esto.children('li').removeClass('opens');
								}
								else {
									target.parent().addClass('selected');
									esto.css({
										visibility: 'hidden',
										top: '0px'
									});
									esto.children('li').removeClass('opens');
								}
								esto.next(".customStyleSelectBox").remove();
								esto.customStyle();
								return false;
							})
						}
						else{
							esto.toggle();
						}
                    });

                });
            }
        }
    });
})(jQuery);


// ONLOAD
//$(document).ready(function(){
	//$('.tenda').customStyle();
//});

