$(document).ready(function() {
	
	// submit email-me with ajax
	$('.error').hide();
	$("#submit-btn").click(function() {
		
		// validate presence of email
		$('.error').hide();
		var email = $("#em-email").val();
		if (email == "") {
			$("#em-error").show();
			$("#em-email").focus();
			return false;
		}
		var url = $("#em-url").val();
		var lan = $("#em-lan").val();
		
		var dataString = 'email=' + email + '&url=' + url + "&lan=" + lan;
		//alert (dataString);return false;
		$.ajax({
		  type: "POST",
		  url: "/sendmail",
		  data: dataString,
		  dataType: 'json',
		  success: function(data) {
			if (!data.error) {
			    $('.ms-email-me').html(data.msg).hide().fadeIn(1500);
			} else {
				$('#em-error').html(data.msg).show();
			}
		  }
		});
		return false;
		
	});
	
	// mysize form faq expansion
	$(".form-faq").hide();
	// assign click event
	$(".a-form-faq").click(function(){
	    $(this).next().animate({height: "toggle"}, {duration: "fast"});
	});
	
	// select 1st input field when document ready
	$("#id_l").select();
	
	// get input field values
	var form_v = {
	    "id_l": $("#id_l").val(),
	    "id_c": $("#id_c").val()
	}
	
	// set focus to active field, set blur to inactive field
	$("input[type='text']").focus(function(){
	    $(this).addClass("focus"); 
	    $(this).select();
	    if ($(this).val() == form_v[$(this).attr("id")])
            $(this).attr("value", "");
	});
	$("input[type='text']").blur(function(){
        $(this).removeClass("focus");
        if ($(this).val() == "")
            $(this).val(form_v[$(this).attr("id")]);
	});
	
	// style checkboxes
	$(".cb").css("display", "none").before("<span class='cb-span'></span>");
	$(".cb:checked").prev().addClass("checked");
	$("#chckbx-cont span").click(function(){
	    if($(this).hasClass("checked")) {
	        $(this).removeClass("checked");
	        $(this).next().attr("checked", false);
	    }
	    else {
	        $(this).addClass("checked");
	        $(this).next().attr("checked", true);
	    }
	});
	
	// assign klick on checkbox to switch replacement image because of label
	$(".cb").click(function(){
		if($(this).prev().hasClass("checked")) {
	        $(this).prev().removeClass("checked");
	        $(this).attr("checked", false);
	    }
	    else {
	        $(this).prev().addClass("checked");
	        $(this).attr("checked", true);
	    }
	});
	
	// assign active state to submit button
	$("button").mousedown(function(){
	    $(this).addClass("active");
	});

    // slide h2 on mysize page (does not work on iphone)
    //$(".slide").slideToggle(800);
    
    // panel collapsing on mysize page
    $(".slide").click(function(){
        $(this).siblings().animate({height: "toggle"}, {duration: "fast"});
    });

});
