
var Local = {

    onCommentReply: function(id) {
		Comments.openReply(id);
	},

    onCancelReply: function(id) {
		Comments.closeReply();
	}

};

var debug = false // set to true to turn on logging via alerts. console logging will always run when available.

function _log(string) {
	if(typeof(console) != 'undefined') {
		console.log(string)
	} else if(debug) {
		alert(string)
	}
}


if (!window['wt']) {
    window['wt'] = {};
    var wt = window['wt'];
}

(function($) {

	var $ = jQuery;


$(document).ready(function(){
	$('a').click(function() { $(this).blur() })
	ie_hover();
	bocx();
	slideshow();
	shop();
	//heroes();
	wt.hero(); // Crossfades hero shots if applicable
	var cells = $('#twocolumn td')
	$('#twocolumn').empty()
	cells.each(
		function() {
			if($(this).hasClass('even')) {
				$('#twocolumn').append($(jQuery('<tr />')).append($(this)))
			} else {
				$('#twocolumn tr:last-child').append($(this))
			}
		}
	)

	$('html#cakes td a.thumbnail, html#cakes td h2 a').each(
		function() {
			var url = $(this).attr('href')
			$(this).attr({href: 'javascript:void(0)'}).click(
				function() {
					$(this).blur()
					$('#lightbox').empty()
					$('#lightbox').addClass('cake_lightbox')
					$('#lightbox').load(url + ' div.item_content', {},
						function() {
							$('#lightbox div.item_content').append(
									$(jQuery('<div />'))
										.addClass('close')
										.click(
											function() {
												setTimeout(
													function() {
														$('#lightbox').hide()
														setTimeout(function() {
															$('#lightbox').removeClass('cake_lightbox').removeClass('video_lightbox')
														}, 510)
														$('#overlay').css({display: 'none'})
													}, 5
												)
											}
										)
								)
						}
					)
					setTimeout(function() {
						$('#overlay').css({display: 'block'});
						if($('#lightbox').css('display') == 'none') {
							$('#lightbox').fadeIn(500)
							if(jQuery.browser.msie && jQuery.browser.version < 7) {
								window.scrollTo(0,0)
							}
						}
					},5)
				}
			)
		}
	)
	
	$('div.cakes-and-treats td h2 a').each(
		function() {
			if($(this).attr('href') == 'javascript:void(0)') {
				return false;
			}
			$(this).replaceWith($(this).html())
		}
	)
	

})


function ie_hover() {
	if(!jQuery.browser.msie || (jQuery.browser.msie && jQuery.browser.version >= 7)) {
		return
	}
	var csshoverReg = /(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):hover/i;
	var currentSheet = null;

	parseStylesheets();	

	function parseStylesheets() {
		var sheets = window.document.styleSheets;
		for(var i = 0; i < sheets.length; i++) {
			parseStylesheet(sheets[i])
		}
	}

	function parseStylesheet(sheet) {
		if(sheet.imports) {
			try{
				var imports = sheet.imports;
				for(var i=0; i < imports.length; i++) {
					parseStylesheet(imports[i])
				}
			} catch(securityException) {
			}
		}
		try{
			var rules = sheet.rules;
			currentSheet = sheet;
			for(var x = 0; x <= rules.length; x++) {
				parseCSSRules(rules[x])
			}
		} catch(securityException) {
		}
	}
	
	function parseCSSRules(rule) {
		var select = rule.selectorText;
		var styles = rule.style.cssText;
		if(!csshoverReg.test(select) || !styles) {
			return;
		}

//		$('#debug').append($(jQuery('<p />')).html(select + ' { ' + styles + ' }'))

		var elem = select.replace(/:hover/,'');

		currentSheet.addRule(select.replace(/:hover/,'.psuedohover'), styles)

		$(elem).each(
			function() {
				$(this).hover(
					function() {
						$(this).addClass('psuedohover')
					},
					function() {
						$(this).removeClass('psuedohover')
					}
				)
			}
		)
	}
}

function bocx() {
	$('body').append(
		$(jQuery('<div />'))
			.attr({id: 'overlay'})
			.click(
				function() {
					$('#lightbox').hide()
					setTimeout(function() {
						$('#lightbox').removeClass('cake_lightbox').removeClass('video_lightbox')
					}, 501)
					$('#overlay').css({display: 'none'})
				}
			)
			.hide()
	)
	$('body').append(
		$(jQuery('<div />'))
			.attr({id: 'lightbox'})
			.click(
				function() {
					$('#overlay, #lightbox').stop()
				}
			)
			.css({display: 'none'})
	)
	$('body').append($(jQuery('<div />')).attr({id: 'preload'}).css({position: 'absolute', top: -10000, left: -10000}))
	if(jQuery.browser.safari) {
		$('#overlay, #lightbox').css({display: 'none'})
	} else if(jQuery.browser.msie && jQuery.browser.version < 7) {
		$('#overlay').height($('body').height())
	}

	$('a[rel*=lightbox]').each(
		function() {
			$(this).attr({src: $(this).attr('href'), href: 'javascript:void(0)'})
			$(this).click(
				function() {
					$(this).blur()
					var large = $(jQuery('<img />'))
					large.attr({src: $(this).attr('src'), alt: $(this).attr('title')})
					$('#preload').html(large)
					var dimensions = {x: large.width(), y: large.height()}
					if(large.attr('src').match(/\.png/i) && jQuery.browser.msie && jQuery.browser.version < 7) {
						large.css({background: 'none', filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + large.attr('src') + "', sizingMethod='scale')"}).attr({src: '/images/clear.gif', width: dimensions.x, height: dimensions.y })
					}
					if($(this).next('a[href*=pdf]').length > 0) {
						var pdf = $(this).next('a[href*=pdf]').attr('href')
					} else {
						pdf = false
					}
					if(pdf) {
						$('#lightbox').html(large)
							.append(
								$(jQuery('<div />'))
									.addClass('close')
									.click(
										function() {
											setTimeout(
												function() {
													$('#lightbox').hide()
													setTimeout(function() {
														$('#lightbox').removeClass('cake_lightbox').removeClass('video_lightbox')
													}, 510)
													$('#overlay').css({display: 'none'})
												}, 5
											)
										}
									)
							)
							.append(
								$(jQuery('<a />'))
									.addClass('pdf')
									.attr({href: pdf, title: 'Download PDF'})
							)
					} else {
						$('#lightbox').html(large)
							.append(
								$(jQuery('<div />'))
									.addClass('close')
									.click(
										function() {
											setTimeout(
												function() {
													$('#lightbox').hide()
													setTimeout(function() {
														$('#lightbox').removeClass('cake_lightbox').removeClass('video_lightbox')
													}, 510)
													$('#overlay').css({display: 'none'})
												}, 5
											)
										}
								)
						)

					}
					$('#overlay').css({display: 'block'});
					$('#lightbox').css({height: ''})
					$('#lightbox').fadeIn(500)
					if(jQuery.browser.msie && jQuery.browser.version < 7) {
						window.scrollTo(0,0)
					}
/*
					$('#lightbox').animate({
						width: dimensions.x,
						height: dimensions.y,
						marginLeft: (0 - dimensions.x) / 2,
						marginTop: (0 - dimensions.y) / 2
					},500)
*/
				}
			)
		}
	)
	$('div#video_sidebar a').each(
		function() {
			if($(this).attr('href') == 'javascript:void(0)') {
				return false;
			}
			var url = $(this).attr('href')
			$(this).attr({href: 'javascript:void(0)'})
			$(this).click(
				function() {
					$('#lightbox')
						.load(url + ' embed')
						.addClass('video_lightbox')
					$('#overlay').css({display: 'block'});
					$('#lightbox').fadeIn(500)
					if(jQuery.browser.msie && jQuery.browser.version < 7) {
						window.scrollTo(0,0)
					}
				}
			)
		}
	)
}

var duration = 5;
var transition = 1;
var rotate = new Array;

function slideshow() {
	$('div.slideshow').each(
		function(i) {
			var container = $(this)
			slides = container.children('img').size() ? container.children('img') : false
			if(slides) {
				wrap = $(jQuery('<div />'))
				wrap.addClass('slide_wrapper').css({position: 'relative', left: 0, whiteSpace: 'nowrap'})
				var wrap_width = 0;
				slides.each(
					function() {
						if($(this).width() > container.width()) {
							$(this).css({ width: container.width(), height: 'auto' })
						} else if($(this).height() > container.height()) {
							$(this).css({ height: 'auto', height: container.height() })
						}
						wrap.append($(this))
						wrap_width = wrap_width + $(this).width()
					}
				)
				container.html(wrap).css({overflow: 'hidden'})
				$(slides[0]).addClass('current')
				wrap.css({width: wrap_width})
				container.append(
					$(jQuery('<div />'))
						.addClass('controls')
						.addClass('controls_play')
						.append(
							$(jQuery('<div />'))
								.addClass('play_pause')
								.click(
									function() {
										container.find('div.controls').css({display: 'none'})
																		if(container.find('div.controls').hasClass('controls_play')) {
											slideshow_rotate(container, 'next')
											rotate[i] = setInterval(
												function() {
													slideshow_rotate(container, 'next')
												}, 
												(duration + transition) * 1000
											)
											container.find('div.controls').removeClass('controls_play').addClass('controls_pause')
										} else {
											clearInterval(rotate[i])
											container.find('div.controls').removeClass('controls_pause').addClass('controls_play')
										}
									}
								)
						)
						.append(
							$(jQuery('<div />'))
								.addClass('prev')
								.attr({title: 'Previous Image'})
								.click(
									function() {
										clearInterval(rotate[i])
										slideshow_rotate(container, 'prev')
										container.find('div.controls').removeClass('controls_play').addClass('controls_pause')
										rotate[i] = setTimeout(
											function() {
												rotate[i] = setInterval(
													function() {
														slideshow_rotate(container, 'next')
													},
													(duration + transition) * 1000
												)
											}, (duration + transition) * 1000
										)
									}
								)
						)
						.append(
							$(jQuery('<div />'))
								.addClass('next')
								.attr({title: 'Next Image'})
								.click(
									function() {
										clearInterval(rotate[i])
										slideshow_rotate(container, 'next')
										container.find('div.controls').removeClass('controls_play').addClass('controls_pause')
										rotate[i] = setTimeout(
											function() {
												rotate[i] = setInterval(
													function() {
														slideshow_rotate(container, 'next')
													},
													(duration + transition) * 1000
												)
											}, (duration + transition) * 1000
										)
									}
								)
						)
				)
				container.hover(
					function() {
						container.find('div.controls').css({display: 'block'})
					},
					function() {
						container.find('div.controls').css({display: 'none'})
					}
				)
			}
		}
	)
}

function slideshow_rotate(container, direction) {
	function dir(node, find, direction) {
		if(direction == 'prev') {
			return node.prev(find)
		} else if(direction == 'next') {
			return node.next(find)
		}
	}
	var current = container.find('img.current')
	if(dir(current, 'img', direction).length <= 0 && direction == 'next') { // This is the last image
		var clone = container.find('img:first-child').clone(true)
		container.find('div.slide_wrapper').append(clone)
		setTimeout(
			function() {
				container.find('div.slide_wrapper').css({left: 0})
				clone.remove()
				container.find('img:first-child').addClass('current')
			}, duration * 1000
		)
	} else if(dir(current, 'img', direction).length <= 0 && direction == 'prev') {
		return false;
	}
	var current_now = container.find('img.current')
	current_now.removeClass('current')
	dir(current_now, 'img', direction).addClass('current')
	container.find('div.slide_wrapper').animate({
		left: (0 - dir(current, 'img', direction).position().left)
	}, transition * 1000)
}

function shop() {
	if($('body').hasClass('shop') == false) {
		return;
	}
	$('ul.grid li.grid-item').each(
		function() {
			$(this).hover(
				function() {
					$(this).addClass('hover')
				},
				function() {
					$(this).removeClass('hover')
				}
			)
		}
	)
	$('p#item-order-buttons a[href=#]').each(
		function() {
			$(this).click(
				function(e) {
  				        e.preventDefault();
					$(this).parents('form').submit()
				}
			)
		}
	)
	$('#left ul.navigation li').each(
		function() {
			if(String(window.location).match($(this).find('a').attr('href')) != null) {
				$(this).addClass('selected')
			}
		}
	)
}

wt.hero = function() { // Heroshot crossfader; set options below this function

	if(!$('.heroshots').size()) {
		return;
	}
	$('.heroshots').each(function(index) {
        var heroshot = $(this);
        var interval = null;
		var fade = heroshot.find('input[name=fadevalue]');
		fade = fade.val() * 1000;
		heroshot.find('input[name=fadevalue]').remove();

		var dur = heroshot.find('input[name=showvalue]');
		dur = dur.val() * 1000;
		heroshot.find('input[name=showvalue]').remove();
		
		heroshot.attr({
            'fade': fade,
            'dur': dur
		})
		if(wt.hero.useForeground) {
			heroshot.append(
				$(jQuery('<div class="foreground" />'))
			);
		}
		
		if(wt.hero.useCaptions) {
			if(heroshot.children('a:first-child').size()) {
				var firstCaption = heroshot.children('a:first-child').children('img').attr('alt')
			} else if(heroshot.children('img:first-child').size()) {
				var firstCaption = heroshot.children('img:first-child').attr('alt')
			} else {
				var firstCaption = ''
			}

			heroshot.append(
				$(jQuery('<div class="caption" />'))
					.append(
						$(jQuery('<span class="holder" />'))
							.show()
							.text(firstCaption)
					)
			)
		}
		
		if(heroshot.children('a,img').size() < 2) { // Less than two images, we don't need to xfade or add controls
			return
		}
		
		if(wt.hero.useControls) {
			heroshot.append(
					$(jQuery('<div class="controls" />'))
						.append(
							$(jQuery('<ul />'))
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a href="#previous" class="previous" title="Previous">Previous</a>'))
												.click(function(c){
													c.preventDefault()
													clearInterval(interval)
													wt.hero.rotate('prev', heroshot)
													heroshot.find('.pause').hide()
													heroshot.find('.play').show()
												})
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a href="#pause" class="pause" title="Pause">Pause</a>'))
												.click(function(c){
													c.preventDefault()
													clearInterval(interval)
													heroshot.find('.pause').hide()
													heroshot.find('.play').show()
												})
												.css({display: 'block'})
												.show()
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a href="#play" class="play" title="Play">Play</a>'))
												.click(function(c){
													c.preventDefault()
													interval = setInterval(function(){
														wt.hero.rotate('next', heroshot)
													}, dur + fade)
													heroshot.find('.pause').show()
													heroshot.find('.play').hide()
												})
												.css({display: 'block'})
												.hide()
										)
								)
								.append(
									$(jQuery('<li />'))
										.append(
											$(jQuery('<a href="#next" class="next" title="Next">Next</a>'))
												.click(function(c){
													c.preventDefault()
													clearInterval(interval)
													wt.hero.rotate('next', heroshot)
													heroshot.find('.pause').hide()
													heroshot.find('.play').show()
												})
										)
								)
						)
				)
		}
		
		heroshot.children('a:first-child,img:first-child').attr({current: 'current'})
		heroshot.children('a:not(:first-child),img:not(:first-child)').hide()
		if(jQuery.browser.safari) {
			heroshot.children('a:not(:first-child),img:not(:first-child)').css({display: 'none'})
		}
		
		interval = setInterval(function() {
			wt.hero.rotate('next', heroshot)
		}, dur + fade)
		
	});

	


	wt.hero.rotate = function(dir, heroshot) {
		if(typeof dir == 'undefined') {
			var dir = 'next'
		}
		if(typeof heroshot == 'undefined') {
			return
		}
		
		var images = heroshot.children('a,img')
		var current = heroshot.children('a[current],img[current]')
		if(dir == 'next') {
			if(current.next('a,img').size()) {
				var to = current.next('a,img')
			} else {
				var to = $(images[0])
			}
		} else {
			if(current.prev('a,img').size()) {
				var to = current.prev('a,img')
			} else {
				var to = $(images[images.size() - 1])
			}
		}
        
        var fade = heroshot.attr('fade')
		current.removeAttr('current').fadeOut(fade)
		
		if(wt.hero.useCaptions) {
			heroshot.find('.holder').fadeOut(fade / 2, function(){
				heroshot.find('.holder').text(to.find('img').size() ? to.find('img').attr('alt') : to.attr('alt'))
				heroshot.find('.holder').fadeIn(fade / 2)
			})
		}

		if(wt.hero.useForeground) {
			if(to.href != 'undefined') {
				heroshot.find('.foreground').bind('click',function() {
					window.location = to.href
				})
			} else {
				heroshot.find('.foreground').unbind('click',function() {
					window.location = to.href
				})
			}
		}
		to.attr({current: 'current'}).fadeIn(fade)
	}
}

// Heroshot options
wt.hero.useForeground = true
wt.hero.useControls = true
wt.hero.useCaptions = true


// END WORK


})(jQuery);



var $j = jQuery.noConflict();

var Fade = {
	_once: false,
	once: function() {
		if (this._once) return;
		
		if ($j.browser.mozilla) {
			$j('body').css({ opacity: 0.999 });
		}

		this._once = true;
	},

	initContainer: function(install) {
		if(this.size() != false) {
			Fade.once();			
			var fade = this.find('input[name=fadevalue]').val() * 1000;
			var show = this.find('input[name=showvalue]').val() * 1000;
			this.find('input[name=fadevalue], input[name=showvalue]').remove();
			
			var images = this.children();
			var  current = 0;						
			if (install) { images = install.apply(this); }
			else { $j(images[current]).show().siblings().hide(); }

			setInterval(function() { current = Fade.step(fade, current, images); }, fade + show);
		}
		return this;
	},	

	step: function(fade, current, images) {
		if (images.length == 1) return;

		var next = (current + 1) % images.length; 
		$j(images[current]).fadeOut(fade);
		$j(images[next]).fadeIn(fade);
		
		return next;
	}	
};

$j.fn.fadeContainer = Fade.initContainer;
