//data
var aGallery = [{
    'photoUrl': '/templates/pages/images/testimonials/1.jpg',
    'title': '<p>"I started receiving treatments from Dr. Scholz in April 2004. My initial requirements were for a rotator cuff injury that had caused me daily pain for 15 years. Within about three months I was pain free for the first time in 15 years! I now see Dr. Scholz at my leisure for preventative appointments throughout the year. I have also received successful treatments for other ailments such as back difficulties, knee, and hip problems. I have yet to find a joint or muscular ailment that Dr. Scholz could not treat with success."</p>',
    'signed':'<strong>Bart B.</strong><br /> Construction Worker'
}, {
    'photoUrl': '/templates/pages/images/testimonials/2.jpg',
    'title': '<p>"As a professional dancer, my career and livelihood depend on the health of my body. For six years, while dancing at the Alberta Ballet, I was fortunate enough to be treated by Dr. Scholz. His creative and intuitive approach to chiropractic work kept me healthy and working hard in the studio and enjoying every performance. Not every chiropractor is created the same and Calgary is very lucky for the talent and compassion of Dr. Scholz!"</p>',
    'signed':'<strong>Leigh Allardyce</strong> <br/> Professional Dancer'
}, {
    'photoUrl': '/templates/pages/images/testimonials/3.jpg',
    'title': '<p>"I\'d badly sprained my back during an exercise session, three weeks before our mountaineering trip to Bolivia. Our trip was in jeopardy. Through a series of ART and manipulation sessions, Dr. Sunderland was quickly able to get me back on my feet and on to the mountain. Thanks, Linsay!"</p>',
    'signed':'<strong>Tim T.</strong>'
}]

$(function() {

    if($("#gallery").length > 0) {

        createGallery();
        theInterval();
    }
    if($('#appointmentForm').length > 0) {
        appDate();
        $('#appointmentForm').validate(
        {
            errorPlacement: function(error, element) {

                error.appendTo("#formError");
            },
            rules: {
                fname:"required",
                lname:"required",
                phone:{required:true, phoneUS:true},
                email:{required:true, email:true},
                date:{required:true, date:true},
                time:"required"
            },
            messages: {
                fname:null,
                lname:null,
                phone:null,
                email:null,
                date:null,
                time:null}
        });
    }
    //create
    $('#Navigation ul').superfish();

    function appDate() {
        if($('#appointmentForm #date').length > 0) {
            $('#appointmentForm #date').datepicker();
        }
    }

    //same height columns
    var leftColHeight = $('#leftCol').height();
    var rightColHeight = $('#rightCol').height();
    if( rightColHeight > leftColHeight && rightColHeight >= 580 ) {
        $('#leftCol').css({'height':rightColHeight});
    } else if ( rightColHeight < leftColHeight && leftColHeight >= 580 ) {
        $('#rightCol').css({'height':leftColHeight});
        $('#leftCol').css({'height':leftColHeight});
    } else {
        $('#leftCol').css({'height': '580px'});
    }
    $('#Navigation li.NavigationPageAncestor').has('.NavigationPageCurrent').addClass('NavigationPageCurrent');
});//end dom ready
function createGallery() {
    //dom
    $('#gallery').empty();
    var galleryHtml = '';
    galleryHtml += '<div id="gImgCont"><img id="gImg" height="145" src="'+aGallery[0].photoUrl+'"/></div>';
    galleryHtml += '<div id="ctrl"><a id="prev" href="#"><</a><a id="next" href="#">></a></div>';
    galleryHtml += '<div id="caption">'+aGallery[0].title+'</div>';
    galleryHtml += '<div id="signed">'+aGallery[0].signed+'</div>';

    $('#gallery').html(galleryHtml);
    var i = 0;
    //clicks
    $('#next').bind('click', function() {
        if (i < aGallery.length - 1) {
            i++;
            $('#gImg').attr('src', aGallery[i].photoUrl).hide().fadeIn(700);
            $('#caption').html(aGallery[i].title);
            $('#signed').html(aGallery[i].signed);
            return false;
        } else {
            i = 0;
            $('#gImg').attr('src', aGallery[i].photoUrl).hide().fadeIn(700);
            $('#caption').html(aGallery[i].title);
            $('#signed').html(aGallery[i].signed);
            return false;
        }
    });
    $('#prev').bind('click', function() {
        if (i != 0) {
            i--;
            $('#gImg').attr('src', aGallery[i].photoUrl).hide().fadeIn(700);
            $('#caption').html(aGallery[i].title);
            $('#signed').html(aGallery[i].signed);
            return false;
        } else {
            i = aGallery.length-1;
            $('#gImg').attr('src', aGallery[i].photoUrl).hide().fadeIn(700);
            $('#caption').html(aGallery[i].title);
            $('#signed').html(aGallery[i].signed);
            return false;
        }
    });
}

var theInt = null;
var curclicked = 0;
var stop = 0;

theInterval = function(cur) {
    clearInterval(theInt);

    theInt = setInterval( function() {
        $('#next').trigger('click');
        curclicked++;

        if( aGallery.length-1 == curclicked ) {
            curclicked = 0;
        }

        $("#gallery").mouseover( function() {
            if (stop==0) {
                clearInterval(theInt);
                stop=1;
            }
        });
        $("#gallery").mouseout( function() {
            stop=0;
            theInterval();
        });
    }, 5000);
};
//create slideshow
$('#headerImage').nivoSlider({
    effect:'fade',
    animSpeed:700,
    pauseTime:5000,
    controlNav:false,
    directionNav:false
});
