
document.documentElement.className += " js";

if(document.location.href.indexOf('england') == -1)
{
	var strCountry = 'Ireland';
	var strCity = 'Dublin';
	var myEngland = 0;
} else {
	var strCountry = 'England';
	var strCity = 'London';
	var myEngland = 1;
}

function getPrefix()
{
	if($('input#v-create-title').is('input'))
	{
		return 'v-create-';
	}
	if($('input#p-create-title').is('input'))
	{
		return 'p-create-';
	}
	if($('input#od-create-title').is('input'))
	{
		return 'od-create-';
	}
	if($('input#moto-create-title').is('input'))
	{
		return 'moto-create-';
	}
	if($('input#ud-create-data_ur').is('input'))
	{
		return 'ud-create-';
	}
	if($('input#post-title').is('input'))
	{
		return 'post-';
	}
}

function initMaps()
{
// 	alert(document.location.href.indexOf('ntb'));
	if((document.location.href.indexOf('ntb') > 0))
		return false;

	if($('#gMap').is('div'))
	{
		var vx = $('#gMapX').text();
		var vy = $('#gMapY').text();
/*		$('#gMap').height(300).hide(200).show(200).height(300);*/
		$('#gMap').jmap({maptype:"map", clickmarker:false});
		var point = new GLatLng(vy,vx);
		searchView(point);
	}
	
	var prefix = getPrefix();
	if($('input#'+prefix+'map_lat').is('input'))
	{
		// prepare interface
		$('input#'+prefix+'map_lat').before('<div id="gMapEditWrapper" style="display: block; clear: both;"></div>');
		$('#gMapEditWrapper').html('<div id="gMapEdit" style="display: block; clear: both; height: 350px"></div>');
		$('#gMapEditWrapper').before('<h4 style="clear: both;">Znajdź na mapie lokację</h4>');
		$('#gMapEdit').before('<div id="mapSearch"><p>Szukaj adresu</p><label>ulica: <input type="text" name="street" value="" id="street" /></label><label>miasto: <input type="text" name="city" value="" id="city" /></label><label>państwo:<input type="text" name="country" value="'+strCountry+'" id="country" /></label><input type="button" id="btnSearchAddress" class="button" value="Szukaj" /></div>');
		$('#gMapEdit').before('<p id="gMapHelp"></p>');
		$('#gMapEdit').after('<input type="button" class="button" id="gMapEditUsun" value="Usuń z mapy" />');
		$('#gMapHelp').text('Wyszukaj najpierw ulicę i miasto.');
		$('#gMapEdit').jmap({maptype:"map", clickmarker:false});
		
		
		// prepare start point
		var pktStart = new GPoint(0,0);
		var marker = new GMarker(pktStart, {draggable: true});
		$('#gMapEdit').myMap().addOverlay(marker);
		
		if($('input#'+prefix+'map_lat').val() == '')
		{
			GGeocoder = new GClientGeocoder();
			GGeocoder.getLatLng(''+strCity+' '+strCountry+'', function(point)
				{
					if(!point)
					{
						alert('nf');
					} else {
						pktStart = point;
						editMapSetLocation(pktStart, marker, true);
						marker.hide();
					}
				}
			);
		} else {
			pktStart = new GLatLng($('input#'+prefix+'map_lng').val(),$('input#'+prefix+'map_lat').val(), true);
			editMapSetLocation(pktStart, marker, true);
		}
		
		var dragPointStart = GPoint(0,0);

		GEvent.addListener(marker, "dragstart", function(){
			dragPointStart = marker.getPoint();
		});				
		
		GEvent.addListener(marker, "dragend", function(){
			pointlocation = marker.getPoint();
			editMapDragLocation(dragPointStart, pointlocation, marker);
		});				
		
		$('#btnSearchAddress').click(
			function()
			{	
				var address = $('#street').val() + ' '+$('#city').val() + ' ' + $('#country').val();
				GGeocoder = new GClientGeocoder();
				GGeocoder.getLatLng(address, function(point)
					{
						pktStart = point;
						$('#gMapEdit').myMap().setCenter(pktStart);
						setHelpClick();
						return true;
					}
				);
			}
		);
		
		$('#gMapEditUsun').click(
			function()
			{
				marker.hide();
				$('input#'+prefix+'map_lng').val('');
				$('input#'+prefix+'map_lat').val('');
			}
		);
		
		GEvent.addListener($('#gMapEdit').myMap(), "dblclick", function(overlay,point) 
			{
				var map = $('#gMapEdit').myMap();
				editMapSetLocation(point, marker, false, true);
				return false;
			}
		);
	}
	
	if(prefix == 'v-create-')
	{
		selectMultiSearchLocation('v-create-id_v_county', marker);
		
		$('select#v-create-id_v_county').click(
			function()
			{
				selectMultiSearchLocation('v-create-id_v_county', marker);
				return true;
			}
		);
		
		$('div#v-create-id_v_county input[@type=checkbox]').click(
			function()
			{
				selectMultiSearchLocation('v-create-id_v_county', marker);
				return true;
			}
		);
	}
	
	if(prefix == 'p-create-')
	{
		selectAjaxSearchLocation('p-create-id_county', 'p-create-id_district', marker);
		
		$('select#p-create-id_county').change(
			function()
			{
				selectAjaxSearchLocation('p-create-id_county', 'p-create-id_district', marker);
				return true;
			}
		);
	}
	
	if(prefix == 'ud-create-')
	{
		selectAjaxSearchLocation('ud-create-id_county', 'ud-create-id_district', marker);
		
		$('select#ud-create-id_county').change(
			function()
			{
				selectAjaxSearchLocation('ud-create-id_county', 'ud-create-id_district', marker);
				return true;
			}
		);
	}
	
	if(prefix == 'moto-create-')
	{
		selectAjaxSearchLocation('moto-create-id_county', 'moto-create-id_district', marker);
		
		$('select#moto-create-id_county').change(
			function()
			{
				selectAjaxSearchLocation('moto-create-id_county', 'moto-create-id_district', marker);
				return true;
			}
		);
	}	
	
	if(prefix == 'post-')
	{
		selectAjaxSearchLocation('post-id_category', '', marker);
		
		$('select#post-id_category').click(
			function()
			{
				selectAjaxSearchLocation('post-id_category', '', marker);
				return true;
			}
		);
	}
}


