$(document).ready(function(){
	
	//$("#resize").lazyload({  placeholder : "../images/loader.gif",  effect : "fadeIn" });
	$("#resize").lazyload({ threshold : 3000 });
	
	resizeImageSCut();
	
	$('.nav').width($(window).width()-163);
	
	
	//alert($('.nav').width());
	
	$('.nav, #contact, #footer').hover(function(){	
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	
	 $('#footer').hover(function(){
		$(this).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$(this).animate({opacity:0.3},{
			queue: false,
			duration: 600
		});
	});
	
	$('#contact').hover(function(){
		$('.bg_contact').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_contact').animate({opacity:0.7},{
			queue: false,
			duration: 600
		});
	});
	
	$('.container_contact').hover(function(){
		$('.bg_contact2').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_contact2').animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
	});
	
	$('.nav').hover(function(){
		$(this).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$(this).animate({opacity:0.9},{
			queue: false,
			duration: 600
		});
	});
	
	$('.sectiune').hover(function(){
		$('.bg_sectiune_top, .bg_sectiune_bottom',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
		
	},function(){
		$('.bg_sectiune_top, .bg_sectiune_bottom',$(this)).animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
		
	});
	
	$('.sectiune_bottom').hover(function(){
		$(this).prev('.bg_sectiune_bottom').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$(this).prev('.bg_sectiune_bottom').animate({opacity:0.6},{
			queue: false,
			duration: 600
		} );
	});
	
	$('.containter_harta').hover(function(){
		$('.bg_harta, .bg_harta_bottom',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
		
	},function(){
		$('.bg_harta, .bg_harta_bottom',$(this)).animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
		
	});
	
	
	$('.containter_harta2').hover(function(){
		$('.bg_harta2',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
		
	},function(){
		$('.bg_harta2',$(this)).animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
		
	});
	
	$('.containter_panorama360').hover(function(){
		$('.bg_submeniu_panorama360, .bg_meniu_panorama360',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
		
	},function(){
		$('.bg_submeniu_panorama360, .bg_meniu_panorama360',$(this)).animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
		
	});
	
	$('.item').hover(function(){
		$('.bg_item',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_item',$(this)).animate({opacity:0.3},{
			queue: false,
			duration: 600
		});
	});
	
	$('.produs').hover(function(){
		$('.bg_produs').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_produs').animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
	});
	
	$('.container_text').hover(function(){
		$('.bg_text').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_text').animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
	});
	
	$('.container_spatii').hover(function(){
		$('.bg_spatii').animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_spatii').animate({opacity:0.6},{
			queue: false,
			duration: 600
		});
	});
	
	$('.container_img_name ').hover(function(){
		$('.bg_img_name',$(this)).animate({opacity:1},{
			queue: false,
			duration: 600
		} );
	},function(){
		$('.bg_img_name',$(this)).animate({opacity:0.3},{
			queue: false,
			duration: 600
		});
	});
	
	$('.sectiune:last').css({'margin':0});
	$(window).resize(function(e){
		resizeImageSCut();
		$('.nav').width($(window).width()-163);		
	});
	
	$('.ac_contact input[type=text]').each(function(){			
		$(this).focus(function(){
			var text = $(this).val();
			var id = $(this).attr('id');	
			if((text) == (' '+id) ){
				 $(this).val('');	
			}
		}).blur(function(){
			var text = $(this).val();
			var id = $(this).attr('id');	
			if (text == '') {
				$(this).val(' '+id);
			}else {
				$(this).val(text);
			}		
		});

	});
	
	$('.hide').live('click',function(){
		$('.container_contact .contact, .container_contact .contact, .succes_container').hide();
		$('.container_contact, .bg_contact2').animate({
			'height':'25px'
		});
		$(this).removeClass('hide');
		$(this).addClass('show');
		
	});
	$('.show').live('click',function(){
		$('.container_contact .contact, .container_contact .contact, .succes_container').show();
		$('.container_contact, .bg_contact2').animate({
			'height':'340px'
		});
		$(this).addClass('hide');
		$(this).removeClass('show');
	});
	
	$('.harta_bottom a').each(function(index){
		$(this).click(function(){
			//var flashMovie = window.document.MyFlashWidget;
			//flashMovie.chMap(index+1);
			$('.harta_bottom a').removeClass('activ');
			$(this).addClass('activ');
			var text = $(this).text();
			$('.harta h2').text(text);
			$('.harta div').addClass('hidden');
			$('.harta div').eq(index).removeClass('hidden');
		});
	});
	
	$('#otherInformation').focus(function(){
		var text = $(this).val();
		var id ='Alte surse - precizaţi';	
		if(text == id ){
			 $(this).val('');	
		}
	}).blur(function(){
		var text = $(this).val();
		var id ='Alte surse - precizaţi';	
		if (text == '') {
			$(this).val(id);
		}else {
			$(this).val(text);
		}		
	});
	
	
	$('#sursa-t').click(function(){
		$('#sursa-t').css('background','white')
		$('#sursa').removeClass('hidden');
	});
	$('#ok').click(function(){
		$('#sursa').addClass('hidden');
		//alert($('#sursa #banner').attr('checked'));
		if($('#sursa #banner').attr('checked') != 0 || $('#sursa #promovare').attr('checked') != 0 || 
		$('#sursa #rezi').attr('checked') != 0|| $('#sursa #prieteni').attr('checked') != 0 || 
		$('#sursa #consultanti').attr('checked') != 0 || ($('#sursa #alte').val()!='Alte surse - precizaţi' && $('#sursa #alte').val()!='')){
			$('#sursa-t').val('Vă mulţumim pentru alegere.').css('background','white');	
		}else{
			$('#sursa-t').val('Alegeţi din listă.').css('background','red');
		}
		
		
	});
	
	
	$('#mail_contact').submit(function(){
		//alert('submit');
		var error = '';
		var nume = trim($('#mail_contact #nume').val());
		var email = trim($('#mail_contact #email').val());
		var telefon = trim($('#mail_contact #telefon').val());
		
		if(nume == 'nume' || nume == '' ){
			error+='Nu ati completat numele. ';
		}
		if(email == 'email' || email == '' ){
			error+='Nu ati completat adresa de email. ';
		}else{
			if(validate(email)==false){
				error+='Adresa de email nu este valida. ';
			}
		}
		
		if (trim(telefon) == 'telefon' || trim(telefon) == '') {
			error += 'Nu ati completat numarul de telefon. ';
		}
		else {
			if (checkPhone(trim(telefon)) == false) {
				error += 'Numarul de telefon nu este valid ';
			}
		}
		
		if (error) {
			$('#error').html('<span class="red">*</span> '+error);
			return false;
		}
		else {
			// var dataString = 'nume='+ nume + '&email=' + email + '&telefon=' + telefon;
			var dataStr = 'otherInformation=null&about=null&Language=ro'+ '&AllowReceiveOffer=false' + 
				'&Message=Quick form contact' + '&Occupation=null&Budget=0&NoRooms=0&Email=' + email + '&MobilePhone=' + telefon+
				'&LastName=null&FirstName=' + nume + '&companie=null';
			  //alert (dataStr );return false;
				$.ajax({
				    type: "POST",
				    url: "process.php",
				    data: dataStr ,
					success: function() {						
						$('#error').html('<span id="bula_mesaj">Mesajul dvs. a fost trimis.<br/>In cel mai scurt timp veţi fi contactat telefonic de unul dintre consultanţii noştri, pentru a vă oferi mai multe informaţii despre proiect.<br /><br />Vă mulţumim! Echipa Asmita Gardens</span>')
						.fadeIn('slow').animate({opacity: 1.0}, 6000).fadeOut('slow', function() {
							$(this).remove();
							//window.location = "http://www.asmitagardens.ro/";
						});	
						//$('#error').text('Datele au fost trimise.').fadeIn(1200).fadeOut(1200);	
						$('#mail_contact #nume').val(' nume').wait(2400);
						$('#mail_contact #email').val(' email').wait(2400);
						$('#mail_contact #telefon').val(' telefon').wait(2400);
					    }
				  });
			return false;
		}
		
		
	});
	
	
	$('#contact_main').submit(function(){
		
		var eroare = 1;
		var email = $('fieldset:first input#Email').val();
		
		$('fieldset:first input, fieldset:first textarea').each(function(){
			if($(this).val()== '' || $(this).val()== 'Completaţi această casetă'){
				$(this).css('background','red').val('Completaţi această casetă');
				eroare *= 0;
			}else {
				$(this).css('background','white');
				eroare *= 1;	
			}
		});
		
		if(validate(email) == false && $('fieldset:first input#Email').val()!=''){
			$('fieldset:first input#Email').css('background','red').val('Adresa de email invalidă');
			eroare *= 0;
		}
		if(!checkPhone($('fieldset:first input#MobilePhone').val()) && $('fieldset:first input#MobilePhone').val()!=''){
			$('fieldset:first input#MobilePhone').css('background','red').val('Număr de telefon invalid');
			eroare *= 0;
		}
		
		
		
		if($('fieldset input#sursa-t').val() == 'Alegeţi din listă'){
			$('fieldset input#sursa-t').css('background','red');
			eroare *= 0;
		}else{
			$('fieldset input#sursa-t').css('background','white');
			eroare *= 1;
		}
		
		
				
		if(eroare==1)
		{
			
			var buget=new Array();
			buget[50000]='<150.000';
			buget[75000]='150.000-200.000';
			buget[100000]='200.000-250.000';
			buget[125000]='>250.000';
			
			var FirstName = $('#FirstName').val();
			var LastName = $('#LastName').val();
			var MobilePhone = $('#MobilePhone').val();
			var Email = $('fieldset:first input#Email').val();
			
			
			var Message = $('#Message').val();
			var Occupation = $('#Occupation').val();
			var companie = $('#companie').val();
			var NoRooms = $('#NoRooms').val();
			var Budget = $('#Budget').val();
			
			
			
			var about_a = new Array();
			/*if($('input#banner').attr('checked') == true) about_a[0] = 85;
			if($('input#promovare').attr('checked') == true) about_a[1] = 14;
			if($('input#rezi').attr('checked') == true) about_a[2] = 18;
			if($('input#prieteni').attr('checked') == true) about_a[3] = 11;
			if($('input#consultanti').attr('checked') == true) about_a[4] = 12;
			var about ='';
			for(i=0; i<5; i++){
				if(about_a[i]){ 
					if(i==4){
						var about =about + about_a[i];
					}else{
						var about =about + about_a[i] + ',';
					}
					
				}
			}*/
			var about ='';
			var count = $('input[name="about"]').length;
			$('input[name="about"]').each(function(index){
				
				if($(this).attr('checked') == true){
					if(index == count-1){
						about += $(this).val();
					}else{
						about += $(this).val() + ',';
					}
					
				}
			});
			
			
		
			var otherInformation = '';
			
			if( $('input#otherInformation').val() == 'Alte surse - precizaţi'){
				otherInformation = 'undefined';
			}else {
				otherInformation = $('input#otherInformation').val();
			}
			var AllowReceiveOffer = false;
			if($('input#AllowReceiveOffer').attr('checked') == true) AllowReceiveOffer = true;
			
			
			
			var dataStr = 'otherInformation=' + otherInformation +'&about='+ about +'&Language=ro'+ '&AllowReceiveOffer='+ AllowReceiveOffer  + '&Message=' + Message + '&Occupation=' + Occupation + '&Budget=' + Budget + '&NoRooms=' + NoRooms + '&Email=' + Email + '&MobilePhone=' + MobilePhone + '&LastName=' + LastName + '&FirstName=' + FirstName + '&companie=' + companie;
				 
			
			
			$.ajax({
			    type: "POST",
			    url: "contact1.php",
			    data: dataStr,
			    success: function(){

				  	$('.succes_container').css('z-index','5');
					$('#succes').html('Mesajul dvs. a fost trimis.<br/>In cel mai scurt timp veţi fi contactat telefonic de unul dintre consultanţii noştri, pentru a vă oferi mai multe informaţii despre proiect.<br /><br />Vă mulţumim! Echipa Asmita Gardens')
					.fadeIn('slow').animate({opacity: 1.0}, 6000).fadeOut('slow', function() {
							$(this).remove();
							window.location = "http://www.asmitagardens.ro/";
					});
					

				}
			  });
			
			
			return false;
		}else{
				$('.succes_container').css('z-index','5');
				$('#succes').html('Va rugam completati campurile marcate cu rosu.<br /><br />Vă mulţumim! Echipa Asmita Gardens')
				.fadeIn('slow').animate({opacity: 1.0}, 1500).fadeOut('slow', function() {
						//$(this).remove();
						$('.succes_container').css('z-index','0');
						
				});
		return false;
			
		}
		

		
	});
	
	$('#contact_main fieldset:first input, #contact_main fieldset:first textarea, #Occupation, #companie').focus(function(){
		if($(this).val()== 'Completaţi această casetă' || $(this).val()== 'Adresa de email invalidă'  || $(this).val()== 'Număr de telefon invalid'){
			$(this).val('').css('background','white');
		}
		
	});
	
	
	
	$('.meniu_panorama360 a').each(function(index){
		$(this).click(function(){
			$('.meniu_panorama360 a').removeClass('activ');
			$(this).addClass('activ');
			$('.submeniu_panorama360').addClass('hidden');
			$('.submeniu_panorama360').eq(index).removeClass('hidden');
			
		});
	});
});

function resizeImageSCut() {
	var winH = $(window).height();
	var winW = $(window).width();
	
	$('#bg_wrap').css({height: winH, width: winW}).css({visibility:"visible", display:"block"});	
	
	var ratio = winW/winH;
	
	var imgH = $('img#resize').height();
	var imgW = $('img#resize').width();
	var imgratio =imgW/imgH;
	
		
	if (ratio > imgratio) {	
		// se recalculeaza height
		var newHeight = (winW / imgW) * imgH;
		var newWidth = winW;
	} else {
		// se recalculeaza width
		var newHeight = winH;
		var newWidth = (winH / imgH) * imgW;
	}
	
	// centrare imagine
	newTop = 0 - ((newHeight - winH) / 2);
	//newTop = 0;
	newLeft =  0 - ((newWidth - winW) / 2);
	//newLeft = 0;

	// redimensionare imagine
	$('img#resize').css({height: newHeight, width: newWidth});	
	// repozitionare imagine
	$('img#resize').css({top: newTop, left: newLeft});
		
}

function validate(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if(reg.test(email) == false) {
	return false;
	}
}

function checkPhone(str) 
{
	var phone2 = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
	if (str.match(phone2)) {
   		return true;
 	} else {
 		return true;
 	}
}
function trim(str) {
	return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };


