/* ==========================================
 * Rendering Banners PlaceHolder
 * @access public
 * @param PageName - (string) Page object name
/* ========================================== */
// Allowed PlaceHolders
var AllowedAdvPlaceHolders = [
	'BottomBanner1',
	'LeftBanner1',
	'LeftBanner2',
	'LeftBanner3',
	'RightBanner1',
	/*'SearchBlock',*/
	'PropertyWithImage1',
	/*'LeftProperty1',*/
	'SmallProperty1',
	'SmallProperty2',
	'SmallProperty3',
	'SmallProperty4',
	'TextBlock1',
	'TextBlock2'
]
var AdvPlaceHoldersMatch = {
	'LeftBanner'	 	 : /(LeftBanner)/g,
	'RightBanner'	 	 : /(RightBanner)/g,
	'BottomBanner'		 : /(BottomBanner)/g,
	'PropertyWithImage'	 : /(PropertyWithImage)/g,
	'SmallProperty'		 : /(SmallProperty)/g,
	/*'Search'	 		 : /(Search)/g,*/
	'Text'		 		 : /(Text)/g
}
var AdvObject = []; // store of Addvertisment objects
var AdvObjectIterator = 0; // iterator for Advertisment object

var RenderedPropertiesCollector = []; // store of already rendered properties
if (Cookie && Cookie.isSet('_realestate_pi')) Cookie.del('_realestate_pi'); // clear properties Ids

/* ==========================================
 * Advertisment Control for Page
 * @access public
 * @param PageName - (string) Page object name
/* ========================================== */
function AdvertismentPlaceHoldersControl(PageName){
	if ( !use_enhanced_js ) return false;
	
	var pref = '';
	var BannerLeftPlaceHolderHTML = '<table cellpadding="0" cellspacing="0" class="leftpromo"><tr><td class="tl"><img src="'+ ImgPath +'pix.gif"/></td><td class="empty">&nbsp;</td><td class="tr"><img src="'+ ImgPath +'pix.gif"/></td></tr><tr><td class="bg"><img src="'+ ImgPath +'pix.gif" width="8" height="1"/></td><td class="content" id="BannerLeftPlaceHolderContainer"></td><td class="bg"><img src="'+ ImgPath +'pix.gif" width="8" height="1"/></td></tr><tr><td class="bl"><img src="'+ ImgPath +'pix.gif"/></td><td class="empty">&nbsp;</td><td class="br"><img src="'+ ImgPath +'pix.gif"/></td></tr></table>';
	var BottomPropertiesPlaceHolderHTML = '<table cellpadding="0" cellspacing="0" width="100%" class="ind-bottom-block"><tr valign="top" id="BottomPropertiesListContainer"><td class="item pr">&nbsp;</td><td class="item pr">&nbsp;</td><td class="item pr">&nbsp;</td><td class="item">&nbsp;</td></tr></table>';
	if (PageName == 'indexpage'){
		var BigPropertyPlaceHolderHTML = '<div><img src="'+ ImgPath +'txt/ind-featured-properties.gif" width="151" height="14" alt="Featured Properties"/></div><div class="pad6"></div>';
		$('BigProperty').innerHTML = BigPropertyPlaceHolderHTML;
	}
	$('BannerLeftPlaceHolder').innerHTML = BannerLeftPlaceHolderHTML;
	$('BottomPropertiesList').innerHTML = BottomPropertiesPlaceHolderHTML;
	
	var i=0;
	for (PlaceHolder in AdvPlaceHolders) {
		if (AllowedAdvPlaceHolders.in_array(PlaceHolder)) {
			var Type = '';
			for (Matching in AdvPlaceHoldersMatch){
				if (PlaceHolder.match(new RegExp(AdvPlaceHoldersMatch[Matching]))){
					Type = Matching;
					break;
				}
			}
			AdvObject[i] = {
				'Position' : AdvPlaceHolders[PlaceHolder],
				'Type'	   : Type
			}
			i++;
			setTimeout ('_renderAdvertisment("'+PageName+'")', 1000*i);
		}
	}
}

