﻿function PhotoGallery(url,size,count,pId) {
    this.url            = url;
    this.numPhotos      = count * 1;
    this.photoIdx       = pId * 1;
    this.size           = size;
	this.photoDiv       = document.getElementById('photoDiv');
	this.statusDiv 		= document.getElementById('statusDiv');
	this.photo 			= document.getElementById('photo');
    this.preLoad        = new Image();
    this.nextLoad       = new Image();
	this.ssi         = null;
}
PhotoGallery.prototype.startSlideshow = function(speed) {
    clearInterval(this.ssi);
    var self = this;
    this.ssi = setInterval(function() { self.nextSlide() }, speed);
}
PhotoGallery.prototype.stopSlideshow = function() {
    clearInterval(this.ssi);
}
PhotoGallery.prototype.nextSlide = function() {
    this.photoIdx = this.photoIdx + 1 == this.numPhotos ? 0 : this.photoIdx + 1;
    this.preLoad.src = this.url + this.photoName(this.photoIdx);
    this.display();
}
PhotoGallery.prototype.next = function() {
    clearInterval(this.ssi);
    this.photoIdx = this.photoIdx + 1 == this.numPhotos ? 0 : this.photoIdx + 1;
    this.load();
}
PhotoGallery.prototype.prev = function() {
    clearInterval(this.ssi);
    this.photoIdx = this.photoIdx - 1 <= -1 ? this.numPhotos - 1 : this.photoIdx - 1;
    this.load();
}
PhotoGallery.prototype.load = function() {
    var self = this;
    this.preLoad.onload = function() { self.display() };
    this.preLoad.src = this.url + this.photoName(this.photoIdx);
}
PhotoGallery.prototype.display = function() {
    this.photoDiv.style.width  = this.preLoad.width + 'px';
    this.photoDiv.style.height = this.preLoad.height + 'px';
    this.statusDiv.className = '';
    this.statusDiv.innerHTML = ((this.photoIdx + 1)+' / '+(this.numPhotos));
    this.photo.src = this.preLoad.src;
    this.nextLoad.src = this.url + this.photoName(this.photoIdx + 1);
    document.getElementById('idx' + this.photoIdx).className = 'on';
}
PhotoGallery.prototype.photoName = function(idx) {
    return ((idx + 1) < 10) ? '0' + (idx + 1) + this.size + '.jpg' : (idx + 1) + this.size + '.jpg';
}
function VirtualTour(container) {
    this.vt1 = document.getElementById('vt1');
    this.vt2 = document.getElementById('vt2');
    this.vt3 = document.getElementById('vt3');
    this.width = 0;
    this.vtleft = 0;
    this.polarity = 1;
    this.delay = 15;
    this.ppi = 1;
    this.interval = null;
    
    var self = this;
    container.onmousemove = function(event) { self.mouseEvent(event); };
}
VirtualTour.prototype.scroll = function() {
    this.vt1.style.left = this.vtleft + 'px';
    this.vt2.style.left = (this.vtleft + (this.width * (this.polarity * -1))) + 'px';
    this.vt3.style.left = (this.vtleft + (this.width * this.polarity)) + 'px';
    this.vtleft += this.polarity * this.ppi;
    if(this.vtleft <= (this.width * -1) || this.vtleft >= this.width)
        this.vtleft = 0;
}
VirtualTour.prototype.stop = function() {
    clearInterval(this.interval);
}
VirtualTour.prototype.loadHandler = function() {
    this.width = this.vt1.width;
    this.vtleft = 0;
    this.polarity = 1;
    this.ppi = 1;
    
    var self = this;
    this.interval = setInterval(function() { self.scroll(); },this.delay);
}
VirtualTour.prototype.start = function(src) {
    var self = this;
    this.vt1.onload = function() { self.loadHandler(); }
    this.vt3.src = this.vt2.src = this.vt1.src = src;
}
VirtualTour.prototype.mouseEvent = function(event) {
    var e;
    if(window.event)
        e = window.event;
    else
        e = event;
    var pos = Math.ceil(e.clientX - (screen.width / 2));
    this.ppi = Math.round((Math.abs(pos) / 180) * (Math.abs(pos) / 180));
    if(pos < -100)
        this.polarity = 1;
    if(pos > 100)
        this.polarity = -1;
}
function VirtualTourGallery(url,size,count,pId) {
    this.url            = url;
    this.size           = size;
    this.numPhotos      = count * 1;
    this.photoIdx       = pId * 1;
	this.photoDiv       = document.getElementById('vtDiv');
	this.statusDiv 		= document.getElementById('statusDiv');
    this.preLoad        = new Image();
    this.nextLoad       = new Image();
    this.vt             = new VirtualTour(this.photoDiv);
}
VirtualTourGallery.prototype.next = function() {
    this.photoIdx = this.photoIdx + 1 == this.numPhotos ? 0 : this.photoIdx + 1;
    this.load();
}
VirtualTourGallery.prototype.prev = function() {
    this.photoIdx = this.photoIdx - 1 <= -1 ? this.numPhotos - 1 : this.photoIdx - 1;
    this.load();
}
VirtualTourGallery.prototype.load = function() {
    var self = this;
    this.preLoad.onload = function() { self.display(); };
	this.preLoad.src = this.url + this.photoName(this.photoIdx);
}
VirtualTourGallery.prototype.display = function() {
    var windowWidth = this.windowWidth();
    var w = (this.preLoad.width - 120) > (windowWidth - 120) ? (windowWidth - 120) : (this.preLoad.width - 120);
    
    this.photoDiv.style.width = w + 'px';
    this.photoDiv.style.height = this.preLoad.height + 'px';

    this.nextLoad.src = this.url + this.photoName(this.photoIdx + 1);
    this.statusDiv.className = '';
    this.statusDiv.innerHTML = ((this.photoIdx + 1)+' / '+(this.numPhotos));
    document.getElementById('idx' + this.photoIdx).className = 'on';
    
    this.vt.stop();
    this.vt.start(this.preLoad.src);
}
VirtualTourGallery.prototype.photoName = function(idx) {
    return ((idx + 1) < 10) ? '0' + (idx + 1) + this.size + '.jpg' : (idx + 1) + this.size + '.jpg';
}
VirtualTourGallery.prototype.windowWidth = function() {
    if(window.innerWidth)
        return window.innerWidth;
    else if (document.all)
        return document.body.clientWidth;
}
VirtualTourGallery.prototype.stopSlideshow = function() {
}

