/// Author: M.Hazar ARTUNER
/// Date: 01.04.2010
/// Version: 1.0

jQuery.fn.cb = function(data){
	return this.each(function(){
		$(this).after(convertToLi(data));
		
		var comboPos=$(this).offset();
		var comboLeft = comboPos.left;
		var comboTop = comboPos.top + $(this).height();
		var comboWidth = $(this).width();
		var height = 80;
		var label = $(this);
		
		$("#combo").css({
			"list-style":"none",
			"height":"0",
			"width":comboWidth,
			"position":"absolute",
			"left":comboLeft,
			"top":comboTop
			});
		
		var btnLeft = comboLeft + comboWidth - $("#comboButton").width();
		var btnTop = comboPos.top;
		
		$("#comboButton").css({
			"height":$(this).height(),
			"position":"absolute",
			"left":btnLeft,
			"top":btnTop
		});
		
		$("#comboButton").click(function(){
			if($("#combo").height() > 0)
				$("#combo").animate({"height":"0"});
			else
				$("#combo").animate({"height":height});
		});
		
		$(this).click(function(){
			if($("#combo").height() > 0)
				$("#combo").animate({"height":"0"});
			else
				$("#combo").animate({"height":height});
		});
		
		$("#combo li").click(function(){
			$("#combo").animate({"height":"0"});
			label.attr("value",$(this).attr("id"));
			label.html($(this).html());
		});
		
	});
	
	
	function convertToLi(data)
	{
		var html = '<ol id="combo">';
		
		for(var i=0; i<data.contents.length; i++)
		{
			html += '<li id="' + data.contents[i].countryId + '">' + data.contents[i].countryName + "</li>";
		}
		
		html += "</ol>";
		
		html +="<span id='comboButton'></span>";
		
		return html;
	}
};
