function getCount(){
	return count;
}
function setCount(nr){
	count = nr;
	
}

function getUrlVars(text)
{
	var vars = [], hash;
	hashes=text.split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
	return vars;
}
function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}
//upload
function calculate(){
	var d = document.getElementById('poza_input');
	poza_left=findPosX(d);
	poza_top=findPosY(d);
	//$("#poza_hidden").offset({ top: poza_top, left: poza_left });
	alert(poza_top);
}
jQuery(document).ready(function() {

	if(typeof($('#poza').val())!='undefined'){
		$(function(){
				var btnUpload=$('#poza');
				var status=$('#status');
				//var count=1;
				new AjaxUpload(btnUpload, {
					action: base_path+'upload-file.php',
					name: 'poza',
					data : {editeaza:$('[name=edit]').val(),id:$('[name=id_edit]').val()},
					onSubmit: function(file, ext){
						 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
							// extension is not allowed 
							status.text('Only JPG, PNG or GIF files are allowed');
							return false;
						}
						if(count >5)
						{
							status.text('Doar 6 incercari!');
							return false;
						}
						status.text('Uploading...');
					},
					onComplete: function(file, response){
						//On completion clear the status
						status.text('');
						result = decodeURIComponent(response);
						result = getUrlVars(result)
						//Add uploaded file to list
						if(result['success']=="true"){
							$('<li id="'+result['id']+'"></li>').appendTo('#files').html('<img src="'+base_path+result['filename']+'" alt="" /><br />'+file+'<span class="delete" onclick="javascript:deletePoza(\''+result['id']+'\',\''+$('[name=id]').val()+'\')">delete</span>').addClass('success');
							count++;
						} 
						else{
							$('<li></li>').appendTo('#files').text(file).addClass('error');
							$('<li></li>').appendTo('#files').text('error:'+result['error1']+'\n'+result['error2']).addClass('error');
						}
						if(count>5)
						{
							$('#poza_input').css('display','none');
						}
					}
				});
			});
		}
		
	//scroll
	function mycarousel_initCallback(carousel){
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	if($('#rotator-anunturi').length != 0){		
		jQuery('#rotator-anunturi').jcarousel({
			auto: 5,
			animation: 2000,
			scroll: 1,
			vertical: true,
			wrap: 'last',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: mycarousel_initCallback
		});
	}
	if($('#rotator-publicatii').length != 0){		
		jQuery('#rotator-publicatii').jcarousel({
			auto: 3,
			animation: 2000,
			scroll: 1,
			vertical: true,
			wrap: 'last',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: mycarousel_initCallback
		});
	}
	if($('#rotator-agentii').length != 0){		
		jQuery('#rotator-agentii').jcarousel({
			auto: 4,
			animation: 2000,
			scroll: 1,
			wrap: 'last',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: mycarousel_initCallback
		});
	}
	//end scroll
	
	$(".an-continut").click(function(){
		window.location=$(this).find("a").attr("href"); return false;
	});
	
	$(".chenar-container").click(function() {
		window.location=$(this).find("a").attr("href"); return false;
	});
	
	$(".chenar-container").hover(
		function() {
			$(this).find("div.chenar").css("background", "#e9f2f7");
		}, 
		function() {
			$(this).find("div.chenar").css("background", "");
		}
	);
	
	$(".anunt").hover(
		function() {
			$(this).find("div.an-continut").css("background", "#eff6f5");
		}, 
		function() {
			$(this).find("div.an-continut").css("background", "");
		}
	);
	
	//adauga anunt
	if($("#tip_inchiriere").length != 0 || $("#reactiveazaAnunt").length != 0 ) {
		$("#poza").addClass('no-width');
		
		$(".buttons span").click(
			function() {
				var p_split = $(this).parent().find('p').html().split(" ");
				var span_split = $(this).html().split(" ");
				var durata = p_split[0];
				var tip = span_split[3];
				$("#durata").val(durata);
				$("#tip_plata").val(tip);
				$("#mobil_plata").html("Dupa adaugarea anuntului, veti fi redirectionat catre Mobil Pay.<br />Anuntul va fi valabil " + durata + " de zile dupa efectuarea platii.");
				if(durata == 6) {
					$("#txt_sms").html('');
					$("#txt_net").html('');
					//if($("#infoline").css("display")) 
					$('#infoline').fadeOut(600);
					$('#plata_container').fadeOut(600);
				}
				else if(durata == 10) {
					$("#txt_sms").html("Trimite \"chirie\" prin SMS la 1424 si apoi introdu codul primit aici:");
					$("#txt_net").html("Tarif 1 EUR in retelele Orange, Vodafone, Cosmote, Zapp. Tariful nu include TVA.");
				}
				else if(durata == 40) {
					$("#txt_sms").html("Trimite \"chirie\" prin SMS la 1413 si apoi introdu codul primit aici:");
					$("#txt_net").html("Tarif 3 EUR in retelele Orange, Vodafone, Cosmote, Zapp. Tariful nu include TVA.");
				}
				$(this).addClass('selected');
				$(".buttons").find("span").not(this).removeClass('selected');
				if($("#infoline").css("display") == 'none') $('#infoline').fadeIn(600);
				
				switch(tip) {
					case 'SMS':
						$("#mobil_plata").fadeOut(600);
						$("#info_mobilpay").fadeOut(600);
						setTimeout("$('#plata_container').fadeIn(600);$('#sms_plata').fadeIn(600);$('#info_sms').fadeIn(600);", 600);
					break;
					
					case 'mobilpay':
						$("#sms_plata").fadeOut(600);
						$("#info_sms").fadeOut(600);						
						setTimeout("$('#plata_container').fadeIn(600);$('#mobil_plata').fadeIn(600);$('#info_mobilpay').fadeIn(600);", 600);
					break;
				}
			}
		);
	
		$("#destination_toggle3").attr("href","");
		$("#destination_toggle3").click(function(){switchVisible('3');return false;});
		
		$("#destination_toggle1").attr("href","");
		$("#destination_toggle1").click(function(){switchVisible('1');return false;});
		
		$("#destination_toggle2").attr("href","");
		$("#destination_toggle2").click(function(){switchVisible('2');return false;});
	}
	
	$("#xlist").tagcloud({
	type:"list",sizemin:11,colormin:"a3adbf",colormax:"d1d8e3",sizemax:20
	});
	
});

