window.onload=add_stylesheet; //if you add this on the fly, it takes about 3 seconds to kick in during which the page is U.G.L.Y #

destination = new Array('digg','stumbleupon','myspace','facebook','twitter','linkedin','aol','ask','delicious','gmail','google','reader','hotmail','live','typepad','ymail'); //where we can share to

var tracking_id = document.getElementById('share_tracking_id').innerHTML;

check_ref(); //check the referrer - see if they are accessing the job from our share tracker (well, we can only assume)

function check_ref(){
    var ref = document.referrer;
    if (ref != ''){
        for(i=0;i<destination.length;i++) {
            var re = new RegExp(destination[i]);
            if (ref.match(re)){
                var img = '<img src="http://counter.adcourier.com/' + tracking_id +'.gif?referer='+ref+'">';
                    //commented whilst we think how/where to record referrals.
                    //document.getElementById('share_content').innerHTML = img; 
            }
        }
    }
}

function sharethis(search){

                document.getElementById('share_preview').style.display = 'none';
                if (!search){
                    search = '';
                }
                search = search.toLowerCase();
                search = search.split(' ').join(''); //we dont have spaces in our results - rightly or wrongly.
                var re = new RegExp(search);

                var results = new Array();

                for(d=0;d<destination.length;d++) {
                    if (destination[d].match(re)){
                        results.push(destination[d]);
                    }
                }

                results.sort();

                var content = '';
    
                if (!search){ //only reprint the search box etc if we need to.
                    //header
                    content = '<div id="share_header">BOOKMARK AND SHARE';
                
                    //search boxes.
                    content = content + '<br /><input id="bb_share_tracking_id" value="'+tracking_id+'" type="hidden"><input id="bb_share_search" type="text" style="background-image:url(\'http://counter.adcourier.com/share/images/magnifying-glass.gif\'); background-repeat:no-repeat; padding-left:20px; width: 90%; margin-top: 5px; border:1px solid #CCCCCC;" onkeyup="filter_list()" value="'+ search +'"></div>';
            
                    content = content + '<div id="share_what">'; //results box

                }

                //notification area
                content = content + '<div id="bb_share_notification" style="text-align: center; display: none; font-weight: bold; padding: 5px;">&nbsp;</div>';

                var numberofresults = 2;

                if (search){ //if its a search, show results per line
                    numberofresults = 1;
                }

                //results
                for(i=0;i<results.length;i++) {                      
                      content = content + '<div class="line">';
                      content = content + '<div class="unit size1of'+numberofresults+'"><img src="http://counter.adcourier.com/share/images/icons/'+results[i]+'.jpg"><a href="http://counter.adcourier.com/share/share.cgi?url='+ document.location.href +'&destination='+results[i]+'&tracking_id='+ tracking_id + '" target="_blank" onclick="javascript: bb_share_notify(\'Successfully shared to \',\''+results[i]+'\');">&nbsp;'+results[i]+'</a></div>';
                    
                    if (!search){
                      i++;
                      if (results[i]){ //might not always be a second result.
                         content = content + '<div class="unit size1of2 lastUnit"><img src="http://counter.adcourier.com/share/images/icons/'+results[i]+'.jpg"><a href="http://counter.adcourier.com/share/share.cgi?url='+ document.location.href +'&destination='+results[i]+'&tracking_id='+ tracking_id + '" target="_blank" onclick="javascript: bb_share_notify(\'Successfully shared to \',\''+results[i]+'\');">&nbsp;'+results[i]+'</a></div>';
                      }
                    }
                      content = content + '</div>';
                } 

                if (results.length == 0){
                    content = content + '<p style="margin-top: 50px; font-weight: bold; text-align: center;">No Results</p>';
                }

                if (!search){ //already there if its a search.
                    //footer
                    content = content + '</div><div id="share_footer"><a style="float: left;" href="javascript: void(0);" onclick="javascript: document.getElementById(\'light\').style.display=\'none\';document.getElementById(\'fade\').style.display=\'none\'; return false;">(x) close</a><a style="float: right;" href="javascript: void();" onclick="javascript: bookmarksite(); return false;">bookmark (+)</a></div>';
                }

                //show it :)
                if (!search){ //just update the results div if its a search
                    document.getElementById('share_content').innerHTML = '<div id="light" class="share_me">'+content+'</div><div id="fade" class="black_overlay"></div>';
                } else {
                    document.getElementById('share_what').innerHTML = content;
                }
                document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';
                document.getElementById("bb_share_search").focus();
                return 1;
}

