function qmPage() {
    this.numImagesMain = 7;
		this.numImagesNow = 0;
		this.images = new Array();
		this.cached = new Array();
		if(document.location.pathname == '/' || document.location.pathname == '/index.php') {
        this.sitePart = 'main';
    }
    else {
        this.sitePart = 'inner';
    }
		setTimeout('qmPage.construct();', 0);
    this.interval = false;
    this.intCount = 0;
}

qmPage.prototype.construct = function(direction) {
    for(i=0; i<this.numImagesMain; i++) {
		    this.images[i] = new Image;
		    this.images[i].src = '/images/splash_'+this.sitePart+'_'+i+'.jpg';
		    this.cached[i] = false;
		}
		img = new Image();
		img.src = '/images/loading.gif';
}

qmPage.prototype.pager = function(direction) {
    var holder = document.getElementById('image');
		if(!holder) {
		    return false;
		}
		this.numImagesNow = this.numImagesNow + (direction > 0 ? 1 : -1);
		if(this.numImagesNow < 0) {
		  this.numImagesNow = this.numImagesMain - 1;
		}
		if(this.numImagesNow >= this.numImagesMain) {
		  this.numImagesNow = 0;
		}
		var newImg = '/images/splash_'+this.sitePart+'_' + this.numImagesNow + '.jpg';
		var img = new Image();
		img.onload = function() {
		    qmPage.pagerMainImage(newImg);
				qmPage.cached[qmPage.numImagesNow] = true;
		}
		this.pagerMainLoader();		
		img.src = newImg;
}

qmPage.prototype.pagerMainLoader = function() {
    document.getElementById('loader').style.display = 'block';
}

qmPage.prototype.pagerMainImage = function(img) {
//    document.getElementById('image').style.backgroundImage = img;
    document.getElementById('image').style.background = 'transparent url(' + img + ') no-repeat scroll center';
    document.getElementById('loader').style.display = 'none';
}

qmPage.prototype.photoGetImages = function() {
    var holder = document.getElementById('photoList');
    var els = holder.getElementsByTagName('img');
    if(els.length < 1) return false;
    var imgList = new Array();
    for(i=0,j=els.length; i<j; i++) {
        imgList.push(els[i]);
    }
    return imgList;
}

qmPage.prototype.photoGetWidth = function() {
    var els = this.photoGetImages();
    var width = 0; 
    for(i=0,j=els.length; i<j; i++) {
        width += els[i].width + 7;
    }
    return width;
}

qmPage.prototype.photoTestWidth = function() {
    var width = this.photoGetWidth(who);
    var holder = document.getElementById('photoList');
    if(width < holder.offsetWidth) {
        document.getElementById('photoMoveLeft').style.display = 'none';
        document.getElementById('photoMoveRight').style.display = 'none';
        holder.style.width  = holder.offsetWidth + 'px';
        holder.style.margin = '0';
    }
}

qmPage.prototype.photoRePage = function(direction) {
    step = 20;
    var holder = document.getElementById('photoListSlider');
    if(holder.style.marginLeft) {
        var numeric = holder.style.marginLeft.replace('px','');
    }
    else {
        var numeric = 0;
    }
    if(direction < 0 && numeric >= 0) {
        document.getElementById('photoMoveLeft').style.display = 'none';
        return false;
    }
    else if(direction > 0 && numeric <= step) {
        document.getElementById('photoMoveLeft').style.display = 'block';
    }
    var holderWidth = document.getElementById('photoList').offsetWidth;
    var sliderWidth = this.photoGetWidth();
    if(direction > 0 && holderWidth - numeric >= sliderWidth) {
        document.getElementById('photoMoveRight').style.display = 'none';
        return false;
    }
    else if(direction < 0 && holderWidth - numeric <= sliderWidth) {
        document.getElementById('photoMoveRight').style.display = 'block';
    }
    var res = numeric - 0 - direction * step;
    if(res < holderWidth - sliderWidth) {
        res = holderWidth - sliderWidth;
    }
    holder.style.marginLeft =  res + 'px';
    if(this.intCount > -1) {
        if(this.intCount == 3) {
            clearInterval(this.interval);
            this.interval = setInterval('qmPage.photoRePage('+direction+');', 150);
        }
        if(this.intCount == 6) {
            clearInterval(this.interval);
            this.interval = setInterval('qmPage.photoRePage('+direction+');', 100);
        }
        if(this.intCount == 10) {
            clearInterval(this.interval);
            this.interval = setInterval('qmPage.photoRePage('+direction+');', 50);
        }
        this.intCount++; 
    }
}

qmPage.prototype.photoInterval = function(direction) {
    if((direction == 0) || (this.interval != false)) {
        clearInterval(this.interval);
        this.interval = false;
        return false;
        this.intCount = -1;
    }
    this.interval = setInterval('qmPage.photoRePage('+direction+');', 300);
    this.intCount = 0;
}

qmPage.prototype.photoChange = function(who, path) {
//    var newSrc = path.replace('max=100', 'max=600');
    var newSrc = path.replace('small', 'big');
    var img = new Image();
    img.src = newSrc;
    document.getElementById('photoMainImg').style.display = 'none';
    document.getElementById('photoMain').style.height = '400px';
    document.getElementById('photoMainName').innerHTML = '';
    img.onload = function() {
        document.getElementById('photoMain').style.height = '';
        document.getElementById('photoMainImg').src = newSrc;
        document.getElementById('photoMainImg').style.display = 'block';
        document.getElementById('photoMainName').innerHTML = document.getElementById('photoComment_'+who).innerHTML;
    }
}

qmPage.prototype.showMap = function(address) {
    if(typeof this.map == 'undefined') {
	this.map = new GMap2(document.getElementById('hotelMap'));
        this.geocoder = new GClientGeocoder();
    }
    this.geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
            document.getElementById('hotelMap').style.display = 'none';
	} else {
    	    qmPage.map.setCenter(point, 16);
            var marker = new GMarker(point);
            qmPage.map.addOverlay(marker);
//            marker.openInfoWindowHtml(address);
        }
      }
    );
}

var qmPage = new qmPage();
