
var DesignWidgets_ToolbarItem = Class.create({
	id: '',
	pushButton: true,
	toggleButton: false,
	disabled: false,
	pushed: false,
	normalIcon:'',
	overIcon:'',
	pushedIcon:'',
	normalTextColor:'',
	overTextColor:'',
	pushedTextColor:'',

	onClickAction: function(toolbarItem) {},
	
	initialize: function(id,pushButton,toggleButton,pushed,disabled,normalIcon,normalText,overIcon,overText,pushedIcon,pushedText,disabledIcon,disabledText) {
		this.id = id;
		this.pushButton = pushButton;
		this.toggleButton = toggleButton;
		this.pushed = pushed;
		this.disabled = disabled;
		this.normalIcon = normalIcon;
		this.normalTextColor = normalText;
		this.overIcon = overIcon;
		this.overTextColor = overText;
		this.pushedIcon = pushedIcon;
		this.pushedText = pushedText;
		this.disabledIcon = disabledIcon;
		this.disabledText = disabledText;
		
		pb.media.imagePreloader.load(this.normalIcon);
		pb.media.imagePreloader.load(this.overIcon);
		pb.media.imagePreloader.load(this.pushedIcon);
		pb.media.imagePreloader.load(this.disabledIcon);
	},
	
	disable: function() {
		this.disabled = true;
		this.setState('disabled');
	},
	
	enable: function() {
		this.disabled = false;
		this.setState('normal');
	},
	
	setDisabled: function(disabled) {
		this.disabled = disabled;
		if (this.disabled) this.setState('disabled');
		else this.setState('normal');
	},
	
	click: function() {		
		if (!this.pushButton || this.disabled) return;
		if (this.toggleButton) {
			this.pushed = !this.pushed;
		}
		this.onClickAction(this);
		this.out();
	},
	
	over: function() {
		if (!this.pushButton || this.disabled) return;
		$(this.id).style.color = this.overTextColor;

		if (this.toggleButton && this.pushed) return;
		$(this.id).style.backgroundImage = 'url(' + this.overIcon + ')';
	},
	
	out: function() {
		if (!this.pushButton || this.disabled) return;
		if (this.pushed) {
			$(this.id).style.backgroundImage = 'url(' + this.pushedIcon + ')';
			$(this.id).style.color = this.pushedTextColor;
		}
		else {
			$(this.id).style.backgroundImage = 'url(' + this.normalIcon + ')';
			$(this.id).style.color = this.normalTextColor;
		}
	},
	
	toggle: function(isPushed) {
		this.pushed = isPushed;
//		if (this.toggleButton) {
//			this.pushed = !this.pushed;
//		}
		this.out();
	},
	
	setState: function(state) {
		this.disabled = false;
		switch(state) {
			case 'normal':
			$(this.id).style.backgroundImage = 'url(' + this.normalIcon + ')';
			$(this.id).style.color = this.normalTextColor;
			$(this.id).style.cursor = 'hand';
			break;
			case 'over':
			$(this.id).style.backgroundImage = 'url(' + this.overIcon + ')';
			$(this.id).style.color = this.overTextColor;			
			$(this.id).style.cursor = 'hand';
			break;
			case 'pushed':
			$(this.id).style.backgroundImage = 'url(' + this.pushedIcon + ')';
			$(this.id).style.color = this.pushedTextColor;
			$(this.id).style.cursor = 'hand';
			break;
			case 'disabled':
			$(this.id).style.backgroundImage = 'url(' + this.disabledIcon + ')';
			$(this.id).style.color = this.disabledTextColor;
			$(this.id).style.cursor = 'default';
			this.disabled = true;
			break;
		}
		
	}
	
});

var DesignWidgets_Toolbar = Class.create({
	items: {},

	addItem: function(item,action) {
		this.items[item.id] = item;
		item.onClickAction = action;
	},
	
	onItemClick: function(itemId) {
		var item = this.items[itemId];
		if (item) {
			item.click();
		}
	},
	
	onItemOver: function(itemId) {
		var item = this.items[itemId];
		if (item) {
			item.over();
		}
	},
	
	onItemOut: function(itemId) {
		var item = this.items[itemId];
		if (item) {
			item.out();
		}
	},
	
	onItemToggle: function(itemId,isToggled) {
		var item = this.items[itemId];
		if (item) {
			item.toggle(isToggled);
		}
	},
	
	setState: function(itemId,state) {
		var item = this.items[itemId];
		if (item) {
			item.setState(state);
		}
	}
});

var DesignWidgets_Window = Class.create({
	close: function() {
		window.close();
	}
});

var DesignWidgets = Class.create({
	toolbar: new DesignWidgets_Toolbar(),
	mainWindow: new DesignWidgets_Window()
});

var designWidgets = new DesignWidgets();

