/* Copyright (c) 2010 Webrover (Corporate site http://www.webrover.ru)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * $Id: $
 */

/**
 *
 * @example jQuery(".select-conteiner").jCastomSelect({maxheight: 120});
 *
 * @name jCastomSelect
 * @type jQuery
 * @return jQuery
 * @author Gorbushko Aleksandr (Corporate blog of Alexander  http://blog.webrover.ru/gorbushko/)
 * @subauthor Kir V (http://blog.webrover.ru/kirv/)
 */

jQuery.wbrselect = { 
	maxheight: 100, // максимальная высота выпадающего поля в пикселях
	scrollbarWidthW: 12,
	dragMaxHeightW: 90,
	istitle: false // отображать ли тайтл у лейбла со значением (нужно, если он обрезается)
}

	$.fn.createCastSel = function(){
		var selectHead, selectBody, selectFoot, itemClass;
		selectHead = selectBody = selectFoot = itemClass = '';
		var selectItem = $(this).find('option');
		var selectedSelectItem = selectItem.filter(':selected');
		
		selectHead = '<div class="inpunts_cont">';
		selectHead += '<span class="select_value">' + selectedSelectItem.html() + '</span>';
		selectHead += '<input type="hidden" name="'+ $(this).attr("name") +'" value="'+selectedSelectItem.val()+'" />';  // jScrollPaneContainer
		selectHead += '<div class="inpunts"><div class="left-input"><div class="right-input"><div class="fill-input"><div class="jScrollPaneContainer"><div class="select_list">';
		
		var evenodd = true;
		selectItem.each(function(){
			if($(this).val() != "0"){
				var itemClass  = $(this).val();
				if($(this).attr('selected')) itemClass_ = ' selected'; else itemClass_ = '';
				if (evenodd) DitemClass='odd'; else DitemClass='even';
				evenodd = !evenodd;
				selectBody += '<div class="'+DitemClass+'"><a href="javascript:void(0);" class="'+itemClass_+'" val="' + itemClass + '">' + $(this).html() + '</a></div>';
			}
		});
		selectFoot = '</div></div></div></div></div></div></div>';
		return selectHead + selectBody + selectFoot;
	}

	$.fn.setCastSelAction = function(superobject,current){		
		// superobject MUST BE == .inpunts_cont
		superobject.find('.select_value').unbind("mouseenter");
		superobject.find('.select_value').bind("mouseenter",function(){
			mainobj = $(this).closest('.inpunts_cont');//.find('.jScrollPaneContainer');
			if (mainobj.attr("scroll")!='true')
			{
				$('.inpunts').not(mainobj.find('.inpunts')).removeClass('act');
				$('.inpunts').not(mainobj.find('.inpunts')).closest('.inpunts_cont').find('.select_value').removeClass('select_value-act');
				mainobj.find('.inpunts').toggleClass('act');
				mainobj.closest('.inpunts_cont').find('.select_value').toggleClass('select_value-act');
				$('.jScrollPaneContainer').not(mainobj.find('.jScrollPaneContainer')).addClass('hidden');
				/*if (mainobj.find('.jScrollPaneContainer').attr("class").indexOf('hidden')<0)
					mainobj.find('.jScrollPaneContainer').addClass('hidden');
				else*/
					mainobj.find('.jScrollPaneContainer').removeClass('hidden');
				//$(this).find('.jScrollPaneContainer').toggleClass('hidden');
			}
			mainobj.attr("scroll","false");
			/*mainobj.find('.jScrollPaneContainer').hover(
					function(){},
					function(){	
						$(this).closest('.inpunts_cont').find('.select_value').removeClass('select_value-act');
						$(this).closest('.inpunts').removeClass('act');
						$(this).addClass('hidden');
				});
			*/
			$(this).find('.jScrollPaneTrack').hover(function()
			{
				mainobj.attr("scroll","true");
			},
			function()
			{
				mainobj.attr("scroll","false");
			});
		});
		superobject.find('.select_list a').unbind("click");
		superobject.find('.select_list a').bind("click",function(){
			$(this).closest('.inpunts').removeClass('act');
			$(this).closest('.inpunts_cont').children('.select_value').html($(this).html());
			$(this).closest('.inpunts_cont').children('input').val($(this).attr('val'));
			if (current.istitle)
				$(this).closest('.inpunts_cont').children('.select_value').attr("title",$(this).html());
				
			superobject.find('.select_list a.selected').removeClass('selected');
			$(this).addClass('selected');
			//$(this).closest('form').submit();
		});		
		return true;
	}
	
	$.fn.getPadRight =  function(){
		var res;
		res = $(this).css('padding-right');
		res = 1*(res.substr(0,res.length - 2));
		return res;
	}
	
	$.fn.getPadLeft =  function(){
		var res;
		res = $(this).css('padding-left');
		res = 1*(res.substr(0,res.length - 2));
		return res;
	}
	
	$.fn.jCastomSelect = function(options){
		var current;
		// настройки
		current = jQuery.extend({},jQuery.wbrselect,options);
		max_h = current.maxheight;

		objSelect = $(this).clone();
		objSelArea = $(this).find('form');

		var thisparent;
		zindex_ = 900;
		var padRight, padLeft, nextLevelWidth;
		$(this).find("select").each(function(){
			thisparent = $(this).parent();
			thisparent.append($(this).createCastSel());
			
			thisparent = thisparent.find('.inpunts_cont:last');
			
			// z-index для перекрытия по вертикали
			thisparent.css("z-index",zindex_);
			zindex_--;
			
			var maxWidth = 0;
			thisparent.find('.inpunts .select_list a').each(function(){
				var curWidth = $(this).outerWidth();
				if(curWidth > maxWidth) maxWidth = curWidth;
			});

			padRight = thisparent.find('.select_list').getPadRight();
			padLeft = thisparent.find('.select_list').getPadLeft();
			nextLevelWidth = maxWidth + padLeft + padRight;	
			thisparent.find('.select_list').width(nextLevelWidth);

			thisparent.find('.select_value').width(thisparent.find('.select_list').width());

			padRight = thisparent.find('.right-input').getPadRight();
			nextLevelWidth += padRight;
			thisparent.find('.jScrollPaneContainer').width(nextLevelWidth);
			
			thisparent.find('.inpunts_cont').width(thisparent.find('.jScrollPaneContainer').outerWidth());
			thisparent.find('.inpunts').width(thisparent.find('.jScrollPaneContainer').outerWidth());
			
			thisparent.setCastSelAction(thisparent,current);
		});
		
		objSelArea.find('select').remove();
		
		
		/*objSelArea.find('.inpunts_cont').each(function(){
			$(this).css("z-index",zindex_);
			zindex_--;
			var maxWidth = 0;
			$(this).find('.inpunts .select_list a').each(function(){
				var curWidth = $(this).outerWidth();
				if(curWidth > maxWidth) maxWidth = curWidth;
			});
			
			var padRight, padLeft, nextLevelWidth;
			padRight = $(this).find('.select_list').getPadRight();
			padLeft = $(this).find('.select_list').getPadLeft();
			nextLevelWidth = maxWidth + padLeft + padRight;	
			$(this).find('.select_list').width(nextLevelWidth);
			
			$(this).find('.select_value').width($(this).find('.select_list').width());
			
			padRight = $(this).find('.right-input').getPadRight();
			nextLevelWidth += padRight;
			$(this).find('.jScrollPaneContainer').width(nextLevelWidth);
			
			$(this).find('.inpunts_cont').width($(this).find('.jScrollPaneContainer').outerWidth());
			$(this).find('.inpunts').width($(this).find('.jScrollPaneContainer').outerWidth());
		});
		*/
		//$(this).setCastSelAction($(this));
		
		//$(this).find('.jScrollPaneContainer').jScrollPane({showArrows:false,scrollbarWidth:current.scrollbarWidthW,
		//	dragMaxHeight:current.dragMaxHeightW});
		
		
		
		$(this).find('.jScrollPaneContainer').each(function(){
			// нормализация высоты
			h_ = $(this).innerHeight();
			if (h_ > max_h)
			{
				$(this).height(max_h).find('.select_list').jScrollPane('revalidate');				
				//$(this).find('.select_list').jScrollPane({showArrows:false,scrollbarWidth:12,dragMaxHeight:94});//.jScrollPane('revalidate');	
			}

			$(this).addClass("hidden");			
		});

		
		$('body').click(function(e){
		
			if (($(e.target).closest('.inpunts_cont').length==0))
			{
				$('.inpunts_cont .inpunts').removeClass('act');				
				$('.inpunts_cont .jScrollPaneContainer').addClass('hidden');
				$('.inpunts_cont .select_value').removeClass('select_value-act');
			}
		});		
		
		return $(this);
	}

