// Application-Wide Ajax Support Routines
//
// Consolidated, Rewritten, and/or Refactored by Lalee Consulting LLC (www.lalee.net)
// for Aloha Island Lei & Floral.
// Copyright (c) 2008, All Rights Reserved.

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Event.observe(window,"load",init);
function init() {
  //if (navigator.platform.indexOf("Mac") < 0) {
  //new ImageRoller("rollover", {duration: 0.3});
  //}else{
  // custom rollover (no animation) for Mac browsers 
		
  var c = "rollover";
  this.imageCount = $A($$("li."+c)).length;
  $A($$("li."+c)).each(function(image) {
    Event.observe(image, 'mouseover', function() {
      if (image.childElements()[0].childElements()[0].getStyle('visibility') != 'hidden') { 
        image.childElements()[0].childElements()[0].setStyle({ visibility: 'hidden'  }); 
        showSubMenu(image);
      }
    });
    Event.observe(image, 'mouseout', function() { 
      if (image.childElements()[0].childElements()[0].getStyle('visibility') != 'visible') {
        image.childElements()[0].childElements()[0].setStyle({ visibility: 'visible' });
        hideSubMenu(image);
      }
    });
  });

  if (navigator.appName.indexOf("Explorer") < 0) {
    var subc = "subRollover";
    this.imageCount = $A($$("a." + subc)).length;
    $A($$("a." + subc)).each(function(image){
      Event.observe(image, 'mouseover', function(){
        if (image.childElements()[0].getStyle('visibility') != 'hidden') {
          image.childElements()[0].setStyle({visibility: 'hidden'});
        }
      });
      Event.observe(image, 'mouseout', function(){
        if (image.childElements()[0].getStyle('visibility') != 'visible') {
          image.childElements()[0].setStyle({visibility: 'visible'});
        }
      });
    });
			
    var specialty = "specialtyRollover";
    this.imageCount = $A($$("a." + specialty)).length;
    $A($$("a." + specialty)).each(function(image){
      Event.observe(image, 'mouseover', function(){
        if ($("specialty_label").childElements()[0].getStyle('visibility') != 'hidden') {
          $("specialty_label").childElements()[0].setStyle({visibility: 'hidden'});
        }
      });
      Event.observe(image, 'mouseout', function(){
        if ($("specialty_label").childElements()[0].getStyle('visibility') != 'visible') {
          $("specialty_label").childElements()[0].setStyle({visibility: 'visible'});
        }
      });
    });
  }
}

function showSubMenu(sender){
  sender.getElementsByTagName('div')[0].style.left='auto';
}
 
function hideSubMenu(sender){
  sender.getElementsByTagName('div')[0].style.left='-999px';
}
 
var offsetfrommouse=[50, -55]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset 50,-25 default
function showprev(imagesrc, oid, w, h, preview_caption){
  prev_h = h;
  prev_w = w;

  newHTML = '<div id="preview_box">';
  newHTML = newHTML + '<img src="' + imagesrc + '" border="0"><p id="preview_caption">' + preview_caption + '</p>';
  newHTML = newHTML + '</div>';

  getprevobjnostyle("previewcontainer").innerHTML = newHTML;
  setTimeout('getprevobj("previewcontainer").visibility="visible"',400);
  document.onmousemove=followmouse;
}

function hideprev(){
  getprevobj("previewcontainer").visibility="hidden";
  getprevobj("previewcontainer").left="-1500px";
  document.onmousemove="";
}

