/*****************************************************************
/*      Copyright notice :
/*   Script created by and copyright : www.cass-hacks.com
/*    This work is licensed according to the following Terms and Conditions :
/*    Copyright (c) 2007 & 2008 cass-hacks.com</p>
/*    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
/*    files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, 
/*    merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished 
/*    to do so, subject to the following conditions:
/*    1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
/*    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
/*        in the documentation and/or other materials provided with the distribution, and in the same place and form as other copyright, 
/*        license and disclaimer information.
/*    3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product 
/*        includes software developed by Cass-hacks.com (http://cass-hacks.com/) and its contributors", in the same place and 
/*        form as other third-party acknowledgments. Alternately, this acknowledgment may appear in the software itself, in the same form 
/*        and location as other such third-party acknowledgments.
/*    4. Except as contained in this notice, the name of Cass-hacks.com shall not be used in advertising or otherwise to 
/*        promote the sale, use or other dealings in this Software without prior written authorization from Cass-hacks.com.
/*    
/*    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
/*    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
/*    DISCLAIMED. IN NO EVENT SHALL THE XFREE86 PROJECT, INC OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
/*    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
/*    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
/*    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
/*    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
/*    POSSIBILITY OF SUCH DAMAGE.
/*    
/*    If you find something on this site useful, please use the contact form to inform Cass-hacks of your application.  If need be, 
/*    modifications could be made to any of the code found on this site to more accurately fit a given application's needs and 
/*    requirements.
/*
******************************************************************/

// Thumbnail onload
var bIs_IE = false;
if (window.addEventListener)
  window.addEventListener('load',init_images,false);
else if (window.attachEvent) {
  bIs_IE = true;
  window.attachEvent('onload',init_images);
}

function init_images () {
  var drp_shdw1 = new Image();
  drp_shdw1.src = 'http://' + window.location.hostname + '/images/examples/full_t_sm.gif';
  var drp_shdw2 = new Image();
  drp_shdw2.src = 'http://' + window.location.hostname + '/images/examples/full_r_sm.gif';
  var drp_shdw3 = new Image();
  drp_shdw3.src = 'http://' + window.location.hostname + '/images/examples/full_b_sm.gif';
  var drp_shdw4 = new Image();
  drp_shdw4.src = 'http://' + window.location.hostname + '/images/examples/full_l_sm.gif';
  var drp_shdw5 = new Image();
  drp_shdw5.src = 'http://' + window.location.hostname + '/images/examples/full_tr_sm.gif';
  var drp_shdw6 = new Image();
  drp_shdw6.src = 'http://' + window.location.hostname + '/images/examples/full_br_sm.gif';
  var drp_shdw7 = new Image();
  drp_shdw7.src = 'http://' + window.location.hostname + '/images/examples/full_bl_sm.gif';
  var drp_shdw8 = new Image();
  drp_shdw8.src = 'http://' + window.location.hostname + '/images/examples/full_tl_sm.gif';

  opaque_region = document.getElementById('opaqued');
  var imgs = opaque_region.getElementsByTagName('img');
  for (var x=0;x<imgs.length;x++) {
    if (imgs[x].className.indexOf('expando') != -1) {
      imgs[x].onmouseover = init_expansion;
    }
  }
}

var step = 5;
var wait = 20;
var steps = 15;
var inter_size_width = 160;
var sm_shdwWidth = 8;
var min_opacity = 35;
var thumbs_id = 'thmb';
var source_id = 'full';
var thm_image = null;
var thmWidth, thmHeight, thmLeft, thmTop;
var thmCntrX,thmCntrY;
var full_height,full_width;
var stepX,stepY,stepWidth,stepHeight;
var crntX,crntY,crntWidth,crntHeight;
var expando_timer;
var sm_shdwStep, sm_shdwCurrent;
var opacity, opacity_step;
var bShowing = false;
var thm_container = null;
var thm_insertion = null;
var small_expand = null;
var full_container = null;
var full_insertion = null;
var full_expand = null;
var opaque_region = null;
var scrollParent = null;
pre_loader = null;

