var d = document;
var popupIFrame;
var popupDiv;
var popupHeader;
var popupFooter;

///* var localPath = location.pathname;
// if (localPath == "/emp/u/member/viewMember.do" && top.frames.length > 0) 
//   { top.location.href='/emp/u/index.vm?success=join';
//} else if (top.frames.length > 0) {
//   top.location=self.location;
//}*/

function startErUp2() {
if (d.getElementById('aBlok_level2')) {
	if (imageURL) d.getElementById('aBlok_level2').style.backgroundImage = 'url(' + imageURL + ')'
 } else {
  setTimeout ("startErUp2()", 50)
 }
}




//Used for pages that cannot communicate with the main page (ie, a survey hosted in San Fran)
//For these, displayFg should be 'false'
//Otherwise, the page needs javascript that tells its parent to display the popinside.
//This was done so that the previous content of the popinside can be cleared
//before displaying the new content.
function showContentBox(displayFg, url) {
   popupIFrame.src = url;
   if (displayFg != "true") {
      popupIFrame.style.visibility = "visible";
   } 
}

//elem is the id of an element that the popinside will be ancored to.
//client is the what will be displayed in the header
//url is the url to the content to be displayed withint the popinside
//hgt is the height of the popinside
//displayFg: if 'true' the content of the popinside will communicate with the parent
//to set its visibility to true.  Otherwise this script will set the visibility.
function showPopup(elem, client, url, hgt, displayFg) {
   var myElem = d.getElementById(elem);
   var myPoint = getPosition(myElem);
  // alert(myPoint.left+ "   "+myPoint.top);
  // alert(url);
  //alert("in popup");
   var offsetHeight = 0;
   if (d.all){
      offsetHeight = d.body.offsetHeight;
   }
   else {
      offsetHeight = innerHeight;
   }
   popupIFrame = d.getElementById('popup');
   popupDiv = d.getElementById('popupbox');
   popupHeader = d.getElementById('popupHeader');
   popupFooter = d.getElementById('popupFooter');

   if (popupIFrame.src != url) {
     
      hideDiv();
      popupIFrame.style.height = parseInt(hgt) + "px";
      popupDiv.style.height = parseInt(hgt) + "px";
      if (d.all) {
         popupIFrame.style.width = 345 + "px";
         popupDiv.style.width = 345 + "px";
      }
      else {
         popupIFrame.style.width = 340 + "px";
         popupDiv.style.width = 345 + "px";
      }
   
  if (document.documentElement.clientWidth > 0) {
   // alert("A");
   // lk();
   if (myPoint.left + 345 > document.documentElement.clientWidth + document.documentElement.scrollLeft) {
   	popupDiv.style.left = document.documentElement.clientWidth + document.documentElement.scrollLeft - 360 + "px";
        popupHeader.style.left = document.documentElement.clientWidth + document.documentElement.scrollLeft - 360 + "px";
        popupFooter.style.left = document.documentElement.clientWidth + document.documentElement.scrollLeft - 360 + "px"; 
   }
   else {
   	 // alert('B');
   	 // lk();
       popupDiv.style.left = myPoint.left + "px";
       popupHeader.style.left = myPoint.left + "px";  	
       popupFooter.style.left = myPoint.left + "px";        
   }
   if (myPoint.top + parseInt(hgt) + 26  > document.documentElement.clientHeight + document.documentElement.scrollTop) {
   // alert('C');
   	 // lk();
        popupHeader.style.top = document.documentElement.clientHeight - 9 - parseInt(hgt) - 26 + document.documentElement.scrollTop + "px";
   	popupDiv.style.top = document.documentElement.clientHeight - 9 - parseInt(hgt) + document.documentElement.scrollTop + "px";
        popupFooter.style.top = document.documentElement.clientHeight - 10 + document.documentElement.scrollTop + "px"; 
   }
   else {
     //alert('D');
   	 // lk();
        popupHeader.style.top  = myPoint.top + "px";
        popupDiv.style.top = (myPoint.top + 26) + "px";	
        popupFooter.style.top  = (myPoint.top + 26 + parseInt(hgt) - 1) + "px";
   }
      showHeader(client);
      popupDiv.style.visibility = "visible";
      popupFooter.style.visibility = "visible";           
      popupFooter.innerHTML = '<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#2e4769"><tr><td>';
      popupFooter.innerHTML += '&nbsp;</td></tr></table>';
      setTimeout("showContentBox('"+displayFg+"','"+ url+"');", 50);
   }
   else {
	// alert('E');
   	 // lk();
      popupDiv.style.top = (myPoint.top + 26) + "px";
      popupHeader.style.top  = myPoint.top + "px";
      popupDiv.style.left = myPoint.left + "px";
      popupHeader.style.left = myPoint.left + "px";
      showHeader(client);
      popupDiv.style.visibility = "visible";
      popupFooter.style.top  = (myPoint.top + 26 + parseInt(hgt) - 1) + "px";
      popupFooter.style.left = myPoint.left + "px"; 
      popupFooter.style.visibility = "visible";           
     // popupFooter.innerHTML = '<table width="100%" border="0" cellpadding="0" cellspacing="0"  bgcolor="#2e4769"><tr bgcolor="#2e4769"><td bgcolor="#2e4769">';
     // popupFooter.innerHTML += ' &nbsp;</td></tr></table>';
      setTimeout("showContentBox('"+displayFg+"','"+ url+"');", 50);
      
   //reposition the popinside if the popinside would appear
   //above or below the current viewing area
   if (myPoint.top - document.body.scrollTop < 0){
      
      document.body.scrollTop = myPoint.top - 20;
   }
   else if (myPoint.top - document.body.scrollTop + parseInt(hgt) + 36 + 16 > offsetHeight){
      document.body.scrollTop = myPoint.top - offsetHeight + parseInt(hgt) + 36 + 16;
   }

   if (myPoint.left - document.body.scrollLeft + 340 > document.body.clientWidth){
     // alert('F');
   	 // lk();
      //document.body.scrollLeft = myPoint.left - document.body.clientWidth + 340 + 40;
   }
   
   var  popright=parseInt(popupDiv.style.left)+parseInt(popupDiv.style.width);
 if(popright>document.body.clientWidth)
 {
 	var mm=popright-document.body.clientWidth+15;
 	var newleft=myPoint.left-mm;
 	popupDiv.style.left =newleft + "px";
    popupHeader.style.left =newleft+ "px";
    popupFooter.style.left = newleft + "px";
    //alert('G');
 }

   }

}
   }
