/* 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: $
 */

/**
 * Replace the vertical scroll bars on any matched elements with a fancy
 * styleable (via CSS) version. With JS disabled the elements will
 * gracefully degrade to the browsers own implementation of overflow:auto.
 * If the mousewheel plugin has been included on the page then the scrollable areas will also
 * respond to the mouse wheel.
 *
 * @example jQuery(".select-conteiner").jCastomSelect();
 *
 * @name jCastomSelect
 * @type jQuery
 * @return jQuery
 * @author Gorbushko Aleksandr (Corporate blog of Alexander  http://blog.webrover.ru/gorbushko/)
 */



	$.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()+'" />';
		selectHead += '<div class="inpunts"><div class="left-input"><div class="right-input"><div class="fill-input"><div class="jScrollPaneContainer"><div class="select_list">';
		selectItem.each(function(){
			if($(this).val() != "0"){
				var itemClass  = $(this).val();
				if($(this).attr('selected')) itemClass += ' selected';
				selectBody += '<div><a href="javascript:void(0);" class="' + itemClass + '">' + $(this).html() + '</a></div>';
			}
		});
		selectFoot = '</div></div></div></div></div></div></div>';
		return selectHead + selectBody + selectFoot;
	}
	
	$.fn.setCastSelAction = function(){	
		$('.inpunts_cont').click(function(){
			$('.inpunts').not($(this).find('.inpunts')).removeClass('act');
			$(this).find('.inpunts').toggleClass('act');
			$('.jScrollPaneContainer').not($(this).find('.jScrollPaneContainer')).addClass('hidden');
			$(this).find('.jScrollPaneContainer').toggleClass('hidden')
				.mouseleave(
					function(){					
						$(this).closest('.inpunts').removeClass('act');
						$(this).addClass('hidden');
				});
		});		
		$('.select_list a').click(function(){
			$(this).closest('.inpunts_cont').children('.select_value').html($(this).html());
			$(this).closest('.inpunts_cont').children('input').val($(this).attr('class').split(' ')[0]);
			$('.select_list a').not($(this)).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.getBorderWidth =  function(){
		var res, padding;
		res = $(this).outerWidth() - $(this).innerWidth();
		padding = $(this).css('padding-left');
		padding = 1*(padding.substr(0,padding.length - 2));
		res -= padding;
		padding = $(this).css('padding-right');
		padding = 1*(padding.substr(0,padding.length - 2));
		res -= padding;
		return res;
	}
	
	$.fn.jCastomSelect = function(){
		objSelect = $(this).clone();
		objSelArea = $(this).find('form');
		objSelArea.children().remove();
		objSelect.find('select').each(function(){
			objSelArea.append($(this).createCastSel());
		});
		var tstBrowser = jQuery.browser;
		if(tstBrowser.msie && tstBrowser.version < 8) var inpunts_counter = 0;
		objSelArea.find('.inpunts_cont').each(function(){
			var padRight, padLeft, nextLevelWidth, borderWidth;
			if(tstBrowser.msie && tstBrowser.version < 8){
				$(this).css('z-index', -1*inpunts_counter);
				inpunts_counter++;
			};
			if($(this).find('.inpunts').width()){	
				nextLevelWidth = $(this).find('.inpunts').width();
				$(this).find('.inpunts_cont').width(nextLevelWidth);
				
				borderWidth = $(this).find('.jScrollPaneContainer').getBorderWidth();				
				nextLevelWidth -= borderWidth;
				$(this).find('.jScrollPaneContainer').width(nextLevelWidth);
				
				padRight = $(this).find('.right-input').getPadRight();
				nextLevelWidth -= padRight;
				
				padRight = $(this).find('.select_list').getPadRight();
				padLeft = $(this).find('.select_list').getPadLeft();
				nextLevelWidth -= padLeft + padRight;
				
				$(this).find('.select_list').width(nextLevelWidth);	
				$(this).find('.select_value').width(nextLevelWidth);						
				$(this).find('.inpunts .select_list a').width(nextLevelWidth);
				
			} else{
				var maxWidth = 0;
				$(this).find('.inpunts .select_list a').each(function(){
					var curWidth = $(this).outerWidth();
					if(curWidth > maxWidth) maxWidth = curWidth;
				});

				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(nextLevelWidth);
			
				padRight = $(this).find('.right-input').getPadRight();
				nextLevelWidth += padRight;
				$(this).find('.jScrollPaneContainer').width(nextLevelWidth);			
				$(this).find('.inpunts').width(nextLevelWidth);
				$(this).find('.inpunts_cont').width(nextLevelWidth);
			}
		});
		
		$(this).find('.jScrollPaneContainer').addClass('hidden');
		$(this).setCastSelAction();
		return $(this);
	}

