// Copyright 2009 Scott Carter.  All Rights Reserved.


// Invocation of BigTweet
function bigt_fKB1_bigtweet(num){
    // Get a reference to our global object
    eval("var bigt = bigt_" + num);     
     
    // Get the query args.  
    // Not passing dom (domain) or pur (placement url)
    // since kernel is not currently loaded in all cases.
    var args = "?n_wdgt_u=" + encodeURIComponent(bigt.widget_username) + 
    "&n_wdgt_wid=" + encodeURIComponent(bigt.widgetId) + 
    "&n_wdgt_pid=" + encodeURIComponent(bigt.placementId) +
    "&n_wdgt_dom=&n_wdgt_pur=";
    
    // Any user provided overrides?
    if(typeof bigt.bigtweet_prefix != "undefined"){
        args += "&n_wdgt_prefix=" + encodeURIComponent(bigt.bigtweet_prefix);
    }
    
    // Title and URL are special for the case of '/widget_inline' (top == self) 
    // If user provides an empty string '' we encode this to recognize it 
    // specially on the back end.
    if(typeof bigt.bigtweet_title != "undefined"){
        if( (top == self) && (bigt.bigtweet_title == '') ) {
            bigt.bigtweet_title = 'EMPTY_STRING';
        }
        args += "&n_doc_title=" + encodeURIComponent(bigt.bigtweet_title);
    }
    if(typeof bigt.bigtweet_url != "undefined"){
        if( (top == self) && (bigt.bigtweet_url == '') ) {
            bigt.bigtweet_url = 'EMPTY_STRING';
        }
        args += "&n_location_href=" + encodeURIComponent(bigt.bigtweet_url);
    }
    
    
    // Inline allowed if not in IFRAME, or if this is seed domain (seed domain
    // page is allowed in IFRAME).        
    if( (top == self) || (location.hostname == bigt.btSeedDomain) ) {
        var e=document.createElement('script');
        e.setAttribute('type','text/javascript');
        e.setAttribute('src',bigt.webRoot + '/widget_inline' + args);
        document.body.appendChild(e);
    }
    else {
        window.open(bigt.webRoot + "/widget_window" + args,"tweet",'width=800,height=500,location=yes,menubar=no,resizable=yes,scrollbars=no,status=yes,titlebar=yes,toolbar=no');
    }
}


// Element resets for an <a>
function bigt_fKB1_reset_a(elem){
    elem.style.border = "0px";
    elem.style.lineHeight = "normal";
    elem.style.margin = "0px";
    elem.style.outlineStyle = "none";  // Get rid of dotted line in Firefox
    elem.style.padding = "0px";
}


// Element resets for a <div>
function bigt_fKB1_reset_div(elem){
    elem.style.border = "0px";
    elem.style.lineHeight = "normal";
    elem.style.margin = "0px";
    elem.style.outlineStyle = "none";
    elem.style.padding = "0px";
    elem.style.position = "static";
}


// Element resets for a <div>
function bigt_fKB1_reset_span(elem){
    elem.style.border = "0px";
    elem.style.lineHeight = "normal";
    elem.style.margin = "0px";
    elem.style.outlineStyle = "none";
    elem.style.padding = "0px";
    elem.style.position = "static";
}


