//
// preload misc images
var btn_send_wishlist_ovr = new Image();
btn_send_wishlist_ovr.src = '/images/btn_send_wishlist_ovr.gif';

//
// rollover function
function swapImg( target, src ) {
 target.src = src
}





//
// BEGIN preload inspiration images
//
var currentInspirationPic = 0;
var inspirationPics = [];
var totalInspirationPics = 10;

for(var i=0; i<totalInspirationPics; i++) {
 var img = new Image()
 img.src = "/images/inspiration/inspiration_" + (i+1) + ".jpg"
 inspirationPics.push(img);
}

function nextInspirationPic() {
 currentInspirationPic ++
 currentInspirationPic %= totalInspirationPics

 var p = document.getElementById('inspirationPic');
 p.src = inspirationPics[ currentInspirationPic ].src
}
function prevInspirationPic() {
 currentInspirationPic --
 if(currentInspirationPic < 0){
  currentInspirationPic = totalInspirationPics - 1;
 }

 var p = document.getElementById('inspirationPic');
 p.src = inspirationPics[ currentInspirationPic ].src
}
//
// END preload inspiration images
//






//
// BEGIN toggle function for "inspiration" tab
//
var tabVisible = false;
function toggleTab() {
 var drawer = document.getElementById("inspiration");
 var box = document.getElementById("inspirationBox");
 var tab = document.getElementById("inspirationTab");
 var cont = document.getElementById("inspirationContainer");

 if(!tabVisible){
  var t = new Tween(drawer.style,'marginLeft',Tween.regularEaseOut, -486 , -1, 0.2, 'px');
  t.start();
  box.style.visibility = "visible";
  tab.className = "inspirationTabOn"
  tabVisible = true;
  cont.style.width = "600px"
 }else{
  var t = new Tween(drawer.style,'marginLeft',Tween.regularEaseOut, -1,-486, 0.2, 'px');
  t.start();
  t.onMotionFinished = function() {
   box.style.visibility = "hidden"
   tab.className = "InspirationTab"
   cont.style.width = "50px"
  }
  tabVisible = false;
 }

}
//
// END toggle function for "inspiration" tab
//






//
// BEGIN js popup class
//
var Popup = function(content)
{

 this.dim = document.createElement("div");
 this.dim.id = "dim"
 this.dim.onclick = function(){ window.popup.close(); }
 document.body.appendChild(this.dim)

 this.popupObj = document.createElement("div");
 this.popupObj.className="popup"
 this.popupObj.innerHTML = '<div class="all"><div class="header"><img src="/images/btn_close_layer.gif" onclick="window.popup.close();" class="btn_close" /></div>' + content + '</div>'
 this.popupObj.owner = this
 document.body.appendChild(this.popupObj)
 window.popup = this;
}

Popup.prototype.hide = function()
{
 this.popupObj.style.visibility = "hidden"
}
Popup.prototype.show = function()
{
 this.popupObj.style.visibility = "visible"
}
Popup.prototype.moveTo = function(x,y)
{
 this.popupObj.style.left = x + "px";
 this.popupObj.style.top = y + "px";
}
Popup.prototype.moveBy = function(x,y)
{
 this.popupObj.style.left = ( parseInt(this.popupObj.style.left) + x ) + "px";
 this.popupObj.style.top = ( parseInt(this.popupObj.style.top) + y ) + "px";
}
Popup.prototype.center = function(x,y)
{
 if(navigator.appName.indexOf("Explorer") > -1){
  var x = ( document.body.offsetWidth / 2 ) - ( this.popupObj.offsetWidth / 2 );
  var y = ( document.body.offsetHeight / 2 ) - ( this.popupObj.offsetHeight / 2 );
 }else{
  var x = ( window.innerWidth / 2 ) - ( this.popupObj.offsetWidth / 2 )
  var y = ( window.innerHeight / 2 ) - ( this.popupObj.offsetHeight / 2 )
 }
 this.popupObj.style.left = x + "px"
 this.popupObj.style.top = y + "px"
}

Popup.prototype.close = function(x,y)
{
 document.body.removeChild(this.popupObj)
 document.body.removeChild(this.dim)
}
//
// END js popup
//






function makePopUpImage(src) {
 var p = new Popup('<img src='+src+' onload="window.popup.center();window.popup.show();" />');
 //
 // hide popup
 p.hide();
 // "img.onload" will reveal and center
 // the popup once the image has loaded
}





//
// add friends functions
//
function addFields() {
 var target = document.getElementById("fieldHolder");

 var l = document.createElement("label");
 l.innerHTML = "Friend's Name";
 target.appendChild(l);

 var i = document.createElement("input");
 i.type = "text";
 i.className = "text";
 i.value = "";
 i.name = "friend_name[]"
 target.appendChild(i);

 var l = document.createElement("label");
 l.innerHTML = "Friend's Email";
 l.style.width = "auto";
 target.appendChild(l);

 var i = document.createElement("input");
 i.type = "text";
 i.className = "text";
 i.value = "";
 i.name = "friend_email[]"
 target.appendChild(i);

 var br = document.createElement("br");
 br.style.clear = "left";
 target.appendChild(br);
}





//
// Comment textarea stuff
//
var defaultComment = "Add a comment to this item to personalize it for your friends and family."
function hideComment(id){
 document.getElementById('comment' + id).style.display='none';
 document.getElementById('commentTextArea' + id).innerHTML = defaultComment;
}

function showComment(id){
 var comDiv = document.getElementById('comment' + id)
 comDiv.style.display='block';
 document.getElementById('commentTextArea' + id).innerHTML = defaultComment;
 // reset
 comDiv.parentNode.innerHTML=comDiv.parentNode.innerHTML
}

function clearDefaultComment(id){
 var ta = document.getElementById('commentTextArea' + id);
 if(ta.innerHTML=="Add a comment to this item to personalize it for your friends and family."){
  ta.innerHTML = "";
 }
}


//
// add product to wish list
function addProduct(sku) {
 var c = document.getElementById('commentTextArea' + sku);
 var time = new Date().getTime();
 loadElement('wishListItems','/add','sku='+sku+'&comment='+c.value+'&time='+time, 'POST');
}


//
// create a cross-browser XMLHttpRequest
//
function createXMLHttpRequest()
{
 var request = false;
 if(navigator.appName.indexOf("Explorer") > -1){
  request = new ActiveXObject("Msxml2.XMLHTTP");
  if(!request){
   request = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }else{
  request = new XMLHttpRequest();
 }
 if (!request){
  alert("Error initializing XMLHttpRequest!");
 }
 return request;
}


//
// load a URL using XMLHttpRequest and
// pass output to a given element on the page
//
function loadElement(elem, url, params, method)
{
 var request = createXMLHttpRequest()
 request.onreadystatechange = Delegate.create(this, this.onResponse, request, elem)

 if(method == "POST") {
  request.open(method, url, true);
  //Send the proper header information along with the request
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.setRequestHeader("Content-length", params.length);
  request.setRequestHeader("Connection", "close");

  request.send(params);
 }else{
  request.open(method, url + "?" + params, true);
  request.send(null);
 }
}


//
// response from XMLHttpRequest
//
function onResponse(e, request, elem)
{
 //
 // response from server (readyState: 4)
 // check for status code 200 OK
 //
 if (request.readyState == 4 && request.status == 200) {
  var elemTag = document.getElementById(elem)
  elemTag.innerHTML = request.responseText
 }
};



