$(function () {
    $("#services #banner ul li h2").click(function () {
        target = $(this).parent().attr("id");
        // Check if target section is either 1st or 2nd                             
        if ($(this).parent().is(":first-child")) {
            // If this section is open, close it
            if ($(this).parent().hasClass("open")) {
                $("#" + target + " ul").slideUp("100", function () {
                    $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
                    $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                });
                $(this).parent().removeClass("open");
            }
            else {
                // Check if a section is already open
                if ($("#services #banner ul li.open").length == 0) {
                    $("#services #banner ul li:nth-child(2)").animate({ "left": "445px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "545px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                    $("#" + target + " ul").slideDown("slow");
                    $(this).parent().addClass("open");
                }
                else {
                    $("#services #banner ul li.open ul").slideUp("100", function () {
                        $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
                        $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                        $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                        $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200', function () {
                            $("#services #banner ul li.open").removeClass("open");
                            $("#services #banner ul li:nth-child(2)").animate({ "left": "445px" }, '1200');
                            $("#services #banner ul li:nth-child(3)").animate({ "left": "545px" }, '1200');
                            $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                            $("#" + target + " ul").slideDown("slow");
                            $("#" + target).addClass("open");
                        });
                    });
                }
            }
        }
        else if ($(this).parent().is(":nth-child(2)")) {
            // If this section is open, close it
            if ($(this).parent().hasClass("open")) {
                $("#" + target + " ul").slideUp("100", function () {
                	 $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
	                 $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
    	             $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
        	         $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                });
                $(this).parent().removeClass("open");
            }
            else {
                // Check if a section is already open
                if ($("#services #banner ul li.open").length == 0) {
                	$("#services #banner ul li:nth-child(1)").animate({ "left": "-80px" }, '1200');
                	$("#services #banner ul li:nth-child(2)").animate({ "left": "70px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "545px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                    $("#" + target + " ul").slideDown("slow");
                    $(this).parent().addClass("open");
                }
                else {
                    $("#services #banner ul li.open ul").slideUp("100", function () {
                        $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
                        $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                        $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                        $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200', function () {
                            $("#services #banner ul li.open").removeClass("open");
                            $("#services #banner ul li:nth-child(1)").animate({ "left": "-80px" }, '1200');
		                	$("#services #banner ul li:nth-child(2)").animate({ "left": "70px" }, '1200');
		                    $("#services #banner ul li:nth-child(3)").animate({ "left": "545px" }, '1200');
		                    $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                            $("#" + target + " ul").slideDown("slow");
                            $("#" + target).addClass("open");
                        });
                    });
                }
            }
        }
        else if ($(this).parent().is(":nth-child(3)")) {
        	$(this).parent().children('ul').css('left', '270px');
            // If this section is open, close it
            if ($(this).parent().hasClass("open")) {
                $("#" + target + " ul").slideUp("100", function () {
                    $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
                    $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                });
                $(this).parent().removeClass("open");
            }
            else {
                // Check if a section is already open
                if ($("#services #banner ul li.open").length == 0) {
                    $("#services #banner ul li:first-child").animate({ "left": "-80px" }, '1200');
                    $("#services #banner ul li:nth-child(2)").animate({ "left": "20px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "120px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                    $("#" + target + " ul").slideDown("slow");
                    $(this).parent().addClass("open");
                }
                else {
                    $("#services #banner ul li.open ul").slideUp("100", function () {
                        $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                        $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                        $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                        $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200', function () {
                            $("#services #banner ul li.open").removeClass("open");
                            $("#services #banner ul li:first-child").animate({ "left": "-80px" }, '1200');
		                    $("#services #banner ul li:nth-child(2)").animate({ "left": "20px" }, '1200');
		                    $("#services #banner ul li:nth-child(3)").animate({ "left": "120px" }, '1200');
		                    $("#services #banner ul li:last-child").animate({ "left": "645px" }, '1200');
                            $("#" + target + " ul").slideDown("slow");
                            $("#" + target).addClass("open");
                        });
                    });
                }
            }
        }
        else {
        	$(this).parent().children('ul').css('left', '270px');
            // If this section is open, close it
            if ($(this).parent().hasClass("open")) {
                $("#" + target + " ul").slideUp("100", function () {
                     $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200');
	                 $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
    	             $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
        	         $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                });
                $(this).parent().removeClass("open");
            }
            else {
                // Check if a section is already open
                if ($("#services #banner ul li.open").length == 0) {
                    $("#services #banner ul li:first-child").animate({ "left": "-80px" }, '1200');
                    $("#services #banner ul li:nth-child(2)").animate({ "left": "20px" }, '1200');
                    $("#services #banner ul li:nth-child(3)").animate({ "left": "120px" }, '1200');
                    $("#services #banner ul li:last-child").animate({ "left": "220px" }, '1200');
                    $("#" + target + " ul").slideDown("slow");
                    $(this).parent().addClass("open");
                }
                else {
                    $("#services #banner ul li.open ul").slideUp("100", function () {
                        $("#services #banner ul li:nth-child(2)").animate({ "left": "165px" }, '1200');
                        $("#services #banner ul li:nth-child(3)").animate({ "left": "363px" }, '1200');
                        $("#services #banner ul li:last-child").animate({ "left": "560px" }, '1200');
                        $("#services #banner ul li:first-child").animate({ "left": "-31px" }, '1200', function () {
                            $("#services #banner ul li.open").removeClass("open");
                            $("#services #banner ul li:nth-child(3)").animate({ "left": "120px" }, '1200');
                            $("#services #banner ul li:nth-child(2)").animate({ "left": "20px" }, '1200');
                            $("#services #banner ul li:first-child").animate({ "left": "-80px" }, '1200');
                            $("#services #banner ul li:last-child").animate({ "left": "220" }, '1200');
                            $("#" + target + " ul").slideDown("slow");
                            $("#" + target).addClass("open");
                        });
                    });
                }
            }
        }
    });
});