/**
 * ...
 * @author Aska
 */

var Nplurk = {
	init:function(){
		$('#plurksend').bind('click', Nplurk.plurkSend);
		$('#plurk-form a').bind('click', function(){
			Nplurk.hide();
		});
	}, userPostToPlurk:function(){
		var plurk = $('#plurk-form');
		plurk.center();
		plurk.fadeIn('fast');
	}, plurkSend:function(){
		var path = location.pathname;
		var username = $('#username').val();
		var password = $('#password').val();
		$.get(path+'php/plurk.php', 
		{username:username, password:password}, 
		Nplurk.postResponse);
	}, postResponse:function(data){
		if (data == 'in') {
			alert('已成功發佈至您的Plurk');
			$('#plurk-form').fadeOut('fast');
		} else {
			alert('無法登入Plurk, 請確認您的帳號或密碼是否有誤!');
		}
	}, hide:function(){
		$('#plurk-form').fadeOut('fast');
	}
}

var Nbookmark = {
	init:function(){
		this.url = window.location.href;
		this.title = '新高創 NHC Web';
	}, userPostToBookmark:function(){
		if (document.all) {
			//ie
			window.external.AddFavorite(Nbookmark.url, Nbookmark.title);
			alert('已成功加入您的書籤.');
		} else if (window.sidebar) {
			//mozilla ff
			window.sidebar.addPanel(Nbookmark.title, Nbookmark.url, '');
			alert('已成功加入您的書籤.');
		} else {
			//safari, kong, opera, chrome... just don't bother
			alert('您使用的瀏覽器不支援自動加入書籤, 建議您手動加入書籤.');
		}
	}
}

var Ntwitter = {
	init:function(){
		this.label = '推薦新高創官網?';
		this.defaultContent = '@NHC 新高創官網: http://www.nhc.tw/';
		
		twttr.anywhere(function(T){
			T('#tbox').tweetBox({
				label: Ntwitter.label,
				defaultContent: Ntwitter.defaultContent
			});
		});
		
		this.frame = $('#tbox');
		$('#tbox a').bind('click', function(){
			Ntwitter.hide();
		});
	}, userPostToTwitter:function(){
		var tbox = $('#tbox');
		tbox.center();
		tbox.fadeIn('fast');
	}, hide:function(){
		$('#tbox').fadeOut('fast');
	}
};

var Nfacebook = {
	init:function(){
		
	}, userPostToWall:function(){
		var url = 'http://www.facebook.com/sharer/sharer.php?u=http://www.nhc.tw/index.html&t=';
		var wName = 'NHC';
		var wWidth = 640;
		var wHeight = 480;
		//var wLeft = parseInt((screen.width/2) - (wWidth/2));
		//var wTop = parseInt((screen.height/2) - (wHeight/2));
		var wSize = 'width='+wWidth+', height='+wHeight+
		', scrollbars=yes, menubar=no, toolbar=no';
		//', left='+wLeft+', top='+wTop+
		//', screenX='+wLeft+', screenY='+wTop+
		window.open(url, wName, wSize);
	}
}

/*var Nfacebook = {
	init:function(){
		this.APP_ID = '229120523781395';
		this.login = false;
		
		FB.init({
			appId  : this.APP_ID,
			status : true, // check login status
			cookie : false, // enable cookies to allow the server to access the session
			xfbml  : true  // parse XFBML
		});
		
		FB.getLoginStatus(Nfacebook.onFacebookLoginStatus);
	}, onFacebookLoginStatus:function(response){
		if (response.status=="connected" && response.session)
		{
			//alert('logged in');
			//Nfacebook.postToWallUsingFBUi();
			Nfacebook.login = true;
		}
		else
		{
			//alert('not logged in');
			//Nfacebook.facebookLogin();
			Nfacebook.login = false;
		}
	}, userPostToWall:function(){
		if (Nfacebook.login) {
			Nfacebook.postToWallUsingFBUi();
		} else {
			Nfacebook.facebookLogin();
		}
	}, facebookLogin:function(){
		var loginUrl="http://www.facebook.com/dialog/oauth/?"+
			"scope=publish_stream&"+
			"client_id="+Nfacebook.APP_ID+"&"+
			"redirect_uri="+document.location.href+"&"+
			"response_type=token";
		window.location=loginUrl;
	}, postToWallUsingFBUi:function(){
		var data=
		{
			method: 'stream.publish',  
			message: "新高創廣告真有趣",
			display: 'iframe',
			caption: "新高創廣告",
			name: "NHC",
			picture: 'http://www.nhc.tw/img/fb_thumb.png',    
			link: "http://www.nhc.tw/",  // Go here if user click the picture
			description: "1998年7月新高創廣告公司成立，累積10餘年專業代銷實戰經驗，企業體日益精實壯大。在「專業、策略、創新」的高績效營運理念下，新高創憑藉動見觀瞻的領導團隊、健全的財務結構、高素質的人力資源與創新的行銷策略，在房地產代銷業百家爭鳴的戰場中，以高標準的自我要求、紮實穩健的步伐，打造大格局新局面。",
			actions: [{ name: '前往', link: 'http://www.nhc.tw/' }],			
		}
		FB.ui(data, Nfacebook.onPostToWallCompleted);
	}, onPostToWallCompleted:function(response){
		if (response)
		{
			//console.log(response);
			if (response.error)
			{
				alert(response.error.message);
			}
			else
			{
				if (response.id)
					alert("Posted as post_id "+response.id);
				else if (response.post_id)
					alert("Posted as post_id "+response.post_id);
				else
					alert("Unknown Error");
			}
		}
		// user cancelled
	}
};*/

