var timer;
var divheight=0;
var timer1;
var comment_coupon_id;
var expieredflag=0;
var expieredheight=0;
var topflag=0;
var topheight=0;
function findX(pos)
{
  var left = 0;
  if (pos.offsetParent)
  {
     while (pos.offsetParent)
     {
      left += pos.offsetLeft
      pos = pos.offsetParent;
     }
  }
  else if (pos.x)
      left += pos.x;
  return left;
}

function findY(pos)
{
  var top = 0;
  if (pos.offsetParent)
  {
      while (pos.offsetParent)
      {
          top += pos.offsetTop
          pos = pos.offsetParent;
      }
  }
  else if (pos.y)
      top += pos.y;
  return top;
}
function show_share(share,share_holder)
{
  var posx = findX(document.getElementById(share));
  var posy = findY(document.getElementById(share));
  var colortab = document.getElementById(share_holder);
  colortab.style.display = "block";
  colortab.style.position = "absolute";
  posy=posy+10;
  colortab.style.left = posx+"px";
  colortab.style.top = posy+"px";
}

function del_share(share_holder)
{
   clear_share(share_holder);
}

function clear_share(share_holder)
{
  var tab_id = document.getElementById(share_holder);
  tab_id.style.display = "none";
}
// Function to Show Comments
function showComments(coupon_id,commentdiv,inner_commentdiv,commenttext,comment_holder)
{
  divheight=0;
  var divtext=document.getElementById(commenttext).innerHTML;

  var tbl;
  tbl  = '<div id="'+commentdiv+'" style="background-color:#E4F3FF;visibility:hidden;height:0px;width:0px;overflow:hidden">';
  tbl += '<table border="0"  width="271" cellpadding="5">';
  tbl += '<tr><td align="right" class="comment-link" valign="top" height="15"><span style="cursor:pointer" onclick="hideComments(\''+commentdiv+'\',\''+inner_commentdiv+'\',\'256\')" onmouseover=\'this.className="comment-link-hover"\' onmouseout=\'this.className="comment-link"\'>Close comments</span>&nbsp;&nbsp;</td></tr>';
  tbl += '<tr><td align="center"><div class="comment-class"  id="'+inner_commentdiv+'">';
  tbl += '<table border="0" bgcolor="#FFFFFF" cellspacing="0" cellpadding="5" width="90%">';

  divtext=divtext.split('||&lt;&gt;||');
  if(divtext.length>1)
  {
    for(var i=0;i<divtext.length-1;i++)
    {
        txt=divtext[i].split('|&lt;&gt;|');
        tbl += '<tr><td class="comment-line-height" align="left">';
        tbl +='<span class="comment-text">'+txt[0]+'</span><br />';
        tbl +='<img src="/images/comments.gif">&nbsp;&nbsp;<span class="comment-text">'+txt[1]+'</span></td></tr>';
    }
  }
  else
  {
       tbl += '<tr><td class="comment-line-height" align="left"><span class="comment-text">Be the 1st to leave a comment!</span></td></tr>';
  }
  tbl += '</table>';
  tbl += '</div>';
  tbl += '</td>';
  tbl += '</tr>';
  tbl += '<tr><td align="right" class="comment-link" valign="bottom"><span style="cursor:pointer" onclick="return show_add_comment(\''+coupon_id+'\')" onmouseover=\'this.className="comment-link-hover"\' onmouseout=\'this.className="comment-link"\'>Add comment</span>&nbsp;&nbsp;</td></tr>';
  tbl += '</table>';
  tbl += '</div>';
  document.getElementById(comment_holder).innerHTML=tbl;

  document.getElementById(commentdiv).style.width="292px";
  document.getElementById(commentdiv).style.visibility="visible";
  document.getElementById(inner_commentdiv).style.visibility="visible";
  document.getElementById(inner_commentdiv).style.width="285px";
  setHeightWidth(commentdiv,inner_commentdiv);
}
function setHeightWidth(commentdiv,inner_commentdiv)
{
  divheight=divheight+15;
  if(divheight<256)
  {
    if(divheight<191)
    {
        document.getElementById(inner_commentdiv).style.height=divheight+"px";
    }
    document.getElementById(commentdiv).style.height=divheight+"px";
    timer1 = setTimeout('setHeightWidth("'+commentdiv+'","'+inner_commentdiv+'")',10);
  }
}
function hideComments(commentdiv,inner_comment,divheight)
{
  document.getElementById(commentdiv).style.overflow="hidden";

  divheight=divheight-15;
  if(divheight>=0)
  {
      if(divheight<200)
      {
       document.getElementById(inner_comment).style.height=divheight+"px";
      }
      document.getElementById(commentdiv).style.height=divheight+"px";
      timer1 = window.setTimeout('hideComments("'+commentdiv+'","'+inner_comment+'","'+divheight+'")',10);
  }
  else
  {
      document.getElementById(commentdiv).style.height="0px";
      document.getElementById(commentdiv).style.width="0px";
      document.getElementById(commentdiv).style.visibility="hidden";
      document.getElementById(inner_comment).style.height="0px";
      document.getElementById(inner_comment).style.width="0px";
      document.getElementById(inner_comment).style.visibility="hidden";
  }

}
// Function to display Add Comment Box
function show_add_comment(id)
{
   comment_coupon_id=id;

   if(typeof(window.innerWidth)=="number")
   {
    //Non-IE
    commentWidth=window.innerWidth;
    commentHeight=window.innerHeight;
  }
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    //IE 6+ in 'standards compliant mode'
    commentWidth=document.documentElement.clientWidth;
    commentHeight=document.documentElement.clientHeight;
  }
  else if(document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    //IE 4 compatible
    commentWidth=document.body.clientWidth;
    commentHeight=document.body.clientHeight;
  }

  var element=document.getElementById("comment_background");
  var level=0.7;
  element.style.opacity = level;
  element.style.MozOpacity = level;
  element.style.KhtmlOpacity = level;
  element.style.filter = "alpha(opacity=" + (level * 100) + ");";
  element.style.position="fixed";
  element.style.visibility="visible";

  commentWidth=parseInt(commentWidth/2);
  commentHeight=parseInt(commentHeight/2);
  commentWidth=commentWidth-180;
  commentHeight=commentHeight-140;
  document.getElementById("comment").style.position="fixed";
  document.getElementById("comment").style.top=commentHeight+"px";
  document.getElementById("comment").style.left=commentWidth+"px";
  document.getElementById("comment").style.background="#ffffff";
  document.getElementById("comment").style.visibility="visible";
}
function cancel_add_comment()
{
  comment_coupon_id="";
  var element=document.getElementById("comment_background");
  var level=0;
  element.style.opacity = level;
  element.style.MozOpacity = level;
  element.style.KhtmlOpacity = level;
  element.style.filter = "alpha(opacity=" + (level * 100) + ");";
  element.style.position="absolute";
  element.style.visibility="hidden";

  document.getElementById("comment").style.position="absolute";
  document.getElementById("comment").style.visibility="hidden";
}
// Function to Display more Coupons
function display_more_coupons(id)
{
  var coupon_display=document.getElementById('coupon_display'+id).style;
  var view_more=document.getElementById('view_more'+id).style;
  coupon_display.visibility='visible';
  coupon_display.height='100%';
  view_more.visibility='hidden';
  view_more.height="0px";
}

