(function() {

	var Showrooms = window.Showrooms = function() {
		return new Showrooms.prototype.init();
	}

	var _aShowrooms = [];
        var _nCatCount = 0, _nCatIndex = 0;
        var _oNextCat = {}, _oPrevCat = {}, _oShowroomsContent = {}, _oShowroomsRoot = {}, _oShowroomsWrap = {};

	function InitShowrooms() {
                var oEl = {};
                oEl = Get.byId('viewShowrooms');
                _oShowroomsRoot = oEl.parentNode;
                _nCatCount = document.showroomCategories.categoryCount.value;

                Events.add(oEl, 'mouseover', GetShowrooms);
                Events.add(oEl, 'mouseout', GetShowrooms);
        };

        function GetShowrooms(e) {
                var bIsOver = e.type == 'mouseover' || e.type == 'click' ? true : false;

		if(bIsOver) {
			if(_aShowrooms[_nCatIndex]) {
				SetShowrooms(_aShowrooms[_nCatIndex]);
			} else {
				var nCatId = (_nCatIndex > 0) ? _aShowrooms[_nCatIndex-1].id : 0;
				Load.script({ url: 'http://www.bobvila.com/includes/Site/remote_masthead_showrooms.php?id='+nCatId+'&index='+_nCatIndex, onload: function() {
					_aShowrooms[_nCatIndex] = showrooms;
					SetShowrooms(showrooms);
				}});
			}
		} else {
			_oShowroomsWrap.className = 'hideMe';
		}

        };

        function SetShowrooms(showrooms) {
                var aComps = showrooms.companies;
                var sNext = '', sOut = '', sPrev = '';

                if(_oShowroomsWrap.nodeType != 1) {
                        _oShowroomsWrap = document.createElement('div');
                        _oShowroomsWrap.id = 'showroomsList';
                        _oShowroomsWrap.className = 'hideMe';
                        _oShowroomsWrap.innerHTML = '<h2><a href="/ProductServices/Categories/Showrooms/">Showrooms from BobVila.com</a></h2>';
                        _oShowroomsContent = document.createElement('div');
                        _oShowroomsWrap.appendChild(_oShowroomsContent);
                        _oShowroomsRoot.appendChild(_oShowroomsWrap);

                        _oShowroomsContent.innerHTML = '<img src="/images/global/animated_load_white.gif" height="26" width="26" border="0"/>';
                        _oShowroomsWrap.className = 'showMe';
                }

                var i = 0, j = aComps.length;
                sOut = (_nCatIndex > 0) ? '<h3>Category: '+showrooms.category+'</h3><ul>' : '<h3>Featured Category: '+showrooms.category+'</h3><ul>';
                while(i < j) {
                        sOut += ('<li><a href="'+aComps[i].url+'">'+aComps[i].name+'</a> - '+aComps[i].desc+'</li>');
                        i++;
                }
                sOut += '</ul>';
                sNext = '<span>&nbsp;|&nbsp;<a id="nextCategory" href="/ProductServices/Categories/Showrooms/#Next_Category">Next</a>&nbsp;&gt;&gt;</span>';
                sPrev = '<span>&lt;&lt;&nbsp;<a id="prevCategory" href="/ProductServices/Categories/Showrooms/#Previous_Category">Prev</a>&nbsp;|&nbsp;</span>';
                sOut += '<div class="clearFix"><p>'+sPrev+'<a href="/ProductServices/Categories/Showrooms/">View all categories</a>'+sNext+'</p></div>';

                _oShowroomsContent.innerHTML = sOut;

                _oNextCat = Get.byId('nextCategory');
                _oNextCat.parentNode.className = (_nCatIndex < _nCatCount) ? 'showMe' : 'hideMe';
                Events.add(_oNextCat, 'click', function(e) { e.preventDefault(); _nCatIndex++; GetShowrooms(e); });

                _oPrevCat = Get.byId('prevCategory');
                _oPrevCat.parentNode.className = (_nCatIndex > 0) ? 'showMe' : 'hideMe';
                Events.add(_oPrevCat, 'click', function(e) { e.preventDefault(); _nCatIndex--; GetShowrooms(e); });

                Events.add(_oShowroomsWrap, 'mouseover', function() { _oShowroomsWrap.className = 'showMe'; });
                Events.add(_oShowroomsWrap, 'mouseout', function() { _oShowroomsWrap.className = 'hideMe'; });

                _oShowroomsWrap.className = 'showMe';
        };

	Showrooms.prototype = {
		init : function() {
			InitShowrooms();
		}
	}
	
})();
