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
		wt.collection_gallery();
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		if($('div.services #bd'|'div.pointe-general-contractors #bd').size()) {
        			$('#services_hero_caption p span').text($('#imageFadeContainer img:first').attr('alt'))
        		}
        $('table tr').hover(function() {
        	        $(this).addClass('hover');
        	    },
        	    function() {
        	        $(this).removeClass('hover');

        	    })
        
	})
	
	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 = heroshot.attr('fade')
			current.removeAttr('current').fadeOut(fade / 2)
			
			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($('div.services #bd'|'div.pointe-general-contractors #bd').size()) {
    				$('#services_hero_caption p').fadeOut(fade, function(){
    					$('#services_hero_caption p span').text(to.find('img').size() ? to.find('img').attr('alt') : to.attr('alt'))
    					$('#services_hero_caption p').fadeIn(fade)
    				})
    			}
                
			}
			
		    
			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 / 2)
		}
	}

	// Heroshot options
	wt.hero.useForeground = true
	wt.hero.useControls = true
	wt.hero.useCaptions = true

    wt.collection_gallery = function() {
        var gallery = $('div.properties #bd div.layouts');
        if(!gallery.size()) { return; }

        var galleryLinks = gallery.find("a");
        galleryLinks.each(function() {
            $(this).css({'cursor':'pointer'});
            var linkItem = $(this).attr('href');
            
            // 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()
    }
    
})();
