/*
**Jmx Product Option Group
**Author:Camen
**Date:2007-4-14
**Last modified 2008-4-29
**
*/
var JmxOptionGroup=new (function(){
	this.OptionGroups=[];
	this.pictureUrlBase="http://www.mymodelships.com/productInfo/";
	this.ProductPrice=0;
	this.ProductID=0;
	this.isLoaded=false;
	this.getOptionIDs=function(){
		var a=[];
		for(var i=0;i<this.OptionGroups.length;i++){
			a[a.length]=this.OptionGroups[i].selectValue;
		}
		return a;
	}
	this.GetTotalPrice=function(){
	    var Price=Number(this.ProductPrice);
		for(var i=0;i<this.OptionGroups.length;i++){
		    if(this.OptionGroups[i].selectText!=''){
		            Price +=Number(this.OptionGroups[i].selectValue);
		    }
		}
		return Number(Price.toFixed(2));
	}
	this.preLoadImage=function(){
	    if(window.addEventListener){
	        window.addEventListener("load",JmxOptionGroup.LoadImage,false);
	    }else{
	        window.attachEvent("onload",JmxOptionGroup.LoadImage);
	    }
	}
	this.LoadImage=function(){
	    var Images=[];
	    for(var i=0;i<JmxOptionGroup.OptionGroups.length;i++){
	        for(var j=0;j<JmxOptionGroup.OptionGroups[i].optionItems.length;j++){
	            if(JmxOptionGroup.OptionGroups[i].optionItems[j].image!=""){
	                Images[Images.length]=new Image();
	                Images[Images.length-1].src=JmxOptionGroup.pictureUrlBase + JmxOptionGroup.OptionGroups[i].optionItems[j].image;
	            }
	        }
	    }
	}
	this.ShowPicture=function(GroupIndex){
		var w=window.open();
		var g=JmxOptionGroup.OptionGroups[GroupIndex];
		var Html='<Table border="0" cellspacing="1" cellpadding="2" style="position:absolute;top:8px;left:8px;font-size:8pt;" bgcolor="#cfcfcf" align="center">\
				  <Tr>\
					<td colspan="2" bgcolor="#ffffff" style="font-size:14pt;color:#988600;"><b>' + g.name+ '</b></td>\
				  </tr>';
		for(var i = 0;i<g.optionItems.length;i++){
			Html += '<tr>\
					<td bgcolor="#ffffff">' + g.optionItems[i].name + '</td>\
					<td bgcolor="#ffffff">';
			if(g.optionItems[i].image!=''){
				Html += '<img src="' + JmxOptionGroup.pictureUrlBase + g.optionItems[i].image + '">';
			}
			Html += '</td>\
				  </tr>';
		}
		Html += '</Table>'
		w.document.write(Html);
	}
	this.fixLableWidth=function(){
		if(JmxOptionGroup.isLoaded){
			var lb=document.getElementsByTagName("label");
			var widthMax=0;
			for(var i=0;i<lb.length;i++){
				if(lb[i].name == "Jmx_OptionGroup_Label" && lb[i].offsetWidth > widthMax)
					widthMax = lb[i].offsetWidth;
			}
			if(widthMax<10)widthMax=150;
			for(var i=0;i<lb.length;i++){
				if(lb[i].name=="Jmx_OptionGroup_Label")
					lb[i].style.width = widthMax + "px";
			}
		}
	}
})
var OptionGroup=function(GroupID,GroupName,OptionItems){
	this.index=JmxOptionGroup.OptionGroups.length;
	JmxOptionGroup.OptionGroups[JmxOptionGroup.OptionGroups.length]=this;
	this.id='JmxOptionGroups_' + this.index;
	this.containerId='JmxOptionGroup_Container_' + this.index;
	this.groupID=GroupID;
	this.name=GroupName;
	this.optionItems=OptionItems;
	this.html='';
	this.subTag="_sub";
	this.selectId=0;
	this.selectText="";
	this.selectValue=0;
	this.selectIncrease=0;
	this.selectImage="";
	this.addOption=function(item){
		this.optionItems[this.optionItems.length]=item;
		return item;
	}
	this.renderSub=function(Index){
		this.resetSub();
		if( typeof this.optionItems[Index].subGroup == "object"){
			this.optionItems[Index].subGroup.render(this.id + this.subTag);
		}else{
			if(document.getElementById(this.id + this.subTag))
				document.getElementById(this.id + this.subTag).innerHTML="";
		}
		this.selectId=this.optionItems[Index].id;
		this.selectText=this.optionItems[Index].name;
		this.selectValue=this.optionItems[Index].value;
		this.selectIncrease=this.optionItems[Index].increase;
		this.selectImage=this.optionItems[Index].image;
	}
	this.resetSub=function(){
		for(var i=0;i<this.optionItems.length;i++){
			if(typeof this.optionItems[i].subGroup == "object"){
				this.optionItems[i].subGroup.selectId=0;
				this.optionItems[i].subGroup.selectText="";
				this.optionItems[i].subGroup.selectValue=0;
				this.optionItems[i].subGroup.selectIncrease=0;
				this.optionItems[i].subGroup.selectImage="";
				this.optionItems[i].subGroup.resetSub();
			}
		}
		//fixed the lable width
		JmxOptionGroup.fixLableWidth();
	}
	this.render=function(objID){
		this.html ='<div id="' + this.containerId + '" style="padding:5px 0"><label name="Jmx_OptionGroup_Label" style="display:block;float:left;">' + this.name + ':</label>&nbsp;<select name="Jmx_OptionGroup_' + this.groupID + '" id="' + this.id + '" onchange="JmxOptionGroup.OptionGroups['+this.index+'].renderSub(this.options.selectedIndex)">';
		if(this.optionItems.length>0){
			var havePicture=false;
			for(var i=0;i<this.optionItems.length;i++){
				this.html +='<option value="'+this.optionItems[i].id+'" >'+this.optionItems[i].name + " +$" + this.optionItems[i].value +'</option>';
				if(this.optionItems[i].image!='')havePicture=true;
			}
		    this.html += '</select>';
			if(havePicture)this.html += '<a href="ShowOptionPicture.aspx?Gid=' + this.groupID + '" target="_blank" title="show option pictures">See Picture</a><!--<input type="button" name="' + this.id + '_seePicture" id="' + this.id + '_seePicture" value="See Picture" onclick="JmxOptionGroup.ShowPicture(' + this.index + ')" class="yellowbutton"/>-->';
		}else{this.html += '</select>';}
        this.html += '</div><div id="' + this.id + this.subTag + '" style="display:none"></div>';
		this.selectId=this.optionItems[0].id;
		this.selectText=this.optionItems[0].name;
		this.selectValue=this.optionItems[0].value;
		this.selectIncrease=this.optionItems[0].increase;
		this.selectImage=this.optionItems[0].image;
		if(typeof objID == "string"){
			if(document.getElementById(objID)){
				document.getElementById(objID).innerHTML=this.html;
				document.getElementById(objID).style.display="";
			}
		}else{
			document.write(this.html);
		}
		if(this.optionItems.length>0){
			if(typeof this.optionItems[0].subGroup == "object"){
				this.renderSub(0);
				setDefault(document.getElementById(this.id),this.optionItems)
			}else{
				this.resetSub();
				setDefault(document.getElementById(this.id),this.optionItems)
			}
		}
		function setDefault(obj,Items){
			var MinItemID=0;
			var MinItemValue=0;
			if(Items.length>0){
				MinItemValue=Items[0].value;
				MinItemID=Items[0].id;
			}
			for(var i=0;i<Items.length;i++){
				if(Items[i].value<MinItemValue){
					MinItemID=Items[i].id;
					MinItemValue =Items[i].value;
				}
			}
			if(Items.length>0){
				obj.value=MinItemID;
				obj.onchange();
			}
		}
	}
}
var Item=function(id,name,value,increase,image,subGroup){
	this.id=id;
	this.name=name;
	this.value=value;//增加的费用
	this.increase=increase;//增加产品价格的百分比
	this.image=image;
	this.value=(this.increase>0)?Number((JmxOptionGroup.ProductPrice*(this.increase/100)).toFixed(2)):this.value;
	this.subGroup=subGroup;
}
