// Default JS Content - Do Not Delete

$(document).ready(function() {
	$('#newsletterapp').load('/i/SITE_090210_09180337_N8I1W/newsletter.cfm', function() {
		$('.ajax').css({'display':'none'});
	});
});


$(document).ready(function(){
	if($('#backgroundColor').text()!=''){
		$(document.body).css('background',$('#backgroundColor').text());
		$(document.body).css('background','#'+$('#backgroundColor').text());
	}
	var url = document.location.href;
	url = url.replace(/\?.*/,'');
	
	$('.signupComponent').each(function(){
		var $f = $(this);
		$f.html('<form>'
			+'<input placeholder="E-mail Address" id="signupEmail">'
			+'<input type="submit" value="Sign Up">'
			+'</form>'
		);
		$('input[placeholder]').placeholder();
		/* Save the Data when the form is submitted */
		$f.find('form').submit(function(){
			var email = $('#signupEmail').val();
			if(!email.match(/@/)){
				alert('Please enter a valid e-mail address');
				return false;
			}
			/* Actual ajax call to save the data */
			$.ajax({
				url: '/connection/i/SITE_100831_16303545_YN1UH/form.cfc',
				data: {
					method: 'signup',
					email: email
				},
				success:function(ret){
					ret = eval(ret);
					alert(ret);
					$('#signupEmail').val('');
				}
			});
return false;
		});
		
	});
	/* Don't do any of this in the CMS */
	if(url.match(/hands-on/i))
		return true;
	/* Sign up and Forward Events */
	$('.signup').click(function(){
		$('#forwardForm').stop(false,true).slideUp();
		var $t = $(this);
		var $f = $('#signupForm');
		/* Create the form if it's not already created */
		if($f.length==0){
			$(document.body).prepend('<div id="signupForm" class="popupForm"></div>');
			$f = $('#signupForm');
			$f.position({
				of: '.signup',
				my: 'left top',
				at: 'left bottom'
			}).hide();
			$f.html('<form>'
				+'<input placeholder="E-mail Address" id="signupEmail">'
				+'<input type="submit" value="Sign Up">'
				+'</form>'
			);
			$('input[placeholder]').placeholder();
			/* Save the Data when the form is submitted */
			$f.find('form').submit(function(){
				var email = $('#signupEmail').val();
				if(!email.match(/@/)){
					alert('Please enter a valid e-mail address');
					return false;
				}
				/* Actual ajax call to save the data */
				$.ajax({
					url: '/connection/i/SITE_100831_16303545_YN1UH/form.cfc',
					data: {
						method: 'signup',
						email: email
					},
					success:function(ret){
						ret = eval(ret);
						alert(ret);
						if(!ret.match(/error/i)){
							docBind({target:'body'});
						}
					}
				});
return false;
			});
		}
		/* Slide it down, and make it slide back up when you click anywhere */
		$f.stop(false,true).slideDown();
		var docBind = function(e){
			var $t = $(e.target);
			if($t[0].id!='signupForm' && $t.closest('#signupForm').length==0){
			$(document.body).unbind('click',docBind);
			$f.stop(false,true).slideUp();
			}
		}
		$(document.body).bind('click',docBind);
		return false;
	});
	$('.forward').click(function(){
		$('#signupForm').stop(false,true).slideUp();
		var $t = $(this);
		var $f = $('#forwardForm');
		/* Create the form if it's not already created */
		if($f.length==0){
			$(document.body).prepend('<div id="forwardForm" class="popupForm"></div>');
			$f = $('#forwardForm');
			$f.position({
				of: '.forward',
				my: 'left top',
				at: 'left bottom'
			}).hide();
			$f.html('<form>'
				+'<input placeholder="Your Name" id="yourName">'
				+'<input placeholder="Their Name" id="forwardName">'
				+'<input placeholder="E-mail Address" id="forwardEmail">'
				+'<input type="submit" value="Send">'
				+'</form>'
			);
			$('input[placeholder]').placeholder();
			/* Save the Data when the form is submitted */
			$f.find('form').submit(function(){
				var email = $('#forwardEmail').val();
				var yourname = $('#yourName').val();
				var name = $('#forwardName').val();
				if(!email.match(/@/)){
					alert('Please enter a valid e-mail address');
					return false;
				}
				if(yourname == ''){
					alert('Please enter your name');
					return false;
				}
				if(name == ''){
					alert('Please enter their name');
					return false;
				}
				/* Actual ajax call to save the data */
				
				/* Actual ajax call to save the data */
				$.ajax({
					url: '/connection/i/SITE_100831_16303545_YN1UH/form.cfc',
					data: {
						method: 'forward',
						email: email,
						yourname: yourname,
						name: name,
						page: document.location.hash.replace(/#/,'')
					},
					success:function(ret){
						ret = eval(ret);
						alert(ret);
						if(!ret.match(/error/i)){
							docBind({target:'body'});
						}
					}
				});
return false;
			});
		}
		/* Slide it down, and make it slide back up when you click anywhere */
		$f.stop(false,true).slideDown();
		var docBind = function(e){
			var $t = $(e.target);
			if($t[0].id!='forwardForm' && $t.closest('#forwardForm').length==0){
			$(document.body).unbind('click',docBind);
			$f.stop(false,true).slideUp();
			}
		}
		$(document.body).bind('click',docBind);
		return false;
	});
	
	/* Load up the guy we're going to manipulate */
	var cL = $('#conleft');
	
	/* If we're on a subpage with no shell, redirect to shell */
	if($('#conright').length==0&&$('#conleft').length==1){
		$(document.body).children().hide();
		document.location.href = url.replace(/([^\/]*\/)$/,'') + '#' + url.replace(/http:\/\/[^\/]*/,'');
	}

	var convertCFQuery = function(original){
		var data = [];
		for(var i=0;i<original.DATA.length;i++){
			var item = {};
			for(var j=0;j<original.COLUMNS.length;j++){
				item[original.COLUMNS[j]]=original.DATA[i][j];
			}
			data.push(item);
		}
		return data;
	};
	
	var pageLoadEvents = function(){
		$('h1 + p').css('margin-top', '0');
		$('h1 + ul').css('margin-top', '0');
		$('h1 + ol').css('margin-top', '0');
		$('h1 + h1').css('margin-top', '0');
		$('h1 + h2').css('margin-top', '0');
		$('h1 + h3').css('margin-top', '0');
		
		$('h2 + p').css('margin-top', '0');
		$('h2 + ul').css('margin-top', '0');
		$('h2 + ol').css('margin-top', '0');
		$('h2 + h1').css('margin-top', '0');
		$('h2 + h1').css('margin-top', '0');
		$('h2 + h3').css('margin-top', '0');
		
		$('h3 + p').css('margin-top', '0');
		$('h3 + ul').css('margin-top', '0');
		$('h3 + ol').css('margin-top', '0');
		$('h2 + h1').css('margin-top', '0');
		$('h2 + h1').css('margin-top', '0');
		$('h2 + h3').css('margin-top', '0');
	};
	pageLoadEvents();
	
	/* Function for loading pages */
	var loadPage = function(page){
		window.scrollTo(0,0);
		document.location.href = '#'+page;
		$('#conleft').html('Loading Content ...');
		$.ajax({
			url:page,
			success:function(data){
				var html = $(data);
				$('#conleft').html(html.filter('#content').find('.contentArea').html());
				
				pageLoadEvents();
				var articleTitle = $('.hinfo h1').text();
				var thisUrl = url.replace(/(http:\/\/[^\/]*).*/,'$1') + page;
				
				/* Share Buttons */
				$('#conleft').append('<br style="clear:both;">'
					 +'<div class="addthis_toolbox addthis_default_style">'
						 +'<span class="addthis_separator">Share:</span>'
						 +'<a class="addthis_button_facebook" addthis:url="' + thisUrl + '" addthis:title="' + articleTitle + '"></a> '
						 +'<a class="addthis_button_twitter" addthis:url="' + thisUrl + '" addthis:title="' + articleTitle + '"></a> '
						 +'<span class="addthis_separator">|</span>'
						 +'<a href="http://www.addthis.com/bookmark.php?v=250&username=canyonranch" class="addthis_button_compact">More</a>'
					 +'</div><br style="clear:both;">' );
				addthis = false;
				window.addthis_config = {
					username : 'canyonranch'
				}
				$.ajax({
					url: "http://s7.addthis.com/js/250/addthis_widget.js?domready=1&"+Math.random(),
					dataType: 'script',
					success: function(){
						addthis.init();
					}
				});
				
				/*********************************** BEGIN COMMENTING SYSTEM ************************************/			
				/* Get and Display approved comments for this page */
				$('#conleft').append(''
					+'<div class="commentList">'
					+'</div>'
				);
				var cL = $('.commentList');
				
				$.ajax({
					url: '/connection/i/SITE_100831_16303545_YN1UH/comments.cfc',
					data: {
						method: 'getComments',
						page: page
					},
					success: function(ret){
						ret = convertCFQuery(eval('('+ret+')'));
						cL.append('<p>' + ret.length + ' response' + (ret.length==1?'':'s') + ' to "' + articleTitle + '"</p>');
						$('.commentTotal').html('(' + ret.length + ' comment' + (ret.length==1?'':'s') + ')');
						for(var i in ret){
							cL.append(
								'<p><b>'
								+ret[i].TITLE
								+' says:</b><br>'
								+'<i>'
								+ret[i].DATECREATED
								+'</i></p>'
								+'<p>'
								+ret[i].CONTENT.replace(/\n/g,'<br>')
								+'</p>'
								
							);
						}
					}
				});
				
				/* Create the comment submission form */
				$('#conleft').append(''
					+'<div class="commentInput">'
						+'<form>'
						+'<div>Name</div>'
						+'<input>'
						+'<div>Comment</div>'
						+'<textarea></'+'textarea>'
						+'<input type="submit" value="Post Comment">'
						+'<p>*Your comment will be reviewed before appearing on the site.</p>'
						+'</form>'
					+'</div>'
				);
				
				/* Bind an event to the comment submission form */
				$('.commentInput form').submit(function(){
					var name = $('.commentInput input:first').val();
					var comment = $('.commentInput textarea').val();
					if(name=='')
						alert('Please enter a name.');
					else if(name.length > 100)
						alert('There is a maximum of 100 characters in the name field. Your name is '+name.length+' characters.');
					else if(comment == '')
						alert('Please fill in your comment.');
					else if(comment.length > 1500)
						alert('There is a maximum of 1500 characters in the comment field. Your comment is '+comment.length+' characters.');
					else{
						/* Save the comment, show the comment */
						$.ajax({
							url: '/connection/i/SITE_100831_16303545_YN1UH/comments.cfc',
							data: {
								method: 'sendComment',
								name: name,
								comment: comment,
								page: page
							},
							success:function(ret){
								var ret = eval(ret);
								alert(ret);
								if(!ret.match(/error/i)){
									$('.commentInput input:first').val('');
									$('.commentInput textarea').val('');
								}
							}
						});
					}
					return false;
				});
				/*********************************** END COMMENTING SYSTEM ************************************/		
				
			}
		});
	};
	
	/* Load the page indicated in the anchor */
	var anchor = document.location.hash.replace(/#/,'');
	if(anchor.length != 0){
		loadPage(anchor);
	}
	/* Update links to go directly to their anchors to avoid unnecessary redirects */
	$('a[href*=/display/]').live('click',function(){
		page = $(this).attr('href').replace(/http:\/\/[^\/]*/,'');
		
		/* If we're not already in the appropriate display shell, go to it */
		if(!url.match(/\/display\//i)){
			document.location.href =  page.replace(/([^\/]*\/)$/,'') + '#' + page;
		}else{
			loadPage(page);
		}
		return false;
	});
});







/************************ GOOGLE Site Search Stuff ****************************/
    var webSearch;
	function loadGoogleStuff(){
google.load('search', '1');
    
	var contentDiv = 'searchResults';
    function addPaginationLinks() {
      /* The cursor object has all things to do with pagination*/
      var cursor = webSearch.cursor;
      var curPage = cursor.currentPageIndex; /* check what page the app is on*/
      var pagesDiv = document.createElement('div');
      pagesDiv.className = 'gs-pages';
      for (var i = 0; i < cursor.pages.length; i++) {
        var page = cursor.pages[i];
        if (curPage == i) { /* if we are on the curPage, then don't make a link*/
          var label = document.createTextNode(' ' + page.label + ' ');
          pagesDiv.appendChild(label);
        } else {
          /* If we aren't on the current page, then we want a link to this page.
           So we create a link that calls the gotoPage() method on the searcher. */
          var link = document.createElement('a');
          link.href = 'javascript:webSearch.gotoPage('+i+');';
          link.innerHTML = page.label;
          link.style.marginRight = '2px';
          pagesDiv.appendChild(link);
        }
      }
    
      var contentDivEl = document.getElementById(contentDiv);
      contentDivEl.appendChild(pagesDiv);
    }
    
    function searchComplete() {
    /* Grab our content div, clear it.*/
    var contentDivEl = document.getElementById(contentDiv);
    contentDivEl.innerHTML = '';
    
      /* Check that we got results */
      if (webSearch.results && webSearch.results.length > 0) {
    	
		var resultTitle = document.createElement('div');
		resultTitle.innerHTML = webSearch.cursor.estimatedResultCount+' results for <b>'+lastQuery+'</b>';
		contentDivEl.appendChild(resultTitle);
		
		var hr = document.createElement('hr');
		contentDivEl.appendChild(hr);

		
        /* Loop through our results, printing them to the page. */
        var results = webSearch.results;
        for (var i = 0; i < results.length; i++) {
          /* For each result write it's title and image to the screen */
          var result = results[i];
          var resultContainer = document.createElement('div');
          resultContainer.className = 'gs-result';
    
          var title = document.createElement('a');
          /* We can use titleNoFormatting so that no HTML tags are left in the title */
          title.innerHTML = result.title;
          title.href = result.unescapedUrl;
    
          var content = document.createElement('div');
          content.innerHTML = result.content;
          content.className = 'gs-result-content';
    
          var url = document.createElement('div');
          url.innerHTML = result.unescapedUrl;
          url.className = 'gs-result-url';
    
          resultContainer.appendChild(title);
          resultContainer.appendChild(content);
          resultContainer.appendChild(url);
    
          /* Put our title + image in the content */
          contentDivEl.appendChild(resultContainer);
        }
    
		var hr = document.createElement('hr');
		contentDivEl.appendChild(hr);
		
        /* Now add the paging links so the user can see more results. */
        addPaginationLinks(webSearch);
      }else{
        var resultContainer = document.createElement('div');
        resultContainer.className = 'gs-result';
        
        resultContainer.innerHTML = 'No Results for <b>'+lastQuery+'</b>';

        contentDivEl.appendChild(resultContainer);
      }
    }
    
    function OnLoad() {
      
      /* Add in a WebSearch */
      webSearch = new google.search.WebSearch();

      /* Restrict our search to a custom search engine */
      webSearch.setSiteRestriction('016109463025896457844:7xh_-ltcso4');

      /* Set the Search Control to get the most number of results */
      webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
      webSearch.setSearchCompleteCallback(this, searchComplete, null);
    
    }
    
    google.setOnLoadCallback(OnLoad);
    

    /* The function called from the search box */
    siteSearch = function (query){
	
		lastQuery = query;
		/* Lightbox if there's not a div defined to put the content in */
		if($('#'+contentDiv).length==0){
			
			$.lightbox({
				height:500,
				width:800,
				content:'<div style="margin: 20px 0px 0px 0px;width:780px;height:480px;overflow:auto;padding:10px"><div id="searchResults" class="txt text copy" style="border:1px solid #e8e9e4;padding:10px;overflow:auto;height:430px;"></div></div>'
			});
			
		}
	
	
        var contentDivEl = document.getElementById(contentDiv);
        contentDivEl.innerHTML = '';
        var resultContainer = document.createElement('div');
        resultContainer.className = 'gs-result';
        resultContainer.innerHTML = 'Searching for <b>'+query+'</b> ...';
        contentDivEl.appendChild(resultContainer);
        
        webSearch.execute(query);
    }

}


/*
	Super simple lightbox, by Derek
	Goals
		1) No images or linked stylesheets required
		2) Can accept customization for styling as needed either through stylesheet or javascript
		3) Can accept just simple content and work okay
		4) Lightweight, small bit of code to include
*/

jQuery.lightbox = function(options){
	
	var c = '';
	if(typeof(options)=='string')c=options;
	
	var o = {
		height: '400',
		width: '600',
		content: c,
		modalcss: {
			background:'#000000',
			filter: 'alpha(opacity=30)',
			opacity: .3
		},
		wincss: {
			background: '#FFFFFF',
			border: '1px solid #000000',
			zIndex: '9999'
		},
		closeboxcss: {
			display: 'inline'
		}
	};
	$.extend(o,options);
	
	/*$(document.body).css('overflow','hidden');*/
	var removeIt = function(){
		$(modal).remove();
		$(win).remove();
		$(window).unbind('resize scroll',moveIt);
		/*$(document.body).css('overflow','');*/
	};
	var modal = document.createElement('div');
	$(modal).css($.extend(o.modalcss,{
		position:	'absolute',
		top: $(document).scrollTop()+'px',
		left: $(document).scrollLeft()+'px',
		height: $(window).height()+'px',
		width: $(window).width()+'px'
	})).click(removeIt).appendTo(document.body);
	
	var closebox = document.createElement('div');
	$(closebox).css($.extend(o.closeboxcss,{
		position: 'absolute',
		cursor: 'pointer',
		top: '0px',
		left:  (o.width-90)+'px'
	})).html('<div class="gs-closebox"></div>').click(removeIt);
	
	var win = document.createElement('div');
	$(win).css($.extend(o.wincss,{
		position: 'absolute',
		top: ($(document).scrollTop()+($(window).height()-o.height)/2)+'px',
		left: ($(document).scrollLeft()+($(window).width()-o.width)/2)+'px',
		height: '38px',
		width: '0px'
	})).html(o.content).prepend(closebox).appendTo(document.body).animate({
		width: o.width+'px'
	},500,'linear',function(){
		$(this).animate({
			height: o.height+'px'
		},500,'linear',function(){
			$(window).bind('resize scroll',moveIt);
		});
	});
	var moveIt = function(){
		$(win).css({
			top: ($(document).scrollTop()+($(this).height()-o.height)/2)+'px',
			left: ($(document).scrollLeft()+($(this).width()-o.width)/2)+'px'
		});
		$(modal).css({
			top: $(document).scrollTop()+'px',
			left: $(document).scrollLeft()+'px',
			height: $(this).height()+'px',
			width: $(this).width()+'px'
		});
	};
};


/*
 * jQuery UI Position 1.8.5
 *
 * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * http://docs.jquery.com/UI/Position
 */
(function(c){c.ui=c.ui||{};var n=/left|center|right/,o=/top|center|bottom/,t=c.fn.position,u=c.fn.offset;c.fn.position=function(b){if(!b||!b.of)return t.apply(this,arguments);b=c.extend({},b);var a=c(b.of),d=a[0],g=(b.collision||"flip").split(" "),e=b.offset?b.offset.split(" "):[0,0],h,k,j;if(d.nodeType===9){h=a.width();k=a.height();j={top:0,left:0}}else if(d.scrollTo&&d.document){h=a.width();k=a.height();j={top:a.scrollTop(),left:a.scrollLeft()}}else if(d.preventDefault){b.at="left top";h=k=0;j=
{top:b.of.pageY,left:b.of.pageX}}else{h=a.outerWidth();k=a.outerHeight();j=a.offset()}c.each(["my","at"],function(){var f=(b[this]||"").split(" ");if(f.length===1)f=n.test(f[0])?f.concat(["center"]):o.test(f[0])?["center"].concat(f):["center","center"];f[0]=n.test(f[0])?f[0]:"center";f[1]=o.test(f[1])?f[1]:"center";b[this]=f});if(g.length===1)g[1]=g[0];e[0]=parseInt(e[0],10)||0;if(e.length===1)e[1]=e[0];e[1]=parseInt(e[1],10)||0;if(b.at[0]==="right")j.left+=h;else if(b.at[0]==="center")j.left+=h/
2;if(b.at[1]==="bottom")j.top+=k;else if(b.at[1]==="center")j.top+=k/2;j.left+=e[0];j.top+=e[1];return this.each(function(){var f=c(this),l=f.outerWidth(),m=f.outerHeight(),p=parseInt(c.curCSS(this,"marginLeft",true))||0,q=parseInt(c.curCSS(this,"marginTop",true))||0,v=l+p+parseInt(c.curCSS(this,"marginRight",true))||0,w=m+q+parseInt(c.curCSS(this,"marginBottom",true))||0,i=c.extend({},j),r;if(b.my[0]==="right")i.left-=l;else if(b.my[0]==="center")i.left-=l/2;if(b.my[1]==="bottom")i.top-=m;else if(b.my[1]===
"center")i.top-=m/2;i.left=parseInt(i.left);i.top=parseInt(i.top);r={left:i.left-p,top:i.top-q};c.each(["left","top"],function(s,x){c.ui.position[g[s]]&&c.ui.position[g[s]][x](i,{targetWidth:h,targetHeight:k,elemWidth:l,elemHeight:m,collisionPosition:r,collisionWidth:v,collisionHeight:w,offset:e,my:b.my,at:b.at})});c.fn.bgiframe&&f.bgiframe();f.offset(c.extend(i,{using:b.using}))})};c.ui.position={fit:{left:function(b,a){var d=c(window);d=a.collisionPosition.left+a.collisionWidth-d.width()-d.scrollLeft();
b.left=d>0?b.left-d:Math.max(b.left-a.collisionPosition.left,b.left)},top:function(b,a){var d=c(window);d=a.collisionPosition.top+a.collisionHeight-d.height()-d.scrollTop();b.top=d>0?b.top-d:Math.max(b.top-a.collisionPosition.top,b.top)}},flip:{left:function(b,a){if(a.at[0]!=="center"){var d=c(window);d=a.collisionPosition.left+a.collisionWidth-d.width()-d.scrollLeft();var g=a.my[0]==="left"?-a.elemWidth:a.my[0]==="right"?a.elemWidth:0,e=a.at[0]==="left"?a.targetWidth:-a.targetWidth,h=-2*a.offset[0];
b.left+=a.collisionPosition.left<0?g+e+h:d>0?g+e+h:0}},top:function(b,a){if(a.at[1]!=="center"){var d=c(window);d=a.collisionPosition.top+a.collisionHeight-d.height()-d.scrollTop();var g=a.my[1]==="top"?-a.elemHeight:a.my[1]==="bottom"?a.elemHeight:0,e=a.at[1]==="top"?a.targetHeight:-a.targetHeight,h=-2*a.offset[1];b.top+=a.collisionPosition.top<0?g+e+h:d>0?g+e+h:0}}}};if(!c.offset.setOffset){c.offset.setOffset=function(b,a){if(/static/.test(c.curCSS(b,"position")))b.style.position="relative";var d=
c(b),g=d.offset(),e=parseInt(c.curCSS(b,"top",true),10)||0,h=parseInt(c.curCSS(b,"left",true),10)||0;g={top:a.top-g.top+e,left:a.left-g.left+h};"using"in a?a.using.call(b,g):d.css(g)};c.fn.offset=function(b){var a=this[0];if(!a||!a.ownerDocument)return null;if(b)return this.each(function(){c.offset.setOffset(this,b)});return u.call(this)}}})(jQuery);
;

/*!
 * HTML5 Placeholder jQuery Plugin
 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
 * @author Mathias Bynens <http://mathiasbynens.be/>
 */
(function(a){a.fn.placeholder=function(){if(this[0]&&'placeholder' in document.createElement('input')){return this}function b(c){if(c.val()===''||c.val()===c.attr('placeholder')){c.addClass('placeholder').val(c.attr('placeholder'))}else{c.removeClass('placeholder')}}a('form:has([placeholder]) :submit').click(function(){if(a(this.form).find('.placeholder').length){a(this.form).find('.placeholder:first').val('').focus().removeClass('placeholder');return !1}});return a(this).each(function(){var c=a(this);if(c.is(':password')||!c.is(':input')){return}b(c);c.focusin(function(){if(c.val()===c.attr('placeholder')){c.val('').removeClass('placeholder')}}).focusout(function(){b(c)})})}})(jQuery);
$(document).ready(function(){$('input[placeholder]').placeholder();});
