// SuperNote setup: Declare a new SuperNote object and pass the name used to
// identify notes in the document, and a config variable hash if you want to
// override any default settings.

var supernote = new SuperNote('supernote', { });

// Available config options are:
//allowNesting: true/false    // Whether to allow triggers within triggers.
//cssProp: 'visibility'       // CSS property used to show/hide notes and values.
//cssVis: 'inherit'
//cssHid: 'hidden'
//IESelectBoxFix: true/false  // Enables the IFRAME select-box-covering fix.
//showDelay: 0                // Millisecond delays.
//hideDelay: 500
//animInSpeed: 0.1            // Animation speeds, from 0.0 to 1.0; 1.0 disables.
//animOutSpeed: 0.1

// You can pass several to your "new SuperNote()" command like so:
//{ name: value, name2: value2, name3: value3 }


// All the script from this point on is optional!

// Optional animation setup: passed element and 0.0-1.0 animation progress.
// You can have as many custom animations in a note object as you want.
function animFade(ref, counter)
{
 //counter = Math.min(counter, 0.9); // Uncomment to make notes translucent.
 var f = ref.filters, done = (counter == 1);
 if (f)
 {
  if (!done && ref.style.filter.indexOf("alpha") == -1)
   ref.style.filter += ' alpha(opacity=' + (counter * 100) + ')';
  else if (f.length && f.alpha) with (f.alpha)
  {
   if (done) enabled = false;
   else { opacity = (counter * 100); enabled=true }
  }
 }
 else ref.style.opacity = ref.style.MozOpacity = counter*0.999;
};

if (!window.ActiveXObject)
{
 supernote.animations[supernote.animations.length] = animFade;
}



// Optional custom note "close" button handler extension used in this example.
// This picks up click on CLASS="note-close" elements within CLASS="snb-pinned"
// notes, and closes the note when they are clicked.
// It can be deleted if you're not using it.
addEvent(document, 'click', function(evt)
{
 var elm = evt.target || evt.srcElement, closeBtn, note;

 while (elm)
 {
  if ((/note-close/).test(elm.className)) closeBtn = elm;
  if ((/snb-pinned/).test(elm.className)) { note = elm; break }
  elm = elm.parentNode;
 }

 if (closeBtn && note)
 {
  var noteData = note.id.match(/([a-z_\-0-9]+)-note-([a-z_\-0-9]+)/i);
  for (var i = 0; i < SuperNote.instances.length; i++)
   if (SuperNote.instances[i].myName == noteData[1])
   {
    setTimeout('SuperNote.instances[' + i + '].setVis("' + noteData[2] +
     '", false, true)', 10);
   	// Extension to interoperate with drag code.
	   if (note.oldClassName)
	   {
	    note.className = note.oldClassName;
	    note.oldClassName = '';
	   }
    cancelEvent(evt);
   }
 }
});


var printLinkDiv = null;
function printLinkHandler(evt, note)
{
 if (!printLinkDiv)
 {
  printLinkDiv = document.createElement('div');
  printLinkDiv.className = 'print-link-div';
  addEvent(printLinkDiv, 'click', function() { printLinkSetup(false) });
  document.body.appendChild(printLinkDiv);
 }
 printLinkDiv.innerHTML = '<h5>' +
  note.getElementsByTagName('h5').item(0).innerHTML +
  '</h5>' + note.__note_data.innerHTML +
  '<div class="print-link-done">Click to exit Print Preview</div>';
 cancelEvent(evt);
 printLinkSetup(true);
 setTimeout('window.print()', 1000);
};

function printLinkSetup(print)
{
 document.getElementById('container').style.display = print ? 'none' : 'block';
 printLinkDiv.style.height = 'auto';
 printLinkDiv.style.display = print ? 'block' : 'none';
 //if (window.ActiveXObject) printLinkDiv.style.height =
 // (document.documentElement.scrollHeight || document.body.scrollHeight) + 'px';
 // Hide all notes on print.
 if (print) for (var noteID in supernote.notes)
  setTimeout('supernote.setVis("' + noteID + '", false, true)', 100);
}

