var currentSelectedCat = 0;
var currentSelectedSubCat = 0;
function leftCatClick(id) {
	if (!$("catList" + id)) return;
	
	var ind = currentSelectedCat;
	if(currentSelectedCat) {
		$("catList" + ind).style.display = "none";		
		//$("catLI" + ind).className = "";
		$("catLIA" + ind).className = "link1";
	}
	currentSelectedCat = id;
	ind = id;
	$("catList" + ind).style.display = "";
	//$("catLI" + ind).className = "leftCatSel";
	$("catLIA" + ind).className = "linksel";	
}

function setSubmenuActive(id) {	
	var ind;
	if(currentSelectedSubCat) {
		ind = currentSelectedCat;	
		$("catLIA" + ind).className = "link1";
	}	
	if(currentSelectedCat == id) return;
	
	currentSelectedSubCat = id;
	ind = id;	
	$("catLIA" + ind).className = "linksel";
	
}


function mainSearchSubmit() {
	var el = $("mainSearch");
	var v = el.value.replace(/^\s+|\s+$/g,"");
	if(v == "") {		
		alert('Introduceti cuvantul cautat');
		el.focus();
		return false;
	}
	return true;
}

function mainNewsletterSubmit() {
	var el = $("mainNewsletter");
	
	if(el.value=="") {
		el.value = " ";
		alert('Introduceti adresa de email');
		el.value = "";
		el.focus();
		return false;
	}
	
	var emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;		
	if(!emailRegExp.test(el.value)) {
		alert('Adresa de email introdusa nu este valida');
		el.focus();
		return false;
	}
	
	return true;
}

var fSliderTimer = null;
var fSliderPage = 0;
var $fSlider = null;
var $currentImg;
var fSliderPages = 0;

var frontMoveSlider = function(toPage) {
	$jcurrentImg.fadeOut(800);
	var $jnextImg = jQuery(".a-banner:nth-child(" + (toPage + 1) + ")", $jfSlider);
	$jnextImg.fadeIn(800);

	$jcurrentImg = $jnextImg;

	fSliderPage = toPage;

	/*
	$jfSlider.fadeOut();
	$jfSlider.css({marginLeft: (-1)*(toPage)* 832 +'px'});
	$jfSlider.fadeIn();
	*/
}

var frontPageSlide = function() {		
	if(fSliderPage + 1 < fSliderPages) {
		frontMoveSlider(fSliderPage + 1);
	}
	else {
		frontMoveSlider(0);
	}
}


var initFrontSlider = function() {

	$jfSlider = jQuery("#bannerSlide #bannerSlideC");
	$jcurrentImg = jQuery(".a-banner:first", $jfSlider);

	fSliderPages = jQuery("#bannerSlide #bannerSlideC .a-banner").size();

	$jcurrentImg.show();

	if(fSliderPages < 2) return;

	fSliderTimer = setInterval ( "frontPageSlide()", 5000 );						
}

function checkEmail(value) {
	var emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;		
	return emailRegExp.test(value);		 
}

var solicitaOfertaSubmit = function(form) {
	//validate fields
	var $form = $j(form);
	var formValues = {};	
	$j("input,textarea", $form).each( function() {
		var v = jQuery.trim($j(this).val());		
		formValues[$j(this).attr("name")] = v;
	});
	
	if(formValues["name"].length == 0) {
		alert("Numele este obligatoriu!");
		$j("input[name=name]", $form).focus();
		return false;
	}
	
	if(formValues["email"].length == 0) {
		alert("Adresa de email este obligatorie!");
		$j("input[name=email]", $form).focus();
		return false;
	}
	
	if(!checkEmail(formValues["email"])) {
		alert("Introduceti o adresa de email valida!");
		$j("input[name=email]", $form).focus();
		return false;
	}
	
	if(formValues["telefon"].length == 0) {
		alert("Telefonul este obligatoriu!");
		$j("input[name=telefon]", $form).focus();
		return false;
	}

	formValues['save'] = 1;
	
	$j(".ajax-loader", $form.parent()).show();
	
	$j.ajax({  
		type: "POST",  
		url: "cere_oferta.php",  
		data: formValues,  
		success: function(msg) {
			$j(".ajax-loader", $form.parent()).hide();
			var $p = $form.parent();
			var h = $p.height() - 50;			
			$p.html('<div class="message" style="height:'+h+'px;padding-top:50px;">Va multumim! Mesajul dumneavoastra a fost trimis! <br /><br /> Veti fi contactat telefonic in cel mai scurt timp</div>');
		}
	});

} 

function catsectionactive(el) {
	var $catSection = $j(el).parent();
	
	if($catSection.hasClass("active"))	{ 
		var $activeCats = $j(".rootCatEl.active", $catSection.parent());
		$activeCats.removeClass("active");
		$j(".rootCatItems", $activeCats).slideUp();
		return;
	}
	
	var $activeCats = $j(".rootCatEl.active", $catSection.parent());
	$activeCats.removeClass("active");
	$j(".rootCatItems", $activeCats).slideUp();

	$catSection.addClass("active");
	$j(".rootCatItems", $catSection).slideDown();

}


jQuery(document).ready(function($) {
	initFrontSlider();
});