function selectMultiSearchLocation(id, marker)
{
	var prefix = getPrefix();
	if($('input#'+prefix+'map_lat').val() != '' || $('#city').val() != '')
		return true;
		
	var countSelected = 0;
	var selectedValue = '';
	var cType = 'none';
	
	if($('select#'+id).is('select'))
	{
		cType = 'multiselect';
		var cSelect = $('select#'+id).get(0);
		for(var i=0; i<cSelect.options.length; i++)
		{
			if(cSelect.options[i].selected)
			{
				countSelected++;
				if(!selectedValue)
					selectedValue = cSelect.options[i].text;
			}
		}
	}
	
	if($('select#'+id).is('select'))
	{
		cType = 'multiselect';
		var cSelect = $('select#'+id).get(0);
		for(var i=0; i<cSelect.options.length; i++)
		{
			if(cSelect.options[i].selected)
			{
				countSelected++;
				if(!selectedValue)
					selectedValue = cSelect.options[i].text;
			}
		}
	}
	
	if($('div#'+id).is('div'))
	{
		cType = 'multicheck';
		// for every label..
		$('div#'+id+' label').each(
			function()
			{
				// check if its input is selected
				if($('input', this).get(0).checked)
				{
					countSelected++;
					if(!selectedValue)
						selectedValue = $(this).text();
				}
			}
		);
	}
	
	if(myEngland)
	{
		if(selectedValue.indexOf('ENG') > -1)
		{
			strCountry = 'England';
		}
		if(selectedValue.indexOf('SCO') > -1)
		{
			strCountry = 'Scotland';
		}
		if(selectedValue.indexOf('WLS') > -1)
		{
			strCountry = 'Welsh';
		}
		if(selectedValue.indexOf('IRL') > -1)
		{
			strCountry = 'Ireland';
		}
		selectedValue = selectedValue.replace(/.*\/ /,'');
		selectedValue = selectedValue.replace('London Regions','London');
	}	

	
	if(selectedValue)
	{
		GGeocoder = new GClientGeocoder();
		GGeocoder.getLatLng(selectedValue + ' '+strCountry+'', function(point)
			{
				if(point)
				{
					editMapSetLocation(point, marker, true);
					marker.hide();
				}
			}
		);
	}
}

