// created by isobar Hong Kong http://www.isobar.hk
function $(n){if(n!=''){return document.getElementById(n);}else{return null;}}
function $t(n,e){
  if(e) return e.getElementsByTagName(n);
  else return document.getElementsByTagName(n);
}
function popwin(url,nam,w,h,resizable,scrollbars,toolbar,locationbar,directories,status,menubar){
  var attr="";
  if(w>0){
    attr=(attr=="") ? "" : attr+",";
    attr+="width="+w;
  }
  if(h<=0&&window.screen.availHeight&&window.screen.height&&window.screen.availHeight!=window.screen.height)
    h=(window.screen.availHeight>window.screen.height) ? window.screen.height : ((window.screen.availHeight<window.screen.height) ? window.screen.availHeight : 680);
  if(document.documentElement&&document.documentElement.clientWidth) h-=60;
  if(BrowserDetect.browser=="Safari"){h+=2;}else if(BrowserDetect.browser=="Firefox"&&BrowserDetect.version>=2) h-=1;
  attr=(attr=="") ? "" : attr+",";
  attr+="height="+h;
  if(resizable!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="resizable="+resizable;
  }
  if(scrollbars!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="scrollbars="+scrollbars;
  }
  if(toolbar!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="toolbar="+toolbar;
  }
  if(locationbar!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="location="+locationbar;
  }
  if(directories!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="directories="+directories;
  }
  if(status!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="status="+status;
    attr=(attr=="") ? "" : attr+",";
    attr+="statusbar="+status;
  }
  if(menubar!=""){
    attr=(attr=="") ? "" : attr+",";
    attr+="menubar="+menubar;
  }
  if(window.screen&&w>0&&h>0){
    var x=(screen.width-w)/2;
    var y=(screen.height-h)/2;
    if(BrowserDetect.browser=="Explorer") y-=24;
    attr+=",top="+y+",left="+x;
  }
  var _wc=window.open(url,nam,attr);
  if(_wc==undefined){
    var s=$("SwireHotels");
    if(s){
      s.callGetUrl(url);
      return true;
    }
  }else{
    _wc.opener=this;
    if(window.focus) _wc.focus();
  }
  return _wc;
}
var sharer={
  title:null,
  url:null,
  chosenService:null,
  services:{facebook:'http://www.facebook.com/sharer.php?u=<url>&t=<title>',twitter:'http://twitter.com/home?status=<title>+<url>',weibo:'http://service.t.sina.com.cn/share/share.php?url=<url>&appkey=&title=<title>&pic=<pic>&ralateUid='},
  share:function(title,url,service,pic){
    this.title=this.url=this.chosenService=null;
    this.title=encodeURIComponent(title);
    this.url=encodeURIComponent(url);
    this.pic=pic;
		if(this.services[service]) {
			this.chosenService=service;
			sharer[service].apply();
    }
  },
  facebook:function(){
             var post=sharer.services.facebook.replace('<url>',sharer.url);
             post=post.replace('<title>',sharer.title);
             if(!popwin(post,'sharer','626','456','yes','no','no','no','no','yes','no')){
               document.location=post;
             }
             post=null;
           },
  //twitter:function(data){
  twitter:function(){
            /*var url = Url.shortUrl(data);
            if(url==''){
              url=this.url;
            }
            var post=sharer.services.twitter.replace('<url>',url);*/
            var post=sharer.services.twitter.replace('<url>',sharer.url);
            post=post.replace('<title>',sharer.title);
            if(!popwin(post,'tweet','960','385','yes','no','no','no','no','yes','no')){
              document.location=post;
            }
            //url=post=null;
            post=null;
          },
  weibo:function(){
          var post=sharer.services.weibo.replace('<url>',sharer.url);
          post=post.replace('<title>',sharer.title);
          post=post.replace('<pic>',sharer.pic);
          if(!popwin(post,'weibo','615','505','yes','no','no','no','no','yes','no')){
            document.location=post;
          }
          post=null;
        }
};
var Url={
  shortUrl:function(data){
             var first_result;
             for (var r in data.results){
               first_result=data.results[r];
               break;
             }
			 if(first_result==undefined) return null;
			if(first_result.shortUrl)
				return first_result.shortUrl.toString();
           },
  encode:function(string){
           return escape(this._utf8_encode(string));
         },
  decode:function(string){
           return this._utf8_decode(unescape(string));
         },
  _utf8_encode:function(string){
                 string=string.replace(/\r\n/g,"\n");
                 var utftext="";
                 for(var n=0;n<string.length;n++){
                   var c=string.charCodeAt(n);
                   if(c<128){
                     utftext+=String.fromCharCode(c);
                   }else if((c>127)&&(c<2048)){
                     utftext+=String.fromCharCode((c>>6)|192);
                     utftext+=String.fromCharCode((c&63)|128);
                   }else{
                     utftext+=String.fromCharCode((c>>12)|224);
                     utftext+=String.fromCharCode(((c>>6)&63)|128);
                     utftext+=String.fromCharCode((c&63)|128);
                   }
                 }
                 return utftext;
               },
  _utf8_decode:function(utftext){
                 var string="";
                 var i=0;
                 var c=c1=c2=0;
                 while(i<utftext.length){
                   c=utftext.charCodeAt(i);
                   if(c<128){
                     string+=String.fromCharCode(c);
                     i++;
                   }
                   else if((c>191)&&(c<224)){
                     c2=utftext.charCodeAt(i+1);
                     string+=String.fromCharCode(((c&31)<<6)|(c2&63));
                     i+=2;
                   }else{
                     c2=utftext.charCodeAt(i+1);
                     c3=utftext.charCodeAt(i+2);
                     string+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));
                     i+=3;
                   }
                 }
                 return string;
               }
}
function shortUrlToFlash(url){
  if (BitlyClient)
    BitlyClient.call('shorten',{'longUrl':(url)}, bitlyCallback);
}

