
var BUTTON_BASE_TYPE = { 
 
 	ONLY_BG		: 0,
 	BG_TEXT		: 1,
	BG_FG		: 2, 	
	BG_FG_TEXT	: 3,
}

var BUTTON_BASE_STATE = { 
 
 	NORMAL		: 0,
 	HIGHLIGHT	: 1,
	SELECT		: 2, 	
	DIM			: 3,
	MAX			: 4
}


function ButtonBase() {

    this.FgImgLayerIndex	= -1;		//Fg Image 
    this.TextLayerIndex		= -1;
       
	this.BgImgPath = new Array(BUTTON_BASE_STATE.MAX);
	this.FgImgPath = new Array(BUTTON_BASE_STATE.MAX);
	  
	this.TextStyle = new Array(BUTTON_BASE_STATE.MAX);
	this.BgImgStyle = new Array(BUTTON_BASE_STATE.MAX);
	this.FgImgStyle = new Array(BUTTON_BASE_STATE.MAX);
   
	this.buttonType = BUTTON_BASE_TYPE.ONLY_BG;
	this.state = BUTTON_BASE_STATE.NORMAL;
   	   	
	this.Id	= null;

	////////// for 720p image
    
    this.FgEID = null;
    this.TextEID = null;	
   	
	
	////////// for 720p image
    
    this.FgEID = null;
    this.TextEID = null;	
   	
   	this.flag_divide = false;
   	    
	this.LeftBGLayerIndex	= -1;
	this.CenterBGLayerIndex	= -1;
	this.RightBGLayerIndex	= -1;
		
    this.LeftBgImgPath = new Array(BUTTON_BASE_STATE.MAX);
    this.CenterBgImgPath = new Array(BUTTON_BASE_STATE.MAX);
    this.RightBgImgPath = new Array(BUTTON_BASE_STATE.MAX);
    
	////////////
	
	CompBase.call(this);
}


ButtonBase.prototype = new CompBase(); 

ButtonBase.prototype.Create = function(eID,nType, OptionDivide){
	
    alert("ButtonBase.Create(" + eID + ")");
    if(eID == null) {
        alert("ButtonBase.Create()  >>>   Param is null !!  ");
        return false;
    }
    	
	this.CreateBG(eID);	
	
	if(nType > 1) {
		this.CreateFG(eID + "Fg");	
	}
	if((nType % 2) > 0) {
		this.CreateText(eID + "Text");
	}
	
		
	if(OptionDivide > 0)
	{
		this.CreateDiveMode(eID,OptionDivide,nType);
	}
		
}

ButtonBase.prototype.CreateFG = function(eID){

    this.FgEID = eID;
    		
    var imageLayer = new ImageLayer(eID);
    
	this.FgImgLayerIndex = this.LayerArray.length;
	
    this.InsertLayer(imageLayer);
	
}

ButtonBase.prototype.CreateText = function(eID){

    this.TextEID = eID;	
    
    var textLayer = new TextLayer(eID);
    
	this.TextLayerIndex = this.LayerArray.length;
	
    this.InsertLayer(textLayer);
	
}


ButtonBase.prototype.SetBgImage = function(stateIndex,ImagePath){

    	
    if(this.flag_divide == true)
    {
    	alert("bgImage : " + ImagePath);
    	
    	var result = ImagePath.indexOf('_center'); 
    	var Url;
    	if(result != -1)
    	{
    		Url = ImagePath.substring(0,result);
    		
    		alert("Url : " + Url);
    	}

    	this.LeftBgImgPath[stateIndex] = Url + '_left.png';
    	this.CenterBgImgPath[stateIndex] = Url + '_center.png';    	
    	this.RightBgImgPath[stateIndex] = Url + '_right.png';
    	
   	}
   	else
   	{
    	this.BgImgPath[stateIndex] = ImagePath;   		
   	}
    	    
    if(stateIndex == BUTTON_BASE_STATE.NORMAL)
    {

	    if(this.flag_divide == true)
	    {	    	
	   		this.LayerArray[this.LeftBGLayerIndex].SetBgImage(this.LeftBgImgPath[0]);
	   		this.LayerArray[this.CenterBGLayerIndex].SetBgImage(this.CenterBgImgPath[0]);
	   		this.LayerArray[this.RightBGLayerIndex].SetBgImage(this.RightBgImgPath[0]);	    	
	    }
	    else
	    {
	    	this.LayerArray[this.BGLayerIndex].SetImage(ImagePath);
	    }
    }
}

