function scrollImagesList(){
  var listing_photo_height=$("#photos_container").height();
  if(listing_photo_height>250){
		$("#photos_container").css({height:"250px",overflow:"auto"});
		//$("#listing2").jScrollPane({showArrows:true, scrollbarWidth: 15,dragMaxHeight:20,scrollbarMargin:12});
  }
}
function checkAll(form,status){
	if(status==true){
		$("input:checkbox",$("#"+form)).each(function(i){
			this.checked=true;
			changeBg(this,form);
		});
	}else{
		$("input:checkbox",$("#"+form)).each(function(i){
			this.checked=false;
			changeBg(this,form);
		});
	}
}
function changeBg(el,form){
	//alert(el.checked);
	if(el.checked==true) $(el).parents("tr").addClass("over")
	else {
		$(el).parents("tr").removeClass();	
		$("#"+form+" tr:even").addClass("alt");
	}
}
function changeRightBg(el,status){
	if(status==true) $(el).parents("li").addClass("right_check_on");
	else {
		$(el).parents("li").removeClass();
		$(el).parents("li").addClass("right_check");
	}
}
function editPhotoDescr(photo_id){
	$.post("edit_photo_descr.php",{photo_id:photo_id,ajax:"true"},function(data){
		$("#photo_descr_content").html(data);
		return false;
	});
	$("#photo_descr").show();
}

function edit_photo_descr(photo_id){
	$.post(
		"edit_photo_descr.php",
		{
			photo_id:photo_id,
			time:Date.parse(new Date()),
			description_1:$("#description_1",$("#photos_edit")).val(),
			description_2:$("#description_2",$("#photos_edit")).val(),
			description_3:$("#description_3",$("#photos_edit")).val(),
			description_4:$("#description_4",$("#photos_edit")).val(),
			description_5:$("#description_5",$("#photos_edit")).val(),
			active:$("input[@name=active][@checked]",$("#photos_edit")).val(),
			hierarchy:$("#hierarchy",$("#photos_edit")).val()
		},
		function(data){
		$("#photo_descr_content").html(data);
		return false;
	});
}

function editRateDescr(rate_id){
	$.post("edit_rate_descr.php",{rate_id:rate_id,ajax:"true"},function(data){
		$("#rate_descr_content").html(data);
		calendar();
		return false;
	});
	$("#rate_descr").show();
	//$("#start_date").datePicker();
	//$("#end_date").datePicker();
}

