﻿/************************************************************************
* this code has been drastically modified from its original form -- (DwightDwyer @ DancingKangaroo)
* CMotion Image Gallery II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
*************************************************************************/
var Carousel={ // begin the Carousel object

restarea : 70, // width of the "neutral" area in the center of the gallery in px
scrollarea : 30,  // end of carousel that will activate scroll (replace restarea) 
maxspeed : 2,  // top scroll speed in pixels. Script auto creates a range from 0 to top speed.

iedom : document.all||document.getElementById,
orientation : "horizontal", // either "vertical" or "horizontal"
scrollspeed : 0,
movestate : "",   // 'up','down' or 'left','right'
actualheight : 0,
loadedyes : 0,
cross_scroll : null,
crossmain : null,
mainobjoffset :0,
menu_height : 0,
menuwidth : 0,
maxwidth : 0,            //...of carousel
actualwidth : 0 ,        //...of carousel
righttime : null,
lefttime : null,
uptime : null,
downtime : null,


direction : function(where) {
this.scrollspeed = this.maxspeed ; 
switch (where)
{
    case 'left':    this.moveleft(); break;
    case 'right':  this.moveright(); break;
    case 'up':     this.moveup(); break;
    case 'down': this.movedown(); break;
} 
},

ietruebody : function() {
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
},

getposOffset : function(what, aOrientation){
var totaloffset=(aOrientation=="horizontal")? what.offsetLeft: what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
    totaloffset=(aOrientation=="horizontal")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
}
return totaloffset;
},

moveup : function() {
if (this.loadedyes){
    this.movestate="up";
    if (this.iedom&&parseInt(this.cross_scroll.style.top)>(this.menu_height-this.actualheight)){
        this.cross_scroll.style.top=parseInt(this.cross_scroll.style.top)-this.scrollspeed+"px";
    }
}
this.uptime = setTimeout("Carousel.moveup()",10);
},

movedown : function() {
if (this.loadedyes){
    this.movestate="down";
    if (this.iedom&&parseInt(this.cross_scroll.style.top)<0){
        this.cross_scroll.style.top=parseInt(this.cross_scroll.style.top)+this.scrollspeed+"px";
    }
}
this.downtime=setTimeout("Carousel.movedown()",10);
},

moveleft : function() {
if (this.loadedyes){
    this.movestate="left";
    if (this.iedom&&parseInt(this.cross_scroll.style.left)<0){
        this.cross_scroll.style.left=parseInt(this.cross_scroll.style.left)+this.scrollspeed+"px";
        this.lefttime=setTimeout("Carousel.moveleft()",10);
    }
}
},

moveright : function() {
if (this.loadedyes){
    this.movestate="right";
    if (this.iedom&&parseInt(this.cross_scroll.style.left)>(this.menuwidth-this.actualwidth)){
        this.cross_scroll.style.left=parseInt(this.cross_scroll.style.left)-this.scrollspeed+"px";
        this.righttime=setTimeout("Carousel.moveright()",10);
    }
}
},