function selectAjaxSearchLocation(id, id_dist, marker)
{
	var prefix = getPrefix();
	if($('input#'+prefix+'map_lat').val() != '' || $('#city').val() != '')
		return true;
		
	var countSelected = 0;
	var selectedValue = '';
	var cType = 'none';
	
	if($('select#'+id).is('select'))
	{
		cType = 'select';
		var cSelect = $('select#'+id).get(0);
		if(cSelect.options.selectedIndex)
		{
			selectedValue = cSelect.options[cSelect.options.selectedIndex].text;
		}
		if($('select#'+id_dist).is('select'))
		{
			var dSelect = $('select#'+id_dist).get(0);
			if(dSelect.options.selectedIndex)
			{
				selectedValue = dSelect.options[dSelect.options.selectedIndex].text + ' ' + selectedValue;
			}
		}
	}
	if(myEngland)
	{
		if(selectedValue.indexOf('ENG') > -1)
		{
			strCountry = 'England';
		}
		if(selectedValue.indexOf('SCO') > -1)
		{
			strCountry = 'Scotland';
		}
		if(selectedValue.indexOf('WLS') > -1)
		{
			strCountry = 'Welsh';
		}
		if(selectedValue.indexOf('IRL') > -1)
		{
			strCountry = 'Ireland';
		}
		selectedValue = selectedValue.replace(/.*\/ /,'');
		selectedValue = selectedValue.replace('London Regions','London');
	}	
	
	if(selectedValue)
	{
		GGeocoder = new GClientGeocoder();
		GGeocoder.getLatLng(selectedValue + ' '+strCountry+'', function(point)
			{
				if(point)
				{
					editMapSetLocation(point, marker, true);
					marker.hide();
				}
			}
		);
	}
}


function setHelpDrag()
{
	$('#gMapHelp').text('Teraz możesz ustawić dokładnie punkt przesuwając go myszką. Możesz także ponowić wyszukiwanie wg adresu.');
}

function setHelpClick()
{
	$('#gMapHelp').text('Aby zaznaczyć punkt kliknij dwukrotnie na mapie.');
}


function setLatLng(point)
{
	var prefix = getPrefix();
	$('input#'+prefix+'map_lat').val(point.x);
	$('input#'+prefix+'map_lng').val(point.y);
	return true;
}

function editMapSetLocation(point, marker, discardQuestion, fromClick)
{
	var map = $('#gMapEdit').myMap();
	if(!fromClick)
		map.setCenter(point);
	
	if(!discardQuestion)
	{
		var markerNewPkt = new GMarker(point, {draggable: false});
		map.addOverlay(markerNewPkt);
		
		var myHtml = 'Czy zaznaczyć ten punkt?<br /><a id="srcPktTak" href="javascript:srcPktTakClick(0);">Tak</a> <a id="srcPktNie" href="javascript:srcPktNieClick(0);">Nie</a>';
		markerNewPkt.openInfoWindowHtml(myHtml);
		srcPktTakClick = function()
		{
			var map = $('#gMapEdit').myMap();
			markerNewPkt.closeInfoWindow();
			map.removeOverlay(markerNewPkt);
			map.setCenter(point);
			marker.setLatLng(point);
			marker.show();
			setLatLng(point);
			setHelpDrag();
// 			return false;
		}
		
		srcPktNieClick = function()
		{
			var map = $('#gMapEdit').myMap();
			markerNewPkt.closeInfoWindow();
			map.removeOverlay(markerNewPkt);
// 			return false;
		}
		
	} else {
		marker.setLatLng(point);
	}
}




