window.log = function(){
	log.history = log.history || [];
	log.history.push(arguments);
	if(this.console) console.log( Array.prototype.slice.call(arguments) );
};


$(document).ready(function()
{
	setMainmenu();
	setContactMap();
	setMaillistBubble()
});

//------------------------
//Signup Newsletter
//------------------------
function signupNewsletter()
{
	var txt_email = $('#header div.maillist .text');
	var email = txt_email.val();
	var strHtml_hint = $('#header div.maillist small');
	if(isValidEmail(email))
	{
		//valid email
		strHtml_hint.html(msg_process);
		
		$.post("/app/signupnewsletter", {email:email},function(data){
			switch(data)
			{
				case 'create_success':
					txt_email.val('');
					strHtml_hint.html(msg_signupSuccess);
				break;
				
				case 'email_duplicate':
					strHtml_hint.html(msg_duplicateEmail);
				break;
			}
		});

	}
	else
	{
		//invalid email
		strHtml_hint.html(msg_invalidEmail);
	}
	
}
// ------------------------
//	Maillist Bubble
// ------------------------
function setMaillistBubble()
{
	var bubble = $('#header div.maillist');
	var btn = $('.quicklink a.maillist');
	bubble.css({ 	left: btn.position().left + btn.parent().position().left + 15 }); // x位置由'加入電子報'文字連結實際位置決定
	btn.bind('click', function(e1)
	{
		bubble.fadeIn(300).click(function(e){
			e.stopPropagation();
		});
		e1.stopPropagation();
		$(document).one('click', function() {
			bubble.fadeOut(300);
		})
	});
}

// ------------------------
//	Mainmenu
// ------------------------
function setMainmenu()
{
	var activeBtn = $('#navigation .mainmenu > li > ul:visible').parent(); //取得目前單元按鈕
	$("#navigation .mainmenu > li").children('ul:hidden').parent()
	.mouseenter(function()
	{		
		$(this).addClass('on').children("ul").fadeIn(221);
		activeBtn.children("ul").stop(true,true);
		activeBtn.children("ul").hide()
	})
	.mouseleave(function()
	{
		$(this).removeClass('on').children("ul").hide();
		activeBtn.children("ul").fadeIn();
	})
	/*$('#navigation .mainmenu').mouseleave(function()
	{
		activeBtn.children("ul").fadeIn(221);
	})*/																					 
}

// ------------------------
//	Contact Map 
// ------------------------
function setContactMap() 
{
	return;
	$('<div id="contact-map" class="large"></div>').appendTo('.sidecontact .map');
	$('.sidecontact .map .toggle').bind('click',toggleContactMap);
	$('.sidecontact .map .small').bind('click',toggleContactMap);
}

function loadGmap() // Google Map API
{
	
	var a_lat =25.089486;
	var a_long = 121.525269;
	if (GBrowserIsCompatible()) 
	{
		//$('#contact-map').show();
		var map = new GMap2(document.getElementById('contact-map'));
			map.setCenter(new GLatLng(a_lat,a_long), 18);
			map.setUIToDefault();
		var point = new GLatLng(a_lat,a_long);
		var marker = new GMarker(point);
			map.addOverlay(marker);
		//$('#contact-map').hide();
	}
}

function toggleContactMap()
{
	var cmap = $('#contact-map');
	if(cmap.is(':visible') ) 
	{
		$('.sidecontact .map .toggle').text('展開地圖');
		cmap.hide();
		jQuery.event.remove(document, 'mousedown'); // 刪除:按任一處縮回
	} 
	else 
	{
		$('.sidecontact .map .toggle').text('縮回地圖');
		cmap.show();
		if(cmap.attr('loaded')!='1') {
			loadGmap();
			cmap.attr('loaded','1')
		}
		cmap.css({width:'112px', height:'112px'})
		cmap.animate({width:'500px', height:'350px'}, 500);
		$(document).mousedown(toggleContactMap) // 按任一處縮回
		// 展開地圖高度超過瀏覽器可視高度, 往下捲
		var scroll_gap = cmap.offset().top - $(document).scrollTop()
		if( scroll_gap > cmap.height() )
			$('html, body').animate({scrollTop: cmap.offset().top - ($(window).height()-cmap.height())/2 }, 'slow')
	}
}
