﻿var BQ = {
  getY: function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
      iReturnValue += oElement.offsetTop
      oElement = oElement.offsetParent;
    }
    return iReturnValue;
  },
  getX: function(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
      iReturnValue += oElement.offsetLeft
      oElement = oElement.offsetParent;
    }
    return iReturnValue;
  },
  getOffsets: function(element) {
    function getLeft() { return element.offsetLeft + (element.clientLeft ? element.clientLeft : 0) - element.scrollLeft }
    function getTop() { return element.offsetTop + (element.clientTop ? element.clientTop : 0) - element.scrollTop }
    var p = { x: getLeft() || 0, y: getTop() || 0 }
    while (element = element.offsetParent) {
      if (element.nodeName == 'HTML') break
      p.x += getLeft()
      p.y += getTop()
    }
    if (this.isWebkit()) {
      p.x += pageXOffset
      p.y += pageYOffset
    }
    return p
  },
  readCookie: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
  },
  createCookie: function(name, value, days) {
    var expires = ""
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = "; expires=" + date.toGMTString();
    }
    try {
      document.cookie = name + "=" + value + expires + "; path=/";
    } catch (e) { alert('non cookies for u!') }
  },
  eraseCookie: function(name) {
    this.createCookie(name, "", -1);
  },
  cookiesEnabled: function() {
    this.createCookie('testcookie', 'testok', 1)
    var ck = this.readCookie('testcookie')
    this.eraseCookie('testcookie')
    return ck ? true : false
  },
  isIE: function() {
    return window.navigator.appName === 'Microsoft Internet Explorer'
  },
  isOpera: function() {
    window.navigator.userAgent.toLowerCase().indexOf('opera') != -1
  },
  isWebkit: function() {
    return window.navigator.userAgent.toLowerCase().indexOf('applewebkit') != -1
  },
  centerNode: function(node) {
    if (node == null) return
    var scr = BQ.getAvailScreenHW()
    var snode = this.isWebkit() ? document.body : document.documentElement
    node.style.top = (snode.scrollTop + Math.abs((scr.h - node.offsetHeight) / 2)) + 'px'
    node.style.left = (snode.scrollLeft + ((scr.w - node.offsetWidth) / 2)) + 'px'
  },
  getAvailScreenHW: function() {
    return (this.isIE())
      ? { h: document.documentElement.clientHeight, w: document.documentElement.offsetWidth }
      : { h: window.innerHeight, w: document.documentElement.offsetWidth }
  },
  browserIsCompatible: function() {
    var ver = parseFloat(window.navigator.userAgent.substring(window.navigator.userAgent.indexOf('MSIE ') + 5))
    return this.isIE() && ver < 7 ? false : true
  },
  openBrowserDownload: function() {
    var bd = document.createElement('div')
    bd.style.cssText = 'POSITION: absolute;'
    bd.innerHTML = '<DIV id=brdnld style="BACKGROUND: rgb(240,248,255); POSITION:relative; WIDTH: 300px; BORDER-TOP-STYLE: outset; BORDER-RIGHT-STYLE: outset; BORDER-LEFT-STYLE: outset; TEXT-ALIGN: left; BORDER-BOTTOM-STYLE: outset" align=center><DIV style="MARGIN: 5px 5px 8px">You must upgrade your browser in order to view all Beauty Quotient Formula pages.</DIV><DIV style="MARGIN-LEFT: 14px"><DIV style="MARGIN: 5px"><A href="http://www.microsoft.com/windows/internet-explorer/default.aspx"><IMG style="VERTICAL-ALIGN: middle; MARGIN-RIGHT: 10px" alt=IE height=32 width=32 src="images/IE.png">Download Internet Explorer 8</A></DIV><DIV style="MARGIN: 5px"><A href="http://www.google.com/chrome/index.html"><IMG style="VERTICAL-ALIGN: middle; MARGIN-RIGHT: 10px" height=32 width=32 alt=Chrome src="images/Chrome.png">Download Google Chrome</A></DIV><DIV style="MARGIN: 5px"><A href="http://www.apple.com/safari/download/"><IMG style="VERTICAL-ALIGN: middle; MARGIN-RIGHT: 10px" height=32 width=32 alt=Safari src="images/Safari.png">Download Apple Safari 4</A></DIV></DIV><DIV style="MARGIN: 5px 0px" align="center"><INPUT onclick="this.parentNode.parentNode.style.display=' + "'none'" + '" type=button value="Close"></DIV></DIV>'
    document.body.appendChild(bd)
    this.centerNode(bd)
  },
  mouseover_menuitem:   function(menuelem, submenuname) {
    var elem = document.getElementById(submenuname);
    if (!elem) return;
    elem.style.display = '';
    //debugger
    elem.style.top = menuelem.offsetTop + menuelem.offsetHeight + 'px';
    elem.style.left = menuelem.offsetLeft + 'px';
  },
  getAmazonBQLinkHTML: function(cb) {
    function ok(r) {
      var xmlDoc = BC.parseXML(r)
      var amzlnk = document.createElement('div')
      amzlnk.style.cssText = 'position:relative;z-index:2;width:124px;height:76px;border:outset;font-family:verdana;'
        + '-moz-border-radius:6px;-webkit-border-radius:6px;'
      amzlnk.title = xmlDoc.getElementsByTagName('Title')[0].childNodes[0].nodeValue
      var href = xmlDoc.getElementsByTagName('DetailPageURL')[0].childNodes[0].nodeValue
      var fmtprc = xmlDoc.getElementsByTagName('FormattedPrice')
      //      var lstprc = fmtprc[0].childNodes[0].nodeValue
      var lowprc = fmtprc[1].childNodes[0].nodeValue
      amzlnk.innerHTML = '<a href=' + href + '><img src="http://thebeautyquotient.com/images/bqamz.png" /></a><span style="position:absolute; left:0; margin-left:49px; width:74px; top:0; margin-top:51px; font-weight:bold; color:Maroon; text-align:center; display:block;">' + lowprc + '</span>'
      //      amzlnk.innerHTML = '<a href=' + href + '><img src="http://thebeautyquotient.com/images/bqamz.png" /></a><span style="text-decoration:line-through; position:absolute; margin-left:49px; width:74px; top:30px; text-align:center; display:block; color:Navy; font-size:smaller;">' + lstprc + '</span><span style="position:absolute; margin-left:49px; width:74px; top:48px; font-weight:bold; color:Maroon; text-align:center; display:block;">' + lowprc + '</span>'
      return cb(amzlnk)
    }
    function err(e) { //ignore - for now?
    }
    var request = "&Operation=ItemSearch"
          + "&SearchIndex=Books"
          + "&ResponseGroup=Medium"
          + "&Keywords=" + encodeURI("The Beauty Quotient Formula")

    var ws = app.webService.AWS
    ws(request, ok, err)
  },
  getOtherBookSellerLinks: function() {
    var bsl = document.createElement('div')
    var bn = document.createElement('a')
    bn.href = "http://clickserve.cc-dt.com/link/click?lid=41000000030774913"
    bn.innerHTML = 'Barnes & Noble'
    bn.style.cssText = 'display:block;margin-top:5px;'
    bsl.appendChild(bn)
    
    var bam = document.createElement('a')
    bam.href = 'http://www.booksamillion.com/product/9781401924515'
    bam.innerHTML = 'Books-A-Million'
    bam.style.cssText = 'display:block;margin-top:5px;'
    bsl.appendChild(bam)
    bam = document.createElement('a')
    bam.href = 'http://www.borders.com/online/store/TitleDetail?type=0&catalogId=10001&simple=1&defaultSearchView=List&keyword=1401924514&LogData=[search:+6,parse:+8]&searchData={productId:null,sku:null,type:0,sort:null,currPage:1,resultsPerPage:25,simpleSearch:true,navigation:0,moreValue:null,coverView:false,url:rpp%3D25%26view%3D2%26all_search%3D1401924514%26type%3D0%26nav%3D0%26simple%3Dtrue,terms:{all_search%3D1401924514}}&storeId=13551&sku=1401924514&ddkey=http:SearchResults'
    bam.innerHTML = 'Borders'
    bam.style.cssText = 'display:block;margin-top:5px;'
    bsl.appendChild(bam)
    return bsl
  }
}
function _bqmenu() {
  this.node = null
  this.items = [
        { text: 'INCREASE YOUR BQ', link: 'beautyquotient.html' },
        { text: 'BQ FORMULA QUIZ', link: "javascript:BQ.browserIsCompatible() ? window.location.href='beauty_quotient_quiz.aspx' : BQ.openBrowserDownload();" },
        { text: 'AGELESS REVOLUTION', link: 'agerevolution.html' },
        { text: 'CONTACT US', link: "javascript:BQ.browserIsCompatible() ? window.location.href='ContactUs.aspx' : BQ.openBrowserDownload();" }]
        this.buildMenu = function(si) {
          document.oncontextmenu = null
          var mc = document.createElement('div')
          mc.style.cssText = 'text-align:left; background-color:RGB(214,81,172); height:32px; width:100%; overflow:hidden;'
          for (var i = 0; i < this.items.length; i++) {
            if (i === si) mc.appendChild(this.shareItem(si))
            else {
              var itm = document.createElement('div')
              itm.className = 'nav'
              var lnk = document.createElement('a')
              lnk.className = 'menu'
              lnk.innerHTML = this.items[i].text
              lnk.href = this.items[i].link
              itm.appendChild(lnk)
              mc.appendChild(itm)
            }
          }
          this.node = mc
        }
        this.shareItem = function(i) {
          function shareShow(e) {
            var se = e ? e.target ? e.target : e.srcElement : event.srcElement
            var sm = document.getElementById('sharemenu');
            sm.style.display = "inline";
            var os = BQ.getOffsets(se);
            sm.style.top = (os.y + se.parentNode.offsetHeight) + "px";
            sm.style.left = os.x + "px";
          }
          function shareHide(e) {
            var sm = document.getElementById('sharemenu');
            sm.style.display = "none";
          }
          var ih = document.createElement('div')
          ih.onmouseover = shareShow
          ih.onmouseout = shareHide
          ih.className = "nav"
          ih.style.cssText = "cursor:default;" + (BQ.isIE() && i===0 ? 'width:200px; padding-right:0;':'') + "color:white;font-weight:500;font-size:12px;letter-spacing: 1px;"
          ih.innerHTML = "SHARE"
          ih.onclick = function(e) { alert(this.style.width) }
          return ih
        }
  this.buildShareMenu = function() {
    var menu = document.createElement('div')
    menu.id = 'sharemenu'
    menu.onmouseout = function(e) { this.style.display = 'none' }
    menu.onmouseover = function(e) { this.style.display = 'block' }
    menu.innerHTML = '<a href="http://www.facebook.com/widgets/fan.php?id=352103922478&connections=8#"><img src="images/fbfan.png" alt="Become a Fan." title="Become a Facebook Fan"  /></a>'
      + '<a href="http://www.facebook.com/share.php?" onclick="return fbs_click()" target="_blank" ><img src="images/Facebook.png" style="width:32px; margin-top:2px; border-width:0; height:32px; margin-right:2px;" title="Share on Facebook" alt="" /></a>'
      + '<a href="http://twitter.com/home?status=Taking the Beauty-Quotient Quiz (http://thebeautyquotient.com)." title="Sign in with Twitter"><img style="width:32px; height:32px; border-width:0; margin-right:2px;" src="images/Twitter.png" alt="Sign in with Twitter" /></a>'
      + '<a href="http://thebeautyquotient.blogspot.com/" title="Our Blog"><img style="width:32px;height:32px; border-width:0;" src="images/Blogger.png" alt="Blog" /></a></div>'
    menu.align = "center"
    menu.style.cssText = 'display:none; vertical-align:middle; position:absolute; border:solid 2px RGB(214,81,172); background-color:White; margin-left:2px; width:121px; z-index:2;'
    document.body.appendChild(menu)
  }
}