function editMapDragLocation(point, pointEnd, marker, discardQuestion)
{
	var map = $('#gMapEdit').myMap();
	map.setCenter(pointEnd);
	
	if(!discardQuestion)
	{
		var myHtml = 'Czy zaznaczyć ten punkt?<br /><a id="srcPktTak" href="javascript:void(0);">Tak</a> <a id="srcPktNie" href="javascript:void(0);">Nie</a>';
		marker.openInfoWindowHtml(myHtml);
		$('#srcPktTak').click(
			function()
			{
				marker.closeInfoWindow();
				setLatLng(pointEnd);
				return false;
			}
		);
		$('#srcPktNie').click(
			function()
			{
				marker.closeInfoWindow();
				marker.setLatLng(point);
				return false;
			}
		);
	} else {
		marker.setLatLng(pointEnd);
	}
}


function afterSearch(point)
{
	jmap = $('#gMapEdit').myMap();
	jmap.setCenter(point);
}
/*	var marker = new GMarker(point, {draggable: true});
	jmap.addOverlay(marker);
	var pointlocation = marker.getPoint();
	var address = $('#street').val() + ' '+$('#city').val() + ' ' + $('#country').val();
	marker.openInfoWindowHtml(address);
	GEvent.addListener(marker, "dragend", function(pointlocation){
		marker.openInfoWindowHtml(address);			
	});
}*/

function searchView(point)
{
	jmap = $('#gMap').myMap();
	
	jmap.setCenter(point);
	var marker = new GMarker(point, {draggable: false});
	jmap.addOverlay(marker);
}



function initSearchForm()
{
	if(!$('#szukaj-of').is('div'))
		return false;
		
	if($.browser.msie)
	{
		$('#szukaj-of').append('<a id="szukaj-of-link" href="javascript:void(0)"></a>');
/*		$('a#szukaj-of-link')
			.css('border','1px solid red')
			.css('top','0')
			.css('left','0px');*/
	} else {
		$('#top').append('<a id="szukaj-of-link" href="javascript:void(0)"></a>');
	}
	$('#szukaj-of').css('position','relative');
		
	if(!$('#szukaj-of').get(0).className.match(/.*opened.*/))
	{
		hideSearchForm();
	}
	
	$('#szukaj-of-link')
		.click(
			function()
			{
				if($('#szukaj-of').get(0).className == 'off')
				{
					openSearchForm();
				} else {
					hideSearchForm();
				}
				return false;
			}
		);
	
}

function hideSearchForm()
{
	$('#szukaj-of').get(0).className = 'off';
	$('form#szukaj-form').hide();
}

function openSearchForm()
{
	$('#szukaj-of').get(0).className = 'on';
	$('form#szukaj-form').show();
}


function initNewItemForm()
{
	if(!$('form.js-addNewItem select').is('select'))
		return false;
		
	$('form.js-addNewItem select').change(
		function()
		{
			document.location.href = 'index.php?'+$('form.js-addNewItem select').val();	
			return false;
		}
	);
		
}



function initVersion()
{
	strCountry = 'Ireland';
	strCity = 'Dublin';
	if(document.location.href.indexOf('england') != -1)
	{
		strCountry = 'England';
		strCity = 'London';
	}
}

function showDeleteOfferConfirm()
{
	$('.jsUsunOgloszenie').click(function(){
		return confirm('Czy na pewno chcesz usunąć ogłoszenie?');
	});
}

