var IS_IE /*@cc_on = true @*/
var IS_IE6 /*@cc_on @if (@_jscript_version <= 5.6) = true @end @*/
var DOM_ENABLED = !!(document.getElementById && document.getElementsByTagName)

// JS Mark
function markJS() {
  if (DOM_ENABLED) {
    appendClass(document.body, 'js')
    stripClass(document.body, 'no-js')
  }
}

// TopMenu
var TOP_MENU_TIMER
var TOP_MENU_INITED
function initTopMenu(containerId) {
  if (!DOM_ENABLED) return
  if (!this.DATASET) return
  preloadImage('images/drop_subcategories_bg.gif')
  preloadImage('images/drop_subcategories_shadow_right.png')
  preloadImage('images/drop_subcategories_shadow_bottom.png')
  preloadImage('images/drop_subcategories_shadow_corner.png')
  preloadImage('images/drop_subcategories_bullet.gif')
  if (IS_IE6) {
    document.write(
      '<style type="text/css">' +
      '  #drop-subcategories .shadow-right {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_right.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-subcategories .shadow-bottom {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_bottom.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-subcategories .shadow-corner {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_corner.png",sizingMethod="crop");' +
      '  }' +
      '</style>'
    )
  }
  var items = getElementsByClassName($(containerId), 'td', 'item')
  forEach(items, function(item) {
    item.prc_id = item.id.match(/^tmi-(\d+)/) ? RegExp.$1 : 0
    item.onmouseover = function() {
      if (TOP_MENU_TIMER) clearTimeout(TOP_MENU_TIMER)
      var pos = getElementPosition(this)
      var dropdown = $('drop-subcategories')
      var id = this.prc_id
      var html = '<table cellspacing="0" cellpadding="0"><tr><td class="body"><table cellspacing="0" cellpadding="0">'
      var first = true
      forEach(DATASET.SUBCATEGORIES, function(category) {
        if (category.prc_prc_id == id) {
          if (!first) {
            html += '<tr class="sep"><td></td></tr>'
          }
          if (category.prc_page == CURRENT_PAGE) {
            html += '<tr><td class="item current" nowrap><div>' + encodeHTML(category.prc_title) + '</div></td></tr>'
          } else {
            html += '<tr><td class="item" nowrap><a href="' + category.prc_page + '">' + encodeHTML(category.prc_title) + '</a></td></tr>'
          }
          first = false
        }
      })
      html += '</table></td><td class="shadow-right"></td></tr><tr><td class="shadow-bottom"></td><td class="shadow-corner"></td></tr></table>'
      if (first) {
        dropdown.innerHTML = ''
        dropdown.style.left = '-500px'
        dropdown.style.top = '-500px'
      } else {
        dropdown.innerHTML = html
        dropdown.style.left = pos.x + 1 + 'px'
        dropdown.style.top = pos.y + 27 + 'px'
      }
    }
    item.onmouseout = function() {
      topMenuScheduleClose()
    }
  })
  $('drop-subcategories').onmouseover = function() {
    if (TOP_MENU_TIMER) clearTimeout(TOP_MENU_TIMER)
  }
  $('drop-subcategories').onmouseout = function() {
    topMenuScheduleClose()
  }
  TOP_MENU_INITED = true
}

function topMenuScheduleClose() {
  TOP_MENU_TIMER = setTimeout(function() {
    var dropdown = $('drop-subcategories')
    dropdown.innerHTML = ''
    dropdown.style.left = '-500px'
    dropdown.style.top = '-500px'
  }, 120)
}