function selectOras(form, link){
	val=form.value;
	self.location= link + val;
}

function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function toCount(entrance,exit,text,characters) {
  var entranceObj=getObject(entrance);
  var exitObj=getObject(exit);
  var length=characters - entranceObj.value.length;
  if(length <= 0) {
    length=0;
    text='<span class="disable"> '+text+' </span>';
    entranceObj.value=entranceObj.value.substr(0,characters);
  }
  exitObj.innerHTML = text.replace("{CHAR}",length);
}

function showMap() {
	$("#hartaExpand").slideDown("slow").fadeIn("slow");
	$("#hartaLinkS").css('display', 'none');
	$("#hartaLinkH").css('display', 'block');
}
function hideMap() {
	$("#hartaExpand").slideUp("slow");
	$("#hartaLinkH").css('display', 'none');
	$("#hartaLinkS").css('display', 'block');
}

function afiseazaEdit(id){
	if(document.getElementById('editeaza'+id).style.display == 'none')
		document.getElementById('editeaza'+id).style.display = '';
	else
		document.getElementById('editeaza'+id).style.display = 'none';
}

function isNumber(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode != 8 && charCode > 31 && (charCode < 48 || charCode > 57))
	//charCode != 46 "."
		return false;
}

function isLitera(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if ( (charCode >= 65 && charCode <= 90) || ( charCode >= 97 && charCode <= 122) || charCode == 8)
		return true;
	else return false;
}
/*function checkPret(){
	if($("#maxim").val() == ""){
		$("#maxim").after('<div class="red">Campul este obligatoriu!</div>');
		setTimeout("jQuery.scrollTo('.red', 1500)",600);
		return false;
	}
}*/

function moveToHarta() {
	setTimeout("jQuery.scrollTo('#tabs', 1500)", 0);
	setTimeout("showMap()", 1500);
}

function adaugaAgentie(){
	if(document.getElementById('agentie').value == 'agentie'){
		$("#adauga").fadeOut(600);
		$("#loginMessage").show();
		//$("#loginMessage").addClass("error");
		//setTimeout("$(\"#login\").fadeIn(\"slow\");",600);
		//document.getElementById('adauga').style.display = 'none';
		//document.getElementById('login').style.display = '';
	}
	if(document.getElementById('agentie').value == 'privat'){
		//$("#login").fadeOut(600);
		setTimeout("$(\"#adauga\").fadeIn(\"slow\");",600);
		$("#loginMessage").hide();
		//$("#loginMessage").removeClass("error");
		//document.getElementById('adauga').style.display = '';
		//document.getElementById('login').style.display = 'none';
	}
}