motionengine : function(e) {
var dsocx=(window.pageXOffset)? pageXOffset: this.ietruebody().scrollLeft;
var dsocy=(window.pageYOffset)? pageYOffset : this.ietruebody().scrollTop;
var curposy=window.event? event.clientY : e.clientY? e.clientY: "";
var curposx=window.event? event.clientX : e.clientX? e.clientX: "";
curposy-=this.mainobjoffset-dsocy;
var topbound=(this.menu_height-this.restarea)/2;
var bottombound=(this.menu_height+this.restarea)/2;
if (this.orientation == "vertical") {// orientation == "vertical"
    if (curposy>bottombound){
        this.scrollspeed=(curposy-bottombound)/((this.menu_height-this.restarea)/2) * (this.maxspeed/2);
        if (this.downtime) clearTimeout(this.downtime);
        if (this.movestate!="up") this.moveup();
    }
    else if (curposy<topbound){
        this.scrollspeed=(topbound-curposy)/((this.menu_height-this.restarea)/2) * (this.maxspeed/2);
        if (this.uptime) clearTimeout(this.uptime);
        if (this.movestate!="down") this.movedown();
    }
    else
        this.scrollspeed=0;
}
else {  // orientation == "horizontal"
    var leftbound=(this.mainobjoffset+this.scrollarea);
    var rightbound=(this.menuwidth+this.mainobjoffset-this.scrollarea);
    curposx-=dsocx;
    if (curposx>rightbound){
        this.scrollspeed=(this.maxspeed/2);
        if (this.lefttime) clearTimeout(this.lefttime);
        if (this.movestate!="right") this.moveright();
    }
    else if (curposx<leftbound){
        this.scrollspeed=(this.maxspeed/2);
        if (this.righttime) clearTimeout(this.righttime);
        if (this.movestate!="left") this.moveleft();
    }
    else
        this.scrollspeed=0;
}        
},

contains_ns6 : function(a, b) {
while (b.parentNode)
    if ((b = b.parentNode) == a) return true;
return false;
},

stopmotion : function(e) {
    if (this.downtime) clearTimeout(this.downtime);
    if (this.uptime) clearTimeout(this.uptime);
    if (this.lefttime) clearTimeout(this.lefttime);
    if (this.righttime) clearTimeout(this.righttime);
    this.movestate="";
},

fillup : function() {
if (this.iedom){
    this.crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer;
    this.maxwidth = Common.SetPxValue(this.crossmain.style.maxWidth, this.crossmain.clientWidth, 0);
    if(typeof this.crossmain.style.maxWidth!=='undefined')
        this.crossmain.style.maxWidth=this.maxwidth + 'px';
    this.menuwidth=this.crossmain.offsetWidth;
    this.menu_height=Common.SetPxValue(this.crossmain.style.height, this.crossmain.clientHeight, 0);
    this.mainobjoffset=this.getposOffset(this.crossmain, this.orientation);
    this.cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery;
    this.cross_scroll.style.left=Common.SetPxValue(this.cross_scroll.style.Left, this.cross_scroll.clientLeft, 0);
    this.cross_scroll.style.top=Common.SetPxValue(this.cross_scroll.style.top, this.cross_scroll.clientTop, 0);
    var truecontainer = document.getElementById("trueContainer");
    this.actualheight=Common.SetPxValue(truecontainer.offsetHeight, truecontainer.clientHeight, 0);
    this.actualwidth=document.getElementById? document.getElementById("trueContainer").offsetWidth : document.all['trueContainer'].offsetWidth;
    this.crossmain.onmousemove=function(e) { Carousel.motionengine(e); }
    this.crossmain.onmouseout=function(e) { Carousel.stopmotion(e); }
}
if (window.opera){
    this.cross_scroll.style.top=this.menu_height-this.actualheight+'px';
    setTimeout('Carousel.cross_scroll.style.top=0', 10);
}
this.loadedyes=1;
}

} // end Carousel object

// ************************************************************************
// Image Thumbnail Viewer Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: Jan 22nd, 2007
// ************************************************************************

