var Local = {

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

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

};

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

(function() { // This is where jQuery code can safely go without foobarring prototype
	var $ = jQuery;
	
	$(document).ready(function() { // These functions get called on DOM ready
		$('a').click(function() { $(this).blur(); }); // prevents outlines on links for IE
		wt.hero(); // Crossfades hero shots if applicable
		$('.box-collections ul li a').each(function() {
		  if(window.location.pathname.indexOf($(this).attr('href')) > -1) {
		    $(this).parent('li').addClass('selected');
		  }
		});
		$('.additional-options select#glass-top').each(function() {
		  var select = $(this);
		  var name = select.siblings('input#glass-top-name');
		  var breed = select.siblings('input#breed').add(select.siblings('label[for=breed]'));

		  breed.hide();
		  
		  select.change(function() {
		    name.val(select.find('option:selected').attr('name'));
		    if(select.find('option:selected').val() == 410) {
		      breed.show();
		    } else {
		      breed.hide();
		    }
		  });
		});
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		
	})
	
	wt.formDefaults = function() { // Swaps default text form input values to blank on focus and back to default text on blur
		$('input:text,textarea').each(function() {
			var elem = $(this);
			var defaultText = elem.val();
			elem.focus(function(){
				if(elem.val() == defaultText) {
					elem.val('');
				}
			});
			elem.blur(function(){
				if(elem.val() == '') {
					elem.val(defaultText);
				}
			})
		})
	}

	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 = parseInt(heroshot.attr('fade'),10);
			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 = false
	wt.hero.useControls = false
	wt.hero.useCaptions = true

})();

function fc_PreProcess() {
  if(!jQuery('select#glass-top').size()) { return true; }
  if(jQuery('select#glass-top').val() == 410) {
    if(jQuery('input#breed').val() == '') {
      alert('Please tell us what type of dog you want your top to be');
      return false;
    } else {
      return true;
    }
  } else {
    return true;
  }
}
