var TimeBuffer = [];
var ActiveShow = false;
$(document).ready(function () {
	getOnlineCount();
	setInterval("getOnlineCount()", 10000);
	$('#goTop').click(function() {
		$(document).scrollTo(0,0);
	});
	if (document.all) {
		$("a[href^='javascript:']").live("click", function (e) {
				e.preventDefault();
		});
	}
	if (top.location !== self.location) {
	}
	else {
		//$.getScript('/arrowchat/external.php?type=djs');
		//$.getScript('/arrowchat/external.php?type=js&amp;v=1.0.7');
		/*
		$.getScript('/scripts/jquery.snowfall/snowfall.min.jquery.js',function(){
			$(document).snowfall({round : true, minSize: 1, maxSize:5, flakeCount:30});
		});
		*/
	}
	$('a.cluetip').cluetip({
		hoverIntent: {
			sensitivity:  5,
			interval:     400,
			timeout:      0
		},
		cluetipClass: 'jtip',
		arrows: false,
		dropShadow: false,
		sticky: false,
		mouseOutClose: true,
		closePosition: 'bottom',
		clickThrough: true,
		width: 350,
		showTitle: false,
		waitImage: false
	});
	var close_note = $("#service-note");
	close_note.click(function () {
		jQuery("#service-note").slideUp(1000, function () {
			jQuery(this).hide();
		});
	});
	$("#service-form").submit(function() {
		$('#service-load').append('<div class="center"><img src="/images/ajax-loader.gif" alt="Currently Loading" id="service-loading"></div>');
		var fem = $(this).serialize(),
				note = $('#service-note');
		$.ajax({
			type: "POST",
			url: "/contact/f2m.php",
			data: fem,
			success: function(msg) {
				if ( note.height() ) {
					note.slideUp(1000, function() {
						$(this).hide();
					});
				}
				else note.hide();
				$('#service-loading').fadeOut(300, function() {
					$(this).remove();
					if(msg === 'OK') {
						$('#service-live input.inpt').val("");
						$('#service-live textarea').val("");
					}
					// Message Sent? Show the 'Thank You' message and hide the form
					result = (msg === 'OK') ? '<div class="success">您的訊息已經成功送出了,謝謝您,我們收到後會立刻處理!</div>' : msg;

					var i = setInterval(function() {
						if ( !note.is(':visible') ) {
							note.html(result).slideDown(1000);
							clearInterval(i);
						}
					}, 40);
				}); // end loading image fadeOut
			},
			error: function() {
				alert('很抱歉,傳送資料時發生錯誤');
			}
		});
		return false;
	});
});
function getOnlineCount() {
	$.ajax({
		url: '/ajax/online.php',
		async: true,
		cache: false,
		type: 'GET',
		dataType: 'json',
		success: function (data) {
			//$('#nowonline').html(data.OnlineCount);
			while(data.GetTime.length>0) {
				TimeBuffer.push(data.GetTime.shift());
			}
			getMyTime();
		}
	});
}
function getMyTime() {
	if(ActiveShow == false && TimeBuffer.length > 0) {
		ActiveShow = true;
		var Time = TimeBuffer.shift();
		var s = $('<div></div>').addClass('TimeLabel').html('<p class="Memo">'+Time['Memo']+'</p>人氣 + '+Time['Time']).css({
			opacity: 0,
			top: '50%'
		}).show().appendTo('body').animate({
			opacity: 1,
			top: '30%'
		}, 2000, function () {
			ActiveShow = false;
			if(TimeBuffer.length > 0) {
				getMyTime();
			}
			$(this).delay(1500).animate({
				opacity: 0,
				top: '50%'
			}, 5000, function () {
				$(this).remove();
			});
		});
	}
}
function openContentWindow (pageUrl) {
	if (document.contentWindow != null) {
		document.contentWindow.focus();
	}
	else {
		document.contentWindow = window.open(pageUrl,'contentWindow','height=600,width=200,location=no,menubar=no,status=no,toolbar=no,resizable=yes');
		document.contentWindow.menuWindow = window;
		document.contentWindow.focus();
	}
	return false;
}

