// apply kuler to the twitter-friends-stream color scheme
// user-driven color generation cannot succeed with swatch of all colors
// get viable color set from kuler and apply it to the twitter-friends-stream
// http://kuler-api.adobe.com/rss/get.cfm?listtype=rating&key=71FFAD088575E4B37BF0A4CC646A2B3F
// 1. bgcolor is always black
// 2. 5 colors --> 4 for flowing, 1 for current
// 3. text color --> black/white by brightness
// 4. current text color --> blue/yellow by brightness

FISH = function(id, parent_id) {
	this.init(id, parent_id);
}

Class(
	FISH, DIV,

	"init", function(id, parent_id) {
		FISH.superclass.init.call(this, id, parent_id);
		this.timer = 0;
		this.direction = 1;
		this.moving = false;
		this.intensity = 5; // zIndex
	},

	"create", function(x, y, w, h) {
		FISH.superclass.create.call(this, x, y, w, h);
		this.setClass("me2fish");

		YAHOO.util.Event.addListener(this.id, "mouseover", function(e) {
			this.stop = true;
		}, this, true);
		YAHOO.util.Event.addListener(this.id, "mouseout", function(e) {
			this.stop = false;
		}, this, true);
		YAHOO.util.Event.addListener(this.id, "click", function(e) {
			SEA.direction = Math.floor(Math.random() * 4);
			if( SEA.owner && this.id == SEA.owner.id ) {
			}

			SEA.zoomOut(this);
			this.direction = 1;
			this.speed = 0;
			this.setStyle("background", SEA.color[_CURRENT_+_FISH_]);
			this.setStyle("color", SEA.color[_CURRENT_+_FONT_]);

			SEA.onClickFish(this.fishId, this);
		}, this, true);
	},

	"getSpeed", function(intensity) {
		return intensity + 1;
	},

	"getDirection", function() {
		return SEA.direction;
	},

	"getFont", function() {
		if( this.direction == 5 ) {
			return "12pt 'gill sans mt', arial";
		}

		var fontSize = 8 + this.intensity;
		var fontWeight = Math.floor(Math.random() * 2) ? "" : " bold";

		return fontSize + "pt 'gill sans mt', arial" + fontWeight;
	},

	"getLevel", function() {
		return Math.floor(this.intensity / 2) + 1;
	},

	"getFishColor", function(intensity) {
		var level = this.getLevel();
		return SEA.color[eval("_LEVEL" + level + "_")+_FISH_];
	},

	"getFontColor", function(intensity) {
		var level = this.getLevel();
		return SEA.color[eval("_LEVEL" + level + "_")+_FONT_];
	},

	"setFishColor", function(color) {
		this.setStyle("background", color);
	},

	"setFontColor", function(color) {
		this.setStyle("color", color);
	},

	"setup", function() {
		this.intensity = Math.floor(Math.random() * 10);
		this.speed = this.getSpeed(this.intensity);
		this.direction = this.getDirection();
		this.font = this.getFont();

		var size = getTextSize(this.getText(), this.font, "150%", 600);
		var w = size.width;
		var h = size.height;

		this.setStyle("cursor", "pointer");
		this.setStyle("zIndex", this.intensity);
		this.setStyle("font", this.font);
		this.setStyle("lineHeight", "150%");
		this.setStyle("color", this.getFontColor(this.intensity));
		this.setStyle("background", this.getFishColor(this.intensity));
		this.setStyle("textAlign", "center");

		this.setW(w);
		this.setH(h);

		switch( this.direction ) {
		case 0: // left
		case 1: // right
			this.setX((this.direction == 0) ? SEA.w : -this.w);
			this.setY(10 + Math.floor(Math.random() * (SEA.h - 20 - h)));
			break;
		case 2: // up
		case 3: // down
			this.setX(10 + Math.floor(Math.random() * (SEA.w - 20 - w)));
			this.setY((this.direction == 2) ? SEA.h : -this.h);
			break;
		}

	},

	"moveUp", function() {
		FISH.superclass.setY.call(this, this.y - this.speed);
	},

	"moveDown", function() {
		FISH.superclass.setY.call(this, this.y + this.speed);
	},

	"moveLeft", function() {
		FISH.superclass.setX.call(this, this.x - this.speed);
	},

	"moveRight", function() {
		FISH.superclass.setX.call(this, this.x + this.speed);
	},

	"moveBackward", function(target_w, target_h) {
		target_w = 4;
		target_h = 3;

		if( this.w <= target_w && this.h <= target_h ) {
			this.setX(this.x + Math.floor((this.w - target_w) / 2));
			this.setY(this.y + Math.floor((this.h - target_h) / 2));
			this.setW(target_w);
			this.setH(target_h);

			this.direction = this.getDirection();
			this.speed = 5 + this.speed * 2;

			this.setStyle("zIndex", 1);
			this.setStyle("background", this.getFontColor(this.intensity)); //"#ffffff");
			return;
		}

		var w = (this.w <= target_w + 1) ? target_w : this.w - Math.floor((this.w - target_w) / 2);
		var h = (this.h <= target_h + 1) ? target_h : this.h - Math.floor((this.h - target_h) / 2);

		this.setX(this.x + Math.floor((this.w - w) / 2));
		this.setY(this.y + Math.floor((this.h - h) / 2));
		this.setW(w);
		this.setH(h);
	},

	"moveForward", function(target_w, target_h) {
		target_w = this.target_w;
		target_h = this.target_h;

		if( this.w >= target_w && this.h >= target_h ) {
			this.setX(this.x + Math.floor((this.w - target_w) / 2));
			this.setY(this.y + Math.floor((this.h - target_h) / 2));
			this.setW(target_w);
			this.setH(target_h);

			this.direction = this.getDirection();
			this.speed = 0;

			this.setStyle("zIndex", this.intensity);
			this.setText(this.fish_text);
			this.setStyle("background", SEA.color[_CURRENT_+_FISH_]);
			this.setStyle("color", SEA.color[_CURRENT_+_FONT_]);
			return;
		}

		var w = this.w + Math.floor((target_w - this.w) / 2);
		var h = this.h + Math.floor((target_h - this.h) / 2);
		w = (w >= target_w - 1) ? target_w : w;
		h = (h >= target_h - 1) ? target_h : h;

		this.setX(this.x + Math.floor((this.w - w) / 2));
		this.setY(this.y + Math.floor((this.h - h) / 2));
		this.setW(w);
		this.setH(h);
	},

	"go", function(name) {
		if( this.stop && this.speed ) {
			return;
		}

		if( !this.moving ) {
			if( this.getText() == "" ) {
				this.fishNumber = SEA.getFishNumber();
				this.setText(SEA.getFishName(this.fishNumber));
				this.fishId = SEA.getFishId(this.fishNumber)
				return;
			}
			this.setup();
			this.moving = true;
		}
		else {
			if( (this.direction == 0 && this.x < -this.w) || (this.direction == 1 && this.x > SEA.w) || (this.direction == 2 && this.y < -this.h) || (this.direction == 3 && this.y > SEA.h) ) {
				this.setText("");
				this.moving = false;
			}

			switch( this.direction ) {
			case 0: // left
				this.moveLeft();
				break;
			case 1: // right
				this.moveRight();
				break;
			case 2: // up
				this.moveUp();
				break;
			case 3: // down
				this.moveDown();
				break;
			case 4: // backward
				this.moveBackward();
				break;
			case 5: // forward
				this.moveForward();
				break;
			}
		}
	}
);

