// -----------------------------------------------------------------------------------
//
// This page coded by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// This work is licensed under a Creative Commons License
// Attribution-ShareAlike 2.0
// http://creativecommons.org/licenses/by-sa/2.0/
//
// Associated APIs copyright their respective owners
//
// -----------------------------------------------------------------------------------
// --- version date: 11/28/05 --------------------------------------------------------


// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = 0;//splitURL[1] - 1;

// if no photoId supplied then set default
var photoId = (!photoId)? 0 : photoId;

// CSS border size x 2
var borderSize = 10;

var headerSize = 20;

// Photo directory for this gallery
var photoDir = "";

// Define each photo's name, height, width, and caption
var photoArray = new Array();

// Number of photos in this gallery
var photoNum = photoArray.length;

var useAjax=0;
var jaxPath='';

/*--------------------------------------------------------------------------*/

// Additional methods for Element added by SU, Couloir
Object.extend(Element, {
getWidth: function(element) {
element = $(element);
return element.offsetWidth;
},
setWidth: function(element,w) {
element = $(element);
element.style.width = w +"px";
},
setHeight: function(element,h) {
element = $(element);
element.style.height = h +"px";
},
setSrc: function(element,src) {
element = $(element);
element.src = src;
},
setHref: function(element,href) {
element = $(element);
element.href = href;
},
setInnerHTML: function(element,content) {
element = $(element);
element.innerHTML = content;
}
});

/*--------------------------------------------------------------------------*/

var Slideshow = Class.create();

Slideshow.prototype = {
initialize: function(par_photoId) {
photoId=par_photoId;
this.photoId = par_photoId;
this.photo = 'Photo';
this.photoBox = 'Container';
this.prevLink = 'PrevLink';
this.nextLink = 'NextLink';
this.captionBox = 'CaptionContainer';
this.caption = 'Caption';
this.counter = 'Counter';
this.loader = 'Loading';
this.useAjax = '0';
this.jaxPath = '';
},
initAjaxCall:function(pjaxPath){
this.useAjax='1';
this.jaxPath=pjaxPath;
useAjax = this.useAjax;
jaxPath = this.jaxPath;
},
setOverlayDiv: function() {
var arrayPageSize = getPageSize();
$('overlay_div').style.width = arrayPageSize[0]+'px';
$('overlay_div').style.height = arrayPageSize[1]+'px';
$('overlay_div').style.top = '0px';
$('overlay_div').style.left= '0px';
hideSelectBoxes();
hideFlash();
Element.show('overlay_div');
Element.show('product_slideshow');
},
getCurrentSize: function() {
// Get current height and width, subtracting CSS border size
this.wCur = Element.getWidth(this.photoBox) - borderSize;
this.hCur = Element.getHeight(this.photoBox) - borderSize;
},
getNewSize: function() {
// Get current height and width
this.wNew = photoArray[photoId][1];
this.hNew = photoArray[photoId][2] + headerSize;
},
getScaleFactor: function() {
this.getCurrentSize();
this.getNewSize();

// Scalars based on change from old to new
this.xScale = ((this.wNew) / this.wCur) * 100;
this.yScale = ((this.hNew) / this.hCur) * 100;

//alert(this.wCur+" - "+this.wNew);
},
setNewPhotoParams: function() {
// Set source of new image
Element.setSrc(this.photo,photoDir + photoArray[photoId][0]);
// Set anchor for bookmarking
Element.setInnerHTML('CaptionDesc',photoArray[photoId][3]);
if (useAjax=='1') $('CaptionDesc').className='CaptionDesc_bg';
Element.setHref(this.prevLink, "#" + (photoId+1));
Element.setHref(this.nextLink, "#" + (photoId+1));
Element.setHref('PrevLink2', "#" + (photoId+1));
Element.setHref('NextLink2', "#" + (photoId+1));
},
setPhotoCaption: function() {
// Add caption from gallery array
//Element.setInnerHTML(this.caption,photoArray[photoId][3]);   //Write caption
Element.setInnerHTML(this.counter,('Bild '+(photoId+1)+' / '+photoArray.length));
},
resizePhotoBox: function() {
this.getScaleFactor();
//new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.3, queue: 'front'});    // STUPID BUG
//new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, delay: 0.5, duration: 0.3});		   // STUPID BUG
// Dynamically resize caption box as well
Element.setWidth(this.photoBox,this.wNew);
Element.setHeight(this.photoBox,this.hNew);
Element.setWidth(this.captionBox,this.wNew-(-borderSize));
},
/*
resizePhotoBox: function() {
this.getScaleFactor();
new Effect.Scale(this.photoBox, this.yScale, {scaleX: false, duration: 0.3, queue: 'front'});
new Effect.Scale(this.photoBox, this.xScale, {scaleY: false, delay: 0.5, duration: 0.3});
// Dynamically resize caption box as well
Element.setWidth(this.captionBox,this.wNew-(-borderSize));
},
*/
showPhoto: function(){
new Effect.Fade(this.loader, {delay: 0.5, duration: 0.3});
// Workaround for problems calling object method "afterFinish"
new Effect.Appear(this.photo, {duration: 0.5, queue: 'end',
afterFinish: function()
{
Effect.SlideDown('CaptionContainer',{duration:0.3, afterFinish: function()
{
new Effect.Appear('CaptionPrev', {duration: 0.2, afterFinish: function()
{
new Effect.Appear('CaptionNext', {duration: 0.2, afterFinish: function()
{
new Effect.Appear('CaptionDesc', {duration: 0.2,  afterFinish: function()
{
if (useAjax=='1') new Ajax.Updater('CaptionDesc', jaxPath, {evalScripts:true, method:'post', onComplete:function(){ $('CaptionDesc').className='CaptionDesc';}  });
}});
}})
}})
}})
}});
},
nextPhoto: function(){
// Figure out which photo is next
(photoId == (photoArray.length - 1)) ? photoId = 0 : photoId++;
this.initSwap();
},
prevPhoto: function(){
// Figure out which photo is previous
(photoId == 0) ? photoId = photoArray.length - 1 : photoId--;
this.initSwap();
},
initSwap: function() {

var this_global=this;

// Begin by hiding main elements
Element.hide('CaptionPrev');Element.hide('CaptionDesc');Element.hide('CaptionNext');
Element.show(this.loader);
Element.hide(this.photo);
Element.hide(this.captionBox);
Element.hide(this.prevLink);
Element.hide(this.nextLink);

var myImage = new Image();
myImage.onload = function()
{
photoArray[photoId][1]=this.width;
photoArray[photoId][2]=this.height;
this_global.effectiveSwap();
};
myImage.onerror = function(){ alert("failed to load big image.");};
myImage.src = photoArray[photoId][0];
},
effectiveSwap: function()
{
if(navigator.userAgent.indexOf("Opera")!=-1)  $(this.photo).onload = image_onload();			//Opera image onload bug
else $(this.photo).onload = image_onload;

this.setOverlayDiv();
this.setNewPhotoParams();
this.resizePhotoBox();
this.setPhotoCaption();
//this.showPhoto();

}
}