function edit_rate_descr(rate_id){
	$.post(
		"edit_rate_descr.php",
		{
			rate_id:rate_id,
			time:Date.parse(new Date()),
			nightly_rate:$("#nightly_rate",$("#rates_edit")).val(),
			weekly_rate:$("#weekly_rate",$("#rates_edit")).val(),
			monthly_rate:$("#monthly_rate",$("#rates_edit")).val(),
			start_date:$("#start_date",$("#rates_edit")).val(),
			end_date:$("#end_date",$("#rates_edit")).val(),
			weekend_per_night_rate:$("#weekend_per_night_rate",$("#rates_edit")).val()
		},
		function(data){
		$("#rate_descr_content").html(data);
		calendar();
		return false;
	});
}
function checkPlan(s_m,form,obj){
	$.post("check_plan.php",
			{
				s_multiple:s_m,
				ajax:"true"
			},function(data){
			$("#temp").val(data);
			if(data==0){
				var action="?obj="+obj+"&action=enable";
				$(form).attr("action",action);
				$("#time").attr("value",Date.parse(new Date()));
				$(form).submit();
			}else{
				alert(messages["number_active_propr_overflow"]);
			}
	});
	//return false;
}
function doAction(form,action,obj){
	var form="#"+form;
	if(action=='delete_selected'){//delete selected
		var curent2delete=new Array();
		var k=0;
		$("input:checkbox",$(form)).each(function(i){
			if(this.checked==true) {
				if(!isNaN(this.value)&&(null!=this.value)&&(''!=this.value)){
					curent2delete.push(this.value);
					k++;
				}
			}
		});
		if(k!=0) {
			var confirm_=confirm(messages["are_you_sure_you_want_to_delete_selected"])
			if(confirm_){
				$("#select_multiple").attr("value",curent2delete);
				var action="?obj="+obj+"&action=delete";
				$(form).attr("action",action);
				$("#time").attr("value",Date.parse(new Date()));
				$(form).submit();
			}
		}else{
			alert(messages["you_have_to_select_at_least_one_item"]);
		}
	}//end delete selected
	if(action=='enable_selected'){//enable selected
		var curent2enable=new Array();
		var k=0;
		$("input:checkbox",$(form)).each(function(i){
			if(this.checked==true) {
				if(!isNaN(this.value)&&(null!=this.value)&&(''!=this.value)){
					curent2enable.push(this.value);
					k++;
				}
			}
		});
		if(k!=0) {
			var confirm_=confirm(messages["are_you_sure_you_want_to_enable_selected"])
			if(confirm_){
				$("#select_multiple").attr("value",curent2enable);
				var action="?obj="+obj+"&action=enable";
				$(form).attr("action",action);
				$("#time").attr("value",Date.parse(new Date()));
				$(form).submit();
			}
		}else{
			alert(messages["you_have_to_select_at_least_one_item"]);
		}
	}//end enable selected
	if(action=='member_enable_selected'){
		var curent2enable=new Array();
		var k=0;
		$("input:checkbox",$(form)).each(function(i){
			if(this.checked==true) {
				if(!isNaN(this.value)&&(null!=this.value)&&(''!=this.value)){
					curent2enable.push(this.value);
					k++;
				}
			}
		});
		if(k!=0) {
			var confirm_=confirm(messages["are_you_sure_you_want_to_enable_selected"])
			if(confirm_){
				$("#select_multiple").attr("value",curent2enable);
				//var stat=0;
				var sel_m=curent2enable.toString();
				checkPlan(sel_m,form,obj);
				//var result=$("#temp").val();
				//alert(result);
			}
		}else{
			alert(messages["you_have_to_select_at_least_one_item"]);
		}
	}
	if(action=='disable_selected'){//disable selected
		var curent2disable=new Array();
		var k=0;
		$("input:checkbox",$(form)).each(function(i){
			if(this.checked==true) {
				if(!isNaN(this.value)&&(null!=this.value)&&(''!=this.value)){
					curent2disable.push(this.value);
					k++;
				}
			}
		});
		if(k!=0) {
			var confirm_=confirm(messages["are_you_sure_you_want_to_disable_selected"])
			if(confirm_){
				$("#select_multiple").attr("value",curent2disable);
				var action="?obj="+obj+"&action=disable";
				$(form).attr("action",action);
				$("#time").attr("value",Date.parse(new Date()));
				$(form).submit();
			}
		}else{
			alert(messages["you_have_to_select_at_least_one_item"]);
		}
	}//end disable selected
}
function confirmDelete(url){
	var confirm_=confirm(messages["are_you_sure_you_want_to_delete_selected"]);
	if(confirm_){
		window.location.href=url;
	}
}

