/*
 * jQuery selectbox v0.1.0 - vuliad@gmail.com
 *
 * Copyright (c) 2010 Remin Vladislav 16.04.2010 22:22.
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */


jQuery.fn.selectbox = function(options){
	var p=100; // start z-index!!!;
	return this.each(function() {
	//alert('');
		//	alert(p);
					p--;
		$(this).wrap('<div></div>');
		var selectbox=$(this).parent();
		$(selectbox).attr('class',$(this).attr('class')).addClass('selectbox');
		$(selectbox).css('z-index',p); // Fucking Ie7,6  .. elements
	//	$(selectbox).append('<div>adadssss</div>');
		var selectarrow=$('<div class="selectarrow"></div>');
		var selectlist=$('<div class="selectlist"></div>');
		var selecttext=$('<div class="selecttext"></div>');
		var elemsheight = 0;
		var elem;
		elem = this;
		//$(selectbox).css('overflow','hidden');
		var opened=false;
		opened=false;
		var height;
		height = TotalHeight($(elem));
		if($.browser.version=='6.0') { height+=10;}
		var width;
		width = TotalWidth($(elem));
	//	alert(width);
		//
//console.log(elem);
width=181;
	$(selectbox).width(width);
	height=22;
		$(selectbox).height(height);
	$(selectarrow).height(height);
	$(selectlist).width(width);
		$(selectlist).css('top',-4+height-parseInt($(selectbox).css("borderBottomWidth"), 10));
$(selectlist).css('left',0-parseInt($(selectbox).css("borderLeftWidth"), 10));

		//top:29px; left:-1px;


		$(selectbox).append(selecttext,selectarrow,selectlist);
		//$(this).after();
		$(this).hide();
//console.log($(this).find('option:selected').text());
		//$(this).
		$(selecttext).html($(elem).find('option:selected').text());
		$(this).find('option').each(function ()
											  {
									var selectitem = $('<div class="selectitem"></div>');
									$(selectitem).text($(this).text());
									//console.log();
									$(selectitem).data('pos',$(this).index());
									$(selectlist).append(selectitem);
									elemsheight+=TotalHeight($(selectitem));
									//alert(elemsheight);
									$(selectitem).click(function () {
                                      //if

                                      //alert($(elem).find('option').eq($(this).data('pos')).val());
								//alert('');
					if($(elem).find('option').eq($(this).data('pos')).val()=='>5')
					{
					//$(elem)
					//alert($(ele))
					newelem=$("<input />").attr('name',$(elem).attr('name')).addClass("wassel");
					 // console.log(newelem);
					$(elem).parent().parent().html(newelem);
     				$(newelem).focus();
					}
					else
					{
								$(elem).find('option').eq($(this).data('pos')).attr('selected','selected');
	//							console.log($(elem).find('option').eq($(this).data('pos')));
									$(selecttext).html($(elem).find('option:selected').text());
                    }

								//$(this).
																  });


									$(selectitem).hover(function() { 
																 
													if($(selectlist).hasClass('selexpand')) {			 $(this).addClass("selectactive");}},function () {$(this).removeClass("selectactive");})
											  });


						$(selectbox).click(function () {
				if($(selectbox).css('overflow')=='visible')
						{
						$(selectbox).css('overflow','hidden');
				//$(selectlist).css({'padding-top':'0px'});
				$(selectlist).removeClass('selexpand');
				$(selectlist).animate({'height':4},10, function () { opened=false;});

				}
		else
{
					//	console.log('sd');
				$(selectbox).css('overflow','visible');
				//if(elemsheight>140) {elemsheight=140; $(selectlist).css('overflow','scroll');}
			//	$(selectlist).css({'padding-top':'4px'});
						$(selectlist).addClass('selexpand');
				
				$(selectlist).animate({'height':elemsheight},10,function () {opened=true;});

						}

													});

					$(document).click( function () {
					if(opened)
						{
						$(selectbox).css('overflow','hidden');
						$(selectlist).removeClass('selexpand');
				//$(selectlist).css({'padding-top':'0px'});
				$(selectlist).animate({'height':4},1,function () { opened=false;});
						}
					})


function TotalWidth(theDiv)
		{
//			alert(parseInt(theDiv.css("padding-left"), 10));
///////////var theDiv = $("#theDiv");
			var tWidth = theDiv.width();

			tWidth += parseInt(theDiv.css("padding-left"), 10) + parseInt(theDiv.css("padding-right"), 10); //Total Padding Width
			if(theDiv.css("margin-left")!="auto") {tWidth += parseInt(theDiv.css("margin-left"), 10); }
			if(theDiv.css("margin-left")!="auto") {tWidth +=  parseInt(theDiv.css("margin-right"), 10); }//Total Margin Width

			//alert(parseInt(theDiv.css("margin-left"), 10) );
			tWidth += parseInt(theDiv.css("borderLeftWidth"), 10) + parseInt(theDiv.css("borderRightWidth"), 10); //Total Border Width
			//alert(tWidth);

			return tWidth;
		}


		function TotalHeight(theDiv)
		{
///////////var theDiv = $("#theDiv");
			var tHeight = theDiv.height();
			//alert(tHeight);
			//theDiv.css("padding-top")
			tHeight += parseInt(theDiv.css("padding-top"), 10);

			tHeight += parseInt(theDiv.css("padding-bottom"), 10); //Total Padding Width
			if(theDiv.css("margin-top")!="auto") {tHeight += parseInt(theDiv.css("margin-top"), 10); }

			if(theDiv.css("margin-bottom")!="auto") {tHeight += parseInt(theDiv.css("margin-bottom"), 10); } //Total Margin Width
			if(parseInt(theDiv.css("borderTopWidth"), 10)) {tHeight += parseInt(theDiv.css("borderTopWidth"), 10); }
			//alert(tHeight);

			if(parseInt(theDiv.css("borderBottomWidth"), 10)) { tHeight += parseInt(theDiv.css("borderBottomWidth"), 10); }//Total Border Width

			return tHeight;
		}



							  });
								}
