var is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));

function popupwin(name, url, width, height)
{
  width = (!width) ? 800 : width;
  height = (!height) ? 600 : height;

  var xpos = (screen.width - width)/2;
  var ypos = (screen.height - height)/2;
  w = window.open(url, name,'fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',top='+ypos+',left='+xpos); 
  w.focus();
  return false;
}

/*
function employee_list(id)
{
	var elm = $(id);
	// elm.className = (elm.className == 'div_visible') ? 'div_hidden' : 'div_visible';
	elm.style.display = elm.style.display == 'none' ? 'block' : 'none';
}
*/
function frm_employee_send(city_id, metro_id, agency_id)
{	
        new Ajax('/x_gate/?action=get_employees_list',{
        	'update' : 'contentonly',
        	'data' : {
			'action' : 'get_employees_list',
			'city_id': city_id,
			'metro_id': metro_id,
			'agency_id' : agency_id
        	}
//        	onComplete: Myfunction() {}
        }).request();

}

function set_display(id, handler) 
{
	var elm = $(id);
	if (!elm)  return;
	elm.style.display = elm.style.display == 'none' ? '' : 'none';

	
	
	var toggler = $(handler);
	if ($type(toggler) != 'element')
		return;
	
	if (toggler.hasClass('myclass')) {
		toggler.removeClass('myclass');
		toggler.addClass('myclassclose');
	} else {
		toggler.removeClass('myclassclose');
		toggler.addClass('myclass');
	}
}

var AxContents = {};

var AjaxAccordion = Accordion.extend({

	display: function(index) {
		var toggler = this.togglers[index];
		var element = this.elements[index];

		if(!toggler)
			return;

		var url = toggler.getProperty('ax_url');

		var target = toggler.getProperty('ax_target');
		if (!AxContents[target]) {
			new Ajax(url, {
				method:"get", 
				onComplete: function(text) 
				{
					AxContents[target] = text;
					element.setHTML(text);

					this.parent(index);

					initAjaxPagesNav(this, index);

				}.bind(this)
			}).request();
		} else
			this.parent(index);
	}
});
var acc4_pos = -1;
var acc_pos = -1;


window.addEvent('domready', function(){
	var acc1 = $('accordion');
	if (acc1) {
		new Accordion('tr.atStart', 'div.atStart', {
			display: acc_pos = acc_pos == -1 ? -1 : acc_pos-1,
			opacity: false
		}, acc1);
	}

	var acc4 = $('accordion4');
	if (acc4) {

		new Accordion('tr.atStart4', 'div.atStart4', {
			display: acc4_pos = acc4_pos == -1 ? -1 : acc4_pos-1,
			opacity: false
		}, acc4);
	}

	var acc3 = $('accordion3');
	if (acc3) {
		ddd = new AjaxAccordion('table.atStart3', 'div.atStart3', {
//			display: 0,
			opacity: false,
			alwaysHide : true,
			onActive: function(toggler, element){
				toggler.addClass('myclass');
			},
			onBackground: function(toggler, element){
       				toggler.removeClass('myclass');
			}


		}, acc3);
	}
	
	var acc2 = $('accordion2');
	if (acc2) {
		new AjaxAccordion('table.atStart2', 'div.atStart2', {
//			show : 0,
			display: 0,
			opacity: false,
			alwaysHide : true,
			onActive: function(toggler, element){
				toggler.addClass('myclass');
//				toggler.setStyle('text-style', 'italic');
			},
			onBackground: function(toggler, element){
       				toggler.removeClass('myclass');
//				toggler.setStyle('text-style', 'bold');
			}
		}, acc2);
	}

});


function getSpoPageData(elm)
{
	var url = elm.getProperty('ax_url');
	//<a href="?page=2&form=country_spo&country_id=6&max_date=24-06-2009&min_date=25-05-2009">2</a>
	//var target = toggler.getProperty('ax_target');

	new Ajax(url, {
		method:"get", 
		onComplete: function(text) {
			//AxContents[target] = text;
			elm.setHTML(text);
			//this.parent(index);
		}.bind(this)
	}).request();
}
function initAjaxPagesNav(accordion, countryIndex)
{


	var countryElm = accordion.elements[countryIndex];

	$$("span.pagesbar a").each(function(item, index)
	{
		var curentHref = item.getProperty('href');
		var replaceHref = "javascript:void(0);";

        var vi = curentHref.indexOf("?");


		if( vi > 0)
		{
			curentHref = curentHref.substr(vi);
			
		}
        

		if(curentHref == replaceHref)
			return;

		var axUrl= '/twforms/index.php'+curentHref;
		item.setProperty("href", replaceHref);
		item.addEvent('click', function(event)
		{


  		 	//countryElm.effect('height',{duration: 1000,transition: Fx.Transitions.bounceOut});
  		 	//{duration: 500, wait:false, transition: Fx.Transitions.linear});
  		 	//countryElm.setHTML('');
  		 	accordion.options.height = false;
			accordion.display(countryIndex);

			new Ajax(axUrl, {
				method:"get", 
				onComplete: function(text) 
				{				


					countryElm.setHTML(text);
					initAjaxPagesNav(accordion,countryIndex);

					var tt = function(){
					accordion.display(countryIndex);
					}
					tt.delay(1000);

				}.bind(this)
			}).request();

		});

	}); 

}

function initSPOCountryAjaxPagesNav(mod)
{
	var toursDataElmId = 'tour_'+mod;

	$$("#"+toursDataElmId+" span.pagesbar a").each(function(item, index)
	{
		var curentHref = item.getProperty('href');
		var replaceHref = "javascript:void(0);";

        var vi = curentHref.indexOf("?");


		if( vi > 0)
		{
			curentHref = curentHref.substr(vi);
			
		}
        

		if(curentHref == replaceHref)
			return;

		var axUrl= '/x_gate/'+curentHref;
		item.setProperty("href", replaceHref);
		item.addEvent('click', function(event)
		{

			new Ajax(axUrl, {
				method:"get", 
				onComplete: function(text) 
				{				
					$(toursDataElmId).setHTML(text);
					initSPOCountryAjaxPagesNav(mod);

				}.bind(this)
			}).request();

		});

	}); 

}

