var period = null;
var counter =3;
window.addEvent('domready', function() {
	
	for(c=0;c<NumberOfRecords;c++){
			var url = "index.php?option=com_csbronni&view=tours&layout=module&tmpl=ajax";
			new Ajax(url, {
				method: 'post',
				update: $('cell-'+c),
				data:{	
					block_id: ($('ajaxBlock_id').value),
					item_id: ($('ajaxItem_id').value),
					cache_time:($('cachetime').value),
					uniq_id:($('uniqId-'+c).value)
				},
				onComplete: function(response) {
				counter--;
				//if(counter>0 && response.contains('<!--ItIsMistake-->'))
					//var again = ajaxUpdateMod();
				//else counter = 0;
			 	}		
			}).request();
	}




	
	//period = ajaxUpdateMod();
});

var ajaxUpdateMod = function(){
	}
	
ShowcaseElement = function()
{
	this.departureCity = -1;
	this.destinationCountry = -1; 
	this.destinationCities = [];
	this.hotels = [];
	this.stars = [];
	this.meals = [];
  this.image = null;
  this.is_useimage = -1
  this.country_nickname = null;
	this.departureDateFromNowCount = -1;
	this.departureDateCount = -1;
	this.nightsFrom = -1;
	this.nightsTo = -1;
	this.priceFrom = -1;
	this.priceTo = -1;
	this.currency = -1;
	this.adult = -1;
	this.child = -1;
	this.operatorIds = [];
	this.positionCount = -1;
	this.filtrateIdenticalHotels = false;
}


function toPost(sc) 
{
	
	var hbForm = new Element('form', {
		action: urlHotOffers,
		method: 'post',
		target: '_blank',
		styles: {
			display: 'none'
		}
	});	
	var date_offset = Math.round((sc.departureDateCount - sc.departureDateFromNowCount) / 2);
	// структурные переменные
	new Element('input', { type: 'hidden', name: 'view', value: 'tours' }).injectInside(hbForm);
	new Element('input', { type: 'hidden', name: 'layout', value: 'result' }).injectInside(hbForm);
	new Element('input', { type: 'hidden', name: 'option', value: 'com_csbronni' }).injectInside(hbForm);
	// переменнные запроса
	new Element('input', { type: 'hidden', name: 'departureCity', value: sc.departureCity }).injectInside(hbForm);
	new Element('input', { type: 'hidden', name: 'destinationCountry', value: sc.destinationCountry }).injectInside(hbForm);
	datesFrom = new Date().addDays(strToIntHB2(sc.departureDateFromNowCount) + strToIntHB2(date_offset)).toRUString();
  //var datesFrom = getsDate(sc.departureDateFromNowCount, date_offset);
  
	new Element('input', { type: 'hidden', name: 'checkinDateStart', value: datesFrom }).injectInside(hbForm);
	new Element('input', { type: 'hidden', name: 'checkinDateOffset', value: date_offset }).injectInside(hbForm);
	new Element('input', { type: 'hidden', name: 'currency', value: sc.currency }).injectInside(hbForm);
	



sc.stars.each(function(el){
		new Element('input', { type: 'hidden', name: 'star[]', value: el }).injectInside(hbForm);
	});

	
	if(sc.nightsFrom > -1) new Element('input', { type: 'hidden', name: 'nightsFrom', value: sc.nightsFrom }).injectInside(hbForm);
	if(sc.nightsTo > -1)   new Element('input', { type: 'hidden', name: 'nightsTo', value: sc.nightsTo }).injectInside(hbForm);
	if(sc.adult > -1)      new Element('input', { type: 'hidden', name: 'adultsCount', value: sc.adult }).injectInside(hbForm);
	if(sc.child > -1)      new Element('input', { type: 'hidden', name: 'childrenCount', value: sc.child }).injectInside(hbForm);
	if(sc.priceFrom > -1)  new Element('input', { type: 'hidden', name: 'costFrom', value: sc.priceFrom }).injectInside(hbForm);
	if(sc.priceTo > -1)    new Element('input', { type: 'hidden', name: 'costTo', value: sc.priceTo }).injectInside(hbForm);

	// сделаем высплывающее окно
	// расчитываем ширину создаваемого окна
  var popupWidth = parseInt(screen.width * 0.8); 
  if(popupWidth > 980) popupWidth = 980;
  // формируем список свойств и создаем окно
  var winprops = 'width='+popupWidth+',scrollbars=1,resizable';
  var winname = 'result' + (Math.random() % 1);
  win = window.open('', winname, winprops);
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
  // указываем форме в каком окне будет показан результат
  hbForm.setProperty('target', winname);
	hbForm.injectInside($$('body')[0]); 
	hbForm.submit();
	
}

function getsDate(dateFrom, offset)
{
	var d = new Date();
	var n = new Date(d.getFullYear(), d.getMonth(), d.getDate() + dateFrom + offset);
   var year = n.getFullYear();
   var day = n.getDate();
   var month = n.getMonth() + 1;
   var result = day + '.' + month + '.' + year; 
	return result;
}

function HB2RenderItemsListBox(array, control)
{
	var opt;

	if ((array == null) || (control == null)){return;}
	
	for(var c=control.children.length; c>0; c--) control.removeChild(control.childNodes[c-1]);
	
	for (var i=0;i<array.length; i++)	{
	  opt = new Element('option', {'value': array[i], 'selected': true});
	  opt.setProperty('selected', true);
	  opt.innerHTML = array[i];
	  opt.injectInside(control);
	}
}

function strToIntHB2(str) {
	if ((str != null) && ($type(str) == 'string')) {
		var lastLocation = str.lastIndexOf("px");
		// remove the identified section, if it is a valid region
		if (lastLocation >= 0) str = str.substring(0, lastLocation);
	}
	var obj;
	obj = parseInt(str);
	if (!isNaN(obj)) {return obj;} else {return -1;}
};

function strArrayToIntArrayHB2(arr) {
	var obj = [];
	if (arr.length > 0) {
		for (i=0; i<arr.length; i++) {obj[i] = strToIntHB2(arr[i])}
	}
	return obj;
};

