/*
  *將此頁(或指定連結)推文至 FB, Plurk, Twitter, Google, Yahoo 等等 ...  
  *前往指定 youtube 連結觀看影片
  *設定參數預設值: 
    *FB, Plurk, Twitter: 
      1). 如有指定連結頁面(website)，就使用該連結頁面，否則使用當前頁面(location.href)
      2). 如有指定標題(title)，則使用該標題，否則使用當前網頁標題(document.title)
  
    *Youtube:
      1). 僅輸入 youtube的 帳號(user) 即可 
  *使用方法:
    *直接推文 FB, Plurk, Twitter: 
      blog.facebook.Post();
      blog.plurk.Post();
      blog.twitter.Post();
    *推文可傳參數: (完整連結網址, 連結標題)  
    
    *FB 分享按鈕:
      blog.facebook.Share();     
    *FB 按個讚:
      blog.facebook.Good();         
      
    *Youtube:
      blog.youtube.link();
    *可傳參數: (youtube使用者帳號)     
*/

var blog = {
  //傳送的 URL
  'sendUrl'      : '',
  //傳送的 URL 參數資料
  'sendData'     : '',
  //傳送成功後的接收模式: html, xml, json ... 
  'sendDataType' : 'html',
  //接收資料前的動作
  'sendBefore'   : function(){},
  //處理成功後接收到的資料
  'successData'  : '',
  //成功後所進行的動作
  'successDo'    : function(){},
  //記錄完成次數
  'successTimes' : 0,
  //記錄發送查詢次數
  'queryTimes'   : 0,  
  //進行 Ajax 發送
  'send' : function(){
    //一次僅能執行一個 Ajax 動作: 可以 successDo() 裡執行遞迴發送 / 多重定義
    $.ajax({
      type: 'POST',
      url:  blog.sendUrl,
      data: blog.sendData,
      dataType: blog.sendDataType,
      beforeSend: blog.sendBefore(),
      success: function(getData){
        blog.successData = getData;
        blog.successDo();
        //return 'YES';          
      }
    });       
  },
  //完整網址
  'website' : location.href,
  //網頁標題
  'title'   : document.title,
  //網站描述
  'description' : '',
  //via
  'via' : '',
  //使用語系 : 中文(CH) / 英文(EN) / 日文(JP) ?
  'useLang' : 'CH',
  //FaceBook
  'facebook' : {
    //語系文字對照
    'matchLang' : {
      'Post' : {
        'CH' : '推文',
        'EN' : 'Post',
        'JP' : ''        
      },
      'Good' : {
        'CH' : '按讚',
        'EN' : 'Like',
        'JP' : '' 
      },
      'Share' : {
        'CH' : '分享',
        'EN' : 'Share',
        'JP' : ''         
      }
    },  
    //樣式
    'Style' : {
      //讚
      'Good' : {
        //標準 : 150 x 80
        'standard'    : 'standard',
        //平行計數器 + 按鈕 : 57 x 20 
        'buttonCount' : 'button_count',
        //上面計數器 + 按鈕 : 57 x 57
        'boxCount'    : 'box_count' 
      },
      //分享
      'Share' : {
        //平行計數器 + 按鈕 : 96 x 18
        'buttonCount' : 'button_count',
        //僅按鈕 : 無計數器 : 56 x 18
        'button'      : 'button',
        //上面計數器 + 按鈕 : 57 x 57
        'boxCount'    : 'box_count',                
        //自訂文字 + FB Icon 圖 : 51 x 15
        'iconLink'    : 'icon_link',
        //僅 FB Icon 圖 : 18 x 15
        'icon'        : 'icon'  
      },
      //是否顯示圖片
      'Face'   : 'true',
      //寬度
      'Width'  : 150,
      //高度
      'Height' : 80
    },    
    //Facebook : 分享網址, 分享主題
    'Post' : function(website, title){ 
      website = website || blog.website;
      title   = title   || blog.title;
      var oWin = window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(website) + 
                                                               '&t=' + encodeURIComponent(title), 'sharer');
          oWin.focus();
      return false;
    },
    //按個讚 : 指定區域寫入 iFrame, 樣式, 分享網址
    'Good' : function(divId, goodStyle, website){
      goodStyle = (goodStyle)? blog.facebook.Style.Good[goodStyle] : blog.facebook.Style.Good.buttonCount;
      website   = website   || blog.website;
      var iFrameStyle = 'border:none; overflow:hidden; '
                      + 'width:'  + blog.facebook.Style.Width  + 'px; ' 
                      + 'height:' + blog.facebook.Style.Height + 'px; ';
      var fbUrl       = 'http://www.facebook.com/plugins/like.php';
      var goodIframe  = "<iframe src='" + fbUrl 
                                        + "?href=" + encodeURIComponent(website) 
                                        + "&amp;layout=" + goodStyle 
                                        + "&amp;show_faces=" + blog.facebook.Style.Face 
                                        + "&amp;width=" + blog.facebook.Style.Width 
                                        + "&amp;action=like" 
                                        + "&amp;font" 
                                        + "&amp;colorscheme=light" 
                                        + "&amp;height=" + blog.facebook.Style.Height + "' "
                                        + "scrolling='no' frameborder='0' "
                                        + "style='" + iFrameStyle + "' allowTransparency='true'></iframe>";
      
      //有指定區域 ? 製作 IFrame : 回傳 IFrame
      if(divId) $('#' + divId).html(goodIframe)
      else return goodIframe;       
    },
    //分享按鈕 : 指定寫入區域, 樣式, 分享網址, 分享主題
    'Share' : function(divId, shareStyle, website, title){
      shareStyle = (shareStyle)? blog.facebook.Style.Share[shareStyle] : blog.facebook.Style.Share.buttonCount;
      website    = website || blog.website;
      title      = title   || blog.title;
      //FB Share Url
      var shareUrl  = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(website) + 
                                                        '&t=' + encodeURIComponent(title);
      //FB Share JS
      var scriptUrl = 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
      //FB A Tags Name
      var name      = 'fb_share'
      //製作分享按鈕
      var shareTag = "<a name='" + name + "' " 
                                 + "type='"  + shareStyle    + "' " 
                                 + "href='"  + shareUrl + "'>" 
                                 + blog.facebook.matchLang.Share[blog.useLang] 
                                 + '</a>';      
      //寫入指定區域裡 / 回傳
      if(divId) $('#' + divId).html(shareTag);
      else return shareTag;
      
      //執行外部 JS 檔
      $.getScript(scriptUrl, function(){
        //修正 href : 在呼叫 FB 的 JS 進來後，原本的 <a> 標籤裡的 href 會被 FB JS 重新定義
        $('#' + divId + ' > a').attr('href', shareUrl);
      });      
    }
  },
  //Plurk
  'plurk' : {
    'Post' : function(website, title){
      website = website || blog.website;
      title   = title   || blog.title;
      var oWin = window.open('http://www.plurk.com/?qualifier=shares&amp;status=' + encodeURIComponent(website) +
                                                                             ' (' + encodeURIComponent(title) + ')' );                                                                                                                                                        
          oWin.focus();
      return false;
    }
  },
  //Twitter
  'twitter' : {
    'Post' : function(website, title){
      website = website || blog.website;
      title   = title   || blog.title;
      var oWin = window.open('http://twitter.com/home/?status=' + encodeURIComponent(website) +
                                                           ' (' + encodeURIComponent(title) + ')');                                                           
          oWin.focus();
      return false;                                
    }
  },
  //Youtube
  'youtube' : {
    'Link' : function(user){
      user = user || 'user';
      var oWin = window.open('http://www.youtube.com/user/' + user, '_blank');
          oWin.focus();
      return false;
    }
  },
  //FunP : 推推王 
  'funP' : {
    //單頁直接推文
    'Post': function(website){
      website    = website || blog.website;
      var funUrl = 'http://funp.com/push/submit/';
      var oWin   = window.open( funUrl + '?url=' + encodeURIComponent(website) + '&via=tools' );
          oWin.focus();         
      return false;          
    },
    //分享按鈕 : 指定寫入區域, 樣式( 1 ~ 13 種), 分享網址
    'Share' : function(divId, funStyle, website){
      var funStyleArray = {
         1 : {'w' : 60, 'h': 55 },
         2 : {'w' : 60, 'h': 69 },
         3 : {'w' : 80, 'h': 15 },
         4 : {'w' : 91, 'h': 17 },
         5 : {'w' : 95, 'h': 22 },
         6 : {'w' :115, 'h': 22 },
         7 : {'w' : 60, 'h': 66 },
         8 : {'w' :100, 'h': 34 },
         9 : {'w' :106, 'h': 55 },
        10 : {'w' : 60, 'h': 55 },
        11 : {'w' :100, 'h': 20 },
        12 : {'w' : 80, 'h': 15 },
        13 : {'w' : 80, 'h': 15 }
      };
      funStyle    = funStyle || 1;
      website     = website  || blog.website;
      var funUrl    = 'http://funp.com/tools/buttoniframe.php';
      var funIframe = "<iframe src='" + funUrl 
                                      + '?url='    + encodeURIComponent(website) 
                                      + '&s='      + funStyle + "' " 
                                      + "width='"  + funStyleArray[funStyle]['w'] + "' "
                                      + "height='" + funStyleArray[funStyle]['h'] + "' " 
                                      + "scrolling='no' frameborder='0' marginheight='0' marginwidth='0'"
                                      + '></iframe>';
      //寫入或回傳
      if(divId) $('#' + divId).html(funIframe);
      else return funIframe;        
    }
  },
  //MurMur
  'murmur' : {
    'Post' : function(website){
      website = website || blog.website;
      var oWin = window.open( 'http://murmur.tw/?sharelink=' + encodeURIComponent(website) );                                                           
          oWin.focus();
      return false;                                
    }
  },
  //Delicious : 4個方格子
  'delicious' : {
    'Post' : function(website, title){
      website = website || blog.website;
      title   = title   || blog.title;
      var version = 2;
      var oWin = window.open( 'http://del.icio.us/post?v='         + version
                                                   + '&amp;url='   + encodeURIComponent(website) 
                                                   + '&amp;title=' + encodeURIComponent(title) );
          oWin.focus();
      return false;                                
    }  
  },
  //HemiDemi
  'hemidemi' : {
    //Po 文 : 網址, 分享標題, 描述, VIA(可為空)  
    'Post' : function(website, title, description, via){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      via         = via         || blog.via;
      var description = 2;
      var oWin = window.open( 'http://www.hemidemi.com/user_bookmark/new'
                                                   + '?title='           + encodeURIComponent(title)
                                                   + '&amp;url='         + encodeURIComponent(website) 
                                                   + '&amp;description=' + encodeURIComponent(description)
                                                   + '&amp;via='         + encodeURIComponent(via) );
          oWin.focus();
      return false;                                
    }  
  },
  //MyShare
  'myshare' : {
    'Post' : function(website, title, description){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;      
      var oWin = window.open( 'http://myshare.url.com.tw/index.php?func=newurl&from=mysharepop' 
                                                   + '&amp;url='      + encodeURIComponent(website) 
                                                   + '&amp;desc='     + encodeURIComponent(title)
                                                   + '&amp;contents=' + encodeURIComponent(description) );
          oWin.focus();
      return false;                                
    }  
  },
  //BaiDu
  'baidu' : {
    'Post' : function(website, title, description){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      var oWin = window.open( 'http://cang.baidu.com/do/add?iu=' + encodeURIComponent(website) 
                                                   + '&amp;it='  + encodeURIComponent(title)
                                                   + '&amp;dc='  + encodeURIComponent(description) );
          oWin.focus();
      return false;                                
    }  
  },
  //Google
  'google' : {  
    'Post' : function(website, title, description){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      var oWin = window.open( 'http://www.google.com/bookmarks/mark?op=add'
                                                   + '&amp;bkmk='       + encodeURIComponent(website) 
                                                   + '&amp;title='      + encodeURIComponent(title)
                                                   + '&amp;annotation=' + encodeURIComponent(description) );
          oWin.focus();
      return false;                                
    }  
  },
  //Yahoo
  'yahoo' : {  
    'Post' : function(website, title){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      var oWin = window.open( 'http://tw.myweb2.search.yahoo.com/myresults/bookmarklet'
                                                   + '?t=' + encodeURIComponent(title)
                                                   + '&amp;u=' + encodeURIComponent(website) 
                                                   + '&amp;d=' + encodeURIComponent(description)
                                                   + '&amp;ei=UTF-8' );
          oWin.focus();
      return false;                                
    }  
  },    
  //Digg
  'digg' : {    
    'Post' : function(website, title, description){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      var phase   = 2;
      var oWin = window.open( 'http://digg.com/submit?phase='     + phase
                                                   + '&url='      + encodeURIComponent(website)
                                                   + '&title='    + encodeURIComponent(title) 
                                                   + '&bodytext=' + encodeURIComponent(description) );
          oWin.focus();
      return false;                                
    }  
  },  
  //Technorati
  'technorati' : {    
    'Post' : function(website, title){
      website     = website     || blog.website;
      title       = title       || blog.title;
      var oWin = window.open( 'http://technorati.com/faves?add='   + encodeURIComponent(website)
                                                       + '&title=' + encodeURIComponent(title) );
          oWin.focus();
      return false;                                
    }  
  },  
  //fUrl
  'furl' : {  
    'Post' : function(website, title, description, via){
      website     = website     || blog.website;
      title       = title       || blog.title;
      description = description || blog.description;
      via         = via         || blog.via;
      var oWin = window.open( 'http://www.furl.net/storeIt.jsp'
                                                   + '?t=' + encodeURIComponent(title)
                                                   + '&amp;u=' + encodeURIComponent(website) 
                                                   + '&amp;r=' + escape(via)
                                                   + '&amp;c=' + encodeURIComponent(description)
                                                   + '&amp;p=1' );
          oWin.focus();
      return false;                                
    }  
  },
  //Pixnet
  'pixnet' : {
    'Post' : function(website){
      website  = website || oBlog.website;
      var oWin = window.open( 'http://www.pixnet.net/' + '?done==' + encodeURIComponent(website) );
          oWin.focus();
      return false;
    }
  },   
  //寄信
  'email':{
  	//離滑鼠多遠(寬)
    'xMouse' : 10,
    //離滑鼠多高
  	'yMouse' : 20,
    //提示文字呈現的 div Id
    'tipDivId'    : 'tipDiv1',
    //收信人欄位 Id
    'mailToId' : this.tipDivId + '_MailTo',
    //標題欄位 Id
    'subjectId' : this.tipDivId + '_Subject',
    //內文欄位 Id
    'contentId' : this.tipDivId + '_Content',
    //寄信方式
    'sendMethod' : 'mailServer',
    //Click 事件 : 所要使用 mailto 語法的 Tag Id
    'clickTagAId' : this.tipDivId + '_ClickA',
    //製作寄信表單 : 寄信給朋友
  	'Form' : function(e, mailSubject, mailContent){
      //先將重複的 div Id 移除
      this.Cancel();
      //屬性設定
      this.sendButtonId   = this.tipDivId + '_sendButton';
      this.cancelButtonId = this.tipDivId + '_cancelButton';
      this.formName = this.tipDivId + '_sendFriendsForm';
      this.mailToInputTagName  = 'sendMail';
      this.subjectInputTagName = 'sendSubject';
      this.contentInputTagName = 'sendContent';
      var cssId     = '#' + this.formName;
      var cssStyle  = '<style>'
                    + cssId + '{ width: 300px; height: auto; }'
                    + cssId + ' > div{ width: 100%; float: left; margin: 3px 0; }'
                    + cssId + ' > div > input{ width: 60%; float: left; }'
                    + cssId + ' > div > input.button{ width: 100px; border: 1px solid #999; float: right; margin: 3px; }'
                    + cssId + ' > div > textarea{ width: 60%; height: 150px; float: left; }'
                    + cssId + ' > div > font{ width: 35%; float: left; text-align: right; font-size: 12px; }'
                    + cssId + ' > div > b{ float: right; color: #6fc; font-size: 12px; }'  
                    + '</style>';
      //製作 HTML               
      var inputForm = cssStyle
                    + "<form id='" + this.formName + "' name='" + this.formName + "' method='POST' action='' enctype='multipart/form-data'>"
                    + "<div>"
                    + "  <font>收件人信箱：</font>"
                    + "  <input type='text' id='" + this.mailToId + "' name='" + this.mailToInputTagName + "' value=''/>"
                    + "  <b>可使用逗號隔開多個信箱</b>"
                    + "</div>" 
                    + "<div>"
                    + "  <font>信件標題：</font>"
                    + "  <input type='text' id='" + this.subjectId + "' name='" + this.subjectInputTagName + "' value='" + mailSubject + "'/>"
                    + "</div>" 
                    + "<div>"
                    + "  <font>信件內文：</font>"
                    + "  <textarea id='" + this.contentId + "' name='" + this.contentInputTagName + "'>" + mailContent + "</textarea>"
                    + "</div>" 
                    + "<div>"
                    + "  <input type='button' id='" + this.sendButtonId + "' name='" + this.sendButtonId + "' "
                    + "         onclick='blog.email.Send(\"" + this.sendMethod + "\")' value='寄信' class='button'/>"
                    + "  <input type='button' id='" + this.cancelButtonId + "' name='" + this.cancelButtonId + "' "
                    + "         onclick='blog.email.Cancel();' value='取消' class='button'/>"
                    + "</div>"                                         
                    + '</form>';
      //製作提示文字顯示區塊
			var tooltip  = "<div id='" + this.tipDivId + "'>" + inputForm + "</div>";                  
      //將 tooltip div 新增至 body 標籤裡
      $('body').append(tooltip);
			//新增 tooltip 的 CSS 並顯示出來
      $('#' + this.tipDivId).css({
  			'width'    : 'auto',
        'height'   : 'auto',
        'position' : 'absolute',
        'z-index'  : '999',
        'opacity'  : '0.9',
        'background-color' : '#000',
  			'top'      : (e.pageY + blog.email.yMouse) + 'px',
  			'left'     : (e.pageX / 1.5 + blog.email.xMouse) + 'px',
        'padding'  : '5px',
        'color'    : '#fff',
        'font'     : '0.8em Arial, Times New Roman, Courier, "新細明體", "細明體", "標楷體"'
			}).show('fast');	
  	},
    //取消寄信
    'Cancel' : function(){
      $('#' + this.tipDivId).remove();
    },    
    //直接 MailTo 啟動 Client Outlook 寄信
    'MailTo' : function(mailToValue, subjectValue, contentValue){
      //取得值
      var mailToValue  = mailToValue  || $('#' + this.mailToId).val();
      var subjectValue = subjectValue || $('#' + this.subjectId).val();
      var contentValue = contentValue || $('#' + this.contentId).val();
      //設定副本
      var mailToCC     = '';
      //檢查 mailToValue 是否有逗號隔開
      var hasDotInMailToValue = mailToValue.indexOf(',');
      if(hasDotInMailToValue > 0){
        var mailToArray = mailToValue.split(',');
        //製作 mailTo 正副本
        var mailToArrayLength = mailToArray.length;
        for(i=0; i<mailToArrayLength; i++){
          (i == 0)? mailToValue = mailToArray[0]
                  : mailToCC += '&CC=' + mailToArray[i]; 
        }
      }
      //製作 mailTo 語法
      var mailToStr = 'mailto:'   + mailToValue 
                    + '?subject=' + encodeURIComponent(subjectValue) 
                    + '&body='    + encodeURIComponent(contentValue)
                    + mailToCC;            
      //執行呼叫 Outlook : 只支援 Big5 :: 中文字非 Big5 皆會變成亂碼
      location.href = mailToStr;
    },
    //使用 Mail-Server 寄信
    'MailServer' : function(objForm, sendUrl){
      blog.sendUrl      = sendUrl || './admin/modules/php/send.email.to.friends.php'; 
      blog.sendData     = (objForm)? ext.post_form_all(objForm) : ext.post_form_all( ext.getObj('#' + this.formName) );
      blog.sendData     = blog.sendData.replace(/\n/g, '[nl2br]');
      blog.sendDataType = 'json';
      blog.successDo    = function(){
        alert(blog.successData.message);
        blog.email.Cancel();    
      }
      blog.send();
      //回復初始設定
      blog.sendDataType = 'html';     
    },
    //寄信
    'Send' : function(sendMethod, sendForm){
      //? 使用 Outlook : 使用 Mail-Server
      (sendMethod === 'mailTo')? this.MailTo() : this.MailServer();      
    }
  },             
  //RSS
  'rss' : function(){
    
  }
}

