// stoo slideShow 1.1
// (c)2006 Stoo - http://www.glitch.co.nz
//
// Credit where credit is due: Inspiration from JonDesign's SmoothGallery (http://www.jondesign.net/)
// and of course Valerio's mootools (http://mootools.net/)
// --------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------


// ---------------------------------------------------
// variables
var screenW = (screen.width>1024)?'1280':'1024';

var fastSpeed=200;
var slowSpeed=1000;
var newdir=null;
var busy=0;

var currLayer = null;
var currDir = null;
var scroller  = null;
var scrollbar = null;

var slideMsg = null;
var myGallery = null;

// ---------------------------------------------------
// bac image
function getBac() {

	var path = '../imgs/weddings/bac';
	new Ajax('php/bacImage.php',{ postBody:'path='+path, onComplete:showBac }).request();
}
function showBac(request) {
	//alert(request);
	var img = ( screenW>1024 )? request : 'resizer.php?imgfile=' + request + '&max_width=' + screenW;
	$('bac').setStyles("background-image: url('"+img+"')");
}
// ---------------------------------------------------
// gallery
function getGallery(dir) {

	var path = '../imgs/weddings/'+dir;
	//var dirsize = '1024';
	new Ajax('php/galleryImages.php',{ postBody:'path='+path+'/'+dirsize, update:'gallery', onComplete:showGallery }).request();
	
	//$('bac').setStyle('opacity',0);
	var contHide = new Fx.Style('weddings', 'opacity');
	contHide.start(1,0);
	
	var loadShow = new Fx.Style('loading', 'opacity');
	loadShow.start(0,1);
}

function showGallery() {

	myGallery = new gallery($('gallery'), {
		showInfopane: false,
		embedLinks: false
		//useThumbGenerator: true
	});
	
	var loadHide = new Fx.Style('loading', 'opacity');
	loadHide.start(1,0);
	
	var galShow = new Fx.Style('galleries', 'opacity');
	galShow.start(0,1);
	//$('galleries').style.display = 'block';
}

function hideGallery() {
	//myGallery = null;
	var loadShow = new Fx.Style('loading', 'opacity');
	loadShow.start(0,1);
	
	var galHide = new Fx.Style('galleries', 'opacity');
	galHide.start(1,0);
	$('galleries').style.display = 'none';
	
	var contShow = new Fx.Style('weddings', 'opacity');
	contShow.start(0,1);
	
	var loadHide = new Fx.Style('loading', 'opacity');
	loadHide.start(1,0);
}

function gotoURL(url){
	//var url = 'weddings.php#photos';
	document.location = url;
}


// ---------------------------------------------------
// layers
function hideLayer(lyr) {
	
	var fadeOut = $(lyr).effect('opacity', {duration: fastSpeed });
	fadeOut.start(1,0);
	var fadeOut2 = $('Scrollbar-Container').effect('opacity', {duration: fastSpeed });
	fadeOut2.start(1,0);
	currLayer = null;
}

function showLayer(lyr) {

	if (currLayer==lyr) { // toggle button
		hideLayer(currLayer); 
		
	} else {
		if (currLayer) { hideLayer(currLayer); /*$(currLayer).setOpacity(0);*/ }
		currLayer = lyr;
		
		if (lyr=='photos') { // no scroll
		
			var fadeIn = $(lyr).effect('opacity', {duration: slowSpeed });
			fadeIn.start(0,1);
			var fadeOut = $('Scrollbar-Container').effect('opacity', {duration: fastSpeed });
			fadeOut.start(1,0);
			
		} else {
		
			var fadeIn = $(lyr).effect('opacity', {duration: slowSpeed });
			fadeIn.start(0,1);
			var fadeIn2 = $('Scrollbar-Container').effect('opacity', {duration: slowSpeed });
			fadeIn2.start(0,1);
			
			var top = $(lyr);
			scrollbar.swapContent(top);
			if (top.y) scrollbar.scrollTo(0, top.y);
		}
		
		var printHTML = '<h2>Wedding ' + lyr + '</h2>';
		$('nav_print').setHTML(printHTML);
	}
  
	return false;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

// ---------------------------------------------------
// scrolling area
function scrollbarEvent (o, type) {
	if (type == "mousedown") {
		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "";
		else o.style.backgroundColor = "#000";
	} else {
		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "";
		else o.style.backgroundColor = "#fff";
	}
}

// ---------------------------------------------------
// submit form 
function submitContactForm() {
	
	//slideMsg = new Fx.Slide('error-msg', { duration:fastSpeed });
	//slideMsg.toggle();
	
	$('contactForm').send({ onComplete:this.showMessage });

	var submitHTML='<img src="imgs/nav/loading.gif" alt="loading" />';
	$('contact-Btns').setHTML(submitHTML);
}
           
function showMessage(request) {
	//alert('r:'+	request);
	$('error-msg').setHTML(request);
	
	//scrollbar.reset();
	var top = $('contact');
	scrollbar.swapContent(top);
	if (top.y) scrollbar.scrollTo(0, top.y);
	//slideMsg.toggle();
	
	if ( $('thanks') ) {
	
		hideTheForm();
		
	} else {  
	
		var submitHTML='<a href="#send" onclick="submitContactForm();"><img src="imgs/nav/sendW.png" srcover="imgs/nav/sendW2.png" alt="send" /></a>';
   		$('contact-Btns').setHTML(submitHTML);
   	}
}

function hideTheForm() {
	
	//Element.hidden('contact-Btns');
	//Element.hidden('formFields');
	hideForm = new Fx.Slide('formFields', { duration:fastSpeed }); 
	hideForm.hide();
	hideBtns = new Fx.Style('contact-Btns', 'opacity', { duration:fastSpeed }); 
	hideBtns.hide();
	$('Scrollbar-Container').setOpacity(0);
	
}

// ---------------------------------------------------
// mouseHandler for nested divs

function isMouseLeaveOrEnter(e, handler) {	

	if (e.type != 'mouseout' && e.type != 'mouseover') return false;
	var reltg = e.relatedTarget ? e.relatedTarget :
	e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler) reltg = reltg.parentNode;
	return (reltg != handler);
}

