$(document).ready(function(){
	$('.standard-roundness').corner("round 8px").parent().css('padding', '4px').corner("round 10px");
	$('#menu-nav li ul, blockquote').corner('5px');
	$('#content .main-picture').corner('8px top');
	
	//search
	$('#s').focus(function(){
		if($(this).val() == $search_in_site_lang)
			$(this).val('');
	});
	
	$('#s').blur(function(){
		if(!$(this).val())
			$(this).val($search_in_site_lang);
	});
	
	//newsletter
	$('#newsletter input[name=newsletteremail]').focus(function(){
		if($(this).val() == $newsletter_message_js)
			$(this).val('');
	});
	
	$('#newsletter input[name=newsletteremail]').blur(function(){
		if(!$(this).val())
			$(this).val($newsletter_message_js);
	});
	
	//date-picker
	$("#fond_data_dela, #fond_data_panala").datepicker();
	


	function mycarousel_initCallback(carousel)
	{
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	
	//news carousel
	$('#noutati').jcarousel({
		auto: 3,
		vertical: true,
		scroll: 1,
		wrap: 'last',
		initCallback: mycarousel_initCallback
	});
	
	$('#valori-fonduri').jcarousel({
		auto: 3,
		vertical: true,
		scroll: 1,
		wrap: 'last',
		initCallback: mycarousel_initCallback
	});

	$('#loadDespreInvestitii').load('/wp-content/themes/certinvest/module/get.ultimele.articole.despreinvestitii.php');


	$('#newsletter').click(function()
	{
		$('#popupInfodiv').show();
		$.scrollTo({top:'0px', left:'100px'}, 800);
		$('#popupInfodiv').load('/wp-content/themes/certinvest/include/newsletter.php');
	});
	
	
	/* CONTACT FORM */
	$('#contactForm').submit(function(){
		
		//reset error
		$('#email').css('color', '#4F4F4F');
		$('#email').css('border-color', '#CCCCCC');
		
		$('label[for=email]').css('color', '#4F4F4F');
		$('label[for=mesaj]').css('color', '#4F4F4F');
		
		$('#mesaj').css('color', '#2f2f61');
		$('#mesaj').css('border-color', '#CCCCCC');
		
		var error = false;
		if (!$('#email').val() || !validateEmail($('#email').val())){

				$('#email').css('border-color', '#D3273B');
				$('#email').css('color', '#D3273B');
				$('label[for=email]').css('color', '#D3273B');
				error = true;
		}
		
		if (!$('#mesaj').val()){

				$('#mesaj').css('border-color', '#D3273B');
				$('#mesaj').css('color', '#D3273B');
				$('label[for=mesaj]').css('color', '#D3273B');
				
				error = true;
		}
		
		if(error)		
			return false;
	});

	
	
	
});


/* FUNCTIONS */
function newsletter_submit()
{
	$('#submit').hide();
	
	
	var param ='email='+$.trim($('#email').val())+'&nume='+$.trim($('#nume').val())+'&prenume='+$.trim($('#prenume').val())+'&actiune='+$.trim($('#actiune').val());

	  $.ajax({
		type: "POST",
		url: '/wp-content/themes/certinvest/include/newsletter.php',
		cache: false,
		data: param,
		success: function(html){
		  $('#popupInfodiv').html(html);
		},
		error: function(){
		  //ajax_error();
		  $('#submit').show();
		}
	  });
	  
	  return false;
}

function newsletter_submit_quick()
{
	$('#submit').hide();
	$('#popupInfodiv').show();
	$.scrollTo({top:'0px', left:'100px'}, 800);
	
	var param ='email='+$.trim($('#newsletteremail').val())+'&actiune=a';

	  $.ajax({
		type: "POST",
		url: '/wp-content/themes/certinvest/include/newsletter.php',
		cache: false,
		data: param,
		success: function(html){
		  $('#popupInfodiv').html(html);
		},
		error: function(){
		  //ajax_error();
		  $('#submit').show();
		}
	  });
	  
	  return false;
}

function newsletter_hide()
{
	$('#popupInfodiv').hide();
	$('#popupInfodiv').html('');	
}




/* STRUCTURA FONDULUI LA ZI */
function show_structura_fondului($fond) {
	
	  var param ='fond='+$fond;
	  $('#popupInfodiv').show();
	  $.scrollTo({top:'0px', left:'100px'}, 800);
	
	  $.ajax({
		type: "POST",
		url: '/wp-content/themes/certinvest/include/show_structura_fond.php',
		cache: false,
		data: param,
		success: function(html){
		  $('#popupInfodiv').html(html);
		},
		error: function(){
		  //ajax_error();
		}
	  });
	  
	  return false;
	  
}

/* POPUP AJAX */
function popupAjaxLoad($data) {
	
	  var param ='data='+$data;
	  $('#popupInfodiv').show();
	  $.scrollTo({top:'0px', left:'100px'}, 800);
	
	  $.ajax({
		type: "POST",
		url: '/wp-content/themes/certinvest/include/popupAjaxInfo.php',
		cache: false,
		data: param,
		success: function(html){
		  $('#popupInfodiv').html(html);
		},
		error: function(){
		  //ajax_error();
		}
	  });
	  
	  return false;
	  
}

/* DATE PICKER RO LOCALIZATION */
jQuery(function($){
	$.datepicker.regional['ro'] = {
		closeText: 'Închide',
		prevText: '&laquo; Luna precedentă',
		nextText: 'Luna următoare &raquo;',
		currentText: 'Azi',
		monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie',
		'Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'],
		monthNamesShort: ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun',
		'Iul', 'Aug', 'Sep', 'Oct', 'Noi', 'Dec'],
		dayNames: ['Duminică', 'Luni', 'Marţi', 'Miercuri', 'Joi', 'Vineri', 'Sâmbătă'],
		dayNamesShort: ['Dum', 'Lun', 'Mar', 'Mie', 'Joi', 'Vin', 'Sâm'],
		dayNamesMin: ['Du','Lu','Ma','Mi','Jo','Vi','Sâ'],
		weekHeader: 'Săpt',
		dateFormat: 'yy-mm-dd',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['ro']);
});

function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}
