var tmp_cache = new Object();
tmp_cache['sejururi'] = "";
tmp_cache['cazare'] = "";
tmp_cache['circuite'] = "";
tmp_cache['bilete_avion'] = "";
tmp_cache['croaziere'] = "";
tmp_cache['inchirieri_auto'] = "";
tmp_cache['transport'] = "";
var __last_cache = 'sejururi';
function initSearch(o){
	if(o != 'inchirieri_auto'){
		bindCalendars("d1","d2");
		$("#destinatie").autocomplete("/index.php?p=ajax&a=sugest&&mm="+o,{
			minChars:2,
			matchSubset:0,
			matchContains:1,
			cacheLength:1,
			onItemSelect:selectItem,
			formatItem:formatItem,
			selectOnly:1
		});
	}
	if( o = 'inchirieri_masini' ) incarcaMasini();

}
function cauta( o ){
	$('#cauta_'+__last_cache).removeClass("s_"+__last_cache+"_active");
	$('#cauta_'+__last_cache).addClass("s_"+__last_cache);
	$('#cauta_'+o).addClass("s_"+o+"_active");

	tmp_cache[__last_cache] = $( '#m_cauta' ).html();


	__last_cache = o;
	if(tmp_cache[o] == ""){
		$.ajax({
			type: "POST",
			url: "/index.php?p=ajax&a=mc&sm="+o,
			success: function(r){
				document.getElementById('m_cauta').innerHTML =  r ;
				$('#cauta_'+o).addClass("selected");
				bindCascades();
				tmp_cache[o] = r;
				initSearch(o);

			}
		});
	}
	else{
		$( '#m_cauta' ).html( tmp_cache[o] );
		initSearch(o);

	}
	return;
}

$(document).ready( function(){
	bindCalendars("d1","d2");
})

var Cascade = new Object();
var debug = 0;

function disableNext( o ){
	f = String(o.form.name);
	n = String(o.name);
	if( Cascade[f][n]['l'] ){
		obj = o.form[Cascade[f][n]['l']];
		obj.disabled = true;
		obj.length = 0;
		disableNext( o.form[Cascade[f][n]['l']] );
	}
}

function LoadNextSelect( o ){
	f = String(o.form.name);
	n = String(o.name);
	v = $(o).val();

	if( v ){
		if( Cascade[f][n]['l'] ){
			l = Cascade[f][n]['l'];
			t = Cascade[f][l]['t'];

			o.form[l].disabled = false;
			o.form[l].length = 0;
			o.form[l].options[0] = new Option( "Loading...", "" );
		}

		$.ajax({
			type: "POST",
			url: "/index.php?p=ajax&a=incarca-regiuni",
			data: 'id_tara_'+f+'='+v,
			success: function(r){
				if( debug ) alert( r );
				o.form[l].options[0] = new Option( t, "" )
				aOptions = r.split(",");
				for( i = 0; i < aOptions.length; i++ ){
					vals = aOptions[i].split(":");
					o.form[l].options[i+1] = new Option( vals[1], vals[0] );
				}
			}
		});
		disableNext( o.form[l] );
	}
	else
	disableNext( o );
}

function bindCascades(){
	$('.cascade').each( function(){
		if( this.title ){
			aOpt = this.title.split( ',' );
			f = String(this.form.name);
			n = String(this.name);
			var obj = this;
			jQuery.each( aOpt, function(i){
				if( aOpt[i].indexOf( 'l:' ) == 0 ){
					$(obj).bind( 'change', function(){
						LoadNextSelect( this );
					});
					Cascade[f] = !Cascade[f] ? new Object() : Cascade[f];
					Cascade[f][n]  = !Cascade[f][n] ? new Object() : Cascade[f][n] ;
					Cascade[f][n]['l'] = aOpt[i].substr( 2 );
				}
				if( aOpt[i].indexOf( 't:' ) == 0 ){
					Cascade[f] = !Cascade[f] ? new Object() : Cascade[f];
					Cascade[f][n]  = !Cascade[f][n] ? new Object() : Cascade[f][n] ;
					Cascade[f][n]['t'] = aOpt[i].substr( 2 );
				}
			});
			obj = null;
			$(this).removeAttr('title');
		}
	});
}

var pre_flash = new Array();

pre_flash[0] = {
	link : 'http://www.infoturism.ro/7recomandari/',
	poza : '470_7recomandari.jpg',
	titlu : '7 Recomandari',
	subtitlu : '7 recomandari selectate de catre consultantii nostrii'
}

var pre_current = 1;

