$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$(function() {
    $("#locationNOHO").attr('hidden', true); 
    $("#locationNOHObig").attr('hidden', true); 
    $("#locationNOHO").hide();
    $("#locationNOHObig").hide();
    $(".locationPicker").click(function(evt) {
        evt.preventDefault();

        if ( $(this).text().match(/Hollywood/) ) {
            $("#locationHObig").attr('hidden', false);
            $("#locationNOHObig").attr('hidden', true);
	    $("#locationHObig").show();
	    $("#locationNOHObig").hide();
        } else {
            $("#locationHObig").attr('hidden', true);
            $("#locationNOHObig").attr('hidden', false);
	    $("#locationHObig").hide();
	    $("#locationNOHObig").show();
        }
        
        $(this)
            .addClass('locationchoiceUP')
            .removeClass('locationchoiceHIDE');
        $(this)
            .siblings('.locationPicker')
            .addClass('locationchoiceHIDE')
            .removeClass('locationchoiceUP');

        $(this)
            .find('span')
            .addClass('locationchoiceUPtitle')
            .removeClass('locationchoiceHIDEtitle');
        $(this)
            .siblings('.locationPicker')
            .find('span')
            .addClass('locationchoiceHIDEtitle')
            .removeClass('locationchoiceUPtitle');

        return false;
    });

    $(".locationPickerSmall").click(function(evt) {
        evt.preventDefault();

        if ( $(this).text().match(/Hollywood/) ) {
            $("#locationHO").attr('hidden', false);
            $("#locationNOHO").attr('hidden', true);
	    $("#locationHO").show();
	    $("#locationNOHO").hide();
        } else {
            $("#locationHO").attr('hidden', true);
            $("#locationNOHO").attr('hidden', false);
	    $("#locationHO").hide();
	    $("#locationNOHO").show();
        }

        $(this)
            .addClass('locationchoiceUP')
            .removeClass('locationchoiceHIDE');
        $(this)
            .siblings('.locationPickerSmall')
            .addClass('locationchoiceHIDE')
            .removeClass('locationchoiceUP');

        $(this)
            .find('span')
            .addClass('locationchoiceUPtitle')
            .removeClass('locationchoiceHIDEtitle');
        $(this)
            .siblings('.locationPickerSmall')
            .find('span')
            .addClass('locationchoiceHIDEtitle')
            .removeClass('locationchoiceUPtitle');
        return false;
    });

	function start_forms () {
    $('.appservicebox').load('cal.php');
	}

	start_forms();

    $('ol.cal li a').live('click', function(evt) {
            evt.preventDefault();
            $('.selected-day').removeClass('selected-day');
            $(this).addClass('selected-day');
            $('#cal_day').val( $(this).text() );	
     });

    $('#serviceMEthis').live('focus', function() {
            if ( $(this).val() == 'Describe your issue.' ) {
                $(this).val('');
            }
    });

    $('#serviceMEthis').live('blur', function() {
        if ( $(this).val() == '' ) {
            $(this).val('Describe your issue.');
        }
    });
    $('#cal_name').live('blur', function() {
	if ( $(this).val() == '' ) {
		$(this).val('Type Name');
	}
    });

    $('#cal_email').live('blur', function() {
	if ( $(this).val() == '' ) {
		$(this).val('Type Email');
	}
    });

    $('#cal_name').live('focus', function() {
	if ( $(this).val() == 'Type Name' ) {
		$(this).val('');
	}
    });
   
    $('#cal_email').live('focus', function() {
	if ( $(this).val() == 'Type Email' ) {
		$(this).val('');
	}
    });

    $("#service_form_form").live('submit', function(evt) {
	evt.preventDefault();

	$('.appservicebox').load('serv.php', $('#service_form_form').serializeObject() );
	return false;
    }); 

    $('.clicksevice').live('click', function() {
	var good = true;
	var errors = new Array();
	if (  $('#cal_name').val() == '' || $('#cal_name').val() == 'Type Name' ) {
		good = false;
		errors.push('Please enter your name');
	}
	
	if ( $('#cal_email').val() == '' || $('#cal_email').val() == 'Type Email') {
		good = false;
		errors.push('Please enter your email address');
	}

	if ( $('textarea').val() == '' ) {
		good = false;
		errors.push('Please let us know what this is in regards to.');
	}

	if ( $('#cal_day').val() == '' ) {
		good = false;
		errors.push('Please choose a day for your visit.');
	}

	if ( good ) {
		$('.servicewrap').slideToggle('slow');
	} else {
		var ul = $('<ul class="error">');
		for ( var i in errors ) {
			ul.append( $('<li class="error">' + errors[i] + '</li>') );
		}

		$('#service_section1 .servicewrap').prepend( ul );
	}
    });

	$("#form_start_over").live('click', start_forms);
   
});