function followmouse(e){
  var xcoord=offsetfrommouse[0]
  var ycoord=offsetfrommouse[1]

  var flip_loc_x = 445 // When we get to loc clientWidth - 445 then move the preview to the left of the cursor
  var left_space = -20 // close the gap when we move the preview to the left of the cursor
  var flip_loc_y = 10
  var scrolltop=0

  if(getprevobjnostyle('container').scrollTop)
    scrolltop=getprevobjnostyle('container').scrollTop
  else
    scrolltop=truebody().scrollTop

  var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15

//var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)
  var docheight=document.all? truebody().clientHeight : window.innerHeight

  if (typeof e != "undefined"){
    if (docwidth - e.pageX < flip_loc_x){
      xcoord = e.pageX - flip_loc_x - offsetfrommouse[0] - 10 + left_space; // Move to the left side of the cursor
    } else {
      xcoord += e.pageX;
    }
    if (docheight - e.pageY < (prev_h + flip_loc_y)){
      ycoord += e.pageY - Math.max(0,(flip_loc_y + prev_h + e.pageY - docheight - scrolltop));
    } else {
      ycoord += e.pageY;
    }
  } else if (typeof window.event != "undefined"){
    if (docwidth - event.clientX < flip_loc_x){ // changed from 300
      xcoord = event.clientX + truebody().scrollLeft - flip_loc_x+ left_space; // changed from 286 Move to the left side of the cursor
    } else {
      xcoord += truebody().scrollLeft+event.clientX
    }
    if (docheight - event.clientY < (prev_h + flip_loc_y)){
      ycoord += event.clientY + scrolltop - Math.max(0,(flip_loc_y + prev_h + event.clientY - docheight));
    } else {
      ycoord += scrolltop + event.clientY;
    }
  }

  var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
  var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

  getprevobj("previewcontainer").left=xcoord+"px"
  getprevobj("previewcontainer").top=ycoord+"px"
//getprevobj("previewcontainer").left = x - 150
//getprevobj("previewcontainer").top = y - 70
}

function getprevobj(o){
  if (document.getElementById)
    return document.getElementById(o).style
  else if (document.all)
    return document.all.o.style
}

function getprevobjnostyle(o){
  if (document.getElementById)
    return document.getElementById(o)
  else if (document.all)
    return document.all.o
}

function getObjectByName(n){
  alert(n);
  return(document.getElementsByTagName(n)[0].firstChild);
}

function truebody(){
  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function initSelectShippingMethod(){
  var box1 = $("ship_type_id_7");
  var box2 = $("ship_type_id_3");
  var box3 = $("ship_type_id_2");
  var box4 = $("ship_type_id_6");
  var box5 = $("ship_type_id_8");
	
  if (box1) {
    addEvent(box1, "focus", toggleShippingTime);
    addEvent(box1, "click", toggleShippingTime);
  }
  if (box2) {
    addEvent(box2, "focus", toggleShippingTime);
    addEvent(box2, "click", toggleShippingTime);
  }
  if (box3) {
    addEvent(box3, "focus", toggleShippingTime);
    addEvent(box3, "click", toggleShippingTime);
  }
  if (box4) {
    addEvent(box4, "focus", toggleShippingTime);
    addEvent(box4, "click", toggleShippingTime);
  }
  if (box5) {
    addEvent(box5, "focus", toggleShippingTime);
    addEvent(box5, "click", toggleShippingTime);
  }
  toggleShippingTime();
}

function toggleShippingTime(e) {
  var shipBlock = $("shipping_time");

  shipBlock.style.display = "block";
  return;

// LALEE:  always show shipping-time box
  var boxa = $("ship_type_id_7");
  var boxb = $("ship_type_id_6");
  if (boxb) {
    if (boxa.checked == true || boxb.checked == true) {
      shipBlock.style.display = "block";
    } else {
      shipBlock.style.display = "none";
    }
  } else {
    if (boxa.checked == true) {
      shipBlock.style.display = "block";
    } else {
      shipBlock.style.display = "none";
    }
  }
}

function addItemToCartNotification(el) {
  var not = $('addItemNotifier');
  var elem = $(el);
	
  not.style.visibility = "visible";

  not.style.left = elem.cumulativeOffset().left - 20 + "px";
  not.style.top = elem.cumulativeOffset().top + 25 + "px";
  not.style.opacity = 0;
	
  new Effect.Opacity(not,{duration: 1.0, transition: Effect.Transitions.linear, from: 0,   to: 1.0});
  new Effect.Opacity(not,{duration: 1.0, transition: Effect.Transitions.linear, from: 1.0, to: 0, queue: 'end' });
  $('cornerlinks').highlight();  
}

function showField(ele){
  var elem = $(ele);
  elem.style.display = "block";
}