var _BACKGROUND_ = 0;
var _CURRENT_ = 1;
var _LEVEL1_ = 6; // foremost
var _LEVEL2_ = 5;
var _LEVEL3_ = 4;
var _LEVEL4_ = 3;
var _LEVEL5_ = 2; // farthest
var _FISH_ = 0; // SEA.color[_LEVEL1_][_FISH_]
var _FONT_ = 1; // SEA.color[_LEVEL1_][_FONT_]

var _BACKGROUND_ = 0;
var _CURRENT_ = 1;
var _LEVEL1_ = 11; // foremost
var _LEVEL2_ = 9;
var _LEVEL3_ = 7;
var _LEVEL4_ = 5;
var _LEVEL5_ = 3; // farthest
var _FISH_ = 0; // SEA.color[_LEVEL1_+_FISH_]
var _FONT_ = 1; // SEA.color[_LEVEL1_+_FONT_]

FRIENDS = {
	index: -1,

	data: [],

	clear: function() {
		while( FRIENDS.data.length ) {
			FRIENDS.data.pop();
		}
	},

	setData: function(data) {
		FRIENDS.clear();
		for( var i = 0; i < data.length; i++ ) {
			FRIENDS.data[i] = data[i];
		}
		FRIENDS.index = 0;
	},

	getIndex: function() {
		if( !FRIENDS.data.length ) {
			return -1;
		}

		if( FRIENDS.index >= FRIENDS.data.length ) {
			FRIENDS.index = 0;
		}

		return FRIENDS.index++;
	},

	getDataByIndex: function(index) {
		if( index < 0 || index > FRIENDS.data.length - 1 ) {
			return null;
		}

		return FRIENDS.data[index];
	},

	getDataByName: function(name) {
		for( var i = 0; i < FRIENDS.data.length; i++ ) {
			var friend = FRIENDS.data[i];
			if( friend.name == name ) {
				return friend;
			}
		}

		return null;
	}
};