/* ==========================================
 * Getting Advertisment for Page and PlaceHolder
 * @access private
 * @param PageName - (string) Page object name
/* ========================================== */
function _renderAdvertisment(PageName){
	var AdvType = AdvObject[AdvObjectIterator]['Type']
	var Position = AdvObject[AdvObjectIterator]['Position']
	AdvObjectIterator++;

	if(AdvType == 'LeftBanner'){
		_renderLeftBanner(PageName, Position);
	} else if (AdvType == 'BottomBanner'){
		_renderBottomBanner(PageName, Position);
	} else if (AdvType == 'PropertyWithImage'){
		_renderPropertyWithImage(PageName, Position);
	} else if (AdvType == 'SmallProperty'){
		_renderSmallProperty(PageName, Position);
	} else if (AdvType == 'RightBanner'){
		_renderRightBanner(PageName, Position);
	}
}
/* ==========================================
 * Render Advertisment for LeftBanner
 * @access private
 * @param PageName - (string) Page object name
 * @param Position - (int) Page position
/* ========================================== */
function _renderLeftBanner(PageName, Position){
	var url = AppRoot + 'banner.php';
	var postData = 'PageName='+ PageName +'&PageLocation='+ Position;
	
	new Ajax.Request(url,{
		method: "post",
		parameters: postData,
		onSuccess: function(t) {
			if (t.responseText!=''){
				var html = '';
				html += '<table class="block" id="LeftBanner'+Position+'"><tr><td>';
				html += t.responseText;
				html += '</td></tr></table>';
				html += '<div class="pad5"></div>';
				$('BannerLeftPlaceHolderContainer').innerHTML += html;
				$('BannerLeftPlaceHolder').style.visibility = 'visible';
			}
		},
		onFailure: function(t) {}
	})
}
/* ==========================================
 * Render Advertisment for RightBanner
 * @access private
 * @param PageName - (string) Page object name
 * @param Position - (int) Page position
/* ========================================== */
function _renderRightBanner(PageName, Position){
	var url = AppRoot + 'banner.php';
	var postData = 'PageName='+ PageName +'&PageLocation='+ Position;
	
	new Ajax.Request(url,{
		method: "post",
		parameters: postData,
		onSuccess: function(t) {
			if (t.responseText!=''){
				var html = '';
				html += '<table id="RightBanner'+Position+'" cellpadding="0" cellspacing="0" width="183"><tr><td><img src="'+ ImgPath +'tables/lay-promo-t.gif" width="183" height="17"/></td></tr><tr valign="top"><td class="justy" style="padding:0 13px" align="center" bgcolor="#6dc034">';
				html += t.responseText;
				html += '</td></tr><tr><td><img src="'+ ImgPath +'tables/lay-promo-b.gif" width="183" height="17"/></td></tr></table>';
				$('BannerRightPlaceHolder').innerHTML += html;
				$('BannerRightPlaceHolder').style.visibility = 'visible';
			}
		},
		onFailure: function(t) {}
	})
}
/* ==========================================
 * Render Advertisment for BottomBanner
 * @access private
 * @param PageName - (string) Page object name
 * @param Position - (int) Page position
/* ========================================== */
function _renderBottomBanner(PageName, Position){
	var url = AppRoot + 'banner.php';
	var postData = 'PageName='+ PageName +'&PageLocation='+ Position;
	
	new Ajax.Request(url,{
		method: "post",
		parameters: postData,
		onSuccess: function(t) {
			if (t.responseText!=''){
				var html = '';
				html += '<table id="BottomBanner'+Position+'" cellpadding="0" cellspacing="0"><tr><td>';
				html += t.responseText;
				html += '</td></tr></table>';
				$('BannerBottomPlaceHolder').innerHTML += html;
				$('BannerBottomPlaceHolder').style.visibility = 'visible';
			}
		},
		onFailure: function(t) {}
	})
}
/* ==========================================
 * Render Advertisment for PropertyWithImage
 * @access private
 * @param PageName - (string) Page object name
 * @param Position - (int) Page position
/* ========================================== */
function _renderPropertyWithImage(PageName, Position){
	var url = AppRoot + 'prop.php';
	var postData = 'PageName='+PageName+'&WithImage=1';
	
	if (Cookie.get('_realestate_pi')) RenderedPropertiesCollector[RenderedPropertiesCollector.length] = Cookie.get('_realestate_pi');
	if (RenderedPropertiesCollector.length) postData += '&pi=' + RenderedPropertiesCollector.join('|');
	
	new Ajax.Request(url,{
		method: "post",
		parameters: postData,
		onSuccess: function(t) {
			Cookie.init();
			if (t.responseText!=''){
				$('BigProperty').innerHTML += t.responseText;
				$('BigProperty').style.visibility = 'visible';
				$('BigProperty').style.position = 'static';
			}
		},
		onFailure: function(t) {}
	})
}
/* ==========================================
 * Render Advertisment for SmallProperty
 * @access private
 * @param PageName - (string) Page object name
 * @param Position - (int) Page position
/* ========================================== */
function _renderSmallProperty(PageName, Position){
	var url = AppRoot + 'prop.php';
	var postData = 'PageName='+PageName;
	
	if (Cookie.get('_realestate_pi')) RenderedPropertiesCollector[RenderedPropertiesCollector.length] = Cookie.get('_realestate_pi');
	if (RenderedPropertiesCollector.length) postData += '&pi=' + RenderedPropertiesCollector.join('|');
	
	new Ajax.Request(url,{
		method: "post",
		parameters: postData,
		onSuccess: function(t) {
			Cookie.init();
			if (t.responseText!=''){
					var html = '';
					html += '<table cellpadding="0" cellspacing="0" width="100%" class="lgreenbox"><tr><td class="tl"><img src="'+ ImgPath +'pix.gif"/></td><td></td><td class="tr"><img src="'+ ImgPath +'pix.gif"/></td></tr><tr valign="top"><td></td><td class="content fs11 lh13">';
					html += t.responseText;
					html += '</td><td></td></tr><tr><td class="bl"><img src="'+ ImgPath +'pix.gif"/></td><td></td><td class="br"><img src="'+ ImgPath +'pix.gif"/></td></tr></table>';

					var propContainer = $('BottomPropertiesListContainer');
					var propItems = propContainer.childNodes;
	
					for (var i=0; i < propItems.length; i++)
					{
						if (propItems[i].innerHTML == '&nbsp;' || propItems[i].innerHTML == '')
						{
							propItems[i].innerHTML = html;
							propContainer.style.visibility = 'visible';
							break;
						}
					}
			}
		},
		onFailure: function(t) {}
	})
}