// Function to display Expiered Coupons
function display_expiered_coupons(merchant)
{
  if(expieredflag==0)
  {
      document.getElementById("expired_coupons").style.display="block";
      expieredflag=1;
      document.getElementById("spanexpierdcoupons").innerHTML='&nbsp;&nbsp;<img src="/images/minus.png" border="0" align="absbottom" style="cursor:pointer" onclick="hide_expiered_coupons(\''+merchant.replace("'","\\'")+'\')">&nbsp;&nbsp;<span class="ari-16-gray-link" style="cursor:pointer" onclick="hide_expiered_coupons(\''+merchant.replace("'","\\'")+'\')">Recently Expired&nbsp;'+ merchant.replace("\\'","'") +' Coupons</span>';
      increaseexpieredheight();
  }

}
function increaseexpieredheight()
{
 expieredheight=expieredheight+10;
 document.getElementById("expired_coupons").style.height=expieredheight+"%";
 if(expieredheight<500)
 {
   increaseexpieredheight();
 }
}
// Function to hide Expiered Coupons
function hide_expiered_coupons(merchant)
{
  if(expieredflag==1)
  {
      expieredflag=0;
      document.getElementById("expired_coupons").style.display="none";
      document.getElementById("expired_coupons").style.height="0px";
      document.getElementById("spanexpierdcoupons").innerHTML='&nbsp;&nbsp;<img src="/images/plus.png" border="0" align="absbottom" style="cursor:pointer" onclick="display_expiered_coupons(\''+merchant.replace("'","\\'")+'\')">&nbsp;&nbsp;<span class="ari-16-gray-link" style="cursor:pointer" onclick="display_expiered_coupons(\''+merchant.replace("'","\\'")+'\')">Recently Expired&nbsp;'+ merchant.replace("\\'","'") +' Coupons</span>';
  }
}
// Function to display Top Coupons
function display_top_coupons(merchant)
{
  if(topflag==0)
  {
      document.getElementById("top_coupons").style.visibility="visible";
      topflag=1;
      document.getElementById("spantopcoupons").innerHTML='&nbsp;&nbsp;<img src="/images/minus.png" border="0" align="absbottom" style="cursor:pointer" onclick="hide_top_coupons(\''+merchant.replace("'","\\'")+'\')">&nbsp;&nbsp;<span class="ari-16-gray-link" style="cursor:pointer" onclick="hide_top_coupons(\''+merchant.replace("'","\\'")+'\')">Top&nbsp;'+ merchant.replace("\\'","'") +' Coupons</span>';
      increasetopheight();
  }

}
function increasetopheight()
{
 topheight=topheight+10;
 document.getElementById("top_coupons").style.height=topheight+"%";
 if(topheight<500)
 {
   increasetopheight();
 }
}
// Function to hide top Coupons
function hide_top_coupons(merchant)
{
  if(topflag==1)
  {
      topflag=0;
      document.getElementById("top_coupons").style.visibility="hidden";
      document.getElementById("top_coupons").style.height="0px";
      document.getElementById("spantopcoupons").innerHTML='&nbsp;&nbsp;<img src="/images/plus.png" border="0" align="absbottom" style="cursor:pointer" onclick="display_top_coupons(\''+merchant.replace("'","\\'")+'\')">&nbsp;&nbsp;<span class="ari-16-gray-link" style="cursor:pointer" onclick="display_top_coupons(\''+merchant.replace("'","\\'")+'\')">Top&nbsp;'+ merchant.replace("\\'","'") +' Coupons</span>';
  }
}

