window.addEvent('domready', init);
var galleryList;

function init(){
    currentContent = $('index');
    setContent('index');


    getGalleryList();

    // add email link
    var contact = $('contactEmail');
    var addr = 'contact' + '@' + 'covertcreativegroup.com';
    contact.set('href', "mailto: " + addr);
    contact.appendText(addr);

}

function setContent(page){
    var pageDiv = $(page);
    if (!pageDiv) return;

    if (pageDiv.getStyle('top') == '20px') return;

    var scroll1 = new Fx.Morph(currentContent, {duration: 'long'});
    var scroll2 = new Fx.Morph(pageDiv, {duration: 'long'});

    pageDiv.setStyles({top: '500px'});
    
    scroll1.start({top: '-500px'});
    scroll2.start({top: '20px'});

    currentContent = pageDiv;

    return false;
}

function openMenu(page){
    level1s = $$('div.menuLevel1');
    level1s.each(function(level1){
	if (level1.id == page){
	    level1.getFirst().setStyles({'color': '#385427',
					 'font-weight': 'bold'});
	}
	else {
	    level1.getFirst().setStyles({'color': '#999',
					 'font-weight': 'normal'});
	}

	level2s = level1.getElements('div.menuLevel2');
	level2s.each(function(level2){
	    if (level1.id == page){
		switch(page){
		case 'biosMenu':
		    var height = 70;
		    break;
		case 'videoMenu':
		    var height = 35; // 105
		    break;
		case 'stillMenu':
		    var height = 35;
		    break;
		}
		level2.tween('height', height);
	    }
	    else {
		level2.tween('height', 0);
	    }
	});
    }
		);


}

function getGalleryList(){
    var url = "getGalleryList.php";
    var req = new Request.JSON({url: url,
				method: 'get',
				onComplete: function(message){
				    if (message.status == 'success'){
					galleryList = message.data;
					var thumbDiv = $('galleryThumbs');
					galleryList.each(function(item, index){
					    var wRatio = item.thumbWidth / 100;
					    var hRatio = item.thumbHeight / 100;
					    if (wRatio > hRatio){
						var width = 100;
						var height = item.thumbHeight / wRatio;
					    }
					    else {
						var height = 100;
						var width = item.thumbWidth / hRatio;
					    }
					    
					    var img = new Element('img', {src: item.thumb});
					    img.setStyles({'width': width,
							   'height': height});
					    
					    thumbDiv.adopt(new Element('div', {id: 'gal_' + index, 'class': 'galleryThumb'})
							   .adopt(img)
							   .addEvent('mouseenter', function(){
							       this.setStyle('border', '1px solid #fff');
							   })
							   .addEvent('mouseleave', function(){
                                                               this.setStyle('border', '1px solid #000');
                                                           })
							   .addEvent('click', showGalleryImage)
							  );
					    
					});
				    }
				}
			       });
    req.send(null);
}

function showGalleryImage(){
    var image = galleryList[this.id.substring(4)];
    
    var imageDiv = $('galleryImage');
    imageDiv.empty();
    imageDiv.adopt(new Element('img', {src: image.src}));
}