var thumbnailviewer={ // begin thumbnailviewer object

thumbnailFeature : true, // either true or false : to use thumbnail folder in carousel or not....
absFromLeft: '0px',
absFromTop: '0px',
enableTitle: false, //Should "title" attribute of link be used as description?
enableAnimation: false, //Enable fading animation?
defineLoading: '<img src="images/loading.gif" /> Loading Image...', //Define HTML for "loading" div
scrollbarwidth: 16,
targetlinks:[], //Array to hold links with rel="thumbnail"
thumbBox : null,
thumbImage : null,

constructor : function(aAbsFromLeft, aAbsFromTop)
{
    this.absFromLeft = Common.StripPxValue(aAbsFromLeft, 0);
    this.absFromTop = Common.StripPxValue(aAbsFromTop, 0);
    //this.createthumbBox(); 
},

createthumbBox:function()
{
    //document.write('<div id="thumbBox"><div id="thumbImage"></div></div>')
    //document.write('<div id="thumbLoading">'+this.defineLoading+'</div>')
    this.thumbBox=document.getElementById("thumbBox");
    this.thumbImage=document.getElementById("thumbImage") //Reference div that holds the shown image
    this.thumbLoading=document.getElementById("thumbLoading") //Reference "loading" div that will be shown while image is fetched
    this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
},

centerDiv:function(divobj)
{ //Centers a div element on the page
    var ie=document.all && !window.opera
    var dom=document.getElementById
    var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
    var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
    var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
    var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
    var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight //Full scroll height of document
    divobj.style.visibility="visible"
},

showthumbBox:function()
{ //Show ThumbBox div :: also dumping for all connected objects
    Common.setFeatureView(""); 
    if (Zoom.isVisible()) Zoom.Hide();                                         //hide a zoom box
    otherView.hideGroup(Common.getCurrentImageName());       //nide other view group box
    DescriptionBox.Remove();                                                    //hide a discription box    
    this.centerDiv(thumbnailviewer.thumbBox);                           //show image in question 
    if (DescriptionLink.visibility)  DescriptionLink.Display(null);       //show link
    else                                   DescriptionBox.Display();             //show desc box  
    Zoom.ShowButton(Common.getCurrentImageName());          //show zoom link button  
    otherView.showGroup(Common.getCurrentImageName());     //show other view group box
    Common.buyNow();                                                            //show PayPal Buy button
},

loadimage:function(link)
{   //Load image function that gets attached to each link on the page with rel="thumbnail"
    if (Zoom.isVisible()) Zoom.Hide(); //if zoomBox is visible on the page already
    if (this.thumbBox.style.visibility=="visible") //if thumbox is visible on the page already
        this.closeit() //Hide it first (not doing so causes triggers some positioning bug in Firefox
    if (this.thumbnailFeature)
        var imageHTML='<img src="' + link.getAttribute("href").replace("Thumbnail/", "") + '" />' //Construct HTML for shown image
    else
        var imageHTML='<img src="' + link.getAttribute("href") + '" />' //Construct HTML for shown image
    var sCurrentImg = new String(link.getAttribute("href"));
    Common.setCurrentImage(sCurrentImg.substring(sCurrentImg.lastIndexOf("/")+1));
    this.centerDiv(this.thumbLoading) //Center and display "loading" div while we set up the image to be shown
    this.thumbImage.innerHTML=imageHTML //Populate thumbImage div with shown image's HTML (while still hidden)
    this.featureImage=this.thumbImage.getElementsByTagName("img")[0] //Reference shown image itself
    this.featureImage.onload=function()
    { //When target image has completely loaded
        thumbnailviewer.thumbLoading.style.visibility="hidden"; //Hide "loading" div
        thumbnailviewer.showthumbBox();  //Display "thumbBox" div to the world!
    }
    if (document.all && !window.createPopup) //Target IE5.0 browsers only. Address IE image cache not firing onload bug: panoramio.com/blog/onload-event/
        this.featureImage.src=link.getAttribute("href")
    this.featureImage.onerror=function()
    { //If an error has occurred while loading the image to show
        thumbnailviewer.thumbLoading.style.visibility="hidden" //Hide "loading" div, game over
    }
}, //end of function loadimage

otherimage:function(link)
{   //Load image function not from carousel.
    Common.setFeatureView(""); 
    if (Zoom.isVisible()) Zoom.Hide(); //if zoomBox is visible on the page already
    if (this.thumbBox.style.visibility=="visible") this.closeit(); 
    var imageHTML='<img src="' + link.getAttribute("href")+'" class="thumbCurrentImage" />'; 
    this.thumbImage.innerHTML=imageHTML; 
    this.featureImage=thumbnailviewer.thumbImage.getElementsByTagName("img")[0];
    var sViewImg = new String(link.getAttribute("href"));
    Common.setFeatureView(sViewImg.substring(sViewImg.lastIndexOf("/")+1));
    Zoom.ShowButton(Common.getFeatureViewName());          //show zoom link button  
    this.featureImage.onload=function()
    { //When target image has completely loaded
        thumbnailviewer.centerDiv(thumbnailviewer.thumbBox);  
    }
    if (document.all && !window.createPopup) 
        thumbnailviewer.featureImage.src=link.getAttribute("href");
}, //end of function otherimage

closeit:function()
{   //Close "thumbbox" div function
    this.thumbBox.style.visibility="hidden";
    this.thumbImage.innerHTML="";
},

cleanup:function()
{   //Clean up routine on page unload
    this.thumbLoading=null;
    if (this.featureImage) this.featureImage.onload=null;
    this.featureImage=null;
    this.thumbImage=null;
    for (var i=0; i<this.targetlinks.length; i++) this.targetlinks[i].onclick=null;
    this.thumbBox=null;
},

dotask:function(target, functionref, tasktype)
{   //assign a function to execute to an event handler (ie: onunload)
    var tasktype=(window.addEventListener)? tasktype : "on"+tasktype;
    if (target.addEventListener)
        target.addEventListener(tasktype, functionref, false);
    else if (target.attachEvent)
        target.attachEvent(tasktype, functionref);
},

loadfirst:function()
{ //Load first image 
    if (Zoom.isVisible()) Zoom.Hide(); //if zoomBox is visible on the page already
    if (this.thumbBox.style.visibility=="visible") //if thumbox is visible on the page already
        this.closeit() //Hide it first (not doing so causes triggers some positioning bug in Firefox
    if (Common.emptyCurrentImage())
        Common.setCurrentImage(Common.getFirstImage());
    var imageHTML='<img src="'+Common.getImageLib()+'/'+Common.Gallery+'/'+Common.getCurrentImage()+'" class="thumbCurrentImage" />'; //Construct HTML for shown image
    this.thumbImage.innerHTML=imageHTML; //Populate thumbImage div with shown image's HTML (while still hidden)
    this.featureImage=this.thumbImage.getElementsByTagName("img")[0]; //Reference shown image itself
    this.showthumbBox();
    if (document.all && !window.createPopup) //Target IE5.0 browsers only. Address IE image cache not firing onload bug: panoramio.com/blog/onload-event/
        this.featureImage.src=Common.getCurrentImage();
}, //end of function loadfirst

init:function()
{  //Initialize thumbnail viewer script by scanning page and attaching appropriate function to links with rel="thumbnail"
    var pagelinks=document.getElementsByTagName("a")
    for (var i=0; i<pagelinks.length; i++)
    { //BEGIN FOR LOOP
        if (pagelinks[i].getAttribute("rel") && pagelinks[i].getAttribute("rel")=="thumbnail")
        { //Begin if statement
            pagelinks[i].onclick=function() 
            {
                thumbnailviewer.loadimage(this) //Load image
                return false
            }
            this.targetlinks[this.targetlinks.length]=pagelinks[i] //store reference to target link
        } //end if statement
    } //END FOR LOOP
this.createthumbBox();     
thumbnailviewer.loadfirst()
} //END init() function

} // end thumbnailviewer object

/* ***************************************
 *
 * onload and page functions
 *
 * **************************************/
window.onload=function(){
    Common.preloadImages();
    thumbnailviewer.init();
    Zoom.Show(Common.Zoom); 
    Carousel.fillup();
    DescriptionLink.Display();
    }
//thumbnailviewer.createthumbBox() //Output HTML for the image thumbnail viewer
//thumbnailviewer.dotask(window, function(){thumbnailviewer.init()}, "load") //Initialize script on page load
thumbnailviewer.dotask(window, function(){thumbnailviewer.cleanup()}, "unload")