// TopMenuNewVertical of Categories (pmer)
var TOP_MENU_TIMER
var TOP_MENU_INITED
function initTopMenuNew(containerId) {
  if (!DOM_ENABLED) return
  if (!this.DATASET) return
  preloadImage('images/drop_subcategories_bg.gif')
  preloadImage('images/drop_subcategories_shadow_right.png')
  preloadImage('images/drop_subcategories_shadow_bottom.png')
  preloadImage('images/drop_subcategories_shadow_corner.png')
  preloadImage('images/drop_subcategories_bullet.gif')
  if (IS_IE6) {
    document.write(
      '<style type="text/css">' +
      '  #drop-subcategories .shadow-right {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_right.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-subcategories .shadow-bottom {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_bottom.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-subcategories .shadow-corner {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_corner.png",sizingMethod="crop");' +
      '  }' +
      '</style>'
    )
  }
  var items = getElementsByClassName($(containerId), 'td', 'item')
  forEach(items, function(item) {
    item.prc_id = item.id.match(/^tmi-(\d+)/) ? RegExp.$1 : 0
    item.onmouseover = function() {
      if (TOP_MENU_TIMER) clearTimeout(TOP_MENU_TIMER)
      var pos = getElementPosition(this)
      var dropdown = $('drop-subcategories')
      var id = this.prc_id
      var html = '<table cellspacing="0" border="0" cellpadding="0"><tr><td class="body"><table cellspacing="0" cellpadding="0" border="0">'
      var first = true
      forEach(DATASET.SUBCATEGORIES, function(category) {
        if (category.prc_prc_id == id) {
          if (!first) {
            html += '<tr class="sep"><td></td></tr>'
          }
          if (category.prc_page == CURRENT_PAGE) {
            html += '<tr><td class="item current" nowrap><div>' + encodeHTML(category.prc_title) + '</div></td></tr>'
          } else {
            html += '<tr><td class="item" nowrap><a href="' + category.prc_page + '">' + encodeHTML(category.prc_title) + '</a></td></tr>'
          }
          first = false
        }
      })
      html += '</table></td><td class="shadow-right"></td></tr><tr><td class="shadow-bottom"></td><td class="shadow-corner"></td></tr></table>'
      if (first) {
        dropdown.innerHTML = ''
        dropdown.style.left = '-500px'
        dropdown.style.top = '500px'
      } else {
        dropdown.innerHTML = html
        dropdown.style.left = pos.x + 130 + 'px'
        dropdown.style.top = pos.y - 5 + 'px'
      }
    }
    item.onmouseout = function() {
      topMenuScheduleClose()
    }
  })
  $('drop-subcategories').onmouseover = function() {
    if (TOP_MENU_TIMER) clearTimeout(TOP_MENU_TIMER)
  }
  $('drop-subcategories').onmouseout = function() {
    topMenuScheduleCloseNew()
  }
  TOP_MENU_INITED = true
}

function topMenuScheduleCloseNew() {
  TOP_MENU_TIMER = setTimeout(function() {
    var dropdown = $('drop-subcategories')
    dropdown.innerHTML = ''
    dropdown.style.left = '-500px'
    dropdown.style.top = '-500px'
  }, 120)
}


// Specials
var SPECIALS_TIMER
function specialsInit(containerId, disableDropdown) {
  if (!DOM_ENABLED) return
  if (!this.DATASET) return
//  rolloverLinksInit(containerId)
  preloadImage('images/drop_subcategories_bg.gif')
  preloadImage('images/drop_subcategories_shadow_right.png')
  preloadImage('images/drop_subcategories_shadow_bottom.png')
  preloadImage('images/drop_subcategories_shadow_corner.png')
  preloadImage('images/drop_subcategories_bullet.gif')
  if (IS_IE6) {
    document.write(
      '<style type="text/css">' +
      '  #drop-specials .shadow-right {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_right.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-specials .shadow-bottom {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_bottom.png",sizingMethod="crop");' +
      '  }' +
      '  #drop-specials .shadow-corner {' +
      '    background-image:none;' +
      '    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/drop_subcategories_shadow_corner.png",sizingMethod="crop");' +
      '  }' +
      '</style>'
    )
  }
  rolloverLinksInit(containerId, function(link) {
    if (disableDropdown) return
    if (SPECIALS_TIMER) clearTimeout(SPECIALS_TIMER)
    var pos = getElementPosition(link)
    var dropdown = $('drop-specials')
    var html = '<table cellspacing="0" cellpadding="0"><tr><td class="body"><table cellspacing="0" cellpadding="0">'
    var first = true
    forEach(DATASET.TOP_CATEGORIES, function(category) {
      if (link.href.match(/best-sellers/) && category.prc_id == 95) {
        // Αφαίρεση κατηγορίας "Άλλες Κατηγορίες" από Best Sellers (Request 14/12/2007)
        return
      }
      if (!first) {
        html += '<tr class="sep"><td></td></tr>'
      }
      if (category.prc_page == CURRENT_PAGE) {
        html += '<tr><td class="item current" nowrap><div>' + encodeHTML(category.prc_title) + '</div></td></tr>'
      } else {
        html += '<tr><td class="item" nowrap><a href="' + link.href + '?id=' + category.prc_id + '">' + encodeHTML(category.prc_title) + '</a></td></tr>'
      }
      first = false
    })
    html += '</table></td><td class="shadow-right"></td></tr><tr><td class="shadow-bottom"></td><td class="shadow-corner"></td></tr></table>'
    if (first) {
      dropdown.innerHTML = ''
      dropdown.style.left = '-500px'
      dropdown.style.top = '-500px'
    } else {
      dropdown.innerHTML = html
//      dropdown.style.left = 241 + 'px'
      dropdown.style.left = pos.x + 226 + 'px'
//      dropdown.style.top = pos.y - (IS_IE ? 123 : 124) + 'px'
      dropdown.style.top = pos.y - (IS_IE ? -16 : -15) + 'px'
    }
  }, function() {
    specialsScheduleClose()
  })
  $('drop-specials').onmouseover = function() {
    if (SPECIALS_TIMER) clearTimeout(SPECIALS_TIMER)
  }
  $('drop-specials').onmouseout = function() {
    specialsScheduleClose()
  }
}

