//javascript
var clickStar = 0;
function init_site(){
	$j(".elem_list").unbind('hover').hover(
	function(){$j(this).css('background-color','#ffc')},
	function(){$j(this).css('background-color','#fff')}
	);

	$j(".quick_search").unbind('focus').focus(function(){
		$j(this).css("color","#000");
		if ($j(this).val() == $j(this).attr('title')){
			$j(this).val('');
		}
	});

	$j(".quick_search").unbind('blur').blur(function(){
		$j(this).css("color","#ccc");
		if ($j(this).val() == ''){
			$j(this).val($j(this).attr('title'));
		}
	});

	$j(".cauta-hotel .steluta").unbind('hover').hover(
	function () {
		var id = $j('.steluta').index(this);
		$j(this).addClass("selected");
		$j('.cauta-hotel .steluta:lt('+id+')').addClass("selected");
	},
	function () {
		if (clickStar==0){
			$j('.cauta-hotel .steluta').removeClass("selected");
		}else{
			var tmp = clickStar-1;
			$j('.cauta-hotel .steluta:gt('+tmp+')').removeClass("selected");
		}

	}
	);

	$j(".st_conf .steluta").unbind('hover').hover(
	function () {
		var id = $j('.st_conf .steluta').index(this);
		$j(this).addClass("selected");
		$j('.st_conf .steluta:lt('+id+')').addClass("selected");
	},
	function () {
		if (clickStar==0){
			$j('.st_conf .steluta').removeClass("selected");
		}else{
			var tmp = clickStar-1;
			$j('.st_conf .steluta:gt('+tmp+')').removeClass("selected");
		}

	}
	);

	$j(".steluta").unbind('click').click(function(){

		var cls = $j(this).parent().parent().parent().attr('class');

		var id = $j('.'+cls+' .steluta').index(this);

		if (clickStar == id+1){
			clickStar = 0;
		}else{
			clickStar = id+1;
			var tmp = clickStar-1;
			$j('.'+cls+' .steluta:gt('+tmp+')').removeClass("selected");
		}

		$j('.'+cls+' input[name="stele"]').val(clickStar);
		return false;
	});

	if (PATH.link.a!='admin'){
		$j("#cauta_destinatie").autocomplete(PATH.root+"autocomplete/",{
			delay:10,
			width:'200px',
			minChars:3,
			matchSubset:0,
			matchContains:1,
			cacheLength:10,
			formatItem:formatItem,
			onItemSelect:onItemSelect,
			autoFill:false
		});
	}
	var lang_men_in_miscare=0;
	$j("#drp_arr").unbind('click').bind('click',function(e){
		if ($j('#drpdwn').is(':hidden')){
			lang_men_in_miscare = 1;
			$j('#drpdwn').slideDown('slow',function(){lang_men_in_miscare=0});
		}else{
			$j("#drpdwn").slideUp();
		}
	})
	$j("body").not("#drp_arr").unbind('click').bind('click',function(e){
		if ($j('#drpdwn').is(':visible') && !lang_men_in_miscare){
			$j("#drpdwn").slideUp();
		}
	})

	$j("#drpdwn_st a").unbind('click').bind('click',function(e){
		if ($j('#drpdwn').is(':hidden')){
			$j('#drpdwn').slideDown('slow',function(){lang_men_in_miscare=0;})
		}else{
			$j("#drpdwn").slideUp();
		}
		return false;
	})

	$j('#lg_ch select option').unbind('click').click(function(){
		//alert($j(this).val()+'-'+$j(this).parent().attr('name'));
		$j("input[name='action']").val('Schimb limba');
		$j("#form").submit();

	});

	$j('#ajax_loader').ajaxStart(function(){
		$j(this).show();
	}).ajaxStop(function(){
		$j(this).hide();
	});
	
	$j(document).mousemove(function(e){
		$j("#ajax_loader").css({
			top: e.pageY-10 + "px",
			left: e.pageX+10 + "px"
		});
});
	$j("#plecare_zi").unbind('change').bind('change','plecare',changeHiddenDate);
	$j("#plecare_luna").unbind('change').bind('change','plecare',changeHiddenDate);
	$j("#intoarcere_zi").unbind('change').bind('change','intoarcere',changeHiddenDate);
	$j("#intoarcere_luna").unbind('change').bind('change','intoarcere',changeHiddenDate);
}
$j(function(){
init_site();
})
function changeHiddenDate(e){
	//lf_date_s
	//lf_date_e
	var tip=e.data;
	var d=new Date();
	var curr_year = d.getFullYear();
	if (tip=='plecare'){
			var p_luna = $j("#plecare_luna").val();
			var p_zi = $j("#plecare_zi").val();
			$j("#lf_date_s").val(p_zi+'.'+p_luna+'.'+curr_year);
			var data_e = $j("#lf_date_s").datepicker('getDate');
			var new_dat = new Date(curr_year,data_e.getMonth(),data_e.getDate()+7);
			$j("#lf_date_e").datepicker("setDate",new_dat);
			var data_s = $j("#lf_date_e").datepicker("getDate");
			var tmonth = data_s.getMonth()+1;
			if (tmonth<10){
				tmonth = '0'+tmonth;
			}
			$j("#intoarcere_luna").val(tmonth);
			
			$j("#intoarcere_zi").val(data_s.getDate());
		
	}
	if (tip=='intoarcere'){
			var i_luna = $j("#intoarcere_luna").val();
			var i_zi = $j("#intoarcere_zi").val();
			$j("#lf_date_e").val(i_zi+'.'+i_luna+'.'+curr_year);
		
	}
}
function refreshDates(val){
	var stDate = val.split('.');
	var today = new Date();
	var startDate = new Date(stDate[2],stDate[1]-1,stDate[0]);
	var oneDay=1000*60*60*24;
		
	var dif = Math.ceil((startDate.getTime()-today.getTime())/oneDay);
	
	$j('#data_e').datepicker({minDate: dif+1});
}