ButtonBase.prototype.SetFgImage = function(stateIndex,ImagePath){
    	
    this.FgImgPath[stateIndex] = ImagePath;
    
    if(stateIndex == BUTTON_BASE_STATE.NORMAL)
    {
		this.LayerArray[this.FgImgLayerIndex].SetImage(ImagePath);
    }
}

ButtonBase.prototype.SetBgStyle = function(stateIndex,Style){
    
    this.BgImgStyle[stateIndex] = Style;
    
    if(stateIndex == BUTTON_BASE_STATE.NORMAL)
    {
		this.LayerArray[this.BGLayerIndex].SetStyle(Style);		
    }
}

ButtonBase.prototype.SetFgStyle = function(stateIndex,Style){
    
    this.FgImgStyle[stateIndex] = Style;
    
    if(stateIndex == BUTTON_BASE_STATE.NORMAL)
    {
		this.LayerArray[this.FgImgLayerIndex].SetStyle(Style);		
    }
}

ButtonBase.prototype.SetTextStyle = function(stateIndex,Style){
    
    this.TextStyle[stateIndex] = Style;
    
    if(stateIndex == BUTTON_BASE_STATE.NORMAL)
    {
		this.LayerArray[this.TextLayerIndex].SetStyle(Style);		
    }
}

ButtonBase.prototype.SetText = function(string){
	
	if(this.TextLayerIndex > 0)  
	{
		this.LayerArray[this.TextLayerIndex].SetText(string);
	}

}

ButtonBase.prototype.ChangeMode = function() {
	
	var curState = this.state;
	
	//Set Bg
    if(this.flag_divide == true)
    {
		if(this.CenterBgImgPath[curState]) 
		{
			//check    	
	   		this.LayerArray[this.LeftBGLayerIndex].SetBgImage(this.LeftBgImgPath[curState]);
	   		this.LayerArray[this.CenterBGLayerIndex].SetBgImage(this.CenterBgImgPath[curState]);
	   		this.LayerArray[this.RightBGLayerIndex].SetBgImage(this.RightBgImgPath[curState]);			
		}    	
    }
    else{
		if(this.BgImgPath[curState]) 
		{
			this.LayerArray[this.BGLayerIndex].SetImage(this.BgImgPath[curState]);
		}
    }	
	
	
	if(this.FgImgPath[curState]){
		this.LayerArray[this.FgImgLayerIndex].SetImage(this.FgImgPath[curState]);	
	}
	
	if(this.TextStyle[curState]){
		this.LayerArray[this.TextLayerIndex].SetStyle(this.TextStyle[curState]);
	}

	if(this.BgImgStyle[curState]){
		this.LayerArray[this.BGLayerIndex].SetStyle(this.BgImgStyle[curState]);
	}
	
	if(this.FgImgStyle[curState]){
		this.LayerArray[this.FgImgLayerIndex].SetStyle(this.FgImgStyle[curState]);
	}	
}

ButtonBase.prototype.Show = function() {
	
	this.Showstate = true;
	
	if(this.flag_ClickEvent == true)
	{
		if(this.state == BUTTON_BASE_STATE.NORMAL)
		{
			this.SetClickEvent();	
		}
	}	
	$("#"+ this.LayerArray[this.BGLayerIndex].Obj).css('display', 'block');	
}

ButtonBase.prototype.Hide = function() {
	
	this.Showstate = false;
	
	if(this.flag_ClickEvent == true)
	{
		this.ResetClickEvent();	
	}		
	$("#"+ this.LayerArray[this.BGLayerIndex].Obj).css('display', 'none');
}

