
var Local = {

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

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

};

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}



(function() {

var $ = jQuery

$(document).ready(
	function() {
		search_clean()
		contact_fix()
		
		$('.reset-button input').click(
            function() {
        		$(this).parents('form').find('input:text,textarea').val('')
        		$(this).parents('form').find('input:checked').removeAttr('checked')
        		$(this).parents('form').find('input:selected').removeAttr('selected')
        		$(this).parents('form').find('select option:first-child').attr({selected: 'selected'})
        	}
        )
        
	}
)

$(window).load(function(){
	scrollers()
	heroes(true)
})

function search_clean() {
    var element = $('div#searchresults ol li')
    element.children('div').remove
}

function contact_fix() {
    var element = $('div.form-contact-form form fieldset.form-type-send-a-catalog legend')
    element.replaceWith("<label>" + element.text() + "</label>")
}

function heroes(overlay) {
	if(overlay == undefined) {
		var overlay = false
	}
	if($('#imageFadeContainer').size() <= 0) {
		return
	}
	var container = $('#imageFadeContainer')
	var fade = container.find('input[name=fadevalue]').val() * 1000
	var show = container.find('input[name=showvalue]').val() * 1000
	container.children('input').remove()
	var images = container.children()
	if(overlay) {
		container.prepend(
			$(jQuery('<div />'))
				.attr({id: 'hero-fg'})
		)
	}
	images.hide()
	$(images[0]).show().addClass('current')
	if($(images[0]).attr('href') != undefined && overlay) {
		container.find('#hero-fg').bind('click', function(){ window.location = $(images[0]).attr('href') }).css({cursor: 'pointer'})
	}
	setInterval(
		function() {
			if(overlay) {
				container.find('#hero-fg').bind('click', function(){}).css({cursor: 'default'})
			}
			var from = container.find('.current')
			from.removeClass('current')
			var to = (from.next().size() > 0 ? from.next() : $(images[0]))
			to.addClass('current')
			from.fadeOut(fade)
			to.fadeIn(fade)
			if(to.attr('href') != undefined && overlay) {
				container.find('#hero-fg').bind('click', function(){ window.location = to.attr('href') }).css({cursor: 'pointer'})
			}
		},
		fade + show
	)
}

function scrollers() {
	$('.scrollable #imageFadeContainer').each(
		function() {
			$(this).children('a,img').appendTo($(this).parent('.scrollable'))
			$(this).remove()
		}
	)
	$('.scrollable').each(
		function() {
			var list = $(jQuery('<ul />'))
			list.addClass('scrollable')
			$(this).after(list)
			$(this).children().each(
				function() {
					list.append(
						$(jQuery('<li />')).append($(this))
					)
				}
			)
			$(this).remove()
		}
	)
	
	$('.scrollable').each(
		function() {
			var dur = 500 // Duration of the animations
			var scroller = $(this)
			scroller.css({position: 'relative', left: 0})
			var width = 0;
			scroller.children().each(
				function(i) {
					width = $(this).outerWidth({margin: true}) > width ? $(this).outerWidth({margin: true}) : width;
				}
			)
			scroller.width(width * scroller.children().size())
			var max_left = 0 - (width * scroller.children().size()) + 386
			var container = $(jQuery('<div />'))
			container.addClass('scroller-container')
			scroller.wrap(container)
			scroller.before(
					$(jQuery('<div />'))
						.addClass('previous')
						.click(
							function() {
								var current_left = parseInt(scroller.css('left'),10)
								if(current_left - width < max_left) {
									scroller.animate({
										left: max_left - (width / 3)
									},{
										duration: dur / 2,
										easing: 'swing'
									})
									setTimeout(
										function() {
											scroller.animate({
												left: max_left
											}, {
												duration: dur / 2,
												easing: 'easeOutBounce'
											})
										}, dur / 2
									)
								} else {
									scroller.animate({
										left: current_left - width
									},{
										duration: dur,
										easing: 'swing'
									})
								}
							}
						)
				)
			scroller.after(
					$(jQuery('<div />'))
						.addClass('next')
						.click(
							function() {
								var current_left = parseInt(scroller.css('left'),10)
								if(0 < current_left + width) { // We are past the first image
									scroller.animate({
										left: 0 + (width / 3)
									}, {
										duration: dur / 2,
										easing: 'swing'
									})
									setTimeout(
										function() {
											scroller.animate({
												left: 0
											}, {
												duration: dur / 2,
												easing: 'easeOutBounce'
											})
										}, dur / 2
									)
								} else {
									scroller.animate({
										left: current_left + width
									},
									{
										duration: dur,
										easing: 'swing'
									}
									)
								}
							}
						)
				)
		}
	)
}

})();