// Add a tweet count
function bigt_fKB1_tweetCount(bigt,e_parent){
    var e = document.createElement('a');
    bigt_fKB1_reset_a(e); // <a> resets


    e.id = "bigt_" + bigt.num + "_tweetCount";
    
    // If the count is 0, we don't link out
    if(bigt.widget_count == 0){
        e.href = "javascript:void 0"; 
        e.title = "No tweets yet.";
    }
    else {
        // Stats link
        if(bigt.widget_countShortUrlHash != ""){
            e.href = "http://j.mp/" + bigt.widget_countShortUrlHash + "+";
            e.target = "_blank";
            e.title = "Tweet count. Click for Bit.ly stats.";
        }
        // No url. 
        else {
            e.href = "javascript:void 0";
            e.title = "Tweet count";
        }
    }
    
    
    // Full form factor
    if(bigt.widget_formFactor == 0){
        e.style.fontWeight = "normal";
    }
    else {
        e.style.fontWeight = "bold";
        e.style.marginRight = "5px";
        e.style.fontSize = "14px";
    }
    
    
    e.className = "bigt_" + bigt.num + "_c_link";
    
    e.style.color = "#424242";
    e.style.display = "block";
    e.style.border = "medium none";
    e.style.textAlign = "center";
    e.style.textDecoration = "none";
    e.style.cursor = "pointer";
    
    // Full form factor
    if(bigt.widget_formFactor == 0){
        e.style.height = bigt.tweetCountHeight;
        e.style.width = bigt.tweetCountWidth;
        e.style.background = 'transparent url(' + bigt.webRoot + '/widget_images/widget_button1_count_background.gif) no-repeat scroll 0 0';
    }
    
    e.style.fontFamily = 'arial,verdana,"lucida grande",tahoma,sans-serif';
    e_parent.appendChild(e);
    
    
    // Compact, button only form factors
    if(bigt.widget_formFactor != 0){
        var eSpan = document.createElement('span');
        bigt_fKB1_reset_span(eSpan); // <span> resets
        eSpan.innerHTML = bigt.widget_count;
        e.appendChild(eSpan);
        return;    
    }
    
    
    // Full form factor
    var eSpan1 = document.createElement('span');
    bigt_fKB1_reset_span(eSpan1); // <span> resets
    eSpan1.style.fontSize = "17px";
    eSpan1.style.paddingTop = "3px";
    eSpan1.style.display = "block";
    eSpan1.innerHTML = bigt.widget_count;
    e.appendChild(eSpan1);
    

    var eSpan2 = document.createElement('span');
    bigt_fKB1_reset_span(eSpan2); // <span> resets
    eSpan2.style.fontSize = "10px";
    eSpan2.style.lineHeight = "10px";
    eSpan2.style.display = "block";
    
    if(bigt.widget_count != 1){
        eSpan2.innerHTML = 'tweets'; 
    }
    else {
        eSpan2.innerHTML = 'tweet';
    }

    e.appendChild(eSpan2);
    
}

// Add tweet button text
function bigt_fKB1_tweetText(bigt,e_parent){
    var e = document.createElement('a');
    bigt_fKB1_reset_a(e); // <a> resets

    e.id = "bigt_" + bigt.num + "_tweetText";
    e.href = "javascript:bigt_fKB1_bigtweet(" + bigt.num + ");void 0"; // Invocation of BigTweet
    
    e.innerHTML = bigt.widget_button_label;
    
    e.title = "Send a tweet";
    e.className = "bigt_" + bigt.num + "_c_link bigt_" + bigt.num + "_c_link_label";
    
    e.style.backgroundColor = bigt.widget_button_style_norm_back_color;
    e.style.color = bigt.widget_button_style_norm_color;
    e.onmouseover = bigt_fkB1_tweetTextMouseOver;
    e.onmouseout = bigt_fkB1_tweetTextMouseOut;
    
    e.style.cursor = "pointer";
    e.style.display = "inline";
    e.style.fontSize = "11px";
    e.style.fontWeight = "normal";
    
    // 14px too small for IE6
    e.style.height = "16px"; 
    e.style.lineHeight = "16px"; 
    
    e.style.padding = "0px 4px 0px 5px"; 
    e.style.textAlign = "center";
    e.style.textDecoration = "none";
    
    e.style.borderColor = bigt.widget_button_style_norm_border_color; 
    e.style.borderWidth = "1px";
    e.style.borderStyle = "solid";
    
    e.style.fontFamily = 'arial,verdana,"lucida grande",tahoma,sans-serif';
    e_parent.appendChild(e); 

}


