var Local = {

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

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

};

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

wt.browserSucks = (jQuery.browser.msie && jQuery.browser.version < 7) ? true : false;

(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').live('click',function() { $(this).blur(); })
		wt.membersEntrance();
		wt.heroshots();
		wt.mc_gallery();
	});
	
	$(window).load(function() { // These functions get called when everything has loaded
	    // if($.browser.msie) {
        //     $('.all_round,#top,#featured_prop_slider,div.news #bd .main,div.news #bd .sidebar').corners("10px")
        //     $('.top_round').corners("top-left top-right 10px")
		// }
    	
	})
	
	
	wt.membersEntrance = function() {
		var form = $('#members-entrance .form');
		form.hide()

		$('#members-entrance a#enter-link').click(function(c) {
			c.preventDefault();
			if(wt.browserSucks) {
				form.toggle();
			}
			else {
			    form.slideToggle()
			}
		});
	}
	
    wt.heroshots = function() {
    	var heroshots = $('.heroshots');
    	if(!heroshots.size()) { return; }

    	heroshots.each(function() {
    		var heroshot = $(this);
    		var frame = $(jQuery('<div class="frame"></div>'));
    		heroshot.prepend(frame);

    		if(!heroshot.find('input').size()) { return; }

    		var fade = heroshot.find('input[name=fadevalue]').val() * 1000;
    		var dur = heroshot.find('input[name=showvalue]').val() * 1000;
    		heroshot.find('input').remove();

    		var heroes = heroshot.find('img')

    		var caption = $(jQuery('<div class="caption">' + heroes.get(0).alt + '</div>'))
    		caption.appendTo(heroshot)

    		var images = heroshot.find('img');
    		if(images.size() < 2) { return; }
    		images.css({'display':'none'});
    		images.eq(0).show();
    		if(images.eq(0).parent('a').size()) {
    			frame.bind('click', function() {
    				window.location = images.eq(0).parent('a').attr('href');
    			}).css({'cursor':'pointer'});
    		}

    		setInterval(function(){
    			frame.unbind('click').css({'cursor':'default'});
    			var current = images.filter(':visible');
    			var next = current.parent('a').size() ? (current.parent('a').next('a,img').size() ? current.parent('a').next('a,img') : images.eq(0)) : (current.next('a,img').size() ? current.next('a,img') : images.eq(0));
    			if(next.parent('a').size()) {
    				frame.bind('click', function() {
    					window.location = next.parent('a').attr('href');
    				}).css({'cursor':'pointer'});
    			}
    			if(typeof next[0] != 'undefined' && next[0].nodeName.toLowerCase() == 'a') {
    				frame.bind('click', function() {
    					window.location = next.attr('href');
    				}).css({'cursor':'pointer'});
    				next = next.find('img');
    			}
    			current.fadeOut(fade);

    			heroshot.find('.caption').fadeOut(fade / 2.5, function(){
        			heroshot.find('.caption').text(next.find('img').size() ? next.find('img').attr('alt') : next.attr('alt'))
        			heroshot.find('.caption').fadeIn(fade / 2.5)
        		});

    			next.fadeIn(fade);
    		}, fade + dur);
    	});
    }	
	
    
	wt.mc_gallery = function() {
	    var gallery = $('#mc_gallery');
	    if(!gallery.size()) { return; }
	    
	    var galleryLinks = gallery.find("a[rel=lightbox]")
	    
	    galleryLinks.each(function() {
	        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('<div class="frame"><h3>' + textInfo + '</h3></div>');
					div.append($(img));
					wt.modal.target.html(div);
					wt.modal.popup.fadeIn()
				}
				img.alt = itemTitle;
				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">Close</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()
    }
    
	
})();