Fx.Styles.implement({
    imgrollover: function(w,h){
        this.clearTimer();
        //var pos = this.getPosition();
        var newW = parseInt(w*1.3);
        var newH = parseInt(h*1.3);
        var newT = parseInt( (newH - h)/2 );
        var newL = parseInt( (newW - w)/2 );
        this.custom({ top: [0,-newT], left: [0,-newL], width: [w, newW], height: [h, newH] });
    },
    imgrollout: function(w,h){
        this.clearTimer();
        var newW = parseInt(w*1.3);
        var newH = parseInt(h*1.3);
        var newT = parseInt( (newH - h)/2 );
        var newL = parseInt( (newW - w)/2 );
        this.custom({ top: [-newT,0], left: [-newL,0], width: [newW,w], height: [newH,h] });
	},
	txtrollover: function(w){
        this.clearTimer();
        var newW = parseInt(w*1.3);
        var newL = parseInt( (newW - w)/2 );
        this.custom({ left: [0,-newL], width: [w, newW] });
        
    },
    txtrollout: function(w){
        this.clearTimer();
        var newW = parseInt(w*1.3);
        var newL = parseInt( (newW - w)/2 );
        this.custom({ left: [-newL,0], width: [newW,w] });
	}
});

// ---------------------------------------------------
// init
function run() {
	//Window.disableImageCache(); // Trying to disable the imageCache to fix IE6 issues
	
	getBac();
	domRollover();
	
	// load scroll
	scroller  = new jsScroller($('intro'), 400, 180);
	scrollbar = new jsScrollbar ($('Scrollbar-Container'), scroller, true, scrollbarEvent);
	scrollTween = new jsScrollerTween(scrollbar, true);
	
	// thumbs
	var els = $$('.thumbs');
	var imgs = new Array();
	var txt = new Array();
	var imgfx = new Array();
	var txtfx = new Array();
	//alert (els.length);
	for (var i=0; i<els.length; i++){
		els[i].id = 'th'+i;
		
		imgs[i] = $E('img', els[i]);
		txt[i] = $E('p', els[i]);
		els[i].txt = txt[i];
		//alert(txt[i].firstChild.nodeValue);
		var pos = imgs[i].getCoordinates();
		imgfx[els[i].id] = imgs[i].effects({ duration: 100 });
		txtfx[els[i].id] = txt[i].effects({ duration: 100 });
		//var self = this;
		els[i].addEvent('mouseover', function(){ 
			imgfx[this.id].imgrollover(pos.width,pos.height); 
			txtfx[this.id].txtrollover(pos.width); 
			this.style.zIndex = 150;
			this.txt.setOpacity(1);
			this.txt.style.display = 'inline';
		});
		els[i].addEvent('mouseout', function(){ 
			imgfx[this.id].imgrollout(pos.width,pos.height); 
			txtfx[this.id].txtrollout(pos.width); 
			this.style.zIndex = 0;
			this.txt.setOpacity(0);
			this.txt.style.display = 'none';
		});
		
	}
	
	// show layer if anchor is in url
	var anchorArr = document.URL.split('#');
	if (anchorArr[1]) { showLayer(anchorArr[1]); }
	
}

Window.onDomReady(run);

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