// Publish Func
// 參考來源 : Medialand - Anteater Bookmark function

// 推薦的 Link
var TARGET_HREF_FB  = "http://ipsa.mycosme.com.tw/?";
var TARGET_HREF  = "http://ipsa.mycosme.com.tw/";
// 推薦的 標題
var TARGET_TITLE    = "IPSA自律循環控油粉餅，八小時不脫妝任務";
// 服務
var GOOGLE			 = "Google";
var FACEBOOK	 = "FaceBook";
var PLURK				 = "Plurk";
var TWITTER 		 = "Twitter";
var FAVORITES		 = "Favorites";

var ICON_PATH  = "images/publish/";
var FILE_NAME_EXTENSION = ".png";
var servicesList	 = [GOOGLE, FACEBOOK, PLURK, TWITTER, FAVORITES];

/*
* 設置title
* 可使用此func置換title
*/
function setTitle(title){	
	TARGET_TITLE	 = title;
}

/*
* 取得title
*/
function getTitle(){	
	return TARGET_TITLE;	
}

/*
* 設置url FB
* 可使用此func置換utl
*/
function setHrefFB(url){	
	TARGET_HREF_FB = url;
}

/*
* 設置url 
* 可使用此func置換utl
*/
function setHref(url){	
	TARGET_HREF = url;
}

/*
* 取得推薦的 Link FB
*/
function getHrefFB(){	
	return TARGET_HREF_FB;	
}

/*
* 取得推薦的 Link 
*/
function getHref(){	
	return TARGET_HREF;	
}

/*
* 取得服務列表
* actionscript 可利用此 function 取得資料
*/
function getServicesList(){	
	return servicesList;		
}

/*
* 取得服務 Icon
* actionscript 可利用此 function 取得資料
*/
function getServicesIcon(){	
	// 建立新的陣列
	var _array = servicesList.concat();
	for (var icon in _array) {
		_array[icon] = ICON_PATH + _array[icon] + FILE_NAME_EXTENSION;
	}
	return _array;	
}

/*
* 取得彈跳視窗屬性
* for javascript 
*/
function winOpt() {
	var bounce = {
		w 			: 626, 
		h 			: 346,
		x 			: 0, 
		y 			: 0,
		toolbar : 0,
		status 	: 0
	};
	bounce.x = (screen.width - bounce.w) / 2;
	bounce.y = (screen.height - bounce.h) / 2;
	bounce.opt = 'toolbar='+ bounce.toolbar +',status='+ bounce.status +',width='+ bounce.w +',height='+ bounce.h +',top='+ bounce.y + ',left=' + bounce.x;	
	return bounce.opt;
}

/*
* 發佈推薦
*
*/
function publish(type) {
		var _location = TARGET_HREF;
		_location = encodeURIComponent(_location);
		var _title = TARGET_TITLE;
		_title = encodeURIComponent(_title);
		var _href;
		switch (type) {
			case GOOGLE :
				_href = "http://www.google.com/bookmarks/mark?op=add&bkmk="+ _location + "&title=" + _title;
				break;
			case FACEBOOK :
				_location = TARGET_HREF_FB;
				_href = "http://www.facebook.com/sharer.php?u="+ _location + "&t=" + _title;
				break;
			case PLURK :
				_href = "http://www.plurk.com/?qualifier=shares&status=".concat(_location).concat(' ').concat('(').concat(_title).concat(')');
				break;
			case TWITTER :
				_href = "http://twitter.com/home/?status=" + _title + " " + _location;
				break;
			case FAVORITES :
				addFavorites();									
				return;
				break;
		}
		var _open;
		if (type == FACEBOOK) {
			_open = window.open(_href, 'publish', winOpt());
		} else {
			_open = window.open(_href, 'publish');
		}
		return false;	
}

/*
* 發佈推薦
* 自行定義 title 和 href
*
*/
function publishMe(type, title, href) {
		var _location = href;
		_location = encodeURIComponent(_location);
		var _title = title;
		_title = encodeURIComponent(_title);
		var _href;
		switch (type) {
			case GOOGLE :
				_href = "http://www.google.com/bookmarks/mark?op=add&bkmk="+ _location + "&title=" + _title;
				break;
			case FACEBOOK :
				_href = "http://www.facebook.com/sharer.php?u="+ _location + "&t=" + _title;
				break;
			case PLURK :
				_href = "http://www.plurk.com/?qualifier=shares&status=".concat(_location).concat(' ').concat('(').concat(_title).concat(')');
				break;
			case TWITTER :
				_href = "http://twitter.com/home/?status=" + _title + " " + _location;
				break;
			case FAVORITES :
				addFavorites();									
				return;
				break;
		}
		var _open;
		if (type == FACEBOOK) {
			_open = window.open(_href, 'publish', winOpt());
		} else {
			_open = window.open(_href, 'publish');
		}
		return false;	
}

/*
* 加入書籤
*/
function addFavorites() {						
	var _title = TARGET_TITLE ;
	var _location = TARGET_HREF;						
	if( window.sidebar && window.sidebar.addPanel ) {
		// Gecko (Netscape 6)
		window.sidebar.addPanel( _title, _location, '' );
	} else if( window.external ) {
		// IE
		window.external.AddFavorite( _location, _title );
	} else if( document.layers ) {
		// NS4
		window.alert( 'Please click OK then press Ctrl+D to create a bookmark' );
	} else {
		// Other browsers
		window.alert( 'Please use your browsers\' bookmarking facility to create a bookmark' );
	}
}