SEA = {
	w: 800,

	h: 500,

	fishes: [],

	count: 60,

	index: 0,

	pre: null,

	cur: null,

	timer: 0,

	timer_read: 0,

	direction: 2,

	default_color: ["#75B6D7","#FFEF00","#000000","#FFFFFF","#000000","#F2F2F2","#000000"
,"#EFEFEF","#000000","#D0D0D0","#000000","#B6B6B6","#000000"],
	//["#000000", "#ff3300", "#ffffff", "#303030", "#FFB5B5", "#252525", "#FFBCFE", "#202020", "#A5A8FF", "#151515", "#C9FFC9", "#101010", "#FFE1BF"],
	//default_color: ["#75B6D7", "#FFF700", "#000000", "#FFFFFF", "#000000", "#F2F2F2", "#000000", "#EFEFEF", "#000000", "#D0D0D0", "#000000", "#B6B6B6", "#000000"],

	color: ["#000000", "#ff3300", "#ffffff", "#303030", "#ffffff", "#252525", "#ffffff", "#202020", "#ffffff", "#151515", "#ffffff", "#101010", "#ffffff"],

	color_name: ["Background", "Selected Box Color", "Selected Font Color", "Level1 Box COLOR", "Level1 Font Color", "Level2 Box Color", "Level2 Font Color", "Level3 Box Color", "Level3 Font Color", "Level4 Box Color", "Level4 Font Color", "Level5 Box Color", "Level5 Font Color"],

	id: null,

	full: false,

	create: function(id) {
		YAHOO.namespace("FISH");

		SEA.id = id;
		SEA.setLayout();

		YAHOO.util.Dom.setStyle(SEA.id, "background", SEA.color[_BACKGROUND_]);

		YAHOO.util.Event.addListener(window, "resize", function(e) {
			SEA.setLayout();
		});
	},

	setLayout: function() {
		if( SEA.full ) {
			YAHOO.util.Dom.setStyle(SEA.id, "border", "none");
			SEA.w = Math.max(800, YAHOO.util.Dom.getViewportWidth());
			SEA.h = Math.max(500, YAHOO.util.Dom.getViewportHeight());
		}
		else {
			YAHOO.util.Dom.setStyle(SEA.id, "border", "solid 1px #333333");
			SEA.w = 800;
			SEA.h = 500;
		}
		YAHOO.util.Dom.setStyle(SEA.id, "width", SEA.w + "px");
		YAHOO.util.Dom.setStyle(SEA.id, "height", SEA.h + "px");

		var r = YAHOO.util.Dom.getRegion(SEA.id);
		SEA.w = r.right - r.left;
		SEA.h = r.bottom - r.top;

		YAHOO.util.Dom.setStyle("fish_profile", "width", SEA.w - 110 + "px");
		YAHOO.util.Dom.setStyle("fish_profile_", "width", SEA.w - 110 + "px");
		YAHOO.util.Dom.setStyle(SEA.id, "margin", "0");

		if( SEA.full ) {
			SEA.x = 0;
			SEA.y = 0;
		}
		else {
			SEA.x = Math.max(0, Math.floor((YAHOO.util.Dom.getViewportWidth() - SEA.w) /2));
			SEA.y = Math.max(0, Math.floor((YAHOO.util.Dom.getViewportHeight() - SEA.h) / 2));
		}
		YAHOO.util.Dom.setStyle(SEA.id, "left", SEA.x + "px");
		YAHOO.util.Dom.setStyle(SEA.id, "top", SEA.y + "px");

		SETTING.setLayout();
		TOOLBOX.setLayout();
	},

	changeFull: function(full) {
		SEA.full = full;
		SEA.setLayout();

		if( SEA.full ) {
			for( var i = 0; i < SEA.fishes.length; i++ ) {
				var x = Math.floor((SEA.w - 800) / 2);
				var y = Math.floor((SEA.h - 500) / 2);
				var fish = SEA.fishes[i];
				fish.setXY(x + fish.x, y + fish.y);
			}
			YAHOO.util.Dom.setStyle(document.body, "background", SEA.color[_BACKGROUND_]);
		}
		else {
			YAHOO.util.Dom.setStyle(document.body, "background", "#000000");
		}

		SEA.zoomOut(null);
	},

	generateFish: function() {
		for( var i = 0; i < SEA.count; i++ ) {
			var fish = new FISH("fish" + i, SEA.id);
			fish.create(0, 0, 1, 1);
			fish.setText("");
			SEA.fishes.push(fish);
		}
	},

	getFishNumber: function() {
		return FRIENDS.getIndex();
	},

	getFishName: function(index) {
		var friend = FRIENDS.getDataByIndex(index);
		return friend ? friend.name : "";
	},

	getFishId: function(index) {
		var friend = FRIENDS.getDataByIndex(index);
		return friend ? friend.screen_name : "";
	},

	zoomOut: function(fish) {
		if( fish ) {
			FRIENDS.current = FRIENDS.getDataByName(SEA.getFishName(fish.fishNumber));
			//alert(FRIENDS.current.status.text);
			FRIENDS.clear();
		}

		for( var i = 0; i < SEA.fishes.length; i++ ) {
			if( fish && fish.id == SEA.fishes[i].id ) {
				continue;
			}
			SEA.fishes[i].setText("");
			SEA.fishes[i].direction = 4;
		}

		if( SEA.owner ) {
			SEA.owner.speed = 10;
		}
		if( fish ) {
			SEA.owner = fish;
			SEA.owner.direction = 5;
		}
	},

	onClickFish: function(fish_id, fish) {
	},

	moveFish: function() {
		for( var i = 0; i < SEA.fishes.length; i++ ) {
			SEA.fishes[i].go("SEA.fishes[" + i + "]");
		}

		clearTimeout(SEA.timer);
		SEA.timer = setTimeout("SEA.moveFish()", 50);
	},

	applyColor: function() {
		YAHOO.util.Dom.setStyle(SEA.id, "background", SEA.color[_BACKGROUND_]);

		for( var i = 0; i < SEA.fishes.length; i++ ) {
			if( SEA.fishes[i].speed == 0 ) {
				SEA.fishes[i].setFishColor(SEA.color[_CURRENT_+_FISH_]);
				SEA.fishes[i].setFontColor(SEA.color[_CURRENT_+_FONT_]);
			}
			else {
				var level = SEA.fishes[i].getLevel();
				SEA.fishes[i].setFishColor(SEA.color[eval("_LEVEL" + level + "_")+_FISH_]);
				SEA.fishes[i].setFontColor(SEA.color[eval("_LEVEL" + level + "_")+_FONT_]);
			}
		}

		for( var i = 0; i < SEA.color.length && i < SETTING.color.length; i++ ) {
			SETTING.color[i].setStyle("background", SEA.color[i]);
		}
	},

	saveColor: function(id) {
		if( !id ) {
			return;
		}

		var color = SEA.color.toString();
		color = color.replace(/,/gi, "\",\"");
		color = "[\"" + color + "\"]";

		//alert(color);

		var url = "./php/color.save.php";
		var dat = "callback=SEA.handleSave"
				+ "&id=" + id 
				+ "&color=" + color;
		YAHOO.util.Connect.asyncRequest("POST", url, SEA.SAVE.callback, dat);		
	},

	SAVE: {
		handleSuccess: function(o) {
			if( o.responseText.match("SEA.handleSave") ) {
				eval(o.responseText);
			}
			else {
				alert(o.responseText);
			}
		},

		handleFailure: function(o) {
			alert(o.responseText);
		}
	},

	handleSave: function(json) {
		SEA.onSave(json);
	},

	onSave: function(json) {
	},

	loadColor: function(id) {
		if( !id ) {
			return;
		}

		var url = "./php/color.load.php?callback=SEA.handleLoad&id=" + id;
		var dat = "callback=SEA.handleLoad"
				+ "&id=" + id;
		YAHOO.util.Connect.asyncRequest("GET", url, SEA.LOAD.callback, null);
	},

	LOAD: {
		handleSuccess: function(o) {
			if( o.responseText.match("SEA.handleLoad") ) {
				eval(o.responseText);
			}
			else {
				alert(o.responseText);
			}
		},

		handleFailure: function(o) {
			//alert(o.responseText);
		}
	},

	handleLoad: function(json) {
		SEA.onLoad(json);
	},

	onLoad: function(json) {
	}
};