function lk()
{
 popupDiv = d.getElementById('popupbox');
  alert("clientW:"+document.body.clientWidth
  		
  		+ "\nclientHL:"+document.body.clientHeight
  		+ "\nscrollL:"+document.body.scrollLeft
  		+ "\nscrollT:"+document.body.scrollTop
  		+ "\nscrollW:"+document.body.scrollWidth
  		+ "\nscrollH:"+document.body.scrollHeight
  		+"\n popupL:"+popupDiv.style.left
  		+"\n popupT:"+popupDiv.style.top
  		+"\n popupW:"+popupDiv.style.width
  		+"\n popupH:"+popupDiv.style.height
  		+"\n popupR:"+(popupDiv.style.left+popupDiv.style.width)
  		
  		+"\n dd"+(parseInt(popupDiv.style.left)+parseInt(popupDiv.style.width))
  		
  		);
}
//generate the html for the header portion of the popup
function showHeader(client) {
   var headerStr = '<table width="100%" border="0" cellpadding="4" cellspacing="0" bgcolor="#2e4769">';
   headerStr += '<tr><td>&nbsp;</td>';
   headerStr += '<td align="left" colspan="4" class="text" NOWRAP><strong><font color="#FFFFFF">' + client + '</font></strong></td>';
  // headerStr += '<td align="right" class="text" NOWRAP>';
//   headerStr += '<a href="" onclick="javascript:printFrame();return false;" class="headerClose">Print</a></td>';
  // headerStr += '<td width="17" class="headerClose" >';
 //  headerStr += '<a href="" onclick="javascript:printFrame();return false;">';
  // headerStr += '<img src = "imgs/printericon.gif" width="17" height="18" border="0">';
 //  headerStr += '</a></td><td align="center" class="text" width="2"> | </td>';
   headerStr += '<td colspan="2" align="right" valign="middle"   NOWRAP>';
   headerStr += '<a href="javascript:void(0);" onclick="hideDiv();" style=\'TEXT-DECORATION: none;\' >';
   headerStr += '<span style="font-size: 11px;color:#ffffff;" valign="top">关闭<img   src = "imgs/closeicon.gif" width="11" height="11" border="0"></img></span></a>';
  // headerStr += '<td  align="right"  valign="middle">';
   //headerStr += '<a href="javascript:hideDiv();void(0);"  style=\'TEXT-DECORATION: none; \'>';
  // headerStr += '';
   //headerStr += '</a>';
   headerStr+='</td>';
   headerStr += '</tr>';
   headerStr += '</table>';
 
   popupHeader.style.visibility = "visible";
   popupHeader.innerHTML = headerStr;

}

