/* $Id: common.js,v 1.26 2010/09/28 22:55:43 sgruenholz Exp $ */

var contact_support_window = false; // initialize this global var

// pops a new window
function popWindow(id) { // the id of the anchor tag or the anchor tag object itself
	var url = $(id).href;
	var window_name = $(id).target;
	var window_options = $(id).rel; // get options out of the rel tag
	newWin = window.open(url, window_name, window_options);
	newWin.focus();
}

function bookmark_site() {
	var title = "My Elite Website";//document.title;
	var url = document.URL;
	
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}


Event.observe(window, 'load', function() 
{
	if ($('ACNT')) {
        ACNT = $('ACNT').getValue();
    }
	// every confirmation message has this standard effect:
	// wait 2 seconds (after page load) then fade out over the next 2 seconds
	$$('.conf_msg').each(function(msg) {
		new Effect.Fade(msg, {
			duration: 2,
			delay: 2
		});
	});
	
	// run png fix on ihouse gold icon and any sold graphics
	$$('img.ihouse_gold, img.sold').invoke('pngFix');
	
	// textareas with a max length - automatically put a js counter on the bottom letting the user know
	// how many characters they have left
	$$("textarea[maxlength]").each(function(input_with_maxlength) {
		span = new Element("span");
		span.id = input_with_maxlength.name + "_counter";
		span.addClassName("counter");
		span.update((input_with_maxlength.readAttribute("maxlength") - input_with_maxlength.value.length) + " characters remaining");
		input_with_maxlength.insert({after:span});	
		span.setStyle({width:(span.getWidth() - 20)+"px"});
		Event.observe(input_with_maxlength, "keyup", function(onkeyup_event) {
			if (this.value.length > this.readAttribute("maxlength")) {
				this.value = this.value.substring(0, this.readAttribute("maxlength"));
			}
			$(this.name + "_counter").update((this.readAttribute("maxlength") - this.value.length) + " characters remaining");
		});
	});
	
	// SUBMIT BUTTONS
	// to prevent double-click and to show page is doing something we swap out the submit button with a loading graphic onclick
	$$("input.submit, input.build_submit").each( function(btn) {
		btn.observe("click", function() {
			btn.hide().insert({after:'<img src="/images/web/icons/anim_loading_dots.gif" title="loading" alt="loading" class="loading" />'});
			$$("input.submit").invoke("hide");
		});
	});
	
	$$("input.submit_or_cancel").each( function(btn) {
        btn.observe("click", function() {
            btn.up().hide().insert({after:'<div class="buttons"><img src="/images/web/icons/anim_loading_dots.gif" title="loading" alt="loading" class="loading" /></div>'});
        });
    });

	// preload the loading graphic
	loading_dots = new Image();
	loading_dots.src = "/images/web/icons/anim_loading_dots.gif";
	
	
	// Find any Contact Us form buttons in the navigation and make them do a popup window
	$$('.contact_us_form_link').each( function(s) {
		$(s).observe('click', function(event) {
			popWindow(s);
			Event.stop(event); // prevent the regular link from firing
		});
	});
	
	
	// find any links to the help articles table of contents lightbox
	
	toc_window = new Lightbox("/help/table_of_contents.php", {content_type:'iframe'});
	$$(".toc").each( function(btn) {
		btn.observe("click", function(event) {
			Event.stop(event); // stop href from resolving
			toc_window.open();
		});
	});
	
	
	
	$$("a.contact_support").each( function(btn) {
		if (!contact_support_window) {
			contact_support_window = new Lightbox(btn.href, {content_type:'iframe', height:430});
		}
		btn.observe("click", function(event) {
			Event.stop(event); // stop href from resolving
			contact_support_window.open();
		});
	});
	
	
});