function specialsScheduleClose() {
  SPECIALS_TIMER = setTimeout(function() {
    var dropdown = $('drop-specials')
    dropdown.innerHTML = ''
    dropdown.style.left = '-500px'
    dropdown.style.top = '-500px'
  }, 120)
}

// Rollover Links
function rolloverLinksInit(containerId, overCallback, outCallback) {
  if (!DOM_ENABLED) return
  var links = $(containerId).getElementsByTagName('a')
  forEach(links, function(link) {
    if (!hasClass(link.parentNode, 'current')) {
      var img = link.getElementsByTagName('img')[0]
      //vivos add 26/5/2010 evgaze sfalma h parakatw seira
      //preloadImage(img.src.replace(/\.gif$/, '_rollover.gif'))
      img = null
      link.onmouseover = function() {
        if (!this.isOver) {
          var img = this.getElementsByTagName('img')[0]
          img.src = img.src.replace(/\.gif$/, '_rollover.gif')
          this.isOver = true
          overCallback && overCallback(this)
        }
      }
      link.onmouseout = function() {
        var img = this.getElementsByTagName('img')[0]
        img.src = img.src.replace(/_rollover\.gif$/, '.gif')
        this.isOver = null
        outCallback && outCallback(this)
      }
    } else {
      link.onmouseover = function() {overCallback && overCallback(this)}
      link.onmouseout = function() {outCallback && outCallback(this)}
    }
    link = null
  })
  links = null
}

function kidsHeroSelect(select) {
  if (select.value) {
    location.href = select.value
  }
  return false
}

function validateLoginBox(form) {
  var v = new FormValidator(form)
  v.check('email', 'Email')
  v.check('password', 'Κωδικός')
  v.report()
  return v.status
}

function validateBookReviewForm(form) {
  var v = new FormValidator(form)
  v.check('name', 'Όνομα')
  v.check('email', 'Email')
  v.check('comments', 'Σχόλιο')
  v.checkEmail('email', 'Email');
  v.report()
  return v.status
}

function validateBookRating(form) {
  var options = form.elements['rating']
  var selected
  for (var i = 0; option = options[i]; i++) {
    if (option.checked) {
      selected = option; break
    }
  }
  if (!selected) {
    alert('Επιλέξτε πρώτα ένα βαθμό από το 1 ως το 5'); return false
  } else if (!confirm('Αξιολόγηση του βιβλίου με βαθμό ' + selected.value + '\n\nΕπιβεβαίωση;')) {
    selected.checked = false
    return false
  }
  return true
}

function FormValidator(form, briefThreshold) {
  var failed = []
  var emptyCount = 0
  this.status = true
  this.check = function(name, literal) {
    var field = form.elements[name]
    if (field && trim(field.value) == '') {
      failed[failed.length] = 'Το πεδίο ' + literal + ' δεν έχει συμπληρωθεί'
      emptyCount++
      this.status = false
    }
  }
  this.checkEmail = function(name, literal) {
    var value = trim(form.elements[name].value)
    var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/
    var reg2 = /^.+\@(\[?)[\w\-\.]+\.\w{1,3}(\]?)$/
    if (value && !(!reg1.test(value) && reg2.test(value))) {
      failed[failed.length] = 'Το πεδίο ' + literal + ' δεν περιέχει έγκυρο email'
      this.status = false
    }
  }
  this.report = function() {
    if (emptyCount > (briefThreshold || 3)) {
      alert('Υπάρχουν ' + emptyCount + ' υποχρεωτικά πεδία που δεν έχουν συμπληρωθεί')
    } else if (failed.length > 0) {
      alert(failed.join('\n'))
    }
  }
}

// DOM functions
function $(id) {
  return document.getElementById(id)
}

function echo(s) {
  document.write(s)
}

function hasClass(element, className) {
  var re = new RegExp('(^| )' + className + '( |$)', 'i')
  return re.test(element.className)
}

function appendClass(element, className) {
  if (!hasClass(element, className)) element.className += ' ' + className
}

function stripClass(element, className) {
  var re = new RegExp('(^| )' + className + '( |$)', 'gi')
  element.className = element.className.replace(re, ' ').replace(/^\s+|\s+$/g, '')
}

function addEvent(obj, evType, callback, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, callback, useCapture)
    return true
  } else if (obj.attachEvent) {
    var r = obj.attachEvent('on' + evType, callback)
    return r
  } else {
    return false
  }
}