function initMotoOfferSection()
{
	if(!$('#moto-create-offer_type').is('select'))
		return false;
	
	checkMotoOfferSection();
	
	$('#moto-create-offer_type').change(
		function()
		{
			checkMotoOfferSection();
			return true;
		}
	);	
	
}
	
	
function checkMotoOfferSection()
{
	var strType = $('#moto-create-offer_type').val();

	
	if(strType == 'sell' || strType == 'buy' || strType == 'exchange')
	{
		$('.jsHideMoto').show();
		var lblHtml = $('.jsChangeTitle').html().replace('Tytuł ogłoszenia', 'Model');

		$('.jsChangeTitle').html(lblHtml);
	} 
	else 
	{
		$('.jsHideMoto').hide();
		var lbl2Html = $('.jsChangeTitle').html().replace('Model', 'Tytuł ogłoszenia');

		$('.jsChangeTitle').html(lbl2Html);
	}
		
}

function initMotoOfferDistanceType()
{

	if(!$('#moto-create-distance_type').is('select'))
		return false;
	
	checkMotoOfferDistanceType();
	
	$('#moto-create-distance_type').change(
		function()
		{
			checkMotoOfferDistanceType();
			return true;
		}
	);	
	
}
	
	
function checkMotoOfferDistanceType()
{
	var strDistanceType = $('#moto-create-distance_type').val();

	$('.jsDistanceKm').hide();
	$('.jsDistanceMl').hide();
	

	
	if(strDistanceType == 'km')
	{
		$('.jsDistanceKm').show();

	} 
	else 
	{
		$('.jsDistanceKm').hide();
		$('.jsDistanceMl').show();

	}
		
}