function hideDiv() {
 
   popupIFrame.src = "blank.html";
   popupIFrame.style.visibility = "hidden";
   popupDiv.style. visibility = "hidden";
   popupHeader.style. visibility = "hidden";
   popupFooter.style. visibility = "hidden";
}

//Find the coordinates of an element on the page
function getPosition(elem) {
    var offsetLeft = 0;
    var offsetTop = 0;
   
    while (elem) {
        offsetLeft += elem.offsetLeft;
        offsetTop += elem.offsetTop;
        elem = elem.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof d.body.leftMargin != "undefined") {
        offsetLeft += d.body.leftMargin;
        offsetTop += d.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function printFrame () {
   if (d.all) {
      d.iframepopup.focus();
      d.iframepopup.print(); 
   }
   else {
      window.frames['popup'].focus();
      window.print(); 
   }
}

var timerID1;
var timerID2;
var timerID3;
var openFlg = "N";
var leftBttn;
var rightBttn;
var dir = 1;
var min = 20;
var max = 205;
var current = min;

function StartTheCounter() {
timerID1 = setTimeout('reveal()',1000 * CounterStart);
}

function revealPush(counterNum) {//openFlg is the switch: if opened close, if closed then open
timerID2 = setTimeout("revealPush2("+counterNum+")",4500);
 }

function slide() {
  var rate = 8;
  var slideAmt = 3;
  current = current + (slideAmt * dir);
  if (current > max) {
    d.getElementById("messWrap").style.height = max + "px";
    current = max;
    openFlg = "Y";
  }
  else if (current < min) {
    d.getElementById("messWrap").style.height = min + "px";
    current = min;
    d.getElementById("messWrap").style.height = current + "px";
    elemHideShow();
    openFlg = "N";
  }
  else {
    d.getElementById("messWrap").style.height = current + "px";  
    timerID3 = setTimeout("slide();",rate);	
  }
}

function revealPush2(counterNum) {
  dir = (openFlg == "N") ? "1" : "-1"
  if (openFlg == "N") {
    elemHideShow();
  }
  slide();
  CounterStart = counterNum;
  StartTheCounter(CounterStart); 
}

function reveal() {//openFlg is the switch: if opened close, if closed then open
  dir = (openFlg == "N") ? "1" : "-1"
  if (openFlg == "N") {
    elemHideShow();
  }
  slide();
 //Clear both timers so that the I_block won't open or close automatically
 clearTimeout(timerID1);
 clearTimeout(timerID2);
}

function elemHideShow() {
  if (document.getElementById("earnSearch")) {
    document.getElementById("earnSearch").style.visibility = (openFlg == "N") ? "hidden" : "visible";
  }
 
 // if (document.getElementById("spendForm").in_select_by_category) {
  //  document.getElementById("spendForm").in_select_by_category.style.visibility = (openFlg == "N") ? "hidden" : "visible"
 // }
 // d.getElementById("earnForm").in_select_by_store.style.visibility = (openFlg == "N") ? "hidden" : "visible"
//d.getElementById("earnForm").in_select_by_category.style.visibility = (openFlg == "N") ? "hidden" : "visible"
  // d.getElementById("spendForm").in_select_by_category.style.visibility = (openFlg == "N") ? "hidden" : "visible"
 // d.getElementById("spendForm").in_select_by_merchant.style.visibility = (openFlg == "N") ? "hidden" : "visible"
 // leftBttn = d.getElementById("messageLeft");
 // rightBttn = d.getElementById("messageRight");
//  leftBttn.firstChild.style.backgroundPosition = (openFlg == "N") ? 0+"% "+(-24)+"px" : 0+"% "+(0)+"px"
//  rightBttn.firstChild.style.backgroundPosition = (openFlg == "N") ? 0+"% "+(-24)+"px" : 0+"% "+(0)+"px"
  d.getElementById("content").onclick = (openFlg == "N") ? null : reveal
}

function searchSwap(which) { //displays earn version or the redeem version
 d.getElementById("earnSearch").style.display = (which =="redeem") ? "none" : "inline"
 d.getElementById("spendSearch").style.display = (which =="redeem") ?"inline" : "none"
d.getElementById('earnSearchTab').style.backgroundPosition = (which =="redeem") ? "-76px 0px" : "0px 0px"
d.getElementById('spendSearchTab').style.backgroundPosition = (which =="redeem") ? "-89px 0px" : "0px 0px"
}
function flipRedeem() {
if (document.getElementById('spendSearch')) {
     searchSwap("redeem")
 } else {
  setTimeout ("flipRedeem()", 50)
 }
}

