var clientId = "lmw";
//var domain = "http://127.0.0.1:8080/serve/smartbtn/" + clientId + "/";
var domain = "http://lmw.sharedbook.com/serve/smartbtn/" + clientId + "/";

function SharedBookSmartButton()
{
   // ***********  Private members  ***********

   var cookie_name = clientId + "_sbUserId";

   // Timer ID
   var tid;

   // Count how many times setInterval(.) was called to allow max retries 
   var interval_counter = 0;

   var printableUrl = document.location.href;

   var userIdFromCookie = null;

   // **************************************************************************
   // Privileged functions (public with access to private methods)
   // **************************************************************************

   this.sharedbookAddArticle = function ()
   {
      userIdFromCookie = new Date().getTime();

      // This code generats "var finished_add_multi_urls = 'True'".
      var add_article_url = encodeURI(domain + "add_multiple_urls_to_book.djs?user_id=" + userIdFromCookie + "&article_url=" + printableUrl);
      var add_article_script = document.createElement('script');
      add_article_script.src = add_article_url;
      document.getElementsByTagName('head')[0].appendChild(add_article_script);

      document.getElementById("blockingDiv").style.display = "";      

      tid = setInterval(handleAddMultiURLsToClipboard, 1000);

      function handleAddMultiURLsToClipboard()
      {
         if(typeof finished_add_multi_urls != 'undefined' && finished_add_multi_urls != null)
         {
            clearInterval(tid);
            interval_counter = 0;

            if (finished_add_multi_urls == "True")
            {
               // Reset to allow subsequent requests that will use the same var.
               finished_add_multi_urls = null;

               // This code generats "var articles_exists = 'True'" (or False).
               var finalize_article_url = encodeURI(domain + "verify_articles_in_book.djs?user_id=" + userIdFromCookie);
               var finalize_article_script = document.createElement('script');
               finalize_article_script.src = finalize_article_url;
               document.getElementsByTagName('head')[0].appendChild(finalize_article_script);

               tid = setInterval(goToBridgePage, 500);
            }
            else
            {
               // Reset to allow subsequent requests that will use the same var.
               finished_add_multi_urls = null;

               document.getElementById("blockingDiv").style.display = "none";
               document.getElementById("errorElm").style.display = "";
            }
         }
         else if (interval_counter++ > 120)
         {
            clearInterval(tid);
            interval_counter = 0;

            document.getElementById("errorElm").style.display = "";
            document.getElementById("blockingDiv").style.display = "none";
         }
      }

      function goToBridgePage()
      {
         if(typeof articles_exists != 'undefined' && articles_exists != null)
         {
            clearInterval(tid);
            interval_counter = 0;

            if(articles_exists == "True")
            {
                // Reset to allow subsequent requests that will use the same var.
               articles_exists = null;

               top.document.location.href = encodeURI(domain + "bridge.djs?user_id=" + userIdFromCookie + "&article_url=" + document.location.href);
               document.getElementById("blockingDiv").style.display = "none";
            }
            else
            {
               articles_exists = null;
               
               document.getElementById("blockingDiv").style.display = "none";

//               alert("No chapters found");
               document.getElementById("errorElm").style.display = "";
            }
         }
         else if (interval_counter++ > 120)
         {
            clearInterval(tid);
            interval_counter = 0;

//            alert("Failed to reach the bridge page....");
            document.getElementById("blockingDiv").style.display = "none";
            document.getElementById("errorElm").style.display = "";
         }
      }

   };

   this.popup = function()
   {
      obj = document.getElementById('smartbuttonPopup');
      obj.style.visibility = (obj.style.visibility == 'hidden') ? '' : 'hidden';
      document.getElementById("errorElm").style.display = "none";      
   }

   // **************************************************************************
   // Private functions
   // **************************************************************************
   function elementSetAtt(elm, name, value)
   {
      elm.setAttribute(name, value);
   }
   function createElement(name, value)
   {
      var elm = document.createElement("input");
      elementSetAtt(elm, "type", "hidden");
      elementSetAtt(elm, "name", name);
      elementSetAtt(elm, "value", value);
      return elm;
   }
}