function pre_change(){

	lastElement = pre_flash.length;
	if( pre_current >= lastElement )
	pre_current = 0;

	$( "#holder" ).prepend( '<div class="pozele2" id="pozele2"><a href="'+pre_flash[pre_current].link+'" target="_blank" rel="nofollow" id="pre_link2"><img src="" border="0" width="470" height="195" alt="" id="pre_img" /></a></div>' );
	$( "#pre_img" ).attr({
	'src': "http://static.infoturism.ro/interface/pre_hp/" + pre_flash[pre_current].poza,
	'id': ''
	});

	$( '#textele' ).animate( {"top":"194px"}, 500, "easeInQuad", function(){
		$( "#pre_titlu" ).html( pre_flash[pre_current].titlu );
		$( "#pre_subtitlu" ).html( pre_flash[pre_current].subtitlu );
		$( "#pozele" ).fadeOut( "slow" , function(){
			$( "#pozele" ).remove();
			$( "#pozele2" ).attr({
			"id":"pozele",
			"class":"pozele"});
			$( "#pre_link" ).href = pre_flash[pre_current].link;
			$( "#pre_link2" ).href = pre_flash[pre_current].link;
			$( '#textele' ).animate( {"top":"143px"}, 500, "easeInQuad", function(){
				pre_current++;
			});
		});
	});
}
var sentItem = null;
var sentVal = null;
var stop = false;
var clickedItem = null;
var submit = false;
var noLoca = 0;

function checkSubmit(){
	var lll = '';
	$('.hpn_t_search_destinatie').each(function(i,v){
		if( v.value != '' )
		lll = lll + v.value
	});
	if( lll == '' )
	return false;
	submit = true;
	if( clickedItem )
	return false;
}

function bindKeyUp(){
	$('.locatie').each(function(i,v){
		$(v).bind( "keydown", function(event){
			if( event.keyCode == 13 )
			v.form['d2'].focus();
		});
		$(v).bind( "blur", function(){
			if( stop ) return;
			stop = true;
			sentItem = v;
			sentVal = v.value;
			$(v).addClass( 'locatie_1' );
			$.ajax({
				type: "POST",
				url: "/index.php?p=ajax&a=circuite",
				data: v.name+"="+v.value,
				success: function(r){
					//alert(r);
					if( r == 0 ){
						var dest = new Array();
						if( v.form['destinatie1'].value )
						dest[0] = v.form['destinatie1'].value.toUpperCase();
						if( v.form['destinatie2'].value )
						dest[1] = v.form['destinatie2'].value.toUpperCase();
						if( v.form['destinatie3'].value )
						dest[2] = v.form['destinatie3'].value.toUpperCase();
						alert( "Nu am gasit circuite in baza de date care sa treaca prin \n" + dest.join() );
						sentItem.value = '';
						sentItem.focus();
						stop = false;
						if( sentVal != '' )
						submit = false;
					}
					else{
						if( !stop ){
							if( clickedItem )
							clickedItem.focus();
							else
							v.focus();
							sentItem = null;
							sentVal = null;
						}
						else
						stop = false;
						clickedItem = null;
						if( submit )
						v.form.submit();
					}
					$(v).removeClass( 'locatie_1' );
				}
			});
		});
		$(v).bind( "focus", function(){
			clickedItem = v;
		})
	});
}


var debug = 0;
function incarcaMasini(){
	$('.primire').bind('change', function(){
		oSel = this;
		voSel = oSel.value;
		oForm = oSel.form;

		oForm['predare'].length = 1;
		oForm['marca'].length = 1;
		oForm['model'].length = 1;

		var toload = new Array( "predare", "marca" );

		$.ajax({
			type: "POST",
			url: "/index.php?p=ajax&a=rentacar-loader",
			data: 'primire='+voSel,
			success: function(r){
				if( debug ) alert( r );
				aSelects = r.split( ";" );
				$(aSelects).each(function(i,op){
					aOptions = op.split(",");
					$(aOptions).each(function(j,vl){
						vals = vl.split(":");
						oForm[toload[i]].options[j+1] = new Option( vals[1], vals[0] );
					});
				});
			}
		});
	});
	$('.predare').bind('change', function(){
		oSel = this;
		voSel = oSel.value;
		oForm = oSel.form;

		oForm['marca'].length = 1;
		oForm['model'].length = 1;

		$.ajax({
			type: "POST",
			url: "/index.php?p=ajax&a=rentacar-loader",
			data: 'predare='+voSel,
			success: function(r){
				if( debug ) alert( r );
				aOptions = r.split(",");
				$(aOptions).each(function(j,vl){
					vals = vl.split(":");
					oForm['marca'].options[j+1] = new Option( vals[1], vals[0] );
				});
			}
		});
	});
	$('.marca').bind('change', function(){
		oSel = this;
		voSel = oSel.value;
		oForm = oSel.form;

		oForm['model'].length = 1;

		$.ajax({
			type: "POST",
			url: "/index.php?p=ajax&a=rentacar-loader",
			data: 'marca='+voSel,
			success: function(r){
				if( debug ) alert( r );
				aOptions = r.split(",");
				$(aOptions).each(function(j,vl){
					vals = vl.split(":");
					oForm['model'].options[j+1] = new Option( vals[1], vals[0] );
				});
			}
		});
	});
}