jQuery.fn.calendar=function (){
	this.datepicker({
		showAnim: 'fadeIn',
		minDate: "+1d",
		maxDate: "12m",		
		beforeShow: customRange,
		onClose: modifData_e,
		dateFormat: 'dd.mm.yy'

	});
	return this;
}
function customRange(input) {
	dateMin = $j("#data_s").datepicker("getDate");
	if (dateMin){
		var rangeMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);

		return {minDate: (input.id == "data_e" ? rangeMax : 1),
		maxDate: null};
		//maxDate: (input.id == "data_s" ? $j("#data_e").datepicker("getDate") : null)};
	}
}

function modifData_e(date,picker){
	var id = $j(this).attr('id');
	if (id=='data_s'){
		var data_s = $j(this).datepicker("getDate");
		var data_e = $j("#data_e").datepicker("getDate");
		
		if (data_s>data_e){
			var newData_e = new Date(data_s.getFullYear(), data_s.getMonth(), data_s.getDate() + 1);
			$j("#data_e").datepicker("setDate",newData_e);
		}
	}
}

function ajaxSearch(de_cautat,action,container,tpl){

	de_cautat = de_cautat.replace(/&/,'{amp}');
	
	
	var v = getUrlVars();

	if (v['tara_id']!=''){
		action += "/?tara_id="+v['tara_id'];
	}

	$j.ajax({
		type: "POST",
		url: PATH.root+'ajax/'+action,
		data: 'de_cautat='+de_cautat+'&tpl='+tpl+'&url='+escape(window.location.href),
		success: function(rasp){
			$j("#"+container).html(rasp);
		}

	});
}

function ajaxTrad(obj,section,container,tpl,win){
	if (win!=''){
		WIN.close(win);
	}
	$j.ajax({
		type: "POST",
		url: PATH.root+"ajax/trad_"+section,
		data: "data="+obj.val+"&id="+obj.id+"&tpl="+tpl+'&url='+escape(window.location.href),
		success: function(rasp){

			$j("#"+container).html(rasp)
		}
	});
}