SEA.SAVE.callback = {
	success: SEA.SAVE.handleSuccess,
	failure: SEA.SAVE.handleFailure,
	scope: SEA
};

SEA.LOAD.callback = {
	success: SEA.LOAD.handleSuccess,
	failure: SEA.LOAD.handleFailure,
	scope: SEA
};

// full screen
// color setting
// id input
// login/out
SETTING = {
	wnd: null,

	index: 0,

	color: [],

	create: function() {
		if( SETTING.wnd ) {
			return;
		}

		SETTING.wnd = new WINDOW("color_setting");
		SETTING.wnd.color = "#000000";
		SETTING.wnd.title_color = "#333333";
		SETTING.wnd.create(SEA.x + Math.floor((SEA.w - 218) / 2), SEA.y + 10, 218, 90, "CLOSE");
		SETTING.wnd.setTitle("COLOR SETTING");
		SETTING.wnd.enableDragdrop();
		YAHOO.util.Dom.setStyle(SETTING.wnd.container, "zIndex", "992");

		var btn = new DIV("setting_btn", SETTING.wnd.get_id());
		btn.create(5, 60, 208, 20);
		btn.setStyle("borderTop", "solid 1px #333333");
		btn.setStyle("paddingTop", "7px");
		btn.setStyle("font", "9pt arial");
		btn.setStyle("textAlign", "center");
		btn.setText("<a href='javascript:SETTING.save()' style='color:#ffffff;text-decoration:none;'>SAVE</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:SETTING.hide()' style='color:#ffffff;text-decoration:none;'>CLOSE</a>");
		//btn.setStyle("background", "#ff3333");

		for( var i = 0; i < 13; i++ ) {
			var div = new DIV("color_setting" + i, SETTING.wnd.get_id());
			div.create(11 + Math.ceil(i / 2) * (10 + 19), 10 + (i == 0 || (i % 2 == 1) ? 0 : 10 + 15), 19, 15);
			div.el.title = SEA.color_name[i];
			div.setStyle("background", SEA.color[i]);
			div.setStyle("border", "solid 1px #ffffff");
			div.setStyle("cursor", "pointer");
			
			YAHOO.util.Event.addListener(div.id, "click", function(e) {
				SETTING.index = this;
				ColorPicker.show();
				ColorPicker.setTitle(SEA.color_name[this].toUpperCase());
				ColorPicker.setColor(SEA.color[this].substr(1), true);
			}, i, true);

			SETTING.color[i] = div;
		}
		SETTING.wnd.onClose = SETTING.hide;
		ColorPicker.onChange = function(hex) {
			SEA.color[SETTING.index] = "#" + hex;
			SETTING.color[SETTING.index].setStyle("background", "#" + hex);
			SEA.applyColor();
		}
	},

	setLayout: function() {
		if( !SETTING.wnd ) {
			return;
		}
		SETTING.wnd.setXY(SEA.x + Math.floor((SEA.w - 218) / 2), SEA.y + 10);
		ColorPicker.setLayout();
	},

	show: function() {
		SETTING.create();
		SETTING.wnd.show();
		SETTING.setLayout();
		ColorPicker.show();
		ColorPicker.setColor(SEA.color[SETTING.index].substr(1), true);
	},

	hide: function() {
		ColorPicker.wnd.hide();
		SETTING.wnd.hide();
	},

	save: function() {
		InputForm.show();
	}
};