function VideoGallery(url,count) {
    this.url            = url;
    this.numPhotos      = count * 1;
    this.photoIdx       = 0;
	this.container       = 'videoDiv';
}
VideoGallery.prototype.load = function() {
	var FO = { movie:"flvplayer.swf",width:"640",height:"480",majorversion:"7",build:"0",bgcolor:"#FFFFFF",flashvars:"file=" +  this.url + this.videoName(this.photoIdx) + "&autostart=true"};
	UFO.create(FO, this.container);
}
VideoGallery.prototype.next = function() {
    this.photoIdx = this.photoIdx + 1 == this.numPhotos ? 0 : this.photoIdx + 1;
    this.load();
}
VideoGallery.prototype.prev = function() {
    this.photoIdx = this.photoIdx - 1 <= -1 ? this.numPhotos - 1 : this.photoIdx - 1;
    this.load();
}
VideoGallery.prototype.stopSlideshow = function() {
}
VideoGallery.prototype.videoName = function(idx) {
    return ((idx + 1) < 10) ? '0' + (idx + 1) + '.flv' : (idx + 1) + '.flv';
}


function MediaGallery() {
    this.pGal   = new PhotoGallery(base_url + 'standard/',size,num_photos,index);
    this.vtGal  = new VirtualTourGallery(base_url + 'virtual_tour/',size,num_vts,index);
    this.vidGal = new VideoGallery(base_url + 'video/',num_videos);
    this.curGal = null;
    this.statusDiv = document.getElementById('statusDiv');
    this.numberedList = document.getElementById('numberedList');
}
MediaGallery.prototype.next = function() {
    this.setLoadingStatus();
    this.curGal.next();
}
MediaGallery.prototype.prev = function() {
    this.setLoadingStatus();
    this.curGal.prev();
}
MediaGallery.prototype.initVideoTour = function() {
    this.toggleDiv('videoDiv');
    this.toggleTab('videoTab');
    this.curGal = this.vidGal;
    this.load();
}
MediaGallery.prototype.initPhotoTour = function() {
    this.toggleDiv('photoDiv');
    this.toggleTab('photoTab');
    this.curGal = this.pGal;
    this.load();
}
MediaGallery.prototype.initVirtualTour = function() {
    this.toggleDiv('vtDiv');
    this.toggleTab('vtTab');
    this.curGal = this.vtGal;
    this.load();
}
MediaGallery.prototype.toggleDiv = function(div) {
    document.getElementById('photoDiv').style.display = 'none';
    document.getElementById('vtDiv').style.display = 'none';
    document.getElementById('videoDiv').style.display = 'none';  
    document.getElementById(div).style.display = 'block';
}
MediaGallery.prototype.toggleTab = function(tab) {
    document.getElementById('photoTab').className = '';
    if(document.getElementById('vtTab'))
        document.getElementById('vtTab').className = '';
    if(document.getElementById('videoTab'))
        document.getElementById('videoTab').className = '';
    document.getElementById(tab).className = 'on';
}
MediaGallery.prototype.load = function() {
    this.setLoadingStatus();
    this.loadNumberedList();
    this.curGal.load();
}
MediaGallery.prototype.setLoadingStatus = function() {
    this.statusDiv.className = 'loading';
    this.statusDiv.innerHTML = 'Loading Media...';
}
MediaGallery.prototype.loadNumberedList = function() {
    this.numberedList.innerHTML = '';
    for(var i = 0;i < this.curGal.numPhotos;i++) {
        this.numberedList.innerHTML += '<a id="idx' + i + '" href="#' + i + '" onclick="mGal.loadIndex(' + i + ');">' + (i + 1) + '</a>';
    }
}
MediaGallery.prototype.loadIndex = function(idx) {
    this.curGal.stopSlideshow();
    this.curGal.photoIdx = (idx * 1);
    this.setLoadingStatus();
    this.curGal.load();
}