/** 
	Tweetsignup Widget 
	Version: 0.1
	
	By: Kofi Bosque-Hamilton (@kofic)
**/

var ts_already_loaded = false;
var ts_is_ie = false;
var ts_widget = {
  community_url : "",
  api_version : "0.1",
  get_widget_css : function(options){

		var ts_widget_css = "";
		
		ts_widget_css += ".tsWidget{";
		ts_widget_css += "color: #606060;";
		ts_widget_css += "width: 250px;";
		ts_widget_css += "text-align:left !important;";
		ts_widget_css += "margin-bottom:5px;";
		ts_widget_css += "border:1px solid #ADD8E6;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunityInfo {";
		ts_widget_css += "background-color:#DFF7FE;";
		ts_widget_css += "padding: 10px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunityName {";
		ts_widget_css += "margin-bottom: 5px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunityAvatar img {";
		ts_widget_css += "width: 24px;";
		ts_widget_css += "height: 24px;";
		ts_widget_css += "padding-right: 5px;";
		ts_widget_css += "vertical-align: middle;";
		ts_widget_css += "text-decoration: none;";
		ts_widget_css += "float:left;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommentText{";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunityDescription {";
		ts_widget_css += "font-size:13px;";
		ts_widget_css += "margin-left: 30px;";
		ts_widget_css += "margin-bottom: 5px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunityName a{";
		ts_widget_css += "color: #606060;";
		ts_widget_css += "font-size:16px;";
		ts_widget_css += "font-weight:bold;";
		ts_widget_css += "text-decoration: none;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunitySignupFormTweet a {";
		ts_widget_css += "text-decoration: underline;";
		ts_widget_css += "font-size: 12px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunitySignupForm {";
		ts_widget_css += "margin-bottom: 10px;";
		ts_widget_css += "margin-left:30px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunitySignupButton {";
		ts_widget_css += "margin-bottom: 5px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunitySignupButton input {";
		ts_widget_css += "-moz-border-radius-bottomleft:5px;";
		ts_widget_css += "-moz-border-radius-bottomright:5px;";
		ts_widget_css += "-moz-border-radius-topleft:5px;";
		ts_widget_css += "-moz-border-radius-topright:5px;";
		ts_widget_css += "background-color:#E6E6E6;";
		ts_widget_css += "border:1px solid #CCCCCC;";
		ts_widget_css += "color:#000000;";
		ts_widget_css += "cursor:pointer;";
		ts_widget_css += "padding:4px 8px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommunitySignupButton input:hover{";
		ts_widget_css += "background-color:#D5D5D5;";
		ts_widget_css += "}";
		ts_widget_css += ".tsComments {";
		ts_widget_css += "padding: 10px 10px 0 10px;";
		ts_widget_css += "color: #000000;";
		ts_widget_css += "font-size: 13px;";
		ts_widget_css += "}";

		ts_widget_css += ".tsBlueBar {";
		ts_widget_css += "border-top:1px solid #ADD8E6;";
		ts_widget_css += "}";

		ts_widget_css += ".tsComment {";
		ts_widget_css += "border-bottom: 1px dotted #d8d8d8;";
		ts_widget_css += "margin-bottom: 5px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsPrivacyInfo {";
		ts_widget_css += "padding-left:3px;";
		ts_widget_css += "font-size: 11px;";
		ts_widget_css += "text-decoration: none;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommentAvatar img {";
		ts_widget_css += "padding-right: 5px;";
		ts_widget_css += "padding-bottom: 10px;";
		ts_widget_css += "text-decoration: none;";
		ts_widget_css += "float:left;";
		ts_widget_css += "width: 20px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsCommentFooter, .tsCommentFooter a {";
		ts_widget_css += "color: #707070;";
		ts_widget_css += "font-size: 9.5px;";
		ts_widget_css += "}";
		ts_widget_css += ".tsClear {";
		ts_widget_css += "clear:both;";
		ts_widget_css += "}";
		ts_widget_css += ".tsPoweredBy img {";
		ts_widget_css += "width: 80px;";
		ts_widget_css += "padding: 3px;";
		ts_widget_css += "}";

		return ts_widget_css;
  },

	get_clean_date : function(date_str){
		var d_array = date_str.split("T");
		return d_array[0];
	},
	
	get_reply_info : function(reply_id){
		
	  if ( parseInt(reply_id) > 0 ){

		  var html = '<a href="http://www.tweetsignup.com/comments/';
	    html += reply_id;
	    html += '" target="_blank">';
	    html += 'in a reply';
	    html += '</a>';

			return html;
		}
		
		return "";
        
	},
	
	get_visibility : function(community){
		if (parseInt(community.public) > 0 ){
			return "Public";
		} else {
			return "Members only";
		}
	},
	
	get_widget_html : function(community,show_comments,show_description){
		var ts_widget_text = '<div class="tsWidget">';

		ts_widget_text += '<div class="tsCommunityInfo">';

		ts_widget_text += '<div class="tsCommunityAvatar">';
		ts_widget_text += '<a href="http://www.tweetsignup.com/communities?user=';
		ts_widget_text += community.organizer;
		ts_widget_text += '" target="_blank">';
		ts_widget_text += '<img src="';
		ts_widget_text += community.organizer_avatar;
		ts_widget_text += '" border=0 >';
		ts_widget_text += '</a>';
		ts_widget_text += '</div>';

		ts_widget_text += '<div class="tsCommunityName">';
		ts_widget_text += '<a href="http://www.tweetsignup.com/communities/';
		ts_widget_text += community.url;
		ts_widget_text += '" target="_blank">';
		ts_widget_text += community.name;
		ts_widget_text += '</a> on Tweetsignup';
		ts_widget_text += '</div>';

		if ( show_description == "true" )
		{
		  ts_widget_text += '<div class="tsCommunityDescription">';
		  ts_widget_text += community.description;
		  ts_widget_text += '</div>';
		}

		ts_widget_text += '<div class="tsCommunitySignupForm">';

		ts_widget_text += '<div class="tsCommunitySignupButton">';        
		ts_widget_text += '<input type="button" ';
		ts_widget_text += 'onclick="window.open(\'';
		ts_widget_text += 'http://twitter.com/home?status=';
		ts_widget_text += community.tweet_to_signup;
		ts_widget_text += '\');" ';
		ts_widget_text += 'title="To join tweet the following:';
		ts_widget_text += community.tweet_to_signup;
		ts_widget_text += '" ';
		ts_widget_text += 'value="Join by tweeting..." > ';
		ts_widget_text += '</div>';        

		ts_widget_text += '<div>';
		ts_widget_text += '<span class="tsCommunitySignupFormVisibilityLabel">';
		ts_widget_text += 'Visibility:';
		ts_widget_text += '</span>';
		ts_widget_text += '<span class="tsCommunitySignupFormVisibility">&nbsp;';
		ts_widget_text += this.get_visibility(community);
		ts_widget_text += '</span>';
		ts_widget_text += '</div>';

		ts_widget_text += '<div>';
		ts_widget_text += '<span class="tsCommunitySignupFormPrivacyLabel">';
		ts_widget_text += 'Info required:';
		ts_widget_text += '</span>';
		ts_widget_text += '<span class="tsCommunitySignupFormPrivacy">&nbsp;';
		ts_widget_text += this.get_privacy(community);
		ts_widget_text += '</span>';
		ts_widget_text += '</div>';

		ts_widget_text += '</div>';

		ts_widget_text += '</div>';

		ts_widget_text += '<div class="tsBlueBar"></div>';

		if ( show_comments == "true" )
		{
			ts_widget_text += '<div class="tsComments">';

			for(i=0; i<community.community_comments.length; i++) {

			  ts_widget_text += '<div class="tsComment">';

			  ts_widget_text += '<div class="tsCommentAvatar">';
			  ts_widget_text += '<a href="http://www.tweetsignup.com/users/';
			  ts_widget_text += community.community_comments[i].user_name;
			  ts_widget_text += '" target="_blank">';
			  ts_widget_text += "<img src='"+community.community_comments[i].avatar+"' border=0>";
			  ts_widget_text += '</a>';
			  ts_widget_text += '</div>';
			  ts_widget_text += '<div class="tsCommentText">';
			  ts_widget_text += community.community_comments[i].comment;
			  ts_widget_text += '</div>';

			  ts_widget_text += '<div class="tsCommentFooter">';
			  ts_widget_text += '<a href="http://www.tweetsignup.com/comments/';
			  ts_widget_text += community.community_comments[i].id;
			  ts_widget_text += '" target="_blank">';
			  ts_widget_text += this.get_clean_date(community.community_comments[i].created_at);
			  ts_widget_text += '</a>&nbsp;&nbsp;';
			  ts_widget_text += this.get_reply_info(community.community_comments[i].replied_to);
			  ts_widget_text += '</div>';
			
			  ts_widget_text += '<div class="tsClear">';
			  ts_widget_text += '</div>';

			  ts_widget_text += '</div>';

			}

		  ts_widget_text += '</div>';
			
		}

		ts_widget_text += '<div class="tsPoweredBy">';
		ts_widget_text += '<a href="http://www.tweetsignup.com" target="_blank">';
		ts_widget_text += '<img src="http://static.tweetsignup.com/images/logo.small.png" border=0 >';        
		ts_widget_text += '</a>';
		ts_widget_text += '</div>';

		ts_widget_text += '</div>';
		
		return ts_widget_text;
		
	},

  get_privacy : function(community){
		var privacy = community.community_privacy;
		var privacy_html = "";

		if ( parseInt(privacy.name_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/user.png\" title=\"Your name is required\">";
		}

		if ( parseInt(privacy.email_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/email.png\" title=\"Your email is required and will be opted-in\">";
		}

		if ( parseInt(privacy.phone_number_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/phone.png\" title=\"Your phone number is required\">";
		}

		if ( parseInt(privacy.date_of_birth_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/date.png\" title=\"Your date of birth is required\">";
		}

		if ( parseInt(privacy.address_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/house.png\" title=\"Your address is required\">";
		}

		if ( parseInt(privacy.locale_required) == 1 ){
			privacy_html += "<img src=\"http://static.tweetsignup.com/images/map.png\" title=\"Your locale is required. Locale is the area user lives, not their address.\">";
		}

		privacy_html += "<a onclick=\"document.getElementById('tsPrivacyInfoShowLink" +  community.id + "').style.display = 'none';document.getElementById('tsPrivacyInfoHideLink" +  community.id + "').style.display = '';document.getElementById('tsPrivacyInfoDiv" +  community.id + "').style.display='';return false;\" class=\"tsPrivacyInfo\" href=\"#privacy\" id=\"tsPrivacyInfoShowLink" +  community.id + "\">Show details...</a>";

		privacy_html += "<a style=\"display:none;\" onclick=\"document.getElementById('tsPrivacyInfoShowLink" +  community.id + "').style.display = '';document.getElementById('tsPrivacyInfoHideLink" +  community.id + "').style.display = 'none';document.getElementById('tsPrivacyInfoDiv" +  community.id + "').style.display='none';return false;\" class=\"tsPrivacyInfo\" href=\"#privacy\" id=\"tsPrivacyInfoHideLink" +  community.id + "\">Hide details</a>";


		privacy_html += "<div style=\"display:none;\" id=\"tsPrivacyInfoDiv" +  community.id + "\">";

		privacy_html += "<small>";

		if ( parseInt(privacy.name_required) == 1 ){
			privacy_html += "Your name is required.";
			privacy_html += "<br>";
		}

		if ( parseInt(privacy.email_required) == 1 ){
			privacy_html += "Your email is required and will be opted-in.";
			privacy_html += "<br>";
		}

		if ( parseInt(privacy.phone_number_required) == 1 ){
			privacy_html += "Your phone number is required.";
			privacy_html += "<br>";
		}

		if ( parseInt(privacy.date_of_birth_required) == 1 ){
			privacy_html += "Your date of birth is required.";
			privacy_html += "<br>";
		}

		if ( parseInt(privacy.address_required) == 1 ){
			privacy_html += "Your address is required.";
			privacy_html += "<br>";
		}

		if ( parseInt(privacy.locale_required) == 1 ){
			privacy_html += "Your locale is required.";
			privacy_html += "<br>";
			privacy_html += "<em>";
			privacy_html += "Locale is the area user lives, not their address.";
			privacy_html += "</em>";
			privacy_html += "<br>";
		}

		privacy_html += "</small>";
		privacy_html += "</div>";

		return privacy_html;

  },
  get_communities_api_url : function(){
    return "http://api.tweetsignup.com/" + this.api_version + "/communities/" + this.community_url + ".js";
  },
  get_communities_api_with_callback_url : function(){
    return "http://api.tweetsignup.com/" + this.api_version + "/communities/" + this.community_url + ".callback.js";
  },

  add_stylesheet : function(style) {

    var style_tag = document.createElement("style");
    style_tag.type = "text/css";
    if (ts_is_ie) {
      style_tag.styleSheet.cssText = style;
    } else {
      var inner = document.createDocumentFragment();
      inner.appendChild(document.createTextNode(style));
      style_tag.appendChild(inner);
    }

    if (!ts_is_ie || ts_already_loaded ) {
      document.getElementsByTagName("head")[0].appendChild(style_tag);
    } else {
      window.attachEvent("onload",function() {
        ts_already_loaded = true;
        document.getElementsByTagName("head")[0].appendChild(style_tag);
      })
    }

    return style_tag;
   },

    	
   load_js : function(url){
    
    var head_id = document.getElementsByTagName("head")[0];
        
    if (!document.getElementById){
      return;
    }
            
    var js_script=document.createElement('script')
    js_script.setAttribute("type","text/javascript");
    js_script.setAttribute("src", url);

    head_id.appendChild(js_script);
            
  }
    	
};

var BrowserDetect = {
init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
          || this.searchVersion(navigator.appVersion)
          || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
      },
searchString: function (data) {
                for (var i=0;i<data.length;i++) {
                  var dataString = data[i].string;
                  var dataProp = data[i].prop;
                  this.versionSearchString = data[i].versionSearch || data[i].identity;
                  if (dataString) {
                    if (dataString.indexOf(data[i].subString) != -1)
                      return data[i].identity;
                  }
                  else if (dataProp)
                    return data[i].identity;
                }
              },
searchVersion: function (dataString) {
                 var index = dataString.indexOf(this.versionSearchString);
                 if (index == -1) return;
                 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
               },
dataBrowser: [
             {
string: navigator.userAgent,
        subString: "Chrome",
        identity: "Chrome"
             },
             {   string: navigator.userAgent,
subString: "OmniWeb",
           versionSearch: "OmniWeb/",
           identity: "OmniWeb"
             },
             {
string: navigator.vendor,
        subString: "Apple",
        identity: "Safari",
        versionSearch: "Version"
             },
             {
prop: window.opera,
      identity: "Opera"
             },
             {
string: navigator.vendor,
        subString: "iCab",
        identity: "iCab"
             },
             {
string: navigator.vendor,
        subString: "KDE",
        identity: "Konqueror"
             },
             {
string: navigator.userAgent,
        subString: "Firefox",
        identity: "Firefox"
             },
             {
string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
             },
             {   // for newer Netscapes (6+)
string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
             },
             {
string: navigator.userAgent,
        subString: "MSIE",
        identity: "Explorer",
        versionSearch: "MSIE"
             },
             {
string: navigator.userAgent,
        subString: "Gecko",
        identity: "Mozilla",
        versionSearch: "rv"
             },
             {     // for older Netscapes (4-)
string: navigator.userAgent,
        subString: "Mozilla",
        identity: "Netscape",
        versionSearch: "Mozilla"
             }
      ],
        dataOS : [
        {
string: navigator.platform,
        subString: "Win",
        identity: "Windows"
        },
        {
string: navigator.platform,
        subString: "Mac",
        identity: "Mac"
        },
        {
string: navigator.userAgent,
        subString: "iPhone",
        identity: "iPhone/iPod"
        },
        {
string: navigator.platform,
        subString: "Linux",
        identity: "Linux"
        }
      ]

};

BrowserDetect.init();
if (BrowserDetect.browser == 'Explorer') {
  switch(BrowserDetect.version) {
    case 8:
      ts_is_ie = true;
      break;
    case 7:
      ts_is_ie = true;
      break;
    case 6:
      ts_is_ie = true;
      break;
  }
}
