var _contentClosed=false;
var _introLoaded=false;
var _captionFaded=false;

var ss_text = [
	"Intelligent design exists and is paramount to an enduring and aesthetic landscape",
	"Listening to clients and drawing from our vast experience,<br />Sylvan Soul's designs harmonize and emphasize your property",
	"Attention to detail and the will to do it right!",
	"Experience, creativity, and working knowledge are the cornerstones of Sylvan Soul's designs",
	"Sylvan Soul has the expertise to make your landscape a showplace!"
	];

Effect.DelayedChain = Class.create();
Object.extend(Effect.DelayedChain.prototype, {
    initialize: function(effect, elements, options, timeout){
        this.elements = elements;
        this.effect = effect;
        this.timeout = timeout || 100;
        this.options = Object.extend({}, options || {});

        this.afterFinish = this.options.afterFinish || Prototype.emptyFunction;
        this.options.afterFinish = Prototype.emptyFunction;
        setTimeout(this.action.bind(this),1);
    },
    action: function() {
        if(this.elements.length){ 
            new Effect[this.effect](this.elements.shift(), this.options);
            setTimeout(this.action.bind(this), this.timeout);
        } else {
            if(this.afterFinish) this.afterFinish();
        }
    }
});

Effect.Chain = Class.create();
Object.extend(Effect.Chain.prototype, {
    initialize: function(effect, elements, options){
        this.elements = elements || [];
        this.effect = effect;
        this.options = options || {};
        this.afterFinish = this.options.afterFinish || Prototype.emptyFunction;
        this.options.afterFinish = this.nextEffect.bind(this);
        setTimeout(this.nextEffect.bind(this), 1);
    },
    nextEffect: function(){
        if(this.elements.length)
            new Effect[this.effect](this.elements.shift(), this.options);
        else
            this.afterFinish();
    }
});


window.onload = function(){
	//maximize();
	new Effect.Appear('header',{
		duration:2.7,
		afterFinish:function(){
			setTimeout("splash()",5000);
			/*new Effect.Fade('preheader',{
				duration:2.7,
				afterFinish:function(){
					new Effect.Appear('splash',{duration:2});
					setTimeout("splash()",5000);
				}
			});*/
		}});
	
	
	
}
var tout = null;

PeriodicalExecuter.prototype.registerCallback = function() {
	this.intervalID = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
}

PeriodicalExecuter.prototype.stop = function() {
	clearInterval(this.intervalID);
}

function maximize() {
    window.moveTo(0,0)
    window.resizeTo(screen.availWidth, screen.availHeight)
}

function setNav(){
	var links = $('nav').getElementsByTagName("A");
	for (var i = 0; i < links.length; i++) {
		links[i].onmouseover = function(){
			//this.style.cursor = 'pointer';
		};

		links[i].onmouseout = function(){
			//this.style.cursor = 'hand';
		}
		if(links[i].id=='restart'){
			links[i].onclick = function(){
				location.href='index.html';
			}
		}else{
			links[i].onclick = function(){
				clearTimeout(tout);
				loadContent(this.id, 'main');
			}
		}
	}
}

