// JavaScript Document
jQuery.fn.CRselectBox = jQuery.fn.sBox = function(options){
	options = jQuery.extend({
		animated : false
	},options);
	var _self = this;
	/*构建结构*/
	var _parent = _self.parent();
	var wrapHtml = '<div class="CRselectBox"></div>';
	var $wrapHtml = jQuery(wrapHtml).appendTo(_parent);
	var selectedOptionValue = _self.find("option:selected").attr("value");
	var selectedOptionTxt = _self.find("option:selected").text();
	var name = _self.attr("name");
	var id = _self.attr("id");
	var inputHtml = '<input type="hidden" value="'+selectedOptionValue+'" name="'+name+'" id="'+id+'"/>';
	jQuery(inputHtml).appendTo($wrapHtml);
	var inputTxtHtml = '<input type="hidden" value="'+selectedOptionTxt+'" name="'+name+'_CRtext" id="'+id+'_CRtext"/>';
	jQuery(inputTxtHtml).appendTo($wrapHtml);
	var aHtml = '<a class="CRselectValue" href="#">'+selectedOptionTxt+'</a>';
	jQuery(aHtml).appendTo($wrapHtml);
	var ulHtml = '<ul class="CRselectBoxOptions"></ul>';
	var $ulHtml = jQuery(ulHtml).appendTo($wrapHtml);
	var liHtml = "";
	_self.find("option").each(function(){
		if(jQuery(this).attr("selected")){
			liHtml += '<li class="CRselectBoxItem"><a href="#" class="selected" rel="'+jQuery(this).attr("value")+'">'+jQuery(this).text()+'</a></li>';
		}else{
			liHtml += '<li class="CRselectBoxItem"><a href="#" rel="'+jQuery(this).attr("value")+'">'+jQuery(this).text()+'</a></li>';
		}
	});
	jQuery(liHtml).appendTo($ulHtml);
	/*添加效果*/
	jQuery( $wrapHtml, _parent).hover(function(){
		jQuery(this).addClass("CRselectBoxHover");
	},function(){
		jQuery(this).removeClass("CRselectBoxHover");
	});
	jQuery(".CRselectValue",$wrapHtml).click(function(){
		jQuery(this).blur();
		if( jQuery(".CRselectBoxOptions",$wrapHtml).is(":hidden") ){
			if(options.animated){
				jQuery(".CRselectBoxOptions").slideUp("fast");
				jQuery(".CRselectBoxOptions",$wrapHtml).slideDown("fast");
			}else{
				jQuery(".CRselectBoxOptions").hide();
				jQuery(".CRselectBoxOptions",$wrapHtml).show();
			}
		}
		return false;
	});
	jQuery(".CRselectBoxItem a",$wrapHtml).click(function(){
		jQuery(this).blur();
		var value = jQuery(this).attr("rel");
		var txt = jQuery(this).text();
		jQuery("#"+id).val(value);
		jQuery("#"+id+"_CRtext").val(txt);
		jQuery(".CRselectValue",$wrapHtml).text(txt);
		jQuery(".CRselectBoxItem a",$wrapHtml).removeClass("selected");
		jQuery(this).addClass("selected");
		if(options.animated){
			jQuery(".CRselectBoxOptions",$wrapHtml).slideUp("fast");
		}else{
			jQuery(".CRselectBoxOptions",$wrapHtml).hide();
		}
		if(value=="head")
	    {
	      return false;
	    }
		window.open(value);
		return false;
	});
	jQuery(document).click(function(event){
		if( jQuery(event.target).attr("class") != "CRselectBox" ){
			if(options.animated){
			jQuery(".CRselectBoxOptions",$wrapHtml).slideUp("fast");
			}else{
				jQuery(".CRselectBoxOptions",$wrapHtml).hide();
			}
		}
	});
	_self.remove();
	return _self;
}

