/* (C) 2007-2011 WaiWai Pty Ltd. http://waiwai.com.au/ */

if (!this.WaiWai) WaiWai = {};
if (!this.WaiWai.Today) WaiWai.Today = {};

WaiWai.Today.WeatherInfo = [];			// today's weather
WaiWai.Today.ForexInfo = [];			// today's forex

// class WaiWai.Today.Weather
if(!this.WaiWai.Today.Weather) 
{
	WaiWai.Today.Weather = function(id, high, low, condition, description) 
	{
		this.id = id;
		this.low = low;
		this.high = high;

		this.lowF = Math.round(low * 9 / 5 + 32);
		this.highF = Math.round(high * 9 / 5 + 32);

		if(this.low !== "")
		{
			this.temperature = this.low;
			if(this.high !== "") { this.temperature += "~" + this.high; }
		}
		else { this.temperature = this.high; }
		this.temperature = this.temperature + "ºC";

		if(this.lowF !== "")
		{
			this.temperatureF = this.lowF;
			if(this.highF !== "") { this.temperatureF += "~" + this.highF; }
		}
		else { this.temperatureF = this.highF; }
		this.temperatureF = this.temperatureF + "ºF";

		this.condition = condition;
		this.description = description;
	}; 
}
// class WaiWai.Today.Forex
if(!this.WaiWai.Today.Forex) 
{
	WaiWai.Today.Forex = function(id, rate) 
	{
		this.id = id;
		this.rate = rate;
	}; 
}

//class 渐隐渐现幻灯，适用于综合首页天气和汇率
if(!this.WaiWai.fadeSlider) {
	WaiWai.fadeSlider = function() {
		//c 是计数器， total 是列表长度
		var _this = this, c=0, total;
		this.objList;
		this.objButton = {};
		this.gotoNext = function() {
			_this.objList.eq(c).stop().fadeOut('fast');
			c = c<total-1?c+1:0;
			_this.objList.eq(c).delay('normal').stop().fadeIn('fast');
		}
		this.gotoPrev = function() {
			_this.objList.eq(c).stop().fadeOut('fast');
			c = c==0?total-1:c-1;
			_this.objList.eq(c).delay('normal').stop().fadeIn('fast');
		}
		this.init = function() {
			total = _this.objList.length;
			_this.objButton.next.mouseup(_this.gotoNext);
			_this.objButton.prev.mouseup(_this.gotoPrev);
		}		
	}
}

//class 计时器
if(!this.WaiWai.timer) {
	WaiWai.timer = function() {
		var l, timer = 0, weatherOn = rate = headlines = textlines = slideshow = 1;
		jQuery('#weather ol').hover(
			function() { weatherOn = 0; },
			function() { weatherOn = 1; }
		);
		jQuery('#exchange_rate ol').hover(
			function() { rate = 0; },
			function() { rate = 1; }
		);
		//只有门户首页才运行
		if (jQuery('#nv_portal').hasClass('pg_index')) {
			jQuery('#headline .list').hover(
				function() { headlines = 0; },
				function() { headlines = 1; }
			);
			jQuery('#text_ad_block').hover(
				function() { textlines = 0; },
				function() { textlines = 1; }
			);
			jQuery('#slider_block').hover(
				function() { slideshow = 0; },
				function() { slideshow = 1; }
			);
		}
		var action = function() {
			timer++;
			if(weatherOn) weather.gotoNext();
			if(rate) exchange_rate.gotoNext();
			//只有门户首页才运行
			if (jQuery('body').hasClass('pg_index')) {
				if(timer%2==0) {
					if(slideshow) slider.controls.gotoNext();
				}
				if(timer%4==0) {
					if (headlines) CHL.controls.gotoNext();
					/*if(textlines) text_ad.controls.gotoNext();*/
				}
			}
		}
		var loop = function() {
			l = setInterval(action, 3750);
		}
		jQuery(window).blur(function() {
			clearInterval(l);
			l = null;
		})
		jQuery(window).focus(function() {
			if (l==null) loop();
		})
		loop();
	}
}

if(!this.WaiWai.calendar) {
	WaiWai.calendar = function() {
		var _this = this, timer;
		jQuery('#ww_calendar > .commonTitle').click(function() {
			change();
		});
		jQuery('#ww_calendar').mouseover(function() {
			clearTimeout(timer);
			if (jQuery('#ww_calendar_form').is(':hidden')) timer = setTimeout(openDetail, 5000);
		});
		jQuery('#ww_calendar').mouseout(function() {
			clearTimeout(timer);
			timer = setTimeout(closeDetail, 10000);
		});
		var openDetail = function() {
				jQuery('#ww_calendar_form').stop().show();			
				jQuery('#calendar_list').stop().hide();
				jQuery('#calendar_detail').stop().show();
				jQuery('#ww_calendar > .commonTitle').css({
					width: '640px',
					backgroundPosition: 'right 5px'
				});
		}
		var closeDetail = function() {
				jQuery('#ww_calendar_form').stop().hide();
				jQuery('#calendar_list').stop().show();
				jQuery('#calendar_detail').stop().hide();
				jQuery('#ww_calendar > .commonTitle').css({
					width: 'auto',
					backgroundPosition: 'right -30px'
				});
		}
		var change = function() {
			if (jQuery('#ww_calendar_form').is(':hidden')) openDetail();
			else closeDetail();
		}
	}
}

if (!this.WaiWai.addBgAdLink) {
	WaiWai.addBgAdLink = function(url, titleText) {
		var w = (document.documentElement.clientWidth - 1000) / 2;
		var ids = new Array('bgAdLink_left', 'bgAdLink_right');
		for (var i=0; i<ids.length; i++) {
			var oA = document.createElement('a');
			oA.href= url;
			oA.target = '_blank';
			oA.id = ids[i];
			oA.className = 'bgAdLink';
			oA.title = titleText;
			oA.style.width = w + "px";
			jQuery('body').append(oA);
		}
	}
}

if (!this.WaiWai.addGotoTopButton) {
	WaiWai.addGotoTopButton = function() {
		var createButton = function() {
			var oA = document.createElement('div');
			oA.href= "#";
			oA.id = 'gototop';
			oA.title = '回到頂部 Goto Top';
			jQuery('#body').before(oA);
		}
		var showButton = function() {
			var scrollTopValue = document.documentElement.scrollTop + document.body.scrollTop;
			if (scrollTopValue > 800 && document.body.clientWidth > 1200) jQuery('#gototop').fadeIn('normal');
			else jQuery('#gototop').fadeOut('normal');
		}
		createButton();
		showButton();
		window.onscroll = showButton;
		jQuery('#gototop').click(function() {
			jQuery.scrollTo(0, 800, {easing: 'easeInOutExpo'});
		});
	}
}
