



$(function () {

    var totalPanels = $(".OPscrollcontainer").children().size();

    var regWidth = $(".panel").css("width");
    var regImgWidth = $(".panel img").css("width");
    var regTitleSize = $(".panel h2").css("font-size");
    var regParSize = $(".panel p").css("font-size");

    var movingDistance = 170;

    var curWidth = 150;
    var curImgWidth = 126;
    var curTitleSize = "20px";
    var curParSize = "15px";

    var $panels = $('#OPslider .OPscrollcontainer > div');
    var $container = $('#OPslider .OPscrollcontainer');

    $panels.css({ 'float': 'left', 'position': 'relative' });

    $("#OPslider").data("currentlyMoving", false);

    $container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100)
		.css('left', "0px");

    var scroll = $('#OPslider .scroll').css('overflow', 'hidden');

    var OPintervalID = setInterval(OPRotate, 5000);

    function OPRotate() {
        change(true);
    }

    $("#imgOPPausePlay").click(function () {

        var src = $("#imgOPPausePlay").attr("src");

        if (src.indexOf("pause") > -1) {

            clearInterval(OPintervalID);
            $("#imgOPPausePlay").attr("src", "images/play.png");
        }
        else if (src.indexOf("play") > -1) {

            OPintervalID = setInterval(OPRotate, 5000);
            $("#imgOPPausePlay").attr("src", "images/pause.png");
        }

    });
    function OPPause() {

        clearInterval(OPintervalID);
        $("#imgOPPausePlay").attr("src", "images/play.png");
    }

    function returnToNormal(element) {
        $(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
    };

    function growBigger(element) {
        $(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
    }

    //direction true = right, false = left
    function change(direction) {

        //if not at the first or last panel
        if ((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 3))) {
            OPPause();
            return false;
        }

        //if not currently moving
        if (($("#OPslider").data("currentlyMoving") == false)) {

            $("#OPslider").data("currentlyMoving", true);

            var next = direction ? curPanel + 1 : curPanel - 1;
            var leftValue = $(".OPscrollcontainer").css("left");
            var movement = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;

            $(".OPscrollcontainer")
				.stop()
				.animate({
				    "left": movement
				}, function () {
				    $("#OPslider").data("currentlyMoving", false);
				});

            returnToNormal("#OPpanel_" + curPanel);
            growBigger("#OPpanel_" + next);

            curPanel = next;

            //remove all previous bound functions
            $("#OPpanel_" + (curPanel + 1)).unbind();

            //go forward
//            $("#OPpanel_" + (curPanel + 1)).click(function () { OPPause(); change(true); });

            //remove all previous bound functions															
            $("#OPpanel_" + (curPanel - 1)).unbind();

            //go back
//            $("#OPpanel_" + (curPanel - 1)).click(function () { OPPause(); change(false); });

            //remove all previous bound functions
            $("#OPpanel_" + curPanel).unbind();
        }
    }

    // Set up "Current" panel and next and prev
    growBigger("#OPpanel_3");
    var curPanel = 3;

//    $("#OPpanel_" + (curPanel + 1)).click(function () { OPPause(); change(true); });
//    $("#OPpanel_" + (curPanel - 1)).click(function () { OPPause(); change(false); });

    //when the left/right arrows are clicked
    $(".OPright").click(function () { OPPause(); change(true); });
    $(".OPleft").click(function () { OPPause(); change(false); });


    $(window).keydown(function (event) {
        switch (event.keyCode) {
            case 13: //enter
                OPPause();
                $(".OPright").click();
                break;
            case 32: //space
                OPPause();
                $(".OPright").click();
                break;
            case 37: //left arrow
                OPPause();
                $(".OPleft").click();
                break;
            case 39: //right arrow
                OPPause();
                $(".OPright").click();
                break;
        }
    });

});
