$(function(){
	Orchids.init();	
});






var Orchids = {
	init: function(){
		var that = this,
			header = $('#header'),
			footer = $('#footer'),
			mapAddress = $('#mapAddress');
		
		if ($('feat-fader')){
			that.loadFader();
		}	
		
		$('.modalit').modalit();
				
		that.loadTemplate('header', header, function(){
			$('#contactBtn, #visitgreen').click(function(){
				console.log('click');
				$('html,body').animate({
					scrollTop: $('#footer').offset().top
				}, 2000)
			});
		});
		
		that.loadTemplate('footer', footer, function(){
			$('#mapAddress').click(function(){
				that.openMap();
			});
			
			$('#closegmap').click(function(){
				that.closeMap();
			});
			
			// Get value of user's starting address
			$('#getDirections a').click(function(){
				var d = $('#getDirections input').val();
				if(d =='' || d==null){
					return false
				}else{
					console.log(d);
					that.getDirections();
				}
			});
		});
				
		if ($('#orchids-menu')){
			that.loadTemplate('orchids-menu', $('#orchids-menu'));
		}
		
	},
	
	// function to load templates, "template" param is the file to load,
	// "container" param is the location to load it into
	loadTemplate: function(template, container, callback){
		$.ajax({
			url: "template/"+template+".html",
			type: "GET",
			dataType: "HTML",
			success: function(response, textStatus, jqXHR){
				var data = response;
				//console.log(data);
				container.html(data);
				
				if (typeof callback == 'function') {
					callback();
				}
			}
		});
	},
	
	loadFader: function(){
		var faderItem = $('#feat-fader ul li');
		$('#feat-fader ul li:not(:first)').hide();
		    faderItem.css('position', 'absolute');
		    faderItem.css('top', '0px');
		   // faderItem.css('left', '50%');
		   /* faderItem.each(function() {
		        var li = $(this);
		        $('<img>').attr('src', $(this).attr('src')).load(function() {
		            li.css('margin-left', -this.width / 2 + 'px');
		        });
		    });
			*/
		    var pause = false;
		    
		    function fadeNext() {
		        $('#feat-fader ul li').first().fadeOut().appendTo($('#feat-fader ul'));
		        $('#feat-fader ul li').first().fadeIn();
		    }
		
		    function fadePrev() {
		        $('#feat-fader ul li').first().fadeOut();
		        $('#feat-fader ul li').last().prependTo($('#feat-fader ul')).fadeIn();
		    }
			/*
		    $('#feat-fader, #next').click(function() {
		        fadeNext();
		    });
		
		    $('#prev').click(function() {
		        fadePrev();
		    });
		
		    $('#feat-fader, .button').hover(function() {
		        pause = true;
		    },function() {
		        pause = false;
		    });
			*/
		    function doRotate() {
		        if(!pause) {
		            fadeNext();
		        }    
		    }
		    
		    var rotate = setInterval(doRotate, 3000);
	}
		
}
/*
$.fn.image = function(src, callback){
	return this.each(function(){
		var i = new Image();
		console.log(i);
		i.src = src;
		this.appendChild(i);
		
		if (typeof callback == 'function') {
			callback();
		}
	});
}
*/
/*
$.fn.modalit = function(){
	return this.each(function(){
		$(this).click(function(e){
			var url = $(this).attr("href"),
				image = $('<img />');
  			
  			$('<div id="modalit-curtain"></div><div id="modalit" class="loading"><span class="modalit-close"></span></div>').appendTo('body');
  			
  			$('#modalit-curtain').fadeIn();
  			$('#modalit').fadeIn();
  			$('#modalit .modalit-close').click(function(){
  				$('#modalit-curtain').remove();
  				$('#modalit').remove();
  			});
  			
			  image.attr('src', url)
			  	.load(function () {
			    	$('#modalit').removeClass('loading').append($(this));
			    })
			    .error(function () {
			      // notify the user that the image could not be loaded
			      console.log('not loaded');
			    });
			 
			    e.preventDefault();
		});
	});
}*/
