var saveTopicFormCode = '<div align="center">'
+'  <form action="signup.php" id="save_topic_form" name="save_topic_form" method="post">'
+'    <input type="hidden" name="form_action" id="form_action" value="save_topic">'
+'    <input type="hidden" name="subscriber_id" id="subscriber_id" value="">'
+'  </form>'
+'</div>';

var lightboxCode = '<div align="center" style="width: 500px">'
+'  <form action="signup.php" id="subscription_form" name="subscription_form" method="post">'
+'    <input type="hidden" name="form_action" id="form_action" value="subscribe">'
+'    <input type="hidden" name="domain" id="form_action" value="dummiesbooks.net">'
+'    <input type="hidden" name="landing_referrer" id="landing_referrer" value="">'
+'    <input type="hidden" name="page_title" id="page_title" value="">'
+'    <table cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">'
+'      <tr>'
+'        <td colspan=2>'
+'	      <div align="center" style="width: 450px;">'
+'	        <!--img src="" border="0"-->'
+'		    <div style="font-size: 19px; color: #292929; margin-top: 25px; margin-bottom: 25px;">'
+'		      <span id="page_topic"></span>'
+'            </div>'
+'          </div>'
+'        </td>'
+'      </tr>'
+'      <tr>'
+'        <td align="right"><span style="color: #292929;">Name:</span></td>'
+'	    <td align="left"><input type="text" name="name" value="" style="width:200px; border:1px solid black;"></td>'
+'      </tr>'
+'      <tr>'
+'        <td align="right"><span style="color: #292929;">Email:</span></td>'
+'        <td align="left"><input type="text" name="email" id="email_field" value="" style="width:200px; border:1px solid black;"></td>'
+'      </tr>'
+'      <tr>'
+'        <td align="center" colspan="2">'
+'          <div style="margin-top: 15px;">'
+'            <input class="button" type="button" name="subscription_button" id="subscription_button" onclick="subscribeUser()" value=""></td>'
+'          </div>'
+'      </tr>'
+'      <tr>'
+'        <td colspan="2">'
+'	      <div align="center" style="color: #292929; font-size: 11px; margin-top: 5px; margin-bottom: 15px;">'
+'	        We respect your privacy. Your information will not be shared <br>with any third party and you can unsubscribe anytime.'
+'	      </div>'
+'	    </td>'
+'      </tr>'
+'      <tr>'
+'        <td colspan="2">'
+'	      <div align="right">'
+'	        <a href="javascript:hideLightboxRequest()" style="color: #292929; font-size: 12px; margin-top: 5px; margin-bottom: 5px;">close</a>'
+'	      </div>'
+'	    </td>'
+'      </tr>'
+'    </table>'
+'  </form>'
+'</div>';

var lightboxCodeThankYou = '<div align="center" style="width: 500px">'
+'    <table cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">'
+'      <tr>'
+'        <td colspan=2>'
+'	      <div align="center" style="width: 450px;">'
+'	        <!--img src="" border="0"-->'
+'		    <div style="font-size: 19px; color: #292929; margin-top: 25px; margin-bottom: 25px;">'
+'		      Thank your for joining'
+'		    </div>'
+'	      </div>'
+'        </td>'
+'      </tr>'
+'      <tr>'
+'        <td align="center" colspan="2">'
+'          <div style="margin-top: 2px;">'
+'            <input class="button" type="button" name="confirmation_button" id="confirmation_button" onclick="hideLightboxRequest()" value="Close"></td>'
+'          </div>'
+'      </tr>'
+'    </table>'
+'</div>';

var subscriberID = 0;
var titleTag = '';

// Initialize function
function initializeLightbox() {
  var titleTagObj = document.getElementsByTagName('title') [0];
  titleTag = titleTagObj.innerHTML;
  titleTag = titleTag.replace(' - Find All The Books For Dummies', '');
  //titleTag = titleTag.replace('For Dummies', '');
  titleTag = titleTag.replace('Dummies Books - ', '');
  if(titleTag.search(/For Dummies/i) > 0) {
  	var titleTagArr = titleTag.split("For Dummies");
	titleTag = titleTagArr[0];
  }
  titleTag = 'Sign Up For FREE Information & Deals About<br>dummies Books<br>And Related Topics.';
  
  // include jquery
  includeJquery();
 
  // Set the style sheet
  setStyleSheet();
  // Add the backgroud div
  addBgrDiv();
  // Set up the lightbox div
  setUpLightboxDiv();
  // hide the baground div and lightbox div
  document.getElementsByName("darkBackgroundLayer")[0].style.display = "none";
  document.getElementById('lightbox_div').style.display = "none";
  // cookies
  //checkCookie();
  showLightboxRequest();
}

function showLightboxRequest() {
  var returnVisit = getCookie('return_visit');
  subscriberID = getCookie('subscriber_id');
  if(returnVisit != 1) {
	  var topicName = titleTag;
	  document.getElementById('page_topic').innerHTML = topicName ;
	  //document.getElementById('subscription_button').value = "Click Here For Free Information On " + topicName;
	  document.getElementById('subscription_button').value = "Click Here For Free Info";
	  document.getElementsByName('darkBackgroundLayer')[0].style.display='';
	  document.getElementById('lightbox_div').style.display='';
	  // Place the lightbox at the center
	  var lightBoxObj = document.getElementById('lightbox_div');
	  lightBoxObj.style.position = "fixed";
	  lightBoxObj.style.top = '150px'; //document.body.clientHeight + 700;
	  lightBoxObj.style.left = (document.body.clientWidth/2)-250 + 'px'; //document.body.clientWidth / 2;
	  setCookie('return_visit',1,365);
  } else if(subscriberID != null) {
	  savePageTopic();
  }
}

