/**
* common javascript library for DRAGONFLY websites.
* wrote by jaNg. @ rootbox ( jangkw@dragonflygame.com )
**/

/*
* enable image cache for less than internet explorer 6
*/
//try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

$(document).ready(function() {
	/*if(opener) {
		width = $("#wrapper").width() - document.documentElement.clientWidth;
		height = $("#wrapper").height() - document.documentElement.clientHeight;
		window.resizeBy(width, height);
	}*/

	$("a.tooltip").tooltip();
	$("div.items > a").itemtip();
});

/*
* flash activation
*/
function flashActivate() {
	var object = new String;
	var params = new String;
	var objParams = new String;
	var embedParams = new String;
	var objID = new String;

	this.init = function(objectID, flashURL, W, H) {
		objID = objectID;
		object = "<object id='" +objectID+ "' width='" +W+ "' height='" +H+ "' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'>";
		object += "";
		objParams = "<param name='movie' value='" +flashURL+ "' />";
		embedParams = "src='" +flashURL+ "' ";
		embedParams += "name='" +objectID+ "' ";
		embedParams += "width='" + W + "' height='" + H + "' ";
	};
	this.param = function(param, value) {
		params += "";
		objParams += "<param name='" +param+ "' value='" +value+ "' />";
		embedParams += param + "='" +value+ "' ";
	};
	this.setAttr = function(param, value) {
		if(param.toLowerCase() == "flashvars") {
			getFlashMovieObject(objID).SetVariable(value.split("=")[0], value.split("=")[1]);
		} else {
			getFlashMovieObject(objID).setAttribute(param, value);
		}
	};
	this.load = function() {
		var embedTag = "<embed " + embedParams + "pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' />";
		var objTag = object + objParams + embedTag + "</object>";
		//alert(objTag);
		document.write(objTag);
	};
};

function getFlashMovieObject(movieName) {
	if(window.document[movieName]) return window.document[movieName];
	if(navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; 
	} else {
		return document.getElementById(movieName);
	}
};

/**
* navigation
* writed by jaNg.
**/
var navi = {
	main: 0,
	sub: 0,
	list: null,
	menu: null,
	init: function(o, m, s) {
		this.main	= m || 0;
		this.sub	= s || 0;
		this.list	= o.list = o.children();
		this.menu	= o.menu = o.list.children(".sub");

		o.list.each(function(idx) {
			o.menu.eq(idx).css({"top": $(this).offset().top - 3});
			//$(this).bind("click", function(e) { return false; });
			$(this).hover(function() {
				o.list.eq(m).removeClass("current");
				$(this).addClass("current");
			}, function() {
				$(this).removeClass("current");
			});
		});

		o.bind("mouseleave", function(e) {
			navi.set();
		});

		this.set();
	},
	set: function() {
		this.list.eq(this.main).addClass("current");
		this.menu.eq(this.main).children().children("li").eq(this.sub).addClass("current");
	}
};


(function($) {
	var instances = [];
	function ItemTip(trigger) {
		$(trigger).each(function() {
			var offset = $(this).offset();
			$(this).children("img").css({top: offset.top-50, left: offset.left-5, opacity: 0});
		});

		$(trigger).hover(function() {
			var offset = $(this).offset();
			$(this).children("img").show().css({top: offset.top-50}).animate({top: offset.top-70, opacity: 1}, 200);
		}, function() {
			var offset = $(this).offset();
			$(this).children("img").hide().animate({top: offset.top-50, opacity: 0}, 10);
		});
	}

	// jQuery plugin implementation
	$.prototype.itemtip = function() {
		this.each(function() {
			api = new ItemTip($(this));
			instances.push(api);
		});
		return this;
	};
}) (jQuery);