function submitForm(formId){

	var date = $j('#'+formId).serialize();

	$j.ajax({
		type: "POST",
		url: PATH.root+"submit_form/",
		data: date,
		beforeSend: function(r){
			var w8Btn = '<a href="#" onclick="return false" class="submit_forom_btn"><span>Sending</span></a>';
			$j("#send_btn_box").html(w8Btn);
		},
		success: function(r){
			if (r.search("notif_ok")!=-1){
				$j("#statv").hide();
			}
			$j('#form_cont').html(r);
			init_site();
		}
	});
}


function insNwl(){

	var date = 'email='+$j('#newsl_adr').val();

	$j.ajax({
		type: "POST",
		url: PATH.root+"insert_newsletter/",
		data: date,
		success: function(r){
			$j('#nwl_resp').html(r);
		}
	});
}

function formatItem(row) {
	//<img src='"+PATH.root+"lib/images/main/spacer.gif' alt='' class='hotels_ic'/>
	return '<b style="display:block;">'+row[0]+'</b>' +  row[1] +' - '+ row[2] + ' hotel(s)';
}

function onItemSelect(li) {
	$j("#or_id").val(li.selectTitle);
}

function setLangJS(){
	$j('#drpdn_st a').attr('href',$j(this).attr('href'));
	$j('#cur_lang').attr('src',$j(this+' img').attr('src'));
	$j("#drpdwn").slideUp();

}

var curPos = 0;
function viewGal(direction){
	//view_img
	//$j('#view_img').attr('src','lib/images/'+galerie+'/'+imgSrc[curPos]);
	var nrImg = imgSrc.length;

	if (direction == '-'){
		if (curPos>0){
			nextPos = curPos-1;
			/*$j('#foto_'+imgSrc[curPos]).fadeOut('slow',function(){$j('#foto_'+imgSrc[nextPos]).fadeIn('slow');});*/
			$j('#foto_'+imgSrc[curPos]).css('display','none');
			$j('#foto_'+imgSrc[nextPos]).css('display','block');
			curPos=nextPos;
			if (curPos == 0){
				$j('a.prev_next:eq(0)').css('color','#999');
			}
			$j('a.prev_next:eq(1)').css('color','#f06');
		}
	}else{
		if (curPos<nrImg-1){
			nextPos = curPos+1;
			/*$j('#foto_'+imgSrc[curPos]).fadeOut('slow',function(){$j('#foto_'+imgSrc[nextPos]).fadeIn('slow');});*/
			$j('#foto_'+imgSrc[curPos]).css('display','none');
			$j('#foto_'+imgSrc[nextPos]).css('display','block');
			curPos=nextPos;
			if (curPos == nrImg-1){
				$j('a.prev_next:eq(1)').css('color','#999');
			}
			$j('a.prev_next:eq(0)').css('color','#f06');
		}
	}


}


function viewFoto(ord){
	
	curFotoId = $j('#view_foto img').attr('id');
	/*
		$j('#view_foto img:visible').fadeOut('fast',function(){$j('#foto_'+imgSrc[ord]).fadeIn('fast',function(){galAnim=0;});});
	*/
	$j('#view_foto img:visible').css('display','none');
	$j('#foto_'+imgSrc[ord]).css('display','block');
	curPos=ord;

	if (curPos==0){
		$j('a.prev_next:eq(0)').css('color','#999');
		$j('a.prev_next:eq(1)').css('color','#f06');
	}
	if (curPos==imgSrc.length-1){
		$j('a.prev_next:eq(0)').css('color','#f06');
		$j('a.prev_next:eq(1)').css('color','#999');
	}
	if (curPos>0 && curPos<imgSrc.length-1){
		$j('a.prev_next:eq(0)').css('color','#f06');
		$j('a.prev_next:eq(1)').css('color','#f06');
	}
}

function chPrevImg(semn){

	if (semn=='-'){
		$j('#pach_1').show();
		$j('#pach_2').hide();
		$j('#arr_cont img:eq(1)').show()
		$j('#arr_cont img:eq(0)').hide()
	}
	if (semn=='+'){
		$j('#pach_1').hide();
		$j('#pach_2').show();
		$j('#arr_cont img:eq(1)').hide()
		$j('#arr_cont img:eq(0)').show()
	}
}