function hideLightboxRequest() {
  document.getElementsByName('darkBackgroundLayer')[0].style.display='none';
  document.getElementById('lightbox_div').style.display='none';
}

function includeJquery() {
  var headID = document.getElementsByTagName("head")[0];         
  var node = document.createElement('script');
  node.type  = 'text/javascript';
  node.src  = '/js/jquery-1.4.4.min.js';
  headID.appendChild(node);
}

function setStyleSheet() {
  var headID = document.getElementsByTagName("head")[0];         
  var cssNode = document.createElement('link');
  cssNode.type  = 'text/css';
  cssNode.rel   = 'stylesheet';
  cssNode.href  = '/js/lightbox.css';
  cssNode.media = 'screen';
  headID.appendChild(cssNode);
}

function addBgrDiv() {
 var newdiv = document.createElement('div');
 
 if(newdiv.hasAttribute) {
    // Firefox - set attributes
      newdiv.setAttribute('id', 'darkBackgroundLayer');
      newdiv.setAttribute('name', 'darkBackgroundLayer');
      newdiv.setAttribute('class', 'darkenBackground');
  } else {
    // IE - set attributes
      newdiv.id    = 'darkBackgroundLayer';
      newdiv.name  = 'darkBackgroundLayer';
      newdiv.className = 'darkenBackground';
  }
  var body = document.getElementsByTagName('body') [0];
  body.appendChild(newdiv);
}

function setUpLightboxDiv() {
  var lightboxDiv = document.createElement('div');
  if(lightboxDiv.hasAttribute) {
    // Firefox - set attributes
      lightboxDiv.setAttribute('id', 'lightbox_div');
      lightboxDiv.setAttribute('name', 'lightbox_div');
      lightboxDiv.setAttribute('class', 'lightboxClass');
  } else {
    // IE - set attributes
      lightboxDiv.id    = 'lightbox_div';
      lightboxDiv.name  = 'lightbox_div';
      lightboxDiv.className = 'lightboxClass';
  }
  lightboxDiv.innerHTML = lightboxCode;
  var body = document.getElementsByTagName('body') [0];
  body.appendChild(lightboxDiv);
}

function createElement(type, parentElement, attributes) {
  var el = document.createElement(type);
  for (var attribute in attributes) {
    if(el.hasAttribute) {
      // Firefox - set attributes
      el.setAttribute(attribute, attributes[attribute]);
    } else {
    alert(attribute);
      eval(el.attribute) = attributes[attribute];
    }
  }
  // Append the element
  var parent = document.getElementsByTagName(parentElement) [0];
  parent.appendChild(el);
  return el;
}

// C.O.O.K.I.E.
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function checkCookie() {
  var myusage = getCookie('myusage');
  if (myusage != null && myusage != "") {
    meterCounter = myusage;
  } else {
    myusage = 1;
    if (myusage != null && myusage != "") {
      setCookie('myusage', myusage, 30);
    }
  }
}

// J.Q.U.E.R.Y.  A.J.A.X.
function validInput() {
  return true;
}

function subscribeUser() {
  if(validInput()) {
 	 document.getElementById('landing_referrer').value = document.referrer;
    // Serialize the form
	var subscriptionData = $("#subscription_form").serialize();
	//alert(subscriptionData);
	var emailAddress = document.getElementById('email_field').value;
	$.post(
	  "http://www.zubk.com/newsletter/subscribe.php", 
	  subscriptionData,
      function(data){
		var dataArray = data.split("|"); 
		//alert(dataArray[1]);
		setCookie('subscriber_id',dataArray[1],365);
		document.getElementById('lightbox_div').innerHTML = lightboxCodeThankYou;
		//document.getElementById('subscriber_email').innerHTML = emailAddress;
      }
    );
  }
}

function setSaveTopicFormCodeDiv() {
  var saveTopicFormCodeDiv = document.createElement('div');
  if(saveTopicFormCodeDiv.hasAttribute) {
    // Firefox - set attributes
      saveTopicFormCodeDiv.setAttribute('id', 'save_topic_div');
      saveTopicFormCodeDiv.setAttribute('name', 'save_topic_div');
  } else {
    // IE - set attributes
      saveTopicFormCodeDiv.id    = 'save_topic_div';
      saveTopicFormCodeDiv.name  = 'save_topic_div';
  }
  saveTopicFormCodeDiv.innerHTML = saveTopicFormCode;
  var body = document.getElementsByTagName('body') [0];
  body.appendChild(saveTopicFormCodeDiv);
  document.getElementById('save_topic_div').style.display = '';
}

function savePageTopic() {
	setSaveTopicFormCodeDiv();
	document.getElementById('subscriber_id').value = subscriberID;
	var topicData = $("#save_topic_form").serialize();
    // Serialize the form
	$.post(
	  "/newsletter/subscribe.php", 
	  topicData,
      function(data){
        //alert(data);
      }
    );
}

var tmpcookie = new Date();
var cookieOn = true;
chkcookie = (tmpcookie.getTime() + '');
document.cookie = "chkcookie=" + chkcookie + "; path=/";
if (document.cookie.indexOf(chkcookie,0) < 0) {
  cookieOn = false;
}

if(cookieOn) {
  setTimeout ( initializeLightbox, 5000 );
}