// Restore original appearance of tweet text button when mouse leaves
function bigt_fkB1_tweetTextMouseOut(){
    var regex = /_(\d+)_/;  // Match the number in the center of id
    var num = this.id.match(regex);
    if(num == null){
        return;
    }
    
    // Get a reference to our global object. Second array elem is parenthesized match.
    eval("var bigt = bigt_" + num[1]);    
    
    this.style.backgroundColor = bigt.widget_button_style_norm_back_color;
    this.style.color = bigt.widget_button_style_norm_color;
}


// Change appearance of tweet text button when mouse enters
function bigt_fkB1_tweetTextMouseOver(){
    var regex = /_(\d+)_/;  // Match the number in the center of id
    var num = this.id.match(regex);
    if(num == null){
        return;
    }
    
    // Get a reference to our global object. Second array elem is parenthesized match.
    eval("var bigt = bigt_" + num[1]);    
    
    this.style.backgroundColor = bigt.widget_button_style_hover_back_color;
    this.style.color = bigt.widget_button_style_hover_color;
}


// Process the widget rendering.        
function bigt_fKB1_widgetProcess(bigt){        
 
    // Element creation
    var e;          // New element
    var e_parent;   // Element parent
     
   
    // Display our content height at the chosen size. 
    bigt_fKB1_reset_div(bigt.e_contentDiv); // <div> resets
    
    bigt.e_contentDiv.style.height = bigt.contentDivHeight;
    //bigt.e_contentDiv.style.width = bigt.contentDivWidth; 
    
    // Transparent content background when minimized
    bigt.e_contentDiv.style.backgroundColor = "transparent";  

    bigt.e_contentDiv.style.display = "block";
    

    //
    // ----- Populate our content -----
    //
    
    // Button div & link
    // ---------------------
    e = document.createElement('div');
    bigt_fKB1_reset_div(e); // <div> resets
    
    e.id = "bigt_" + bigt.num + "_contentButton";
    
    e.style.fontFamily = "arial"
    e.style.height = bigt.contentButtonDivHeight;
    e.style.overflow = "hidden";
    
    // We don't center align since we want to be able to allow the user
    // to edit the widget size for Wordpress posts to the minimum
    // button dimensions, and still have the button visible.
    // With center align, the button is centered to the original widget
    // width and could get cut off ...
    e.style.textAlign = "left";

    bigt.e_contentDiv.appendChild(e);
    
    
    e_parent = document.getElementById("bigt_" + bigt.num + "_contentButton");
    
    // Add tweet count for full and compact form factors.
    if( (bigt.widget_formFactor == 0) || 
        (bigt.widget_formFactor == 1) )  {
        bigt_fKB1_tweetCount(bigt,e_parent);
    }

    // Add tweet text for all form factors
    bigt_fKB1_tweetText(bigt,e_parent);

}  // function bigt_fKB1_widgetProcess(){  


