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

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

};

var wt = {}; // This is the setup for the webtools namespace

(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.formDefaults();
		$('input').each(function() { $(this).addClass($(this).attr('type')); });
        $('table.zebra').each(function() {$(this).find('tr:odd').addClass('alt')});
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		
	})
	

    wt.formDefaults = function() {
    	$('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() {

    	if($('#imageFadeContainer').size()) {
    		wt.hero.dur = parseInt($('#imageFadeContainer').find('input[name=showvalue]').val(),10);
    		wt.hero.fade = parseInt($('#imageFadeContainer').find('input[name=fadevalue]').val(),10);
    		var count = 0;
    		wt.hero.heroes = $('#imageFadeContainer img');
    		$(wt.hero.heroes[0]).addClass('visible');
    		wt.hero.container = $(jQuery('<div id="container"></div>'));
    		wt.hero.controls = $(jQuery('<div class="heroshot_navigation"></div>'));
    		var list = $(jQuery('<ul />'));
    		list.appendTo(wt.hero.controls);
    		wt.hero.heroes.each(
    			function(count) {
    				var img = $(this);
    				var item = $(jQuery('<li />'));
    				if(count == 0) {
    					item.addClass('selected');
    				}
    				item.attr("title", count + 1);
    				item.html("&#8226;")
    				item.css({'cursor':'pointer'});
    				item.click(
    					function() {
    						wt.hero.showImg(item, 'jump');
    					}
    				);
    				list.append(item);
    				if(img.parent('a').attr('href')) {
    					wt.hero.container.append(img.parent('a'));
    				} else {
    					wt.hero.container.append(img);
    				}
    			}
    		);
    		wt.hero.container.appendTo('#imageFadeContainer');

    		if(wt.hero.heroes.size() < 1) { return; }

    		wt.hero.controls.appendTo($('#imageFadeContainer'));

    		$(window).load(function() {
    			wt.hero.rotation = setInterval(function() { wt.hero.rotate(); },(wt.hero.dur + wt.hero.fade) * 1000);
    		});
    	}
    }

    wt.hero.rotate = function() {
    	wt.hero.showImg(wt.hero.controls.find('li.selected').next().size() ? wt.hero.controls.find('li.selected').next() : wt.hero.controls.find('li:first-child'), 'rotate');
    }

    wt.hero.showImg = function(item, action) {
    	if(wt.hero.container.not(':animated')) {
    		var controls = wt.hero.controls.find('li');
    		var from_img = wt.hero.heroes.filter('.visible');
    		from_img.removeClass('visible');
    		var to_img = null;

    		if(action == 'jump') {
    			clearInterval(wt.hero.rotation);
    			wt.hero.rotation = setInterval(function() { wt.hero.rotate(); },(wt.hero.dur * 2) * 1000);
    		}

    		var img_no = parseInt(item.attr("title"),10) -1;

    		if(action == 'rotate' && item.is(':first-child')) {
    			if($(wt.hero.heroes[0]).parent('a')) {
    				var first = $(wt.hero.heroes[0]).parent('a').clone(true);
    			} else {
    				var first = $(wt.hero.heroes[0]).clone(true);
    			}
    			wt.hero.container.append(first);
    			to_img = first;
    			setTimeout(
    				function() { 
    					wt.hero.container.css({'left': 0}); 
    					first.remove();
    				},
    				(wt.hero.fade * 1000) + 500
    			);
    		} else {
    			to_img = $(wt.hero.heroes[img_no]);
    			to_img.addClass('visible');
    		}
    		var current = controls.filter('.selected');
    		current.removeClass('selected');
    		var change = 0 - (!(item.is(':first-child') && action == 'rotate') ? (to_img.width() * img_no) : (to_img.width() * wt.hero.heroes.size()));
    		$(controls[img_no]).addClass('selected');
    		wt.hero.container.animate({
    			'left': change + 'px'
    		}, wt.hero.fade * 1000);
    	}
    }
    
})();