function addAffiliateBannerMI(affiliate, type, action, idElement)
{
	if(action==1)
	{
		$(idElement).html("<div style='color:white; font-size:11px; font-family:Verdana,Arial,Helvetica,sans-serif; position:relative; width:468px; height:60px; background-image:url(http://www.anotherfriend.com/affiliate/images/banners/" + type + ".jpg);'><form action='http://www.anotherfriend.com/affiliate/actions.cfm?ac=" + affiliate + "&ac_id=3&banner_id=" + type + "' name='fsearch' method='post'><div style='position:absolute; left:347px; top:10px; width:100px; height:22px; text-align:left; '><input type='submit' value='' style='background-image:url(http://www.anotherfriend.com/images/affiliate_search.gif); cursor:pointer; width:109px; height:21px; background-color:transparent; border:none;' /></div><input type='hidden' name='sf' value='1'><input type='hidden' name='maxage' value='100'><input type='hidden' name='gtype' value='0'><div style='overflow:hidden; clear:both; position:absolute; top:42px; left:15px; width:450px;'><div style='float:left; width:45px; text-align:right; height:22px;'>I am a&nbsp;</div><div style='height:22px; float:left; width:85px;'><select name='am' style='background-color:white;width:70px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='1' selected='selected'>man</option><option value='0'>woman</option></select></div><div style='float:left; width:82px; text-align:right; height:22px;'>Looking for a&nbsp;</div><div style='height:22px; float:left; width:80px;'><select name='igender' style='background-color:white; width:75px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='1'>man</option><option value='0' selected='selected'>woman</option></select></div><div style='float:left; width:55px; margin-left:10px; height:22px;'>Located&nbsp;</div><div style='height:22px; float:left; width:70px;'><select name='County' style='background-color:white;width:80px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='0' selected='selected'>anywhere</option><option value='152'>Antrim</option><option value='153'>Armagh</option><option value='154'>Carlow</option><option value='155'>Cavan</option><option value='156'>Clare</option><option value='157'>Cork</option><option value='158'>Derry</option><option value='159'>Donegal</option><option value='160'>Down</option><option value='161'>Dublin</option><option value='162'>Fermanagh</option><option value='163'>Galway</option><option value='164'>Kerry</option><option value='165'>Kildare</option><option value='166'>Kilkenny</option><option value='167'>Laois</option><option value='168'>Leitrim</option><option value='169'>Longford</option><option value='170'>Louth</option><option value='171'>Limerick</option><option value='172'>Mayo</option><option value='173'>Meath</option><option value='174'>Monaghan</option><option value='175'>Offaly</option><option value='176'>Roscommon</option><option value='177'>Sligo</option><option value='178'>Tipperary</option><option value='179'>Tyrone</option><option value='180'>Waterford</option><option value='181'>Westmeath</option><option value='182'>Wexford</option><option value='183'>Wicklow</option></select></form></div></div></div>");
	}
	else if(action == 2)
	{
		$(idElement).html("<a href='http://www.anotherfriend.com/affiliate/actions.cfm?ac=" + affiliate + "&ac_id=1&banner_id=" + type + "' target='_blank' ><img src='http://www.anotherfriend.com/affiliate/images/banners/" + type + ".jpg' border='0' style='float:none;' ></a>");
	}
	else if(action == 3)
	{
		sendAffiliateData(type, affiliate);
		$(idElement).html("<a href='http://www.anotherfriend.com/affiliate/actions.cfm?ac=" + affiliate + "&ac_id=1&banner_id=" + type + "' target='_blank' ><img src='http://www.anotherfriend.com/affiliate/images/banners/" + type + ".jpg' border='0' style='float:none;' ></a>");
	}
	else if(action == 4)
	{
		sendAffiliateData(type, affiliate);
		$(idElement).html("<div style='color:white; font-size:11px; font-family:Verdana,Arial,Helvetica,sans-serif; position:relative; width:468px; height:60px; background-image:url(http://www.anotherfriend.com/affiliate/images/banners/" + type + ".jpg);'><form action='http://www.anotherfriend.com/affiliate/actions.cfm?ac=" + affiliate + "&ac_id=3&banner_id=" + type + "' name='fsearch' method='post'><div style='position:absolute; left:347px; top:10px; width:100px; height:22px; text-align:left; '><input type='submit' value='' style='background-image:url(http://www.anotherfriend.com/images/affiliate_search.gif); cursor:pointer; width:109px; height:21px; background-color:transparent; border:none;' /></div><input type='hidden' name='sf' value='1'><input type='hidden' name='maxage' value='100'><input type='hidden' name='gtype' value='0'><div style='overflow:hidden; clear:both; position:absolute; top:42px; left:15px; width:450px;'><div style='float:left; width:45px; text-align:right; height:22px;'>I am a&nbsp;</div><div style='height:22px; float:left; width:85px;'><select name='am' style='background-color:white;width:70px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='1' selected='selected'>man</option><option value='0'>woman</option></select></div><div style='float:left; width:82px; text-align:right; height:22px;'>Looking for a&nbsp;</div><div style='height:22px; float:left; width:80px;'><select name='igender' style='background-color:white; width:75px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='1'>man</option><option value='0' selected='selected'>woman</option></select></div><div style='float:left; width:55px; margin-left:10px; height:22px;'>Located&nbsp;</div><div style='height:22px; float:left; width:70px;'><select name='County' style='background-color:white;width:80px;border:1px solid #AAB5D5;font-size:10px; font-family:Verdana,Arial,Helvetica,sans-serif;'><option value='0' selected='selected'>anywhere</option><option value='152'>Antrim</option><option value='153'>Armagh</option><option value='154'>Carlow</option><option value='155'>Cavan</option><option value='156'>Clare</option><option value='157'>Cork</option><option value='158'>Derry</option><option value='159'>Donegal</option><option value='160'>Down</option><option value='161'>Dublin</option><option value='162'>Fermanagh</option><option value='163'>Galway</option><option value='164'>Kerry</option><option value='165'>Kildare</option><option value='166'>Kilkenny</option><option value='167'>Laois</option><option value='168'>Leitrim</option><option value='169'>Longford</option><option value='170'>Louth</option><option value='171'>Limerick</option><option value='172'>Mayo</option><option value='173'>Meath</option><option value='174'>Monaghan</option><option value='175'>Offaly</option><option value='176'>Roscommon</option><option value='177'>Sligo</option><option value='178'>Tipperary</option><option value='179'>Tyrone</option><option value='180'>Waterford</option><option value='181'>Westmeath</option><option value='182'>Wexford</option><option value='183'>Wicklow</option></select></form></div></div></div>");
	}
}

