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.hero() // Crossfades hero shots if applicable
		wt.collection_gallery();
		$('form fieldset fieldset legend, form fieldset.convicted legend').each(function() {
            $(this).replaceWith("<label>" + $(this).text() + "</label>")
		});
	})
	
	$(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() {

    	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);
    	}
    }
    
    
    
    
    wt.collection_gallery = function() {
	    var gallery = $('div.from-the-collection #bd div.lightbox ul');
	    if(!gallery.size()) { return; }
	    
	    var galleryLinks = gallery.find("img")
	    
	    galleryLinks.each(function() {
	        $(this).css({'cursor':'pointer'});
	        var linkItem = $(this).attr('src');
			
	        // var textInfo = $(this).html()
	        // var itemTitle = $(this).attr('alt');
	        $(this).click(function(c) {
	            c.preventDefault();
				var img = new Image();
				wt.modal('<img src="/images/loadingAnimation.gif" class="loading" />');
				img.onload = function() {
					var div = $(jQuery('<div id="large_image"></div>'));
					div.append($(img));
					wt.modal.target.html(div);
					wt.modal.popup.css({'width':(wt.modal.popup.find('img').width() + 20), 'margin-left':-((wt.modal.popup.find('img').width() + 20)/2)}).fadeIn();
				}
				img.src = linkItem;
	        })
	    })
	}
	
	wt.modal = function(html) {
    	if(!$('#overlay').size()) { $('body').append($(jQuery('<div id="overlay" style="display: none"></div>'))) }
    	wt.modal.overlay = $('#overlay').height($('body').height())
    	wt.modal.overlay.click(function() { wt.modal.closeModal() });
    	if(!$('#popup').size()) { $('body').append($(jQuery('<div id="popup" class="lightbox" style="display: none"></div>'))) }
    	wt.modal.popup = $('#popup')
    	wt.modal.closeModal = function() {
    		wt.modal.popup.fadeOut('normal', function(){ $.browser.msie ? wt.modal.overlay.hide() : wt.modal.overlay.fadeOut('normal') })
    		wt.modal.popup.fadeOut('normal', function() {wt.modal.popup.removeClass('login')})
    		wt.modal.target.find('img').remove()
    		wt.modal.overlay.removeClass('modal')
    		wt.modal.popup.removeClass('modal')
    		if($.browser.msie) {
		        $('#bd div.select_options').show()
		    }
		    
    	}

    	wt.modal.openModal = function() {
    		$.browser.msie ? wt.modal.overlay.show() : wt.modal.overlay.fadeIn('normal')
    		wt.modal.overlay.addClass('modal')
    		wt.modal.popup.addClass('modal')
    		wt.modal.popup.fadeIn('normal')
    	}

    	if(!wt.modal.popup.find('a.close').size()) {
    		wt.modal.close = $(jQuery('<a class="close" href="#close">&times;</a>'))
    		wt.modal.close.click(function(c){ c.preventDefault(); wt.modal.closeModal(); })
    		wt.modal.close.appendTo(wt.modal.popup)
    	}

    	if(typeof wt.modal.target == 'undefined') {
    		wt.modal.target = $(jQuery('<div class="content-wrapper"></div>'))
    		wt.modal.popup.append(wt.modal.target)
    	}

    	wt.modal.target.html(html)
    	wt.modal.openModal()
    }
	
    
})();