/**
* tooltip
* Copyright (c) 2009 Tero Piirainen (http://flowplayer.org/tools/tooltip.html)
* modified by jaNg.
**/
(function($) {
	var instances = [];
	// static constructs
	$.tools = $.tools || {};
	$.tools.tooltip = {
		version: '1.1.2',
		conf: {
			// default effect variables
			effect: 'slide',
			fadeOutSpeed: "fast",
			predelay: 0,
			delay: 30,
			opacity: 1,
			lazy: undefined,
			// 'top', 'bottom', 'right', 'left', 'center'
			position: ['top', 'left'],
			offset: [0, 50],
			cancelDefault: true,
			relative: true,
			oneInstance: true,
			// for slide effect
			direction: 'up', // down, left, right
			bounce: false,
			slideOffset: 10,
			slideInSpeed: 200,
			slideOutSpeed: 10,
			slideFade: !$.browser.msie,
			// type to event mapping
			events: {
				def: 			"mouseover,mouseout",
				input: 		"focus,blur",
				widget:		"focus mouseover,blur mouseout",
				tooltip:		"mouseover,mouseout"
			},
			api: false
		}
	};
	// directions for slide effect
	var dirs = {
		up: ['-', 'top'],
		down: ['+', 'top'],
		left: ['-', 'left'],
		right: ['+', 'left']
	};
	var effects = {
		slide: [
			function(done) {
				var conf = this.getConf(),
					 tip = this.getTip(),
					 params = {opacity: conf.opacity},
					 dir = dirs[conf.direction] || dirs.up;

				params[dir[1]] = dir[0] +'='+ conf.slideOffset;

				if (conf.slideFade) { tip.css({opacity:0}); }
				tip.show().animate(params, conf.slideInSpeed, done);
			},
			function(done) {
				var conf = this.getConf(),
					 offset = conf.slideOffset,
					 params = {opacity: 0},
					 dir = dirs[conf.direction] || dirs.up;

				var sign = "" + dir[0];
				if (conf.bounce) { sign = sign == '+' ? '-' : '+'; }
				params[dir[1]] = sign +'='+ offset;

				this.getTip().animate(params, conf.slideOutSpeed, function()  {
					$(this).hide();
					done.call();
				});
			}
		]
	};
	function Tooltip(trigger, conf) {
		var self = this, $self = $(this);
		trigger.data("tooltip", self);

		// find the tip
		var tip = trigger.next();

		/* calculate tip position relative to the trigger */
		function getPosition(e) {
			// get origin top/left position
			var top = conf.relative ? trigger.position().top : trigger.offset().top,
				 left = conf.relative ? trigger.position().left : trigger.offset().left,
				 pos = conf.position[0];

			top  -= tip.outerHeight() - conf.offset[0];
			left += trigger.outerWidth() + conf.offset[1];

			// adjust Y
			var height = tip.outerHeight() + trigger.outerHeight();
			if (pos == 'center') 	{ top += height / 2; }
			if (pos == 'bottom') 	{ top += height; }

			// adjust X
			pos = conf.position[1];
			var width = tip.outerWidth() + trigger.outerWidth();
			if (pos == 'center') 	{ left -= width / 2; }
			if (pos == 'left')   	{ left -= width; }

			return {top: top, left: left};
		}
		
		// event management
		var evt = conf.events[trigger.attr("type")] || conf.events['def'];
		evt = evt.split(/,\s*/);
		if (evt.length != 2) { throw "Tooltip: bad events configuration for " + type; }

		trigger.bind(evt[0], function(e) {
			// close all instances
			if (conf.oneInstance) {
				$.each(instances, function()  {
					this.hide();
				});
			}
			// see if the tip was launched by this trigger
			var t = tip.data("trigger");
			if (t && t[0] != this) { tip.hide().stop(true, true); }

			e.target = this;
			self.show(e);

			// tooltip close events
			evt = conf.events.tooltip.split(/,\s*/);
			tip.bind(evt[0], function() { self.show(e); });
			if (evt[1]) { tip.bind(evt[1], function() { self.hide(e); }); }

		});

		trigger.bind(evt[1], function(e) {
			self.hide(e);
		});

		// ensure that the tip really shows up. IE cannot catch up with this.
		if (!$.browser.msie && !conf.predelay) {
			trigger.mousemove(function()  {
				if (!self.isShown()) {
					trigger.triggerHandler("mouseover");
				}
			});
		}

		// avoid "black box" bug in IE with PNG background images
		if (conf.opacity < 1) {
			tip.css("opacity", conf.opacity);
		}

		var pretimer = 0, title = trigger.attr("title");

		if (title && conf.cancelDefault) {
			trigger.removeAttr("title");
			trigger.data("title", title);
		}

		$.extend(self, {
			show: function(e) {
				if (e) { trigger = $(e.target); }
				clearTimeout(tip.data("timer"));

				if (tip.is(":animated") || tip.is(":visible")) { return self; }

				function show() {
					tip.data("trigger", trigger);
					var pos = getPosition(e);

					if (conf.tip && title) {
						tip.html(trigger.data("title"));
					}

					// onBeforeShow
					e = e || $.Event();
					e.type = "onBeforeShow";
					$self.trigger(e, [pos]);
					if (e.isDefaultPrevented()) { return self; }

					pos = getPosition(e);
					tip.css({position:'absolute', top: pos.top, left: pos.left});

					// invoke effect
					var eff = effects[conf.effect];
					if (!eff) { throw "Nonexistent effect \"" + conf.effect + "\""; }

					eff[0].call(self, function() {
						e.type = "onShow";
						$self.trigger(e);
					});
				}

				if (conf.predelay) {
					clearTimeout(pretimer);
					pretimer = setTimeout(show, conf.predelay);
				} else {
					show();
				}

				return self;
			},
			hide: function(e) {
				clearTimeout(tip.data("timer"));
				clearTimeout(pretimer);

				if (!tip.is(":visible")) { return; }

				function hide() {
					// onBeforeHide
					e = e || $.Event();
					e.type = "onBeforeHide";
					$self.trigger(e);
					if (e.isDefaultPrevented()) { return; }

					effects[conf.effect][1].call(self, function() {
						e.type = "onHide";
						$self.trigger(e);
					});
				}

				if (conf.delay && e) {
					tip.data("timer", setTimeout(hide, conf.delay));
				} else {
					hide();
				}

				return self;
			},
			isShown: function() {
				return tip.is(":visible, :animated");
			},
			getConf: function() {
				return conf;
			},
			getTip: function() {
				return tip;
			},
			getTrigger: function() {
				return trigger;
			},
			// callback functions
			bind: function(name, fn) {
				$self.bind(name, fn);
				return self;
			},
			onHide: function(fn) {
				return this.bind("onHide", fn);
			},
			onBeforeShow: function(fn) {
				return this.bind("onBeforeShow", fn);
			},
			onShow: function(fn) {
				return this.bind("onShow", fn);
			},
			onBeforeHide: function(fn) {
				return this.bind("onBeforeHide", fn);
			},
			unbind: function(name) {
				$self.unbind(name);
				return self;
			}
		});

		// bind all callbacks from configuration
		$.each(conf, function(name, fn) {
			if ($.isFunction(fn)) { self.bind(name, fn); }
		});
	}

	// jQuery plugin implementation
	$.prototype.tooltip = function(conf) {

		// return existing instance
		var api = this.eq(typeof conf == 'number' ? conf : 0).data("tooltip");
		if (api) { return api; }

		// setup options
		var globals = $.extend(true, {}, $.tools.tooltip.conf);

		if ($.isFunction(conf)) {
			conf = {onBeforeShow: conf};

		} else if (typeof conf == 'string') {
			conf = {tip: conf};
		}

		conf = $.extend(true, globals, conf);

		// can also be given as string
		if (typeof conf.position == 'string') {
			conf.position = conf.position.split(/,?\s/);
		}
		// assign tip's only when apiement is being mouseovered
		if (conf.lazy !== false && (conf.lazy === true || this.length > 20)) {
			this.one("mouseover", function(e) {
				api = new Tooltip($(this), conf);
				api.show(e);
				instances.push(api);
			});
		} else {
			// install tooltip for each entry in jQuery object
			this.each(function() {
				api = new Tooltip($(this), conf);
				instances.push(api);
			});
		}

		return conf.api ? api: this;
	};
}) (jQuery);
