/**********************************\
  Common custom Javascript object
  for the Plan2Build site.
  
  REQUIRES PROTOTYPE
\**********************************/

var Plan2Build = Class.create();

Object.extend( Plan2Build, {
	// Static methods/properties

	iconmessages: $H({
		education:  "Education and Training",
		experience: "Business Experience - 5 Years or More",
		supervisor: "Supervisor Experience - 5 Years or More ",
		tradeassoc: "Trade Association and/or Membership",
		license:    "Business Licence and Insurance ",
		estimates:  "Free Estimates Provided",
		qa:         "Quality Assurance and/or Warranties",
		consref:    "Consumer References - 3 or More ",
		indref:     "Industry References - 3 or More",
		awards:     "Awards and/or Achievements ",
		community:  "Community Support and/or Acts of Kindness",
		integrity:  "10 or More References by Directory Users",
		visa:            "Visa",
		mc:              "Mastercard",
		amex:            "American Express",
		interac:         "Interac",
		handicap:        "Handicap Access",
		seniors:         "Seniors' Discounts",
		bbb:             "Better Business Bureau",
		yellowpages:     "Yellow Pages"
	}),

	
	showIconPopup: function( targetlink, iconname ) {
		targetlink = $(targetlink);
		var popup = $( 'iconpopup' );
		var message = $( 'iconpopup_message' );
		
		message.update(Plan2Build.iconmessages[iconname] );
		popup.moveToElement( targetlink, {halign:'center', valign:'above' } );
		popup.style.visibility="visible";
		return true;
	},

	showIconPopupCustom: function( targetlink, customtext ) {
		targetlink = $(targetlink);
		var popup = $( 'iconpopup' );
		var message = $( 'iconpopup_message' );
		
		message.update( customtext );
		popup.moveToElement( targetlink, {halign:'center', valign:'above' } );
		popup.style.visibility="visible";
		return true;
	},

	hideIconPopup: function() {
		var popup = $( 'iconpopup' );
		popup.style.visibility="hidden";		
		return true;
	},
	
	openGoogleMap: function( country, province, city, address ) {
	    var locationurl = "";
	    locationurl += (address.length > 0) ? (escape(address) + ",+") : "";
	    locationurl += (city.length > 0) ? (escape(city) + ",+") : "";
	    locationurl += (province.length > 0) ? (escape(province) + ",+") : "";
	    locationurl += (country.length > 0) ? (escape(country) + ",+") : "";
	    
	    if (locationurl.length < 2) { return; }
	    
	    locationurl = locationurl.substr(0, locationurl.length-2);
	    window.open("http://maps.google.com/maps?f=q&hl=en&geocode=&q=" + locationurl + "&ie=UTF8&z=12&iwloc=addr&om=1");
	},
	
	openGoogleMapFromInputs: function( countryctrl, provincectrl, cityctrl, addressctrl ) {
	    countryctrl = $(countryctrl);  // Dropdown, with integer ID in the value sections
	    provincectrl = $(provincectrl);  // Dropdown, with integer ID in the value sections
	    cityctrl = $(cityctrl);  // Dropdown, with integer ID in the value sections
	    addressctrl = $(addressctrl);  // Text box
	    
	    Plan2Build.openGoogleMap(
	        countryctrl.options[ countryctrl.selectedIndex ].text,
	        provincectrl.options[ provincectrl.selectedIndex ].text,
	        cityctrl.options[ cityctrl.selectedIndex ].text,
	        addressctrl.value
	    );
	},
	
	AvailabilityCalendar: {

	    Update: function( calendardiv, entryid, month, year ) {
	        new Ajax.Updater( $(calendardiv), ("/scripts/availability.aspx?id=" + entryid + "&m=" + month + "&y=" + year), { method: 'get' });
	    }
	    
	},
	
	_setuphelp: function() {
	    var body = $(document.body);
	    var list = body.getElementsByClassName("Plan2Build.SubmitHelp");

        var L2 = [];
	    for(var i=0; i<list.length; i++) {
	        L2.push(list[i]);
	    }	    
	    for(var i=0; i<L2.length; i++) {
	        Plan2Build._setuplink( L2[i] );
	    }
	    
	    // "hack" to force re-layout in IE
	    var j = $('this_bottom');
	    if(j) {
	        j.style.border = "1px solid white";
	        j.style.border = "none";
	    };
	},
	
	_setuplink: function(link) {
	        var e;

	        link._sh = {};
	        link._sh.block = document.createElement("div");
	        link._sh.block.className = "submithelp";
	        link._sh.popup = document.createElement("div");
	        link._sh.popup.className = "popup";
	        link.parentNode.insertBefore(link._sh.block, link);
	        link.parentNode.removeChild(link);
	        link._sh.block.appendChild(link);
	        link._sh.block.appendChild(link._sh.popup);
	        link.className = "help";
	        
	        e = document.createElement("span");
	        e.className="top";
	        link._sh.popup.appendChild(e);
	        e = document.createElement("span");
	        e.className="middle";
	        e.innerHTML = link.innerHTML;
	        link._sh.popup.appendChild(e);
	        e = document.createElement("span");
	        e.className="bottom";
	        link._sh.popup.appendChild(e);
	        
	        link.href='help:';
	        link.title='';
	        link.onclick = function() { return false; }
	        link.onmouseover = function() { this._sh.popup.style.display = "block"; };
	        link.onmouseout = function() { this._sh.popup.style.display = "none"; };
	    }

});

Plan2Build.prototype = {
	// Instance methods/properties

};	

Event.observeDOMReady(Plan2Build._setuphelp);


/* 2010 addition */
(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				
				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));