function verificaAllInput(){ 
	mesaj_eroare = '';
	$('#formular :input').each(function(key,element) {
		$(this).removeClass('empty');
		$("#caption_"+element['name']).removeClass('red');
		obligatoriu = $("#caption_"+element['name']).text();
		if (obligatoriu.indexOf("*")!=-1 && ($(this).val()=='' || $(this).val()=='www.' || (this.type== 'checkbox' && $(this).attr("checked")==false ))){
			mesaj_eroare +="Campul "+element['name'] + " trebuie sa fie completat\n";
			$(this).addClass('empty');
			$("#caption_"+element['name']).addClass('red');
		}
	});
	//if(typeof($("#password").val())!='undefined'){
		if( $("#password").val()!=$("#password2").val() ){
			$("#password2").removeClass('empty');
			$("#password2").addClass('empty');
			$("#caption_password2").removeClass('red');
			$("#caption_password2").addClass('red');
			mesaj_eroare +="Parola rescrisa este gresita!";
			
		}
	//}
	if($("#conditii").length!=0 ){
		$("#caption_cond").removeClass("conditii_error");
		$("#caption_cond").removeClass("red")
		if($('#conditii:checked').length==0){
			mesaj_eroare='Nu ati bifat termenii si conditiile';
			$("#caption_cond").addClass("conditii_error");
			$("#caption_cond").addClass("red");
		}
	}
		
	if(mesaj_eroare!=''){
		setTimeout("jQuery.scrollTo('.red', 1500, {offset:-50})",600);
		return false;
	}
}
/*function verifica(){ 
var values = $('#formular').serialize();
var get = getUrlVars(values);
}*/

function switch_tip(editeaza){
	
	edit = typeof(editeaza) != 'undefined' ? editeaza : 'false';
	var $inputs = $('#formular :input');

	if(edit=='false'){
		$inputs.each(function() {
		if(this.name !='pret_pe_mp' && this.name !='edit' && this.name !='id_edit' && this.name !='editeaza' && this.name !='id' && this.name !='durata' && this.name !='tip_inchiriere' && this.name !='agentie' && this.name !='mobilat' && this.name !='adaugaAnunt' && this.name !='Reseteaza' && this.name !='nr_telefon' && this.name !='security_code_adauga' && this.name !='email' && this.name !='orasId' && this.name !='pret' && this.name !='tip_valuta' && this.type!= 'checkbox'){
			//$(this).val('');
			//$(this).attr("disabled","disabled");
		}
		if(this.type== 'checkbox' && this.name!='conditii' && this.name!='tva_inclus' ) $(this).attr('checked','');
		if(this.name=='pret_pe_mp' ) $(this).attr('checked','checked');
    });	
	}
	switch_tip_ap();
	switch ($("#tip_inchiriere").val()){
		//gars ap
		case '1':
		case '2':
		case '3':
		case '4':
			if(edit=='false'){
				$("#bai option:eq(1)").attr("selected","selected");
				//$("#balcoane option:eq(1)").attr("selected","selected");
				$("#intretinere_inclusa option:eq(1)").attr("selected","selected");
				$("#incalzire option:eq(1)").attr("selected","selected");
				$("#tip option:eq(1)").attr("selected","selected");
			}
			for (i=1;i<=5;i++){
				$(".class"+i).slideUp("slow").fadeOut(600);
				$(".class"+i+" :input").attr("disabled","disabled");
			}
			setTimeout("$(\".class1\").slideDown(\"slow\").fadeIn(\"slow\");",600);
			setTimeout("$('#situata_in').css('display','none');",610);// ptr garsoniera
			$(".class1 :input").attr("disabled","");
			$("#situata_in").attr("disabled","");
		break;
		//casa vila
		case '5':
		case '6':
			if(edit=='false'){
				$("#intretinere_inclusa option:eq(1)").attr("selected","selected");
				$("#incalzire option:eq(2)").attr("selected","selected");
				$("#bai option:eq(1)").attr("selected","selected");
				$("#bucatarii option:eq(1)").attr("selected","selected");
				$("#camere1 option:eq(1)").attr("selected","selected");
			}
			for (i=1;i<=5;i++){
				$(".class"+i).slideUp("slow").fadeOut(600);
				$(".class"+i+" :input").attr("disabled","disabled");
			}
			$(".class2 :input").attr("disabled","");
			setTimeout("$(\".class2\").slideDown(\"slow\").fadeIn(\"slow\");",600);
		break;
		//camera in vila
		case '7':
			for (i=1;i<=5;i++){
				$(".class"+i).slideUp("slow").fadeOut(600);
				$(".class"+i+" :input").attr("disabled","disabled");
			}
			$(".class3 :input").attr("disabled","");
			setTimeout("$(\".class3\").slideDown(\"slow\").fadeIn(\"slow\");",600);
		break;
		//spatiu pentru birouri
		case '8':
			for (i=1;i<=5;i++){
				$(".class"+i).slideUp("slow").fadeOut(600);
				$(".class"+i+" :input").attr("disabled","disabled");
			}
			$(".class4 :input").attr("disabled","");
			setTimeout("$(\".class4\").slideDown(\"slow\").fadeIn(\"slow\");",600);
		break;
		//spatiu comercial
		case '9':
			for (i=1;i<=5;i++){
				$(".class"+i).slideUp("slow").fadeOut(600);
				$(".class"+i+" :input").attr("disabled","disabled");
			}
			$(".class5 :input").attr("disabled","");
			setTimeout("$(\".class5\").slideDown(\"slow\").fadeIn(\"slow\");",600);
		break;
	}
}

