
var Local = {

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

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

};

(function() {
	var $ = jQuery

	$(document).ready(
		function() {
			attachments()
			if($('html#home').size()) {
				var locations =  new Array(
					new Array('chattanooga','http://weather.yahooapis.com/forecastrss?p=37415&u=f'),
					new Array('stuttgart', 'http://weather.yahooapis.com/forecastrss?p=GMXX0128&u=f')
				)
				load_weather(locations, 'small')
				var feeds = new Array('http://www.packworld.com/index.php?option=com_rd_rss&id=116')
				rss(feeds, '.right', 2)
			}
			more_or_less()
			form_defaults()
			$('a').each(function(){$(this).blur()})
			$('a[href*=#]:not([href=#])').click(function() {
				if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
					var $target = $(this.hash);
					$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
					if ($target.length) {
						var targetOffset = $target.offset().top;
						var duration = ($('body').height() / $('html').height()) * 1000
						$('html,body').animate({scrollTop: targetOffset}, duration, 'easeOutSine');
						return false;
					}
				}
			});
			$('#heroshot img').removeAttr('alt')
			$('#navigation li.selected a, #navigation li.ancestor a').addClass('selected')
			$('li.nav-tpc-website a, #heronav a').attr({target: '_blank'})
  		}
	)

	$(window).load(function(){
		if(jQuery.browser.msie && jQuery.browser.version < 7) {
			$('.page_content img').each(function(){
				if($(this).width() > 490) {
					$(this).css({height: 'auto'}).width(490)
				}
			})
		}
	})

	function load_weather(locations, size) {
		if(size == undefined || size == 'small') {
			size = 's'
		} else if(size == 'large') {
			size = 'd'
		}
		if($('#weather').size()) {
			if(!($('#weather table').size())) {
				$('#weather').append(
					$(jQuery('<table />'))
						.addClass('weather-list')
				)
				$('#weather table.weather-list')
					.after(
						$(jQuery('<p />'))
							.append(
								$(jQuery('<a />'))
								.attr({
									href: 'http://weather.yahoo.com/',
									title: 'Powered by Yahoo! Weather'
								})
								.html('Powered&nbsp;By&nbsp;Yahoo!&nbsp;Weather')
							)
							.addClass('yahoo-link')
					)
			} else {
				$('#weather table tr').remove();
			}
			for(var i = 0; i < locations.size(); i++) {
				$('#weather table.weather-list')
					.append(
						$(jQuery('<tr />'))
						.addClass(locations[i][0] + (i == 0 ? ' first' : '') + (i == locations.size() - 1 ? ' last' : ''))
						.load('/weather.php td', {url: locations[i][1], thumb_size: size})
					)
			}
			setTimeout(
				function() {
					load_weather(locations)
				}
			,15 * 60 * 1000)
		}
	}

	function rss(feeds, container, limit) {
		$('body').append(
			$('<div />')
				.attr({id: 'ajax_holder'})
				.css({
					position: 'absolute',
					top: -100000,
					left: -100000,
					display: 'none'
				})
		)
		$(feeds).each(
			function() {
				var feed_url = this
				$.getJSON(
					'/proxy',
					{ url: escape(feed_url) },
					function(json) {
						var items = json.channel.item
						if(items.length) {
							var target = $(jQuery('<div />')).addClass('darkBox feed')
							$('' + container + '').append(target)
							target.append(
								$('<h3 />').html(json.channel.title).append(jQuery('<a />').addClass('rss').attr({href: feed_url}))
							)
							var list = jQuery('<div />').addClass('transparentbox')
							target.append(list)
							for(var i = 0; i < limit; i++) {
								var date = new Date(items[i].pubDate)
								list.append(
									jQuery('<p />')
										.addClass((i == 0) ? 'first' : '')
										.addClass(i == items.length - 1 ? 'last' : '')
										.append(
											jQuery('<a />')
												.attr({
													href: items[i].link,
													title: items[i].title,
													target: '_blank'
												})
												.append(
													jQuery('<strong />')
													.text((date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth()) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '-' + String(date.getFullYear()).substring(2,3) + ' - ')
												)
												.append(items[i].title)
										)
								)
							}
						}
					}
				)
				return
				$('#ajax_holder').load(
					'/proxy.php',
					{ url: escape(this) },
					function() {
						var xml = $('#ajax_holder')
						var items = xml.find('item:lt(' + limit + ')')
						if(items.size()) {
							target.append(list)
							items.each(
							)
						}
					},
					'XML'
				)
			}
		)
	}

	var timeout

	function more_or_less() {
		$('.more-or-less').each(
			function() {
				var parent = $(this)
				parent.mouseover(
					function() {
						clearTimeout(timeout)
					}
				)
				$(this).find('.less').each(
					function() {
						$(this).siblings('.more:visible').slideUp('fast')
						$(this).mouseover(
							function() {
								parent.find('.less').not(this).siblings('.more').slideUp('fast')
								$(this).siblings('.more').slideDown('fast')
							}
						)
					}
				)
				parent.mouseout(
					function() {
						timeout = setTimeout(
							function() {
								parent.find('.more').slideUp('fast')
							}, 1000
						)
					}
				)
			}
		)
	}

	function attachments() {
		var slides		=	$('ul.all-attachments .attachment[class*=" ss_"], ul.all-attachments .attachment[class*=" ss-"]').filter(function() { return String($(this).attr('class')).match(/ ss(-|_)/) != null; });
		var slide_click	=	$('ul.all-attachments .attachment[class*=" sc_"], ul.all-attachments .attachment[class*=" sc-"]').filter(function() { return String($(this).attr('class')).match(/ sc(-|_)/) != null; });
		var video		=	$('ul.all-attachments .attachment[class*= v_], ul.all-attachments .attachment[class*= v-]').filter(function() { return String($(this).attr('class')).match(/ v(-|_)/) != null; });
		var video_click	=	$('ul.all-attachments .attachment[class*= vc_], ul.all-attachments .attachment[class*= vc-]').filter(function() { return String($(this).attr('class')).match(/ vc(-|_)/) != null; });
		var brochures	=	$('ul.all-attachments .attachment.ext-pdf');

		brochures.each(function(){
			$(this).html('Download ' + $(this).html())
		})
		var container	=	$('ul.all-attachments')
		container.empty()
		if(slides.size() || video.size()) {
			var media_container = $(jQuery('<div />'))
			media_container.addClass('media-container')
			if(slides.size() && video.size()) {
				media_container.addClass('two-col')
			}
			media_container.append($(jQuery('<h4 />')).html('Product Images'))
			container.before(media_container)
		}
		if(slides.size()) {
			if(!slide_click.size()) {
				slide_click = $(jQuery('<img class="slides-placeholder" src="/images/clear.gif" />'))
			}
			media_container.append(
					slide_click
						.addClass('slides')
						.click(function() {
							launchSlideshow(slides)
						})
				)
		}
		if(video.size()) {
			if(!video_click.size()) {
				video_click = $(jQuery('<img class="video-placeholder" src="/images/clear.gif" />'))
			}
			media_container.append(
					video_click
						.addClass('video')
						.click(function() {
							launchVideo(video)
						})
				)
		}
		var brochure_section = $(jQuery('<div />'))
		if(brochures.size()) {
			if(brochures.size() > 1) {
				var brochure_title = "Brochures"
			} else {
				var brochure_title = "Brochure"
			}
			brochure_section
				.append(
						$(jQuery('<h4 />'))
							.html(brochure_title)
				)
				.append(
						$(jQuery('<p />'))
							.append(brochures)
				)
		}
		container.replaceWith(brochure_section)
	}

	function launchSlideshow(images) {
		if(!$('#popup').size()) {
			$('body').append(
					$(jQuery('<div id="popup"><div id="overlay"></div></div>'))
				)
		}
		var popup = $('#popup')
		var overlay = popup.find('#overlay')
		if(jQuery.browser.msie && jQuery.browser.version < 7) {
			popup.width($('body').width())
			popup.height($('html').height())
			popup.css({
				top: document.documentElement.scrollTop
			})
			overlay.width($('body').width())
			overlay.height($('html').height())
			$(window).scroll(function(){
				popup.width($('body').width())
				popup.height($('html').height())
				popup.css({
					top: document.documentElement.scrollTop
				})
				overlay.width($('body').width())
				overlay.height($('html').height())
			})
			$(window).resize(function(){
				popup.width($('body').width())
				popup.height($('html').height())
				popup.css({
					top: document.documentElement.scrollTop
				})
				overlay.width($('body').width())
				overlay.height($('html').height())
			})
		}
		overlay.empty()
		if(!images.size()) {
			return
		}
		var container = $(jQuery('<div />'))
		overlay.append(container)
		container.addClass('photo-viewer')
		images.each(
			function(count) {
				$(this).attr({number: count})
			}
		)
		images.hide()
		if(jQuery.browser.safari) {
			images.css({display: 'none'})
		}
		$(images[0]).show().attr({current: 'current'})
		container.append(images)
		if(images.size() == 1) {
			return
		}
		var prev = $(jQuery('<div />'))
		var next = $(jQuery('<div />'))
		var close = $(jQuery('<div />'))
		close
			.addClass('close')
			.click(function() {
				overlay.empty()
				popup.fadeOut()
			})
		container.append(close)
		container.prepend(prev)
		container.append(next)
		prev
			.addClass('prev')
			.click(
				function() {
					var current_img = parseInt(container.find('img[current]').attr('number'), 10)
					if(container.find('img[number=' + (current_img - 1) + ']').size()) {
						container.addClass('loading')
						container.find('img[current]').fadeOut('slow',
							function() {
								container.find('img[current]').removeAttr('current')
								container.find('img[number=' + (current_img - 1) + ']')
									.fadeIn('slow',
										function() {
											container.removeClass('loading')
										}
									)
									.attr({current: 'current'})
							}
						)
					}
					if(container.find('img[number=' + (current_img - 2) + ']').size()) {
						prev.css({visibility: 'visible'})
					} else {
						prev.css({visibility: 'hidden'})
					}
					next.css({visibility: 'visible'})
				}
			)
			.css({visibility: 'hidden'})
		next
			.addClass('next')
			.click(
				function() {
					var current_img = parseInt(container.find('img[current]').attr('number'), 10)
					if(container.find('img[number=' + (current_img + 1) + ']').size()) {
						container.addClass('loading')
						container.find('img[current]').fadeOut('slow',
							function() {
								container.find('img[current]').removeAttr('current')
								container.find('img[number=' + (current_img + 1) + ']')
									.fadeIn('slow',
										function() {
											container.removeClass('loading')
										}
									)
									.attr({current: 'current'})
							}
						)
					}
					if(container.find('img[number=' + (current_img + 2) + ']').size()) {
						next.css({visibility: 'visible'})
					} else {
						next.css({visibility: 'hidden'})
					}
					prev.css({visibility: 'visible'})
				}
			)

		popup.fadeIn('slow')
		simulate_analytics_click('slideshow');
	}

	function launchVideo(video) {
		if(!$('#popup').size()) {
			$('body').append(
					$(jQuery('<div id="popup"><div id="overlay"></div></div>'))
				)
		}
		var popup = $('#popup')
		var overlay = popup.find('#overlay')
		if(jQuery.browser.msie && jQuery.browser.version < 7) {
			popup.width($('body').width())
			popup.height($('html').height())
			popup.css({
				top: document.documentElement.scrollTop
			})
			overlay.width($('body').width())
			overlay.height($('html').height())
			$(window).scroll(function(){
				popup.width($('body').width())
				popup.height($('html').height())
				popup.css({
					top: document.documentElement.scrollTop
				})
				overlay.width($('body').width())
				overlay.height($('html').height())
			})
			$(window).resize(function(){
				popup.width($('body').width())
				popup.height($('html').height())
				popup.css({
					top: document.documentElement.scrollTop
				})
				overlay.width($('body').width())
				overlay.height($('html').height())
			})
		}
		overlay.empty()
		overlay.append(video)
		var close = $(jQuery('<div />'))
		close
			.addClass('close')
			.click(function() {
				popup.fadeOut()
				overlay.empty()
			})

		$.fn.media.defaults.flvPlayer = '/mediaplayer.swf'
		$.fn.media.defaults.width = 600
		$.fn.media.defaults.height = 400

		video.media()

		overlay.find('div.attachment').append(close)

		popup.fadeIn()
		simulate_analytics_click('video');
	}

	function simulate_analytics_click(suffix) {
	    var href = document.location.href;
	    if (href.charAt(href.length - 1) != '/') {
		href += '/';
	    }
	    href += suffix;
	    pageTracker && pageTracker._trackPageview(href);
	}

	function form_defaults() {
		$('input:text, textarea').each(function(){
			var defaultText = $(this).val()
			$(this).focus(function(){
				if($(this).val() == defaultText) {
					$(this).val('')
				}
			})
			$(this).blur(function(){
				if($(this).val() == '') {
					$(this).val(defaultText)
				}
			})
		})
	}
})();
