
var comboScrollPanels=new Array()

function vfComboBox(name,arr,xpos,ypos,wid,vhgt) {

	this.owner=main
	this.name=name
	this.arr=arr
	this.xpos=xpos
	this.ypos=ypos
	this.wid=wid
	this.vhgt=vhgt

	this.opened=false

	this.menu_height=16
	this.item_bg_color="#DDD"
	this.item_bg_color_alt="#FFF"
	this.over_color="#66ccff"
	this.item_height=16
	
	this.createMenu()
	this.createItems()
	
	
	
	this.menuRef.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.menuDown))
	
	//this.owner.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.mainDown))

	for(i=0;i<this.arr.length;i++) {
	
		var itemRef=this.owner.findName(this.name+"item_"+i)
		itemRef.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.itemEnter))
		itemRef.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.itemLeave))
		itemRef.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.itemDown))
	}

}



vfComboBox.prototype.createMenu=function() {


	var xaml_str='<Canvas Name="'+this.name+'_menu" Canvas.Left="'+this.xpos+'" Canvas.Top="'+this.ypos+'" >'
		xaml_str+=' <Rectangle Stroke="#888" Fill="#DDD"  Height="16" Width="150"></Rectangle>'
		xaml_str+=' <TextBlock Name="'+this.name+'mainSelection" Canvas.Left="6" Canvas.Top="1" FontFamily="Verdana" FontSize="11">Select</TextBlock>'
		xaml_str+=' <Canvas Name="'+this.name+'_arrow" Canvas.Top="2" Canvas.Left="136">'
		xaml_str+='  <Rectangle Fill="#555" Width="12" Height="12" />'
		xaml_str+='  <Path Data="M 0,0 L4,-4 -4,-4z" Fill="#FFF" Canvas.Left="6" Canvas.Top="9" />'
		xaml_str+=' </Canvas>'
		xaml_str+='</Canvas>'


	xamlTags=plugin.content.createFromXaml(xaml_str)
	this.owner.children.add(xamlTags)		
	this.menuRef=this.owner.findName(this.name+"_menu")

}


vfComboBox.prototype.createItems=function() {

	var combo_content_str=""
	var combo_box_list_height=0
	var bg_color
	
	for(i=0;i<this.arr.length;i++) {

		bg_color=this.item_bg_color
		
		if(i%2) {
			bg_color=this.item_bg_color_alt
		}

		combo_content_str+='<Canvas Name="'+this.name+'item_'+i+'" Cursor="Hand" Canvas.Left="0" Canvas.Top="'+(i*this.item_height)+'">'
		combo_content_str+=' <Rectangle Fill="'+bg_color+'" Height="'+this.item_height+'" Width="'+this.wid+'"></Rectangle>'
		combo_content_str+=' <TextBlock Canvas.Left="6" Canvas.Top="1" Width="'+(this.wid-40)+'" FontFamily="Verdana" FontSize="11">'+this.arr[i]+'</TextBlock>'
		combo_content_str+='</Canvas>'
	}

	combo_box_list_height=(i*this.item_height)+2
	
	
	//if passed in height value is smaller than 3 items make equal to 3 items
	if(this.vhgt<(this.item_height*3)) {
		this.vhgt=this.item_height*3
	}
	
	//if passed in height value is larger than list height make equal to list height
	if(this.vhgt>combo_box_list_height) {
		this.vhgt=combo_box_list_height
	}


	//global reference for mouse wheel support, still gotta find a better way for this
	scrollPanelArr[this.name+"sp"]=new scrollablePanel(this.name+"sp",this.xpos,(this.ypos+this.menu_height+1),this.wid,this.vhgt,combo_content_str,"#333",9,null,"#EEE",combo_box_list_height)
	scrollPanelArr[this.name+"sp"].setPanelVisibility(0)
	comboScrollPanels.push(scrollPanelArr[this.name+"sp"])
}







function mainDown(s) {
	//alert(s)
	for(each in comboScrollPanels) {
		comboScrollPanels[each].setPanelVisibility(0)
	}
}

vfComboBox.prototype.menuDown=function(s) {
	
	for(each in comboScrollPanels) {
		comboScrollPanels[each].setPanelVisibility(0)
	}
	//alert("menu"+this.name)
	scrollPanelArr[this.name+"sp"].setPanelVisibility(1)
	this.opened=true
}



vfComboBox.prototype.itemEnter=function(s) {
	var rec=s.children.getItem(0)
	this.orig_color=rec.fill
	rec.fill=this.over_color
}

vfComboBox.prototype.itemLeave=function(s) {
	s.children.getItem(0).fill=this.orig_color
}

vfComboBox.prototype.itemDown=function(s) {
	var sel=s.children.getItem(1).text
	s.findName(this.name+"mainSelection").text=sel
	scrollPanelArr[this.name+"sp"].setPanelVisibility(0)
	this.opened=false
}