function getElementByClassName(element, _tagName, _className) {
  var re = new RegExp('(^| )' + _className + '( |$)')
  var children = element.getElementsByTagName(_tagName || '*')
  for (var i = 0, child; child = children[i]; i++) {
    if (re.test(child.className)) {
      return child
    }
  }
}

function getElementsByClassName(element, tagName, className) {
  var re = new RegExp('(^| )' + encodeRE(className) + '( |$)')
  var children = element.getElementsByTagName(tagName)
  var list = []
  for (var i = 0, child; child = children[i]; i++) {
    if (re.test(child.className)) {
      list[list.length] = child
    }
  }
  return list
}

function getParentNode(element, parentTagName, className) {
  if (parentTagName) parentTagName = parentTagName.toLowerCase()
  if (className) className = new RegExp('(^| )' + encodeRE(className) + '( |$)')
  while (element = element.parentNode) {
    if (parentTagName && (element.nodeName.toLowerCase() != parentTagName)) continue
    if (className && !className.test(element.className)) continue
    return element
  }
}

function getChildNodes(element, tagName) {
  tagName = tagName.toUpperCase()
  var list = []
  var child = element.firstChild
  while (child) {
    if (child.nodeName.toUpperCase() == tagName) {
      list[list.length] = child
    }
    child = child.nextSibling
  }
  return list
}

function getNextSibling(element, tagName) {
  tagName = tagName.toUpperCase()
  element = element.nextSibling
  while (element) {
    if (element.nodeName.toUpperCase() == tagName) {
      return element
    }
    element = element.nextSibling
  }
}

function getPreviousSibling(element, tagName) {
  tagName = tagName.toUpperCase()
  element = element.previousSibling
  while (element) {
    if (element.nodeName.toUpperCase() == tagName) {
      return element
    }
    element = element.previousSibling
  }
}

function getElementPosition(element) {
  var x = 0, y = 0
  while (element && element.offsetParent) {
    x += element.offsetLeft
    y += element.offsetTop
    element = element.offsetParent
  }
  return {x:x, y:y}
}

function getElementStyle(element, property) {
  if (element.currentStyle) {
    return element.currentStyle[property]
  } else if (document.defaultView && document.defaultView.getComputedStyle) {
    var style = document.defaultView.getComputedStyle(element, null)
    return (style && style.getPropertyValue) ? style.getPropertyValue(property) : ''
  } else {
    return ''
  }
}

function getCookie(name) {
  var re = new RegExp(encodeRE(name) + '=([^;]*)', 'i')
  var match = document.cookie.match(re)
  if (match) return unescape(match[1])
}
function addCookie(name, value, days, seconds) {
  var cookie = name + '=' + escape(value)
  if (days || seconds) {
    cookie += '; expires=' + new Date(new Date().getTime() + (days || 0) * 24 * 60 * 60 * 1000 + (seconds || 0) * 1000).toGMTString()
  }
  document.cookie = cookie
}
function removeCookie(name) {
  addCookie(name, '', -365)
}

function preloadImage(url) {
  var image = new Image()
  arguments.callee['#' + url] = image
  image.src = url
}
function preloadImages() {
  for (var i = 0; i < arguments.length; i++) {
    preloadImage(arguments[i])
  }
}

function writeFlash(filename, width, height, transparent) {
  document.write(
    '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + width + '" height="' + height + '">'
    + '\n<param name="movie" value="' + filename + '">'
    + '\n<param name="quality" value="best">'
    + (transparent ? '\n<param name="wmode" value="transparent">' : '')
    + '\n<embed src="' + filename + '" quality="best"' + (transparent ? ' wmode="transparent"' : '') + ' bgcolor="#ffffff" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '">'
    + '\n</object>'
  )
}

// Core JS
function encodeRE(s) {
  return s.replace(/([\^\$\.\*\?\+\{\}\(\)\[\]\/\|\\])/g, '\\$1')
}
function encodeJS(s) {
  return s.replace(/([\\'])/g, '\\$1').replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[\x00-\x1f]/g, '')
}
function encodeHTML(value) {
  return String(value).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\x22/g, '&quot;')
}
function trim(s) {
  return String(s).replace(/^\s+|\s+$/g, '')
}
function forEach(array, f) { 
  for (var i = 0, item; item = array[i]; i++) {
    f(item, i)
  }
}
function aspm2(s) {
  var m = 'vyaxgcjhew.flums@qidokntpzrb'
  if (!aspm2.r) {aspm2.r = {}; for (var i = 0; i < 28; i++) aspm2.r[m.charAt(i)] = m.charAt((i + 9) % 28)}
  var o = ''; for (var i = 0; i < s.length; i++) {o += aspm2.r[s.charAt(i)] || s.charAt(i)};
  document.write('<a href="mailto:' + o + '">' + o + '</a>')
}