ButtonBase.prototype.highlight = function(){

    this.state    = BUTTON_BASE_STATE.HIGHLIGHT;
	this.ChangeMode();

	if(this.flag_ClickEvent == true)
	{
		this.SetClickEvent();	
	}	
}

ButtonBase.prototype.blur = function(){

    this.state    = BUTTON_BASE_STATE.NORMAL;
	this.ChangeMode();
	
	if(this.flag_ClickEvent == true)
	{
		this.SetClickEvent();	
	}		
}

ButtonBase.prototype.select = function(){
    
    this.state    = BUTTON_BASE_STATE.SELECT;
	this.ChangeMode();
	
	if(this.flag_ClickEvent == true)
	{
		this.ResetClickEvent();	
	}	
}

ButtonBase.prototype.dim = function(){
    
    this.state    = BUTTON_BASE_STATE.DIM;
	this.ChangeMode();
	
	if(this.flag_ClickEvent == true)
	{
		this.ResetClickEvent();	
	}	
}

ButtonBase.prototype.SetID = function(nID){
    this.ID = nID;
}

ButtonBase.prototype.GetID = function(){
    return this.ID;
}

ButtonBase.prototype.GetState = function(){
    return this.state;
}

ButtonBase.prototype.CreateDiveMode = function(eID,OptionDivide,nType){

	this.flag_divide = true;
	

	var ButtonHeight = 41*2;
	var LeftBtnHTML = "<div id = '" + eID + "_left'></div>";
	var CenterBtnHTML = "<div id = '" + eID + "_center'></div>";
	var RightBtnHTML = "<div id = '" + eID + "_right'></div>";
	
	//alert("LeftBtnHTML  : " + LeftBtnHTML);
	//alert("CenterBtnHTML  : " + CenterBtnHTML);
	//alert("RightBtnHTML  : " + RightBtnHTML);
	
	//insert html
	$("#"+eID).append(LeftBtnHTML);
	$("#"+eID).append(CenterBtnHTML);
	$("#"+eID).append(RightBtnHTML);
	
	var OrginWidth = $("#"+eID).css('width');
	var OrginHeight = $("#"+eID).css('height');

	var WidthLen= OrginWidth.indexOf("px");
	var HeightLen= OrginHeight.indexOf("px");
	
	var w_int = parseInt(OrginWidth.substring(0,WidthLen));
	var h_int = parseInt(OrginHeight.substring(0,HeightLen));
		
	alert("w_int  : " + w_int);
	alert("h_int  : " + h_int);
	
	//add style class
	$("#"+eID+"_left").addClass('cButtonWithTreeObject');
	$("#"+eID+"_left").css('left','0');
	$("#"+eID+"_left").css('width',OptionDivide);
	$("#"+eID+"_left").css('height',h_int);	
				
	$("#"+eID+"_center").addClass('cButtonWithTreeObject');
	$("#"+eID+"_center").css('left',OptionDivide);
	$("#"+eID+"_center").css('width',w_int-(OptionDivide*2));
	$("#"+eID+"_center").css('height',h_int);
				
	$("#"+eID+"_right").addClass('cButtonWithTreeObject');
	$("#"+eID+"_right").css('left',w_int-OptionDivide);			
	$("#"+eID+"_right").css('width',OptionDivide);
	$("#"+eID+"_right").css('height',h_int);
		        	        	
	var left_image_layer = new ImageLayer(eID+'_left');
	var center_image_layer = new ImageLayer(eID+'_center');
	var right_image_layer = new ImageLayer(eID+'_right');

	this.LeftBGLayerIndex = this.LayerArray.length;
    this.InsertLayer(left_image_layer);
        	
	this.CenterBGLayerIndex = this.LayerArray.length;
    this.InsertLayer(center_image_layer);
    
	this.RightBGLayerIndex = this.LayerArray.length;
    this.InsertLayer(right_image_layer);
    
    //Set Z-index
	if(nType > 1) {
		$('#'+this.FgEID).css('z-index','9')
	}
	if((nType % 2) > 0) {
		$('#'+this.TextEID).css('z-index','9')
	}    
    
}