TOOLBOX = {
	container: null,

	full: null,

	setting: null,
	
	me2day: null,

	create: function() {
		TOOLBOX.container = new DIV("toolbox");
		TOOLBOX.container.create(0, 0, 92, 34);
		YAHOO.util.Dom.setStyle(TOOLBOX.container.id, "zIndex", "990");

		TOOLBOX.full = new DIV("toolbox_full", "toolbox");
		TOOLBOX.full.create(5, 5, 24, 24);
		TOOLBOX.full.setStyle("background", "url('image/full.gif')");
		TOOLBOX.full.setStyle("cursor", "pointer");
		TOOLBOX.full.el.title = "Full Screen / Small Screen";
		YAHOO.util.Event.addListener(TOOLBOX.full.get_id(), "click", function(e) {
			TOOLBOX.onFull();
			e.cancelBubble = true;
		});
		DW_UTIL.setOpacity(TOOLBOX.full.el, 0.5);
		YAHOO.util.Event.addListener(TOOLBOX.full.get_id(), "mouseover", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.full.el, 1);
			e.cancelBubble = true;
		});
		YAHOO.util.Event.addListener(TOOLBOX.full.get_id(), "mouseout", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.full.el, 0.5);
			e.cancelBubble = true;
		});
/*
		TOOLBOX.setting = new DIV("toolbox_setting", "toolbox");
		TOOLBOX.setting.create(34, 5, 24, 24);
		TOOLBOX.setting.setStyle("background", "url('image/setting.gif')");
		TOOLBOX.setting.setStyle("cursor", "pointer");
		TOOLBOX.setting.el.title = "Color Setting";
		YAHOO.util.Event.addListener(TOOLBOX.setting.get_id(), "click", function(e) {
			TOOLBOX.onSetting();
			e.cancelBubble = true;
		});
		DW_UTIL.setOpacity(TOOLBOX.setting.el, 0.5);
		YAHOO.util.Event.addListener(TOOLBOX.setting.get_id(), "mouseover", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.setting.el, 1);
			e.cancelBubble = true;
		});
		YAHOO.util.Event.addListener(TOOLBOX.setting.get_id(), "mouseout", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.setting.el, 0.5);
			e.cancelBubble = true;
		});
*/
		TOOLBOX.me2day = new DIV("toolbox_me2day", "toolbox");
		TOOLBOX.me2day.create(34, 5, 24, 24);
		TOOLBOX.me2day.setStyle("background", "url('image/twitter.gif')");
		TOOLBOX.me2day.setStyle("cursor", "pointer");
		TOOLBOX.me2day.el.title = "View Stream by ID";
		YAHOO.util.Event.addListener(TOOLBOX.me2day.get_id(), "click", function(e) {
			TOOLBOX.onTwitter();
			e.cancelBubble = true;
		});
		DW_UTIL.setOpacity(TOOLBOX.me2day.el, 0.5);
		YAHOO.util.Event.addListener(TOOLBOX.me2day.get_id(), "mouseover", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.me2day.el, 1);
			e.cancelBubble = true;
		});
		YAHOO.util.Event.addListener(TOOLBOX.me2day.get_id(), "mouseout", function(e) {
			DW_UTIL.setOpacity(TOOLBOX.me2day.el, 0.5);
			e.cancelBubble = true;
		});

		TOOLBOX.setLayout();
	},

	setLayout: function() {
		if( !TOOLBOX.container ) {
			return;
		}
		TOOLBOX.container.setXY(SEA.x + 1, SEA.y + SEA.h - 34 - 1);
	},

	onFull: function() {
		SEA.full = !SEA.full;
		SEA.changeFull(SEA.full);
	},

	onSetting: function() {
		SETTING.show();
	},

	onTwitter: function() {
		TwitterForm.show();
	}
};