/*--------------------------------------------------------------------------*/
/* overlay div */
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

var xScroll, yScroll;

if (self.pageYOffset) {
yScroll = self.pageYOffset;
xScroll = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
yScroll = document.documentElement.scrollTop;
xScroll = document.documentElement.scrollLeft;
} else if (document.body) {// all other Explorers
yScroll = document.body.scrollTop;
xScroll = document.body.scrollLeft;
}

arrayPageScroll = new Array(xScroll,yScroll)
return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){

var xScroll, yScroll;

if (window.innerHeight && window.scrollMaxY) {
xScroll = window.innerWidth + window.scrollMaxX;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}

var windowWidth, windowHeight;

//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

if (self.innerHeight) {	// all except Explorer
if(document.documentElement.clientWidth){
windowWidth = document.documentElement.clientWidth;
} else {
windowWidth = self.innerWidth;
}
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}

// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

// for small pages with total width less then width of the viewport
if(xScroll < windowWidth){
pageWidth = xScroll;
} else {
pageWidth = windowWidth;
}
//	console.log("pageWidth " + pageWidth)

arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}


function showSelectBoxes(){
var selects = document.getElementsByTagName("select");
for (i = 0; i != selects.length; i++) {
selects[i].style.visibility = "visible";
}
}

// ---------------------------------------------------

function hideSelectBoxes(){
var selects = document.getElementsByTagName("select");
for (i = 0; i != selects.length; i++) {
selects[i].style.visibility = "hidden";
}
}

// ---------------------------------------------------

function showFlash(){
var flashObjects = document.getElementsByTagName("object");
for (i = 0; i < flashObjects.length; i++) {
flashObjects[i].style.visibility = "visible";
}

var flashEmbeds = document.getElementsByTagName("embed");
for (i = 0; i < flashEmbeds.length; i++) {
flashEmbeds[i].style.visibility = "visible";
}
}

// ---------------------------------------------------

function hideFlash(){
var flashObjects = document.getElementsByTagName("object");
for (i = 0; i < flashObjects.length; i++) {
flashObjects[i].style.visibility = "hidden";
}

var flashEmbeds = document.getElementsByTagName("embed");
for (i = 0; i < flashEmbeds.length; i++) {
flashEmbeds[i].style.visibility = "hidden";
}

}

function close_slideshow()
{
Effect.Fade('product_slideshow',{duration:0.2, afterFinish:function(){Effect.Fade('overlay_div',{duration:0.2});$('Photo').style.visibility='hidden';showSelectBoxes();showFlash();}})
}

/*--------------------------------------------------------------------------*/


function image_onload()
{

$('Photo').style.visibility='visible';
var myPhoto = new Slideshow(photoId);
myPhoto.showPhoto();

}

function load_next_photo()
{
var myPhoto = new Slideshow(photoId);
myPhoto.nextPhoto();
}

function load_prev_photo()
{
var myPhoto = new Slideshow(photoId);
myPhoto.prevPhoto();
}