// Function to show label
function showlable(obj)
{
  var posx = findX(document.getElementById(obj));
  var posy = findY(document.getElementById(obj));
  posy=posy+20;
  posx=posx+15;
  document.getElementById("coupon_code").style.left=posx+"px";
  document.getElementById("coupon_code").style.top=posy+"px";
  document.getElementById("coupon_code").style.visibility="visible";
}
function hidelable(obj)
{
  document.getElementById("coupon_code").style.left="1px";
  document.getElementById("coupon_code").style.top="1px";
  document.getElementById("coupon_code").style.visibility="hidden";
}
// Function to Copy Coupon code
function copy(text) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text);
  }
}


// New Share Code Applied on Merchant Page
var addthisurltext;
function myCustomCallback(SharedObject)
{
         /*
         * The sharelet is passed to the myCustomCallback callback routine.
         * We will aler the title and url
         */
         SharedObject.properties.title="Deal Alert";
         SharedObject.properties.url="http://www.dealalert.com";
         SharedObject.properties.summary=addthisurltext;

         /*
         * Return true to show the widget
         */
         return true;
}
function show_share1(share,twitterhref,facebookhref,addthis_text)
{
  var posx = findX(document.getElementById(share));
  var posy = findY(document.getElementById(share));
  var sharetab = document.getElementById("share_text_holder");
  sharetab.style.display = "block";
  sharetab.style.position = "absolute";
  posy=posy+10;
  sharetab.style.left = posx+"px";
  sharetab.style.top = posy+"px";
  document.getElementById("twitterlink").href=twitterhref;
  document.getElementById("facebooklink").href=facebookhref;
  addthisurltext=encode_utf8(addthis_text);
}
function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function hide_share()
{
 document.getElementById("share_text_holder").style.display = "none";
}