function clean_links(){
    var d = document.getElementsByTagName('a');
    for (var i = d.length-1; i > -1; i--) {
            d[i].removeAttribute('target');
    }
    return 1;
}

function sharethis_preview(){
  return false;
  var content = '';
  results = destination.reverse();
  content = '<div id="share_content">';
  content = content + '<div class="line">';

  for(i=10;i<results.length;i++) { //show some random results.
      content = content + '<div class="unit size1of1 lastUnit"><img src="http://counter.adcourier.com/share/images/icons/'+results[i]+'.jpg"><a href="http://counter.adcourier.com/share/share.cgi?url='+ document.location.href +'&destination='+results[i]+'&tracking_id='+ tracking_id + '" target="_blank" onclick="javascript: bb_share_notify(\'Successfully shared to \',\''+results[i]+'\');">&nbsp;'+results[i]+'</a></div>';
  }

  //more and close boxes.
  content = content + '<div class="unit size1of1 lastUnit" style="background: #6b6b6b; line-height:2em; -moz-border-radius-bottomleft: 5px; -webkit-border-bottom-left-radius5px;-moz-border-radius-bottomright: 5px; -webkit-border-bottom-right-radius: 5px; padding-left: 2px; padding-right: 2px; padding-bottom: 2px;"><a style="float: left; color: white;" href="javascript: void(0);" onclick="javascript: document.getElementById(\'share_preview\').style.display=\'none\'; return false;">(x)</a><a style="float: right; color: white;" href="javascript: void(0);" onclick="javascript: sharethis(); return false;">more (+)</a></div>';

  content = content + '</div></div></div>';
  document.getElementById('share_preview').innerHTML = content;
  document.getElementById('share_preview').style.display = '';
  return 1;
}

function bb_share_notify(text,where){
    document.getElementById('bb_share_notification').innerHTML = '<p style="color: red">' + text + where +'</p>';
    document.getElementById('bb_share_notification').style.display = '';
    setTimeout("hidediv('bb_share_notification')", 5000);
    return 1;
}

function filter_list(){
    var search = document.getElementById('bb_share_search').value;
    sharethis(search);
    return 1;
}

function bookmarksite(){
    var title = top.document.title;
    var url = document.location.href;
    var success = 0;
    if (window.sidebar){ // firefox
        window.sidebar.addPanel(title, url, "");
        success = 1;
    }else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
        success = 1;
    }else if(document.all){ // ie
        window.external.AddFavorite(url, title);
        success = 1;
    } else{
         alert("Sorry, we do not support automatic bookmarking for your browser at this time.");
    }
    if (success){
        bb_share_notify('Successfully bookmarked','');
    }
    return 1;
}

function add_stylesheet(){
    if(document.createStyleSheet) {
        document.createStyleSheet('http://counter.adcourier.com/share/css/share.css');
    } else {
        var styles = "@import url('http://counter.adcourier.com/share/css/share.css');";
        var newSS=document.createElement('link');
        newSS.rel='stylesheet';
        newSS.href='data:text/css,'+escape(styles);
        document.getElementsByTagName("head")[0].appendChild(newSS);
    }
    return 1;
}

function hidediv(arg) {
        document.getElementById(arg).style.display = 'none';
        return 1;
}