function init_expansion () {
  if (bShowing) return;
  if (expando_timer) clearTimeout(expando_timer);
  if (small_expand != null) {
    thm_container.parentNode.removeChild(thm_container);
  }
  scrollParent = opaque_region.parentNode;
  thm_image = this;
  thm_image.mouseover = null;
  thmWidth = thm_image.offsetWidth;
  thmHeight =thm_image.offsetHeight;
  thmLeft = getOffsetLeft(thm_image) - scrollParent.scrollLeft;
  thmTop =getOffsetTop(thm_image) - scrollParent.scrollTop;
  thmCntrY = thmTop+thmHeight/2;
  thmCntrX = thmLeft+thmWidth/2;
  small_expand = document.createElement('img');
  small_expand.style.visibility = 'hidden';
  small_expand.src = thm_image.src;
  small_expand.className = 'grow_img';
  thm_container = document.createElement('div');
  document.getElementsByTagName('body')[0].appendChild(thm_container);
  thm_container.className = 'center2';
  thm_container.id = 'thm_container';
  thm_container.innerHTML = '<div class="fs_t">' +
                                            '<div class="fs_r">' +
                                              '<div class="fs_b">' +
                                                '<div class="fs_l">' +
                                                  '<div class="fs_tr">' + 
                                                    '<div class="fs_br">' +
                                                      '<div class="fs_bl">' +
                                                        '<div class="fs_tl" id="thm_insertion">' + 
                                                        '</div>' +
                                                      '</div>' +
                                                    '</div>' +
                                                  '</div>' +
                                                '</div>' +
                                              '</div>' +
                                            '</div>' +
                                          '</div>';
  thm_container.onmouseover = function () {
    doTip(thm_container, 'Click to Expand');
  }
  insertion_point = document.getElementById('thm_insertion');
  insertion_point.appendChild(small_expand);
  crntWidth = thmWidth;
  crntHeight = thmHeight;
  small_expand.style.width = crntWidth+'px';
  small_expand.style.height = crntHeight+'px';
  thm_container.style.top = thmTop-2+'px';
  thm_container.style.left = thmLeft-2+'px';
  small_expand.onmouseout = init_thumb_collapse;
  small_expand.style.visibility = 'visible';
  sm_shdwStep = (sm_shdwWidth/(140 - thmWidth))*step/2;
  sm_shdwCurrent = 0;
  expand_thumb();
}

function expand_thumb () {
  if (expando_timer) clearTimeout(expando_timer);
  crntHeight += step;
  crntWidth += step;
  small_expand.style.height = crntHeight+'px';
  small_expand.style.width = crntWidth+'px';
  thm_container.style.top = parseInt(thmCntrY-crntHeight/2)+'px';
  thm_container.style.left = parseInt(thmCntrX-crntWidth/2)+'px';
  sm_shdwCurrent += sm_shdwStep;
  insertion_point.style.padding = parseInt(sm_shdwCurrent)+'px';
  insertion_point.style.paddingBottom = parseInt(sm_shdwCurrent/2)+'px';
  if (crntWidth < inter_size_width) {
    expando_timer = setTimeout(expand_thumb,wait);
  } else {
    inter_size_height = crntHeight;
    small_expand.onclick = init_expand_full;
  }
}

function init_thumb_collapse () {
  if (bShowing) return;
  small_expand.onclick = null;
  small_expand.onmouseover = null;
  hideTip();
  if (small_expand != null) {
    collapse_thumb();
  }
}

function collapse_thumb () {
  if (expando_timer) clearTimeout(expando_timer);
  crntWidth -= step;
  crntHeight -= step;
  small_expand.style.width = parseInt(crntWidth)+'px';
  small_expand.style.height = parseInt(crntHeight)+'px';
  thm_container.style.top = parseInt(thmCntrY-crntHeight/2)+'px';
  thm_container.style.left = parseInt(thmCntrX-crntWidth/2)+'px';
  sm_shdwCurrent -= sm_shdwStep;
  insertion_point.style.padding = parseInt(sm_shdwCurrent)+'px';
  insertion_point.style.paddingBottom = parseInt(sm_shdwCurrent/2)+'px';
  if (crntWidth > thmWidth)
    expando_timer = setTimeout(collapse_thumb,wait);
  else {
    if (expando_timer) clearTimeout(expando_timer);
    thm_image.onmouseover = init_expansion;
    thm_container.parentNode.removeChild(thm_container);
    small_expand = null;
  }
}

function init_expand_full () {
  if (expando_timer) clearTimeout(expando_timer);
  if (full_expand != null)
    full_container.parentNode.removeChild(full_container);
  bShowing = true;
  pre_loader = new Image();
  var full_img = thm_image.src.replace(thumbs_id,source_id)
  pre_loader.src = full_img;
  if (!pre_loader.complete) {
    pre_loader.onload = prepare_expand_full;
    notify.display(full_img,'Loading image.');
  } else prepare_expand_full();
}

