var pop_exp_slug = "";
var mouse_over_expert = false;

function popContacts(expert_slug) {
	pop_exp_slug = expert_slug;
	var exp_name = $('#expertname_'+expert_slug).text();
	var name = "Contact " + exp_name;
    var content =   '<div id="popup_preloader"></div>' +
                    '<form id="popup_contact_expert">' +
					'<div id="popup_error" class="msg_warn_sm">Entered data is invalid. <span>Please check your data and try again.</span></div>' +
                    '<div class="formRow"><label for="name">Your name:</label><input type="text" id="popup_ex_name" name="name" value=""/></div>' +
                    '<div class="formRow"><label for="email">Your email:</label><input type="text" id="popup_ex_email" name="email" value=""/></div>' +
                    '<div class="formRow"><label for="content">Message:</label><textarea id="popup_ex_content" name="content"></textarea></div>' +
                    '<div class="padRow"><button class="bt_shadow_big bt_prim" value="Submit" name="submit" id="popup_ex_submit" type="submit">Send</button><button class="bt_trans_big" id="popup_ex_cancel">Cancel</button></div>' + 
                    '</form>';
    cvDialog.attach(name, content);
    cvDialog.show();
	cvDialog.resize(525);
	cvDialog.attach_button("#popup_ex_submit", function() {
		var post_data = $("#popup_contact_expert").serializeArray();
		$.post('/' + pop_exp_slug + '/post_contact/', post_data, parse_post_result, 'json');
		$('#popup_preloader').height($('#popup_contact_expert').height());
		$('#popup_preloader').show();
		$('#popup_contact_expert').hide();
        return false;
    });
    cvDialog.attach_button("#popup_ex_cancel", function() {
        cvDialog.hide();
        return false;
    });
}

function parse_post_result(response) {
	if (response.result) {
		cvDialog.show_message('Thank you', 'Your message to '+ $('#expertname_'+pop_exp_slug).text() + ' was successfully sent.', 250, 130);
	} else {
		$('#popup_preloader').hide();
		$('#popup_contact_expert').show();
		$('#popup_error').show();
		cvDialog.resize(525);
	}
}

$(document).ready(function() {
	$('.expertcontact_link').click(function(){
		$('#expertprofile_menu').hide();
		var id = this.id.split('_')[1];
		popContacts(id);
		return false;
	});
	$('#expertprofile_show').click(function() {
		$('#expertprofile_menu').show();
	});
	$('#expertprofile_hide').click(function() {
		$('#expertprofile_menu').hide();
	});
	$('#expertprofile_menu, #expertprofile_show').mouseover(function() {
		mouse_over_expert = true;
	});
	$('#expertprofile_menu, #expertprofile_show').mouseout(function() {
		mouse_over_expert = false;
	});
	$(document).click(function() {
		if (!mouse_over_expert) $('#expertprofile_menu').hide();
	});
});

$(document).ajaxError(function() {
	cvDialog.show_message('System Error!', 'Unable to access the server. Please check your internet connection and try again later.', 300, 130);
});