// Main entry point
function bigt_fKB1_main(num){
    
    // Get a reference to our global object
    eval("var bigt = bigt_" + num);       
  
    // Content div. 
    bigt.e_contentDiv = document.getElementById("bigt_" + num + "_content");    
    
    // Full form factor
    if(bigt.widget_formFactor == 0){
        bigt.tweetCountWidth = "50px";
        bigt.tweetCountHeight = "44px";
        bigt.contentButtonDivHeight = "64px"; // Extra 44px for tweet count
    }
    // Compact & button only form factors
    else {
        bigt.contentButtonDivHeight = "20px"; // Must be at least 16px in compact mode
    }

    // Size of content in minimized state.
    // Set to height of content button div.  
    bigt.contentDivHeight = bigt.contentButtonDivHeight;  
    
    // Widget dimensions at Clearspring for sharing set to 52px x 64px
    // Not necessary to fix an actual widget width.
    //
    // bigt.contentDivWidth = "52px";

    // Widget rendering.  Catch errors for debug.
    try {
        bigt_fKB1_widgetProcess(bigt);
    }
    catch(e){
        alert(e);
    }

}
   

    

    // Create global BigTweet object.  
    // Must be unique in the presence of identical widgets
  
    eval("bigt_65439 = new Object()");       
   
    bigt_65439.webRoot = "http://widget1.bigtweet.com";
    bigt_65439.btSeedDomain = "widget1.bigtweet.com";
    bigt_65439.widgetId = "4a9f1689e574f059";
    bigt_65439.placementId = "m01_dbe9df0c2e6d6c6133cadc78d638bfd5";
    bigt_65439.widget_formFactor = "0";
    bigt_65439.widget_username = "";
    bigt_65439.widget_button_label = "tweet";
    bigt_65439.widget_button_style_norm_color = "#FFFFFF";
    bigt_65439.widget_button_style_norm_back_color = "#4876FF";
    bigt_65439.widget_button_style_hover_color = "#4876FF";
    bigt_65439.widget_button_style_hover_back_color = "#FFFFFF";
    bigt_65439.widget_button_style_norm_border_color = "#00008B";
    bigt_65439.widget_count = "0";
    bigt_65439.widget_countUrl = "HASH(0xfaff2dc)";
    bigt_65439.widget_countShortUrlHash = "HASH(0xfaff2a0)";
    bigt_65439.widget_countTimestamp = "1254855006";
    
    // Sometimes we will be passing around a reference to our
    // global object and will want to access the unique identifier
    bigt_65439.num = 65439;
    
    // Is this our seed domain?   
    // We treat the seed domain specially and provide a generic reference to global object.
    if(location.hostname == "widget1.bigtweet.com"){
        // Did widget on seed domain specify a gid?  Valid ones are less than 999          
        if(999 < 999){
            bigt_65439.num = 999; // Override placement identifier so host page can predict it.
            bigt_999 = bigt_65439; // Get a generic reference to our global object
        }
    }
    
    
    // Any user provided overrides?   
    if(typeof bigtweet_prefix != "undefined"){
        bigt_65439.bigtweet_prefix = bigtweet_prefix;
    }
    
    if(typeof bigtweet_title != "undefined"){
        bigt_65439.bigtweet_title = bigtweet_title;
    }
    
    if(typeof bigtweet_url != "undefined"){
        bigt_65439.bigtweet_url = bigtweet_url;
    }
    
    var undefined;  // Make sure that this exists 
 
    // Reset for future widget invocations
    bigtweet_prefix = undefined;
    bigtweet_title = undefined;
    bigtweet_url = undefined;
    
    
       
    // Our content div
    var bigt_document_65439;
    
    // Form factor 0 & 2 do not use float:left in class bigt_N_c_link
    if( (bigt_65439.widget_formFactor == 0) ||
        (bigt_65439.widget_formFactor == 2) ) {
        bigt_document_65439= '<style type="text/css">a.bigt_' + bigt_65439.num + '_c_link_label {-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;-moz-border-radius-bottomleft:3px;-moz-border-radius-bottomright:3px;-moz-border-radius-topleft:3px;-moz-border-radius-topright:3px; }</style><div id="bigt_' + bigt_65439.num + '_content" style="display:none"></div>';
    }
    else {
        bigt_document_65439= '<style type="text/css">a.bigt_' + bigt_65439.num + '_c_link_label {-moz-background-clip:border;-moz-background-inline-policy:continuous;-moz-background-origin:padding;-moz-border-radius-bottomleft:3px;-moz-border-radius-bottomright:3px;-moz-border-radius-topleft:3px;-moz-border-radius-topright:3px; } a.bigt_' + bigt_65439.num + '_c_link {float: left}</style><div id="bigt_' + bigt_65439.num + '_content" style="display:none"></div>';
    }
       
    
 
    
    document.write(bigt_document_65439);
    bigt_fKB1_main(bigt_65439.num);
    