Position.getPageSize = function() {
  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    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;
  if (self.innerHeight) { // all except Explorer
    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
  pageHeight = Math.max(windowHeight, yScroll);

  // for small pages with total width less then width of the viewport
  pageWidth = Math.max(windowWidth, xScroll);

  return { page: { width: pageWidth, height: pageHeight }, window: { width: windowWidth, height: windowHeight } };
}

function loadContent(inc, div){
	if(!_introLoaded) return false;
	if(!_contentClosed){
		new Effect.Fade('content',{
			duration:0.7,
			afterFinish:function(){
				Element.remove('content');
				if(!_captionFaded){
					Effect.Fade('ss_caption');
					_captionFaded=true;
				}
				_contentClosed=true;
				var div = $(div) || $('main');
				var url = 'php/getcontent.php';
				var pars = 'inc=' + inc;
				var myAjax = new Ajax.Updater( div, url, { method: 'get', parameters: pars});
				new Effect.Appear('main',{queue:'end',afterFinish:function(){
						if(inc=="gallery"){
							loadGallery('hardscape');
						}
					} });
			}
		});
		
	}else{
		new Effect.Fade($('main'),
			{duration:0.7,
			afterFinish: function(){
				if(!_captionFaded){
					Effect.Fade('ss_caption');
					_captionFaded=true;
				}
				var div = $(div) || $('main');
				var url = 'php/getcontent.php';
				var pars = 'inc=' + inc;
				var myAjax = new Ajax.Updater( div, url, { method: 'get', parameters: pars }); 
				new Effect.Appear('main',{duration:1.7,queue:'end',
					afterFinish:function(){
						if(inc=="gallery"){
							loadGallery('hardscape');
						}
					}
				});
			}
			});
		
	}
}


function splash(){
	new Effect.Fade('splash',{duration:1.7});
	new Effect.Scale('panel',137, { 
		duration:1.2, queue:'end', scaleContent: false, scaleX: true, scaleY: false, 
		scaleMode: {originalHeight: 439, originalWidth: 565},
		afterUpdate:function(effect){
			//new Effect.MoveBy('welcome', 0,200);
		},
		afterFinish:function() { 
			Element.remove('intro');
			new Effect.Appear('welcome',{queue:'end'});
			new Effect.Fade('wait',{duration:3,
				afterFinish:function(){Element.remove('wait');}
			});
			tout = setTimeout ("loadSite()",3000);
			
		}
	});
}

function loadSite(){
	
	new Effect.Fade('welcome',{duration:1.7});
	new Effect.Scale('panel',87, { 
		duration:1.2, queue:'end', scaleContent: false, scaleX: false, scaleY: true, 
		scaleMode: {originalHeight: 439, originalWidth: 565},
		afterUpdate:function(effect){	
		},
		afterFinish:function() { 
			_introLoaded=true;
			Element.remove('welcome');
			new Effect.Appear('content',{queue:'end'});
			new Effect.Appear('ss_caption');
			
			new Effect.Appear('footer');
			setNav();
			new Effect.Highlight('body',{
			  duration: 0.8,
			  restorecolor:'#FFFF66',startcolor:'#ffffff',endcolor:'#ffff66' 
			});
			 setTimeout('start_slideshow(1,4,3000)',500);
		}
	});
}

function start_slideshow(start_frame, end_frame, delay) {
	tout = setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
}

var frame = 1;
var div ={};

function appear(frame){
	/*var frm = '#content .slideshow' + frame;
	var css = $$(frm, '#ss_caption');
	new Effect.Chain(
		   'Appear', // The effect name
		    css,  // an array of elements
		   { 
			   duration: 0.5,
			   afterFinish:function(){
				   //div.innerHTML=ss_text[(parseInt(frame)-1)];
			   }
			} 
		);*/
	var div = 'slideshow' + frame;
	new Effect.Appear(div, {duration:0.5, afterFinish:function(){
		new Effect.Appear('ss_caption', {
			beforeStart:function(){
				var cp = $('ss_caption');
				cp.innerHTML = ss_text[frame-1];
			}
		});
	}
	});
	
}

function fade(frame){
	var div = 'slideshow' + frame;
	new Effect.Fade(div);
	new Effect.Fade('ss_caption');
}
						
function switch_slides(frame, start_frame, end_frame, delay) {
	return (function() {
		/*var frm = '#content #slideshow' + frame;
		var css = $$(frm, '#ss_caption');
		//Effect.Fade(css);
		new Effect.Chain(
		   'Fade', // The effect name
		    css,  // an array of elements
		   { duration: 0.5,
			   afterFinish:function(){
					div = $('ss_caption');
					div.innerHTML = ss_text[frame-1];
				} 
		   }
		);*/

		fade(frame);
		if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
		
		//tout = setTimeout("Effect.Appear('slideshow" + frame + "',{afterFinish:function(){div.innerHTML=ss_text[" + (parseInt(frame)-1) + "]}});", 850);
		tout = setTimeout("appear(" + frame + ")", 850);
		tout = setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + 4250);
	})
}

var Timer;

function ScrollLeft(){
  Timer = setInterval("$('gallery_nav').scrollLeft -= 2", 15);
}

function ScrollRight(){
  Timer = setInterval("$('gallery_nav').scrollLeft += 2", 15);
}

function loadGallery(cat){
	var div = $('gallery_nav');
	div.innerHTML = "<p>Please wait... We're loading the gallery</p>";
	var url = 'php/gallery.php';
	var pars = 'cat=' + cat;
	var myAjax = new Ajax.Updater( div, url, { method: 'get', parameters: pars }); 
	//new Effect.Appear('main',{duration:1.7,queue:'end'});
}

var popup = null;

function pop(gal,img){
	if(popup){
		popup.close();
		popup = null;
	}

	var pic = new Image();
	pic.src = "../images/gallery/" + gal + "/" + img;
	var h = pic.height;
	var w = pic.width;
	var height = (h)?(h + 20):600;
	var width = (w)?(w + 20):800;
	var params = "width=" + width + ",height=" + height + ",scrollbars=no,toolbars=no,resize=no,statusbar=no";
	popup = window.open(pic.src,'',params);
}


