var current_count = 0;

$(document).ready(function() {
	// hover color change effect
	$("#sel_slider li").hover(function() {
		$(this).animate({opacity: 0.90}, 100, function(){
			$(this).animate({opacity: 1}, 0);
		} );
	});
	// Trigger mouse move event over the 'menu_holder'.
	$("#sel_menu_holder").mousemove(function(e) {
		// Enable scroll function only when the height of the 'slider' or menu is greater than the 'menu_holder'.
		if($(this).height() < $("#sel_slider").height()) {
			// Calculate the distance value from the 'menu_holder' y pos and page Y pos.
			var distance = e.pageY - $(this).offset().top;
			// Get the percentage value with respect to the Mouse Y on the 'menu_holder'.
			var percentage = distance / $(this).height();
			// Calculate the new Y position of the 'slider'.
			var targetY = -Math.round(($("#sel_slider").height() - $(this).height()) * percentage);
			// With jQuery easing funtion from easing plugin.
			//$('#sel_slider').animate({top: [targetY+"px", "easeOutCirc"]}, { queue:false, duration:200 });
			$('#sel_slider').animate({top: [targetY+"px", "easeOutCirc"]}, {queue:false, duration:800});
			// Without easeing function. by default jQuery have 'swing'.
			//$('#slider').animate({top: [targetY+"px", "easeOutCirc"]}, { queue:false, duration:200 });
		}
	});


(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=125596720858468";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

        setup_survey();
});


function setLoader(target_id)
{
    $('#' + target_id).html('<img src="/images/loading.gif" align="center" />');
}

function post_form_update(url, form_id, target_div)
{
    if (form_id.indexOf('#') == -1)
    {
        form_id = '#' + form_id;
    }

    post_data = $(form_id).serialize();
    setLoader(target_div);
    $.post(url, post_data, function(data){

        obj = jQuery.parseJSON(data);
        msg = obj.msg;
        status = obj.status;

        if (status == '1') {
            $('#contactform_holder').hide();
        }

        $('#' + target_div).html(msg);

//        $('#' + target_div).html(data);
      });      
}

function setup_survey() {
        survey_hide_all();
        $('.survey:first').show();
        $('.indicators li:first').addClass('current_indicator');
}

function survey_hide_all() {
    $('.survey').each(function() {$(this).hide();});
}
function indicators_hide_all() {
    $('.survey_indicator').each(function() {$(this).removeClass('current_indicator');});
}

function survey_activate(group_id, question_id) {

    current_group_id = group_id - 1;
    check_count = 0;

    $('#q_group_' + current_group_id + ' .q_input').each(function(index, obj) { if (obj.checked) { check_count++ } } );

    if (check_count != 4) {
        alert('You need to complete the current set of questions, before you can continue.');
    } else {

        current_count = 0;
        indicators_hide_all();
        survey_hide_all();
        $('#q_group_' + group_id).show();
        $('#indicator_' + question_id).addClass('current_indicator');
    }
}

function survey_mark_complete(question_id) {
    current_count++;
    $('#indicator_' + question_id).addClass('completed_indicator');

    question_count = $('.questions').length;
    complete_count = $('.completed_indicator').length;

    if (question_count == complete_count) {
        $('#btnSubmit').show();
    }
}


function val_accreditation(f) {

    var msg = '';

    if (f.title.value.replace(/ /g, "") == '') {
        msg += '- Title\n';
    }

    if (f.firstname.value.replace(/ /g, "") == '') {
        msg += '- First name\n';
    }

    if (f.surname.value.replace(/ /g, "") == '') {
        msg += '- Surname\n';
    }

    if (f.email.value.replace(/ /g, "") == '') {
        msg += '- Email\n';
    } else if (!validemail(f.email.value.replace(/ /g, ""))) {
        msg += '- Valid email address\n';
    }

    if (f.brokerage.value.replace(/ /g, "") == '') {
        msg += '- Brokerage\n';
    }

    if (msg != '') {
        alert('Please note:\nThe following fields are required:\n' + msg);
        return false;
    } else {
        return true;
    }

}

function show_more() {
    $('#read_more').hide();
    $('#more_info').slideDown(500);
    $('#read_less').show();
}

function show_less() {
    $('#read_less').hide();
    $('#more_info').slideUp(500);
    $('#read_more').show();
}

function show_signup() {
    $('#signup-box').toggle();
}

function post_form_update_basic(url, form_id, target_div)
{
    if (form_id.indexOf('#') == -1)
    {
        form_id = '#' + form_id;
    }

    post_data = $(form_id).serialize();
    setLoader(target_div);
    $.post(url, post_data, function(data){

        $('#' + target_div).html(data);

      });
}

function val_signup(f) {

    var msg = '';

    if (f.signup_email.value == '') {
        msg += '- email address is required.\n';
        f.signup_email.focus();
    }

    if (msg != '') {
        alert('Please note:\n' + msg);
        return false;
    }
    return true;

}

function validemail(val)
{
    var RE1 = /^[A-Za-z0-9_-]+[.A-Za-z0-9_-]*@{1,1}[A-Za-z0-9_-]+[.]{1,1}\w{2,4}/;
    return RE1.test(val);
}