// Important: client name turns to directory name, keep it lower case !
var sbSmartBtnObj = new SharedBookSmartButton();

var dws = ""

dws += '<style>';
dws += '#sharedbook_main {';
dws += '	margin:1px 0px 0px 5px;';
dws += '	padding:0;';
dws += '	z-index: 30;';
dws += '	font: bold 11px Arial, Helvetica, sans-serif;';
dws += '}';
dws += '#sharedbook_main div a {';
dws += '	position: relative;';
dws += '	display: block;';
dws += '	margin: 0;';
dws += '	padding: 5px 10px;';
dws += '	width: auto;';
dws += '	white-space: nowrap;';
dws += '	text-align: left;';
dws += '	text-decoration: none;';
dws += '	color: #000;';
dws += '	font-size:11px;';
dws += '	background: #fff;';
dws += '}';
dws += '#sharedbook_main div a:hover {';
dws += '	background: #ebebeb;';
dws += '	color: #666;';
dws += '}';
dws += '#smartbuttonPopup {';
dws += 'font-family:arial;';
dws += 'font-size:12px;';
dws += 'color:#000;';
dws += '}';
dws += '.smbtnpopup-title {';
dws += 'color: #000;';
dws += 'font-size:22px;';
dws += 'font-weight:bold;';
dws += 'text-decoration: underline;}';
dws += '.smbtnpopup-TextRegular {';
dws += 'margin-bottom:0px;';
dws += 'padding-bottom:0;';
dws += 'font-size:12px;';
dws += 'color:#000;';
dws += '';
dws += '}';
dws += 'a.smbtnpopup-link {';
dws += 'color:#000;';
dws += 'font-size:12px;';
dws += '}';
dws += '.smbtnpopup-smaller {font-size:10px;padding-top:10px;color:#000;}';
dws += '.smbtnpopup-floatr {float:right;position:relative;top:-20px;left:-30px;}';
dws += '</style>';
dws += '<!--SMART BUTTON POPUP DIV-->'
dws += '<div id="smartbuttonPopup" style="position: absolute;	visibility: hidden; width: 500px;	left: 210px;	top: 200px;background:#fff;border:5px groove #fff;padding:10px;z-index:10000;text-align:left;">';
dws += '<span class="smbtnpopup-title "><i>Introducing the Legacy.com Memorial Book</i></span><br />  ';
dws += '<p class="smbtnpopup-TextRegular"  style=""><img src="' + domain + 'images/sftcvr_book_spread_antique.jpg" alt="" align="right" width="200"/>Hold on to the beautiful thoughts and memories shared by friends and loved ones with a full-color, professionally printed and bound keepsake.<br /><br />';
dws += '<b>Features:</b></p>';
dws += '<ul style="margin-top:0px;padding-top:0;" class="smbtnpopup-list"> ';
dws += '<li  style="margin-top:0px;padding-top:0;">Includes memorial features and photos</li>';
dws += '<li>Fully customizable</li>';
dws += '<li>Hardcover and softcover versions</li>';
dws += '<li>Create within minutes</li>';
dws += '</ul>';
dws += '<p id="errorElm" class="error" style="color:red;display:none;" align="right"><b>We\'re sorry but we are unable to process your request.<br />';
dws += 'Please contact our <a href="http://sharedbook.custhelp.com/" target="_blank">Customer Service department</a>.</b></p>';
dws += '';
dws += '<div class="smbtnpopup-floatr">';
dws += '<p style="text-align:center;">';
dws += '<a href="javascript:sbSmartBtnObj.sharedbookAddArticle()" ><img id="sharedbook_add_content_img" src="' + domain + 'images/createABook.gif" alt="" border="0" style="margin:10px 0 10px 0; padding: 0pt;"></a><br />';
dws += '<span class="smbtnpopup-smaller">Click to get started</span></p> </div>';
dws += '<p class="smbtnpopup-TextRegular">Already started your Book? <a href="http://lmw.sharedbook.com/pilot/enterBook.do?bookId=LegacyMemorialWebsite" target="_blank" class="smbtnpopup-link">Sign In Here</a><br /><br />';
dws += 'Give a gift certificate. <a href="http://sharedbookstore.com/lmw.html" target="_top" class="smbtnpopup-link">Click here</p><br />';
dws += '<div align="left"><a href="javascript:sbSmartBtnObj.popup()"><img src="' + domain + 'images/cancel_box.gif" alt="" border="0"/></a> <a href="javascript:sbSmartBtnObj.popup()"  class="smbtnpopup-link">Close</a> </div>';
dws += '</div>';
dws += '</div>';
dws += '<!--end of SMART BUTTON POPUP DIV-->';
dws += '<!-- SharedBook Smart Button START -->';
dws += '<td colspan="2"><div id="sharedbook_main">';
dws += '   <a href="javascript:sbSmartBtnObj.popup()" style="margin: 0pt; padding: 0pt;" name="&lid=nav_micro_left_sharedbook"><img id="sharedbook_state_img" src="' + domain + 'images/btn_txt_printedgb.gif" alt="" style="margin: 0pt; padding: 0pt;" border="0"></a>';
dws += '</div>';
dws += '<div id="blockingDiv" onclick="return false;" style="display:none; z-index:99999; width:1600px; height:1460px;position:absolute;left:0;top:0px; filter:Alpha(Opacity=70); -moz-opacity:0.7; background-color:#999;" align="center">';
dws += '     <div id="divTopLeft"     style="position:absolute;top:350px;left:300px;">';
dws += '       <!--urls used in the movie-->';
dws += '<!--text used in the movie-->';
dws += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="403" height="250">';
dws += '<param name="allowScriptAccess" value="sameDomain" />';
dws += '<param name="movie" value="' + domain + 'images/onload.swf" />';
dws += '<param name="quality" value="high" />';
dws += '';
dws += '<embed src="' + domain + 'images/onload.swf" quality="high" bgcolor="#ffffff" width="403" height="250"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
dws += '</object>';
dws += '';
dws += '<script type="text/javascript">';
dws += 'var ns = (navigator.appName.indexOf("Netscape") != -1);';
dws += 'var d = document;';
dws += 'function JSFX_FloatDiv(id, sx, sy)';
dws += '{';
dws += '	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];';
dws += '	var px = document.layers ? "" : "px";';
dws += '	window[id + "_obj"] = el;';
dws += '	if(d.layers)el.style=el;';
dws += '	el.cx = el.sx = sx;el.cy = el.sy = sy;';
dws += '	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};';
dws += '';
dws += '	el.floatIt=function()';
dws += '	{';
dws += '		var pX, pY;';
dws += '		pX = (this.sx >= 0) ? 0 : ns ? innerWidth :';
dws += '		document.documentElement && document.documentElement.clientWidth ?';
dws += '		document.documentElement.clientWidth : document.body.clientWidth;';
dws += '		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ?';
dws += '		document.documentElement.scrollTop : document.body.scrollTop;';
dws += '		if(this.sy<0)';
dws += '		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ?';
dws += '		document.documentElement.clientHeight : document.body.clientHeight;';
dws += '		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;';
dws += '		this.sP(this.cx, this.cy);';
dws += '		setTimeout(this.id + "_obj.floatIt()", 40);';
dws += '	};';
dws += '	return el;';
dws += '}';
dws += 'JSFX_FloatDiv("divTopLeft",       150,   100).floatIt();';
dws += '</script>';
dws += '';
dws += '      </div>';
dws += '   </div></td>';
   
dws += '<!-- SharedBook Smart Button END -->';

if (!window.smartBtnGM)
{
   document.write(dws);
}
else
{
   smartBtnGM.buttonContainerElement.innerHTML += dws
}