var Media = {
	show:function(url){
		$.facebox({image:url});
	}, showSwf:function(url){
		$.facebox({swf:url});
	}
};

var Finder = {
	init:function(){
		//this.frame = $('#news');
		//this.offsetX = 320;
		//this.offsetY = 180;
		this.visible = false;
		this.timeout = -1;
	},
	showNews:function(){
		Finder.frame = $('#news');
		Finder.offsetX = 320;
		Finder.offsetY = 180;
		
		Finder.show('http://www.nhc.tw/news.php', 1000);
	},
	showNewsCustom:function(url){
		Finder.frame = $('#news');
		Finder.offsetX = 320;
		Finder.offsetY = 180;
		
		Finder.show(url);
	},
	showReserve:function(){
		Finder.frame = $('#reserve');
		Finder.offsetX = 450;
		Finder.offsetY = 240;
		
		Finder.show('http://www.nhc.tw/seehouse.php', 0);
	},
	showCareer:function(){
		Finder.frame = $('#career');
		Finder.offsetX = 270;
		Finder.offsetY = 205;
		
		Finder.show('http://www.nhc.tw/job.php', 1000);
	},
	showCareerForm:function(){
		Finder.frame = $('#career');
		Finder.offsetX = 250;
		Finder.offsetY = 205;
		
		Finder.show('http://www.nhc.tw/jobJoin.php', 0);
	},
	showContact:function(){
		Finder.frame = $('#contact');
		Finder.offsetX = 450;
		Finder.offsetY = 280;
		
		Finder.show('http://www.nhc.tw/contactus.php', 0);
	},
	show:function(src, delay){
		Finder.frame.attr('src', src);
		//Finder.frame.delay(delay).fadeIn('slow');
		if (Finder.timeout) clearTimeout(Finder.timeout);
		Finder.timeout = setTimeout('Finder.frame.fadeIn(\'slow\')', delay);
		
		Finder.rePos();
		
		Finder.visible = true;
	},
	hide:function(){
		if (Finder.visible) {
			//Finder.frame.stop();
			//Finder.frame.css('alpha', 1);
			//Finder.frame.fadeIn(1);
			if (Finder.timeout) clearTimeout(Finder.timeout);
			Finder.frame.fadeOut(0);
			Finder.visible = false;
		}
	},
	rePos:function() {
		var pos = $('#index_swf').position();
		var nx = Finder.offsetX + pos.left;
		var ny = Finder.offsetY + pos.top;
		var left = nx + 'px';
		var top = ny + 'px';
		
		Finder.frame.css('left', left);
		Finder.frame.css('top', top);
	}
};

function init() {
	$('#index_swf').flash({swf:'preloader.swf', width:1100, height:610});
	Finder.init();
	
	//window.fbAsyncInit = Nfacebook.init;
	
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/zh_TW/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
	
	Nfacebook.init();
	Ntwitter.init();
	Nbookmark.init();
	Nplurk.init();
	
	//testing
	//Media.show('img/01_print/print_1_1.jpg');
}

function updatePos() {
	if (Finder.visible) {
		Finder.rePos();
	}
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

$(document).ready(init);
$(window).resize(updatePos);