// Extending the script: you can capture mouse events on note show and hide.
// To get a reference to a note, use 'this.notes[noteID]' within a function.
// It has properties like 'ref' (the note element), 'trigRef' (its trigger),
// 'click' (whether its shows on click or not), 'visible' and 'animating'.
supernote.zIndex = 100;
addEvent(supernote, 'show', function(noteID)
{
 this.notes[noteID].ref.style.zIndex = ++supernote.zIndex;
});
addEvent(supernote, 'hide', function(noteID)
{
});


// AJAX note creation extension (requires HTMLHttpRequest).
SuperNote.prototype.checkWinY = function(newY, note) { with (this)
{
 var noteH = Math.max(400, note.ref.offsetHeight); // assume min height.
 return Math.max(getScrY(), Math.min(newY, getScrY() + getWinH() - noteH - 8));
}};
SuperNote.prototype.createNote = function(noteID, trigRef)
{
 var server = 'product.php?id=' + noteID; // *** change this! ***
 var note = document.createElement('div');
 note.id = 'supernote-note-' + noteID;
 note.className = 'supernote-note snp-mouseoffset';
 var content = document.createElement('div');
 content.className = 'notedefault';
 note.appendChild(content);
 var header = document.createElement('h5');
 header.className = 'supernote-draghandle';
 header.innerHTML = trigRef.getElementsByTagName('img').length ? 
  trigRef.getElementsByTagName('img').item(0).getAttribute('alt') : trigRef.innerHTML;
 content.appendChild(header);
 var closeBtn = document.createElement('a');
 closeBtn.className = 'note-close';
 closeBtn.appendChild(document.createTextNode('X'));
 content.appendChild(closeBtn);
 var data = document.createElement('div');
 data.className = 'notedata';
 data.id = 'noteajax-' + noteID;
 note.__note_data = data;
 content.appendChild(data);
 var printLink = document.createElement('span');
 addEvent(printLink, 'click', function(e) { printLinkHandler(e, note) });
 printLink.className = 'printlink';
 printLink.innerHTML = '[ Print ]';
 content.appendChild(printLink);
 document.getElementsByTagName('body').item(0).appendChild(note);
 if (!this.threader) this.threader = new HTMLHttp.Threader(this.myName + '.threader');
 this.threader.loadInto(server, 'noteajax-' + noteID, function(a, b, c)
 {
  HTMLHttp.copyContent(a, b, c);
  // Create a shadow.
  for (var i = 0; i < 4; i++)
  {
   var shadow = document.createElement('div');
   shadow.style.position = 'absolute';
   shadow.style.backgroundColor = '#000000';
   shadow.style.left =   [ 2,  4,  3,  5][i] + 'px';
   shadow.style.top =    [ 4,  2,  3,  5][i] + 'px';
   shadow.style.opacity = '0.1';
   if (!(document.all && window.ActiveXObject))
   {
    shadow.style.right =  [-4, -2, -3, -1][i] + 'px';
    shadow.style.bottom = [-2, -4, -3, -1][i] + 'px';
   }
   else
   {
    shadow.style.width = note.offsetWidth + 'px';
    shadow.style.height = note.offsetHeight + 'px';
    shadow.style.filter = 'alpha(opacity=10)';
   }
   note.appendChild(shadow);
  }
 });
 return note;
};



// Draggable note extension (requires DragResize);
var dragnote = new DragResize('dragnote', {});
dragnote.isElement = function(elm)
{
 if (elm.className && elm.className.indexOf('supernote-note') > -1) return true;
};
dragnote.isHandle = function(elm)
{
 if (elm.className && elm.className.indexOf('supernote-draghandle') > -1) return true;
};
dragnote.ondragfocus = function()
{
 if (!this.element.oldClassName)
 {
  this.element.oldClassName = this.element.className;
  this.element.className = 'snb-pinned ' + this.element.className;
 }
};
dragnote.apply(document);