function bitlyCallback(data){
  var url = Url.shortUrl(data);
  $("flashcontent").shortUrlCallBack(url);
}

function shortUrlGeneric(url,callback){
  if (BitlyClient)
    BitlyClient.call('shorten',{'longUrl':(url)}, callback);
}

var shorturl; 

function bitlyCallbackGeneric(data){
  shorturl=Url.shortUrl(data);
}

function twitterShortUrl(){
  if(shorturl)
    return shorturl
  else
    return window.location.href;
}

function loaded(){
	var share_location;
	if(document.getElementById('share_page')) {
		share_location = document.getElementById('share_page').value;
	} else {
		share_location = window.location;
	}
  shortUrlGeneric(share_location, bitlyCallbackGeneric);
  var sns_content= document.createElement("div");
  sns_content.innerHTML = '<a href="#" onclick="sharer.share(document.title, \''+ share_location +'\',\'facebook\');"><img src="/files/images/icons/social_fb_off.jpg" alt="facebook"/></a> '
    + '<a href="#" onclick="sharer.share(document.title, twitterShortUrl(),\'twitter\');"><img src="/files/images/icons/social_t_off.jpg" alt="twitter"/></a> '+
    '<a href="#" onclick="sharer.share(document.title, \''+ share_location +'\',\'weibo\');"><img src="/files/images/icons/social_wb_off.jpg" alt="weibo"/></a> ';/*+
    '<a href="http://wordpress.com/" target="_blank"><img src="/files/images/icons/social_wp_off.jpg" alt="wordpress"/></a>';*/
  var sns = document.getElementById("social_links")
  if (sns != null)
    sns.appendChild(sns_content);

}
if ( document.addEventListener ) {
  document.addEventListener("DOMContentLoaded", loaded, false);
} else if ( window.attachEvent ) {
  window.attachEvent("onload", loaded);
}