function prepare_expand_full () {
  notify.kill(pre_loader.src);
  full_expand = document.createElement('img');
  full_expand.style.visibility = 'hidden';
  full_expand.src = pre_loader.src;
  delete pre_loader;
  full_container = document.createElement('div');
  document.getElementsByTagName('body')[0].appendChild(full_container);
  full_container.className = 'center2';
  full_container.id = 'full_container';
  full_container.innerHTML = '<div class="fs_t">' +
                                            '<div class="fs_r">' +
                                              '<div class="fs_b">' +
                                                '<div class="fs_l">' +
                                                  '<div class="fs_tr">' +
                                                    '<div class="fs_br">' +
                                                      '<div class="fs_bl">' +
                                                        '<div class="fs_tl" id="full_insertion">' +
                                                        '</div>' +
                                                      '</div>' +
                                                    '</div>' +
                                                  '</div>' +
                                                '</div>' +
                                              '</div>' +
                                            '</div>' +
                                          '</div>';
  full_container.onmouseover = function () {
    doTip(full_container, 'Click to Reduce');
  }
  full_insertion = document.getElementById('full_insertion');
  full_insertion.appendChild(full_expand);
  full_insertion.style.padding = sm_shdwWidth+'px';
  full_insertion.style.paddingBottom = sm_shdwWidth/2+'px';
  full_height = full_expand.offsetHeight;
  full_width = full_expand.offsetWidth;
  full_expand.style.width = crntWidth+'px';
  full_expand.style.height = crntHeight+'px';
  full_expand.className = 'grow_img';
  if (self.innerHeight) { // all except Explorer
    var availWidth = self.innerWidth;
    var availHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    var availWidth = document.documentElement.clientWidth;
    var availHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    var availWidth = document.body.clientWidth;
    var availHeight = document.body.clientHeight;
  }
  if ( (full_height+sm_shdwCurrent*2) > availHeight) {
    full_width = full_width * (availHeight) / (full_height+sm_shdwCurrent*2);
    full_height = availHeight-(sm_shdwCurrent*4);
  }
  if ( (full_width+sm_shdwCurrent*2) > availWidth) {
    full_height = full_height * (availWidth) / (full_width+sm_shdwCurrent*2);
    full_width = availWidth-(sm_shdwCurrent*4);
  }
  if (self.pageYOffset) { // all except Explorer
  	var scrollX = self.pageXOffset;
  	var scrollY = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
  	var scrollX = document.documentElement.scrollLeft;
  	var scrollY = document.documentElement.scrollTop;
  } else if (document.body) { // all other Explorers
  	var scrollX = document.body.scrollLeft;
  	var scrollY = document.body.scrollTop;
  }
  var small_container = document.getElementById('thm_container');
  full_container.style.top = getOffsetTop(small_container)+'px';
  full_container.style.left = getOffsetLeft(small_container)+'px';
  full_expand.style.visibility = 'visible';
  thm_container.style.visibility = 'hidden';
  small_expand.style.visibility = 'hidden';
  crntX = getOffsetLeft(full_container);
  crntY = getOffsetTop(full_container);
  stepY = (crntY - ((scrollY + availHeight/2 - sm_shdwWidth) - full_height/2))/steps;
  stepX = (crntX - ((scrollX + availWidth/2 - sm_shdwWidth) - full_width/2))/steps;
  stepHeight = (full_height - small_expand.offsetHeight)/steps;
  stepWidth = (full_width - small_expand.offsetWidth)/steps;
  opacity = 100;
  opacity_step = (100-min_opacity)/steps;
  expand_full();
}

function expand_full () {
  crntY -= stepY;
  crntX -= stepX;
  crntHeight += stepHeight;
  crntWidth += stepWidth;
  opacity -= opacity_step;
  opaque_region.style.opacity = opacity/100;
  opaque_region.style.filter = 'alpha(opacity='+opacity+')';
  full_container.style.left = parseInt(crntX)+'px';
  full_container.style.top = parseInt(crntY)+'px';
  full_expand.style.width = parseInt(crntWidth)+'px';
  full_expand.style.height = parseInt(crntHeight)+'px';
  if (crntHeight < full_height)
    expando_timer = setTimeout(expand_full,wait);
  else
    full_expand.onclick = collapse_full;
}

function collapse_full () {
  hideTip();
  full_container.onmouseover = null;
  full_expand.onclick = null;
  crntY += stepY;
  crntX += stepX;
  crntHeight -= stepHeight;
  crntWidth -= stepWidth;
  full_container.style.top = parseInt(crntY)+'px';
  full_container.style.left = parseInt(crntX)+'px';
  full_expand.style.height = parseInt(crntHeight)+'px';
  full_expand.style.width = parseInt(crntWidth)+'px';
  opacity += opacity_step;
  opaque_region.style.opacity = opacity/100;
  opaque_region.style.filter = 'alpha(opacity='+opacity+')';
  if (crntHeight + sm_shdwCurrent > inter_size_height)
    expando_timer = setTimeout(collapse_full,wait);
  else {
    small_expand.style.visibility = 'visible';
    thm_container.style.visibility = 'visible';
    if (expando_timer) clearTimeout(expando_timer);
    bShowing = false;
    opaque_region.style.opacity = 1;
    opaque_region.style.filter = 'alpha(opacity=100)';
    full_container.parentNode.removeChild(full_container);
    full_expand = null;
    collapse_thumb();
  }
}

function getOffsetTop (element) {
	var value = element.offsetTop;
	while ( element = element.offsetParent )  {
    if (bIs_IE) {
      btwidth = parseInt(element.currentStyle['borderTopWidth']);
      if (!isNaN(btwidth))
        value += btwidth;
    }
		value += element.offsetTop;
  }
	return value;
}

function getOffsetLeft (element) {
	var value = element.offsetLeft;
	while ( element = element.offsetParent ) {
    if (bIs_IE) {
      blwidth = parseInt(element.currentStyle['borderLeftWidth'])
      if (!isNaN(blwidth))
        value += blwidth;
    }
		value += element.offsetLeft;
  }
	return value;
}