function checkAll(container){
	$("#"+ container + " :input").each(function() {
		$(this).attr('checked','checked');
	})
}
function unCheckAll(container){
	$("#"+ container + " :input").each(function() {
		$(this).attr('checked','');
	})
}
function switch_tip_ap(){
	index = $('#tip_inchiriere option:selected').index();
	if(index == 0 || index == 1 || index == 2 || index == 3 ) {
		setTimeout("$(\"#tip_ap\").slideDown(\"slow\").fadeIn(\"slow\");",600);
		$("#tip").attr("disabled","");
		$("#tip option:eq(1)").attr("selected","selected");
	}
	else if(index == 6 ){
		index = $('#camera_situata_in option:selected').index();
		if(index == 1 || index == 2 || index == 3 ) {
			setTimeout("$(\"#tip_ap\").slideDown(\"slow\").fadeIn(\"slow\");",600);
			$("#tip").attr("disabled","");
		}
		else{	
			$('#tip_ap').slideUp("slow").fadeOut(600);
			//$("#tip_ap").attr("disabled","disabled");
			//alert($("#tip").attr("disabled"));
			$('#tip').val('');
		}
	}
	else{	
		$('#tip_ap').css('display','none');
		$("#tip").attr("disabled","disabled");
		$('#tip').val('');
	}

}
function numar(id){
	if($("#"+id+"1").val()=='alege'){
		$("#"+id+"1").attr("name","");
		$("#"+id+"2").attr("name",id);
		$("#"+id+"2").css("display","");
	}
	else{
		$("#"+id+"1").attr("name",id);
		$("#"+id+"2").attr("name","");
		$("#"+id+"2").css("display","none");
	}
}
function div(id){
	$("#"+id).toggle();
}

function switchVisible(id) {
	id = typeof(id) != 'undefined' ? id : '1';
	//ptr a href class selected din header
	href_selected(id);
	//....................................
	if(id == 3) $('.harta').fadeOut(1000);
	else $('.harta').fadeIn("slow");
	for (i=1;i<=3;i++){
		$("#center"+i).fadeOut(600);
		$("#link"+i).removeClass("tab-activ");
		$("#link"+i).addClass("tab-inactiv");
	}
	setTimeout("$(\"#center"+id+"\").fadeIn(\"slow\");",600);
	$("#link"+id).removeClass("tab-inactiv");
	$("#link"+id).addClass("tab-activ");
	jQuery(document).ready(function() {
		if($(window).scrollTop() < $(".tabs").offset().top)
			setTimeout("jQuery.scrollTo('#tabs', 1500)",610);
	});
}

//a href linkuri sus
function href_selected(id){
	for(i=1;i<=5;i++){
		$("#destination_toggle" +i).removeClass("selected");
	}		
	$("#destination_toggle"+id).addClass("selected");
}