﻿var pcgPortfolioSlide = function (parentEl) {
    var self = this;

    var slides = $('.slide', parentEl);
    var slideCount = slides.length;

    var current = 0;

    var go = function (index) {
        if (slideshowStopped) {
            return;
        }

        if (index < 0) {
            index = slideCount - 1;
        } else if (index >= slideCount) {
            index = 0;
        }

        if (slides.filter('.index_' + index).hasClass('loaded')) {
            current = index;
            slides.not('.index_' + index).fadeOut('slow');
            slides.filter('.index_' + index).fadeIn('slow');

            $('#banner-nav a').removeClass('active').eq(index).addClass('active');
        } else {
            // console.log(index+' not loaded yet');
        }
    };

    var slideshowStopped = false;
    var slideInterval = 8000;
    self.playSlideshow = function () {
        if (!slideshowStopped) {
            window.clearTimeout(timeoutRef);
            timeoutRef = window.setTimeout(slideShow, slideInterval);
        }
    };

    var timeoutRef = false;
    var slideShow = function () {
        go(1 * current + 1);
        timeoutRef = window.setTimeout(slideShow, slideInterval);
    };

    // load each image
    // marking as loaded/ready
    slides.each(function () {
        var slideImage = this;
        var src = $(this).attr('src');
        var imgPreloader = new Image();
        imgPreloader.onload = function () {
            $(slideImage).addClass('loaded');
        };
        imgPreloader.src = src;
    });

    var nav = $('<div></div>');
    for (var i = 0; i < slides.length; i++) {
        (function () {
            var ind = i;

            var a = $('<a></a>')
			.text(i + 1)
			.addClass('nav_' + i)
			.click(function () {
			    go(ind);
			    window.clearTimeout(timeoutRef);
			    playPause.addClass('paused');
			});

            if (i == 0) {
                a.addClass('active');
            }

            nav.append(a);
        })();
    }

    var playPause = $('<a></a>');
    playPause.text('play/pause').addClass('play-pause');
    playPause.click(function () {
        if ($(this).hasClass('paused')) {
            $(this).removeClass('paused');
            window.clearTimeout(timeoutRef);
            self.playSlideshow();
        } else {
            $(this).addClass('paused');
            window.clearTimeout(timeoutRef);
        }
    });

    nav.append(playPause);

    $('#banner-nav').append(nav).show();
};


$(document).ready(function () {
    var cwh = $('#content-wrap').height();
    var srh = $('#sidebar-right').height();
    var sbh = $('#sidebar-right .sidebar-body:eq(0)').height();

    var diff = srh - sbh; // portion of sidebar not taken up by .sidebar-body
    var ht = cwh - diff;

    $('#sidebar-right .sidebar-body:eq(0)').css('min-height', ht + 'px');
    $('#center').css('min-height', cwh + 'px');


    $('#ctl00_ctl00_SearchPanel input[type="text"], #physician-finder input[type="text"]').focus(function () {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    }).blur(function () {
        if (this.value == '') {
            this.value = this.defaultValue;
        }
    });


    var banners = $('#header-inner img');
    if (banners.length > 1) {
        (new pcgPortfolioSlide('#header-inner')).playSlideshow();
    }

});


