$(document).ready(function() {
	$("a[rel^=lightbox]").fancybox({'overlayOpacity':'0.7', 'centerOnScroll':'false', 'imageScale': 'false'});
	$("a[class^=iframe]").fancybox({ 'width' : 750,'height' : '80%', 'overlayOpacity':'0.7', 'centerOnScroll':'false', 'imageScale': 'false', 'titlePosition' : 'inside','type' : 'iframe', 'autoScale' : 'true'});
	$("a[class^=iframe-video]").fancybox({ 'width' : 850,'height' : 600, 'overlayOpacity':'0.7', 'centerOnScroll':'false', 'imageScale': 'false', 'titlePosition' : 'inside','type' : 'iframe', 'autoScale' : 'true'});
	$("#selectCategory").change(function(){
		var show_div_id = String($("#selectCategory option:selected").val());
		//alert(show_div_id);
		if(show_div_id == "F_all"){
			//$('#FutGrpALL > DIV').hide();
			//alert('all');
			$('#FutGrpALL > DIV').show();
		}else{
			$('#FutGrpALL > DIV').hide();
			$('#'+show_div_id).show();
		}
//		alert('aa');
	});
	//menu 
	//Set custom configurations
	var config = {
	     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
	     interval: 10, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 100, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	$("ul#main-menu li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#main-menu li").hoverIntent(config); //Trigger Hover intent with custom configurations
	//open a link in a blank window
	$('a[rel="blank"]').click(function(){
		window.open(this.href);
		return false;
	});
});

//*******************************************************************************
//On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 
    if ( $(this).find(".row").length > 0 ) { //If row exists...
        var biggestRow = 0;	
        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });
		biggestRow = 179;
        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
    } else { //If row does not exist...
        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        rowWidth = 179;
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width
    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}
//*******************************************************************************

function validate_contact_form() {
	var name = $("input#name").val();
	var phone = $("input#phone").val();
	var email = $("input#email").val();
	var captcha = $("input#captcha").val();
	var contact_hash = $("input#contact_hash").val();
	var message = $("#message").val();
	$.post("ajax/_ajax_validate_contact_form.php", { name: name, phone: phone, email: email, message: message, contact_hash: contact_hash, captcha: captcha},
	  function(data){
		if(data.result == 1){
			$('#form-error').removeClass("message_error");
			$("#contact-us").html(data.success);
		}else{
			$('.error-field').removeClass("error-field")
			jQuery.each(data.error, function() {
				  $("#" + this).addClass("error-field");
			});
			$('#form-error').addClass("message_error");
			$('#form-error').html(data.error_msg.join("<br />"));
			$('#img_captcha').click();
		}
	  }, "json"
	);
    return false;
};

function validate_info_suplimentare() {
	var name = $("input#name").val();
	var first_name = $("input#first_name").val();
	var cnp = $("input#cnp").val();
	var email = $("input#email").val();
	var phone = $("input#phone").val();
	var address= $("input#address").val();
	var info_hash = $("input#info_hash").val();
	var message = $("textarea#message").val();
//	alert('111');
	$.post("ajax/_ajax_validate_info_suplimentare.php", { name: name, first_name: first_name, cnp: cnp, email: email, phone: phone, address: address, message: message, info_hash: info_hash},
	  function(data){
	 	//alert('aaa');
		if(data.result == 1){
			$('#form-error').removeClass("message_error");
			$("#contact-us").html(data.success);
		}else{
			$('.error-field').removeClass("error-field")
			jQuery.each(data.error, function() {
				  $("#" + this).addClass("error-field");
			});
			$('#form-error').addClass("message_error");
			$('#form-error').html(data.error_msg.join("<br />"));
		}
	  }, "json"
	);
    return false;
};
function check_cnp_exists(){
	var cnp = $("#CNP").val();
	var account_hash = $("#account_hash").val();
	//var tip_cont = $("#tip_cont").val();
	var tip_cont = $("input[name='varianta']:checked").val();
	//alert(tip_cont);
	$.post("ajax/_ajax_check_cnp_exists.php", { 'cnp': cnp, 'account_hash': account_hash, 'tip_cont': tip_cont},
		  function(data){
			if(data.result == 0){
				var answer = confirm(data.error_msg.join("<br />"));
				if (answer){
					$.post("ajax/_ajax_check_cnp_exists.php", { 'cnp': cnp, 'account_hash': account_hash, 'tip_cont': tip_cont, 'send_email': 1},
						  function(data){
							if(data.result == 1){
								//alert('aaa');
								$("#form-stockhit").remove();
								$("#success_msg").html(data.success); 
							}else{
								//alert('bbb');
							}
						  }, "json"
					);
				}
				else{
					
				}
//				$('#img_captcha').click();
			}
		  }, "json"
	);
}