function runAdMi(adType, idElement)
{
	$.getJSON(
		'index.php?ecmsGetAd='+adType,
		{
			getAd: adType
		},
		
		function(returnData)
		{

/*			if(returnData.ID == '4')
			{
				addAffiliateBannerMI('myireland.pl', 97, 3, '#'+idElement);
				return true;
			}*/
			if(returnData.flash)
			{
				
				var flashvars = {
 					clickParam : returnData.url
				};
				var params = {
					wmode: 'opaque',
					allowscriptaccess: 'always',
					allownetworking: 'all'
				};
				
				var attributes = { };
				
				if(returnData.width < 1)
				{
					returnData.width = 160;
					returnData.height = 160;
				}
			
				//$('#'+idElement).width(160).height(160);
				
//				$('body').append('swfobject.embedSWF('+returnData.flash+', '+idElement+', 160, 160, "9", "", flashvars, params, attributes);');
//				$('body').append('<a href="files/Banners/flash/'+returnData.flash+'">test</a>');
				$('#'+idElement).html('<div id="'+idElement+'Tmp"></div>');
				swfobject.embedSWF('files/Banners/flash/'+returnData.flash, idElement+'Tmp', 160, 160, "9", "", flashvars, params, attributes);
				
			}
			
			if(returnData.image)
			{
				var strToInsert = '<a href="'+returnData.url+'" rel="external"><img src="'+returnData.image+'" ';
				if(returnData.width != '0')
					strToInsert += 'width="'+returnData.width+'" height="'+returnData.height+'"';
				
				strToInsert += ' alt="" /></a>';
				
				$('#'+idElement).append(strToInsert);
				$('#'+idElement+' a')
					.attr('target','_blank')
					.click( function(){ ecmsAdRemoveBkg(); return true; } );
			}
			
// 			if(returnData.toplayer)
// 			{
// 				var pageSize = ecmsAd_getPageSize();
// 				$("body","html").css({height: "100%", width: "100%"});
// 				$("html").css("overflow","hidden");
// 				
// 				$('#'+idElement).wrap('<div id="ecmsAdContent"></div>');
// 				$('#ecmsAdContent').before('<div id="ecmsAdBkg"></div>');
// 				$('#ecmsAdBkg')
// 					.css('position', 'absolute')
// 					.css('width', '100%')
// 					.css('height', pageSize[1]+'px')
// 					.css('background', '#000')
// 					.css('opacity', '0.4')
// 					.css('top', '0')
// 					.css('left', '0')
// 					.click( function(){ ecmsAdRemoveBkg(); return true; } );
// 					
// 				pageSize[0] = Math.round(pageSize[1]/2);
// 				$('#ecmsAdContent')
// 					.css('position', 'absolute')
// 					.css('top', '100px')
// 					.css('border', '1px solid red')
// 					.css('left', pageSize[0]+'px')
// 					.click( function(){ ecmsAdRemoveBkg(); return true; } );
// // 					.css('left', '50%');
// 					
// 				
// 			}
// 			
			return true;
		}
	);
}

function ecmsMiAdRemoveBkg()
{
// 	$('#ecmsAdContent').remove(); 
// 	$("body","html").css({height: "auto", width: "auto"});
// 	$("html").css("overflow","");
// 	$('#ecmsAdBkg').remove(); 
// 	return true;
}

function ecmsMiAd_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}


/* init */
function initializeEcmsMiAds()
{
	$('.ecmsAds').each(
		function(i)
		{
			var adType = $(this).attr('class').replace('right-adv ecmsAds adType','');
			$(this).attr('id','ecmsAdID'+i)
			runAdMi(adType, 'ecmsAdID'+i);
		}
	);
}


/* init */
function initializeMain()
{
// 	initZglosProblem();
	initVersion();
	initMaps();
	initSearchForm();
	initNewItemForm();
	showDeleteOfferConfirm();
	initMotoOfferSection();
	initMotoOfferDistanceType();
	
	initializeEcmsMiAds();
}


$(document).ready(initializeMain);
