/*
Dropdown version 1.2 (01.04.2009)
Copyright(c)2009 Remiya Solutions All Rights Reserved
Website: http://remiya.com

This script may be used for both free and commercial purposes only if the
following conditions are met:

 1. A link back to the author's website is provided on the website, where
    the script is being used.
	
 2. You are hereby licensed to make as many copies of this script as you
    need in order to distribute your own work (including for commercial use).
	You are specifically prohibited from charging, or requesting donations,
	for any such copies without prior written permission.
	
 3. You ARE NOT allowed to distribute for download the script via electronic
    means (Internet, e-mail, etc). This means that this software is to be
	available to download from the official website (http://remiya.com) ONLY.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
$.fn.dropdown = function(options){
    var d={
	    elements:[],
	    width:250, height:28, padding:4,
	    image:"dropdown.gif",
		mouseovercolor:"#8DD8F8",
		mouseoutcolor:"#01BBD6"
	}
	this.value = function(){
	    var sb_id = $(this).attr("id")+"_sb";
		return $("#"+sb_id).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
	};
	this.select = function(position){
	    if(is_int(position)){
		    $(d.elements).each(function(){
			    var sb_id = $(this).attr("id")+"_sb";
				var html = $(this).find("ul:first li:eq("+position+")").html();
				if(html)$("#"+sb_id).html(html);
			});
		}else{
		    $(d.elements).each(function(){
			    var sb_id = $(this).attr("id")+"_sb";
				var lis = $(this).find("ul:first li");
				for(var i=0;i<lis.length;i++){
				    if(position==$(lis[i]).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '')){
					    $("#"+sb_id).html($(lis[i]).html());
						break;
					}
				}
			});
		}
		function is_int(variable){
	        return variable.constructor === Number && Math.round(variable, 0) === variable;
        }
	};
    function init(element){
	    d.elements[d.elements.length]=element;
	    var sb_id = $(element).attr("id")+"_sb";
		var tb_id = $(element).attr("id")+"_tb";
	    $(element).prepend("<table  id='"+tb_id+"' cellspacing='0' '' cellpadding='0' style='margin:0px;border-collapse:collapse;'><tr><td id='"+sb_id+"' valign='middle'>DROPDOWN</td></tr></table>");
		$("#"+tb_id).height(d.height).width(d.width)
		   .css("background-image","url("+d.image+")")
		   .css("background-repeat","no-repeat");
	    // List
		$(element).find("ul:first").css("display","none")
		    //.css("display","none")
		    .css("border","1px solid silver")
			.css("list-style-type","none")
			.css("margin",0).css("padding",0)
			.width(d.width);
		// Mouse over
		$(element).mouseover(function(){
		    $(element).find("ul:first").css("display","block");
		});
		// Mouse out
		$(element).mouseout(function(){
		    $(element).find("ul:first").css("display","none");
		});
		// List items
		$(element).find("ul:first").each(function(){
			$(this).css("z-index","9999").css("position","absolute");
			//$(this).width($("#"+d.id).width());
			$(this).find("li").each(function(){
			    $(this).css("float","left").css("clear","left").css("width","100%");
				$(this).css("background",d.mouseoutcolor);
			    $(this).mouseover(function(){$(this).css("background",d.mouseovercolor);});
				$(this).mouseout(function(){$(this).css("background",d.mouseoutcolor);});
				$(this).click(function(){
				    var old = $("#"+sb_id).text(); 
					$("#"+sb_id).html($(this).html());
					if(d.change){if($(this).text()!=old)d.change($(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''));}
				    if(d.click){d.click($(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''));}
				});
			});
		});
		$(element).find("ul:first").find("li:first").each(function(){
		    $("#"+sb_id).html($(this).html());
		});
	}
	return this.each(function(){
	    if(undefined===window.jquery_remiya_dropdown){window.jquery_remiya_dropdown=[];}
	    if(!$(this).attr("id")){$(this).attr("id","jquery_dropdown_"+window.jquery_remiya_dropdown.length);window.jquery_remiya_dropdown[window.jquery_remiya_dropdown.length]=window.jquery_remiya_dropdown.length;}
		if(undefined!==options)$.extend(d,options);
		init(this);	    
    });
};