$(document).ready(function(){
	menu();
	calendar();
	drawCalendar(1);
	drawCalendar(2);
	drawCalendar(3);
	
	$("input:checkbox",$("#listing_form")).click(function(){
		if($(this).attr("id")=="check_all"){
			checkAll("listing_form",this.checked);
		}else{
			changeBg(this,"listing_form");
		}
	});
	//ajax indicator
	$(".loading").ajaxStart(function(){
		/*
		$(this).wrap("<div style='position:absolute;top:50%;left:50%;width:1px;height:1px;'></div>") 
		$(this).css({left: this.offsetWidth / -2 + "px", top: this.offsetHeight / -2 + "px"});
		*/
   		$(this).show();
 	});
 	$(".loading").ajaxStop(function(){
   		$(this).hide();
 	});
	//flag
	$("#code",$("#languages_add"),$("#languages_edit")).blur(function(e){
		$.post("flag.php",{code:$(this).attr("value")},function(xml){
			    if($("file",xml).text()!=''){
					$("#flag_ico").html(
						"<img src=\"images/flags/"+$("file",xml).text()+"\" />");
					$("#use_flag").show();
					$("#flag_sugestion").attr("value",$("file",xml).text());
			    }else{
			    	$("#flag_ico").html("");
			    	$("#use_flag").html("");
			    }
			});
			return false;

	});
	//delete selected
	$("#delete_selected").click(function(){
		doAction("listing_form","delete_selected",$("#obj_name").attr("value"));
	});//end delete selected
	$("#delete_selected1").click(function(){
		doAction("listing_form","delete_selected",$("#obj_name").attr("value"));
	});//end delete selected
	
	//enable selected
	$("#enable_selected").click(function(){
		doAction("listing_form","enable_selected",$("#obj_name").attr("value"));
	});
	$("#member_enable_selected").click(function(){
		doAction("listing_form","member_enable_selected",$("#obj_name").attr("value"));
	});
	$("#member_enable_selected1").click(function(){
		doAction("listing_form","member_enable_selected",$("#obj_name").attr("value"));
	});
	$("#enable_selected1").click(function(){
		doAction("listing_form","enable_selected",$("#obj_name").attr("value"));
	});
	
	
	//disable selected
	$("#disable_selected").click(function(){
		doAction("listing_form","disable_selected",$("#obj_name").attr("value"));
	});
	$("#disable_selected1").click(function(){
		doAction("listing_form","disable_selected",$("#obj_name").attr("value"));
	});
	
	
	//rights
	$("input:checkbox",$(".rights_form")).each(function(){
		$(this).click(function(){
			changeRightBg(this,this.checked);
		});
	});
	$("select#admin_langs").change(function(e){
		//alert(e);
		//alert(this.value);
		var val=this.value;
		$.post("change_lang.php",{lang:val},function(xml){
			    if($("result",xml).text()!='' && $("result",xml).text()!=0){
			    	var path='index.php?obj=home&lang='+val;
			    	window.location.href=path;
			    	//alert(path.search("obj"));
			    }
			});
			return false;
	});
	//regional 
	$("#loader").ajaxStart(function(){
		$("#fixIeLoader").show();
		$(this).show();
	});
	$("#loader").ajaxStop(function(){
		$("#fixIeLoader").hide();
		$(this).hide();
	});
	$("select#continent_id",$("#properties_add")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_country.php",{continent:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#country_id",$("#properties_add")).html(options);
    			$("#country_id",$("#properties_add"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#continent_id",$("#properties_edit")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_country.php",{continent:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#country_id",$("#properties_edit")).html(options);
    			$("#country_id",$("#properties_edit"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#country_id",$("#properties_add")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_region.php",{country:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#region_id",$("#properties_add")).html(options);
    			$("#region_id",$("#properties_add"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#country_id",$("#properties_edit")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_region.php",{country:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#region_id",$("#properties_edit")).html(options);
    			$("#region_id",$("#properties_edit"))[0].selectedIndex=0;
			});
			return false;
	});
	//ADD 4 regional extra 1 - 2
	//v3.1
	$("select#region_id",$("#properties_add")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_reg_extra1.php",{region:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#regional_ext1_id",$("#properties_add")).html(options);
    			$("#regional_ext1_id",$("#properties_add"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#region_id",$("#properties_edit")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_reg_extra1.php",{region:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#regional_ext1_id",$("#properties_edit")).html(options);
    			$("#regional_ext1_id",$("#properties_edit"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#regional_ext1_id",$("#properties_add")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_reg_extra2.php",{regional_ext1_id:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#regional_ext2_id",$("#properties_add")).html(options);
    			$("#regional_ext2_id",$("#properties_add"))[0].selectedIndex=0;
			});
			return false;
	});
	$("select#regional_ext1_id",$("#properties_edit")).change(function(e){
		var val=$(this)[0].value;
		$.post("change_reg_extra2.php",{regional_ext1_id:val,ajax:"true"},function(data){
			    var options='<option value="...">...</option>';
			    var data_array=data.split("##");
			    for (var i = 0; i < data_array.length; i++) {
			    	var elem_array=data_array[i].split(",");
        			options += '<option value="' + elem_array[0] + '">' + elem_array[1] + '</option>';
      			}
      			$("select#regional_ext2_id",$("#properties_edit")).html(options);
    			$("#regional_ext2_id",$("#properties_edit"))[0].selectedIndex=0;
			});
			return false;
	});
	//end v3.1 add extra regional
	//submit
	$("input[@type=submit]").click(function(){
		$("#time").attr("value",Date.parse(new Date()));
	});
	$("#add_photo").click(function(){
		$("#add_another").attr("value",Date.parse(new Date()));
		$("#properties_add").attr("action","index.php?obj=properties&action=add&section=photos")
		$("#properties_add").submit();
		$("#properties_edit").attr("action","index.php?obj=properties&action=edit&section=photos")
		$("#properties_edit").submit();
		
	});
	$("#add_rate").click(function(){
			$("#add_another").attr("value",Date.parse(new Date()));
			$("#properties_add").attr("action","index.php?obj=properties&action=add&section=rates")
			$("#properties_add").submit();
			$("#properties_edit").attr("action","index.php?obj=properties&action=edit&section=rates")
			$("#properties_edit").submit();
	});
	
	$("input[@type=submit]",$("#properties_add")).click(function(){
			$("#time").val(Date.parse(new Date()));
			//unavailable_dates_=new Array();
			//pending_dates_=new Array();
			//unknown_dates_=new Array();
			
			//loadUpdateCal();
			updateCalendar();
			getDates();
			
			$("#unavailable").attr("value",unavailable_dates_);
			//alert($("#unavailable").val());
			$("#unknown").attr("value",unknown_dates_);
			//alert($("#unknown").val());
			$("#pending").attr("value",pending_dates_);
	});
	$("input[@type=submit]",$("#properties_edit")).click(function(){
			$("#time").val(Date.parse(new Date()));
			//unavailable_dates_=new Array();
			//pending_dates_=new Array();
			//unknown_dates_=new Array();
			
			updateCalendar();
			getDates();
			
			$("#unavailable").val(unavailable_dates_);
			//alert($("#unavailable").val());
			$("#unknown").val(unknown_dates_);
			//alert($("#unknown").val());
			$("#pending").val(pending_dates_);
	});
	$("#add_availability").click(function(){
			//$("#loader").show();
			//updateCalendar();
			//unavailable_dates_=new Array();
			//pending_dates_=new Array();
			//unknown_dates_=new Array();
			
			//loadUpdateCal();
			updateCalendar();
			getDates();
			
			$("#unavailable").val(unavailable_dates_);
			//alert($("#unavailable").val());
			$("#unknown").val(unknown_dates_);
			//alert($("#unknown").val());
			$("#pending").val(pending_dates_);
			//alert($("#pending").val());
			//$("#add_another").val(Date.parse(new Date()));
			//$("#properties_add").submit();
			
	});
	
	$("#close_window_0").click(function(){//add
		$("#photo_descr").hide();
		window.location.href="index.php?obj=properties&action=add&section=photos";
	});
	$("#close_window_1").click(function(){//edit
		$("#photo_descr").hide();
		window.location.href="index.php?obj=properties&action=edit&section=photos";
	});
	
	$("#close_window1_0").click(function(){
		$("#rate_descr").hide();
		window.location.href="index.php?obj=properties&action=add&section=rates";
	});
	$("#close_window1_1").click(function(){
		$("#rate_descr").hide();
		window.location.href="index.php?obj=properties&action=edit&section=rates";
	});
	$("#head_today").next().show();
	$(".head").click(function(){
		$(".head").next().hide();
		$(this).next().show();
	});
	
});