function vCopii(nrCopii,labelVarste){
	$j('#v_copii').html(labelVarste+": ");
	if (nrCopii>0){
		for(i=1;i<=nrCopii;i++){
			$j('#v_copii').append('<input type="text" name="varsta_copil_'+i+'" value="" maxlength="1" style="width:20px;margin-right:5px"/>');
		}
		$j('#v_copii').show();
	}else{
		$j('#v_copii').html(labelVarste+": ");
		$j('#v_copii').hide();
	}
}


function getUrlVars(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

var bifate=0;
function bife(iduri){
	var elemente=document.getElementsByName("check_item[]");
	$('iduri_sel').value = '';
	for (i=0;i<elemente.length;i++){
		if (bifate==0){
			elemente[i].checked=true;
			$('iduri_sel').value = iduri;
		}
		else{
			elemente[i].checked=false;
		}
	}

	if (bifate==0){
		bifate=1;
	}
	else
	{
		bifate=0;
	}

}

function adauga_iduri(obj){
	if (obj.checked == true){
		$('iduri_sel').value +='-'+obj.value;
	}
	else{
		str_4_rep = '-'+obj.value;
		$('iduri_sel').value = $('iduri_sel').value.replace(str_4_rep,"");
	}

}

var countInput = 0;
function adauga_inp_file(id,maxInputs){
	var obj=document.getElementById(id);

	var new_input=document.createElement('input');
	var new_txt=document.createElement('span');
	var new_check=document.createElement('input');

	new_input.setAttribute('type','file');
	new_input.setAttribute('name','foto[]');
	new_input.setAttribute('style','width:200px');
	new_input.setAttribute('size','20');

	new_txt.innerHTML = 'principala ';

	new_check.setAttribute('type','radio');
	new_check.setAttribute('style','width:15px;height:15px;');
	new_check.setAttribute('name','principala');
	new_check.setAttribute('value',countInput);

	if (countInput < maxInputs){
		obj.appendChild(new_input);
		obj.appendChild(new_check);
		obj.appendChild(new_txt);

		var new_input=document.createElement('br');
		new_input.setAttribute('style','clear:both');
		obj.appendChild(new_input);
		countInput++;
	}
}

var WIN = {
	opened:0,
	winName:"",
	term:"",
	open: function(win,terminatie){
		this.winName = win;
		if (this.opened == 0){
			$j('#'+win).css('display','block');
			this.opened = 1;
			this.term = terminatie;
		}else{
			$j('#'+win).css('display','none');
			this.opened = 0;
			this.term = '';
		}
	},
	close: function(win){
		$j('#'+win).css('display','none');
		this.opened = 0;
	},
	send: function(obj){
		$j('input[name="oras_'+this.term+'"]').val(obj.id);
		$j("#click_alege_"+this.term).html(obj.nume);
		$j.ajax({
			type:"POST",
			url:PATH.root+"ajax/info_oras/",
			data:"oras_id="+obj.id,
			success:function(rasp){
				eval(rasp);
				$j('#tara_'+WIN.term).html(raspuns.tara_nume);
				$j('input[name="tara_'+WIN.term+'"]').val(raspuns.tara_id);
			}
		});
		this.close(this.winName);
	}
}

function setFotoPr(id,id_ext,container,tpl,tabel){
	$j.ajax({
		type: "POST",
		url: PATH.root+'ajax/ch_foto_pr',
		data: 'id='+id+'&id_ext='+id_ext+'&tabel='+tabel+'&tpl='+tpl+'&url='+escape(window.location.href),
		success: function(rasp){
			$j("#"+container).html(rasp);
		}

	});
}

function calcDays(data_start,data_end){
	var x = data_start.split('.');
	var y = data_end.split('.');

	var o_zi=1000*60*60*24;

	var date_1=new Date(x[2],(x[1]-1),x[0]);
	var date_2=new Date(y[2],(y[1]-1),y[0]);

	var res=Math.ceil((date_2.getTime()-date_1.getTime())/(o_zi));

	if (res>0){
		$j('#nopti_nr').html(res);
		$j('#nopti').css('display','inline');
		$j('#nr_nopti').val(res);
	}else{
		$j('#nopti').css('display','none');
		$j('#nr_nopti').val('-');
	}

}

function setPublic(id,val){
	
	$j.ajax({
		type: "POST",
		url: PATH.root+PATH.link.a+"/"+PATH.link.b+"/publica/",
		data: 'id='+id+'&val='+val,
		success: function(rasp){
			$j("#publ_cont_"+id).html(rasp);
		}

	});
}

function changeEmailCat(id_email,id_cat){
	$j.ajax({
		type: "POST",
		url: PATH.root+PATH.link.a+"/"+PATH.link.b+"/ch_cat/",
		data: 'id_email='+id_email+'&id_cat='+id_cat,
		success: function(rasp){
			$j("#ch_cat_resp_"+id_email).html(rasp);
		}

	});
}

//gmap
/*function setMarkers(map, locations) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = new google.maps.MarkerImage('lib/images/main/sh-pt-google.png',//logo-pt-google.png
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(37, 32),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 0));
  var shape = {
      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };
  
  for (var i = 0; i < locations.length; i++) {
    var loc = locations[i];
    var myLatLng = new google.maps.LatLng(loc[1], loc[2]);
    eval("var marker_"+i+" = new google.maps.Marker({ position: myLatLng, map: map, icon: image, shape: shape, title: loc[0], zIndex: i });")
  }
  
	var a=[];
	for(i=0;i< locations.length; i++){
		google.maps.event.addListener(eval("marker_"+i), 'click',function(){
				var ind = $(this).attr('z-index');
				if (infowindow[ind]) infowindow[ind].close();
				eval("var infowindow"+ind+" = new google.maps.InfoWindow({content:locations[ind][4]});");
				eval("infowindow"+ind).open(map, eval("marker_"+ind));
		});
	} 
  
}*/

function createMarker(map, latlng, label, html, zindex,url) {
	var image = new google.maps.MarkerImage(
		'lib/images/main/sh-pt-google.png',
	    // This marker is 20 pixels wide by 32 pixels tall.
		new google.maps.Size(37, 32),
	    // The origin for this image is 0,0.
		new google.maps.Point(0,0),
	    // The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(0, 0)
	);
	var shape = {
		coord: [1, 1, 1, 20, 18, 20, 18 , 1],
		type: 'poly'
	};
  
    var contentString = '<b>'+label+'</b><br>'+html;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: image,
        shape: shape,
        title: label,
        zIndex: zindex
        });

    google.maps.event.addListener(marker, 'mouseover', function() {
        infowindow.setContent(html); 
        infowindow.open(map,marker);
        });
    google.maps.event.addListener(marker, 'mouseout', function() {
		infowindow.close(map,marker);
	});
    google.maps.event.addListener(marker, 'click', function() {
		location.href=url;
	});
}

function setMarkers(map, locations) {
  // Add markers to the map
 
  for (var i = 0; i < locations.length; i++) {
    var loc = locations[i];
    var myLatLng = new google.maps.LatLng(loc[1], loc[2]);
    var marker = createMarker(map,myLatLng,loc[0],loc[4],loc[3],loc[5]);
  }
}

function setMarker(map, location) {
	var image = new google.maps.MarkerImage(
		'lib/images/main/logo-pt-google2.png',
	    // This marker is 20 pixels wide by 32 pixels tall.
		new google.maps.Size(38, 37),
	    // The origin for this image is 0,0.
		new google.maps.Point(0,0),
	    // The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(0, 0)
	);
	var shape = {
		coord: [1, 1, 1, 20, 18, 20, 18 , 1],
		type: 'poly'
	};
	var latlng = new google.maps.LatLng(location[0][1], location[0][2]);
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: image,
        shape: shape,
        title: location[0][0]
        });
}