

var Adv = {};

Adv.cache = {};

Adv.index = 0;

Adv.typeIndex = 0;

Adv.indexLength = 0;

Adv.typeLength = 0;

Adv.mouseIn = false;

Adv.AdvertiseController = function( iUrl ) {
	ControllerBase.call( this, iUrl );
};

Adv.AdvertiseController.prototype = new ControllerBase();

Adv.AdvertiseController.prototype.initView = function() {
};

Adv.AdvertiseController.prototype.handle = function( xmlHttp ) {
	//alert(xmlHttp.responseText);
	var docs = Ext.util.JSON.decode( xmlHttp.responseText );
	Adv.cache[Adv.typeIndex] = docs;
};

Adv.processChange = function(){
	var docs = Adv.cache[Adv.typeIndex];
	if( docs==null||docs==undefined ){
		var typeId = Adv.typeIds[Adv.typeIndex];
		Adv.getResult( typeId, Adv.businessId );
		docs = Adv.cache[Adv.typeIndex];
		if( docs==null||docs==undefined ){
			return false;
		}
	}
	Adv.indexLength = docs.length;
	if( Adv.indexLength>0 ){
		Adv.changeView( docs[Adv.index] );
	}
	return true;
};

Adv.autoChange = function(){
	if( !Adv.mouseIn ){
		//alert( "index="+Adv.index+" indexLength="+Adv.indexLength+" typeIndex="+Adv.typeIndex+" typeLength="+Adv.typeLength );
		if( Adv.processChange() ){
			if( Adv.indexLength>0 ){
				if( (Adv.index+1)==Adv.indexLength ){
					Adv.typeIndex = (Adv.typeIndex+1)%Adv.typeLength;
				}
				Adv.index = (Adv.index+1)%Adv.indexLength;
			}else{
				Adv.typeIndex = (Adv.typeIndex+1)%Adv.typeLength;
			}	
		}
	}
	setTimeout( Adv.autoChange, 5000 );
};

Adv.change = function( typeIndex ){
	//alert("change to "+typeIndex);
	Adv.index = 0;
	Adv.typeIndex = typeIndex;
	Adv.processChange();
};

Adv.getResult = function( typeId, businessId ){
	//alert( "typeId="+typeId+" businessId="+businessId );
	var controller = new Adv.AdvertiseController( "adv.htm" );
	controller.addUrlParameter( "tp", typeId );
	controller.addUrlParameter( "bid", businessId );
	controller.doGet( false );
};

Adv.changeView = function( result ){
	Ext.get( "advTitle" ).dom.href = result.url;
	var cname = result.doc[0];
	if( cname==null||cname==undefined ){
		cname = "";
	}
	var ename = result.doc[1];
	if( ename==null||cname==undefined ){
		ename = "";
	}
	var title = trim( cname+" "+ename );
	Ext.get( "advTitle" ).dom.innerHTML = title;
	if( Adv.index==0 ){
		var typeName = Adv.typeNames[Adv.typeIndex];
		if( Adv.title!=""&&Adv.title!=null&&Adv.title!=undefined ){
			Ext.get( "advLabel" ).dom.innerHTML = "--- "+ Adv.title +" ---";
		}else{
			Ext.get( "advLabel" ).dom.innerHTML = " ";
		}
		Ext.get( "advTypeName" ).dom.innerHTML = " "+ typeName +" ";
		Ext.get( "advList" ).dom.href = Adv.getListUrl();
		for( var i=0; i<Adv.typeLength; i=i+1 ){
			var id = "page_"+i;
			if( i==Adv.typeIndex ){
				Ext.get( id ).dom.style.backgroundColor = "#b2e05d";
				Ext.get( id ).dom.style.borderColor = "#b2e05d";
				Ext.get( id ).dom.style.color = "#fff";
			}else{
				Ext.get( id ).dom.style.backgroundColor = "#fff";
				Ext.get( id ).dom.style.borderColor = "#ddd";
				Ext.get( id ).dom.style.color = "#88af3f";
			}
		}
	}
};

Adv.getListUrl = function(){
	var typeId = Adv.typeIds[Adv.typeIndex];
	var listUrl = "advList.htm";
	listUrl = appendUrlParameter( listUrl, "tp", typeId );
	listUrl = appendUrlParameter( listUrl, "bid", Adv.businessId );
	return listUrl;
}

Adv.changeViewWithFade = function( result ){
	var changeFade = function() {
		Adv.changeView( result );
		Ext.get( "advTitle" ).fadeIn( { useDisplay:true, duration:0.5 } ); 
	};
	Ext.get( "advTitle" ).fadeOut( { callback:changeFade, duration:0.5, useDisplay:true });
};

Adv.changeViewWithSlide = function( result ){
	var changeSlide = function() {
		Adv.changeView( result );
		Ext.get( "advTitle" ).slideIn( "", {duration:2} );
	};
	Ext.get( "advTitle" ).slideOut( "", { callback:changeSlide, duration:2, useDisplay:true } );
};

Adv.pageOnMouseOver = function( td, typeIndex ){
	if( typeIndex!=Adv.typeIndex ){
		td.style.backgroundColor = "#f1ffd6";
		td.style.borderColor = "#85bd1e";
		td.style.color = "#638425";
	}
};

Adv.pageOnMouseOut = function( td, typeIndex ){
	if( typeIndex!=Adv.typeIndex ){
		td.style.backgroundColor = "#fff";
		td.style.borderColor = "#ddd";
		td.style.color = "#88af3f";
	}
};

Adv.onMouseOver = function(){
	Adv.mouseIn=true;
	//alert( "mouseIn" );
};

Adv.onMouseOut = function(){
	Adv.mouseIn=false;
	//alert( "mouseOut" );
};




