var ua = $.browser;
var vchar = ua.version.slice(0, 1);
if ((ua.msie && (vchar == '8' || vchar == '7'))) {
    var firststr = 'div.rotator div#rotate_1, div.rotator div#citrotate_1';
}
else {
    var firststr = 'div.rotator div[id$=1]';
}

    function theRotator() {
        if ((ua.msie && (vchar == '8' || vchar == '7'))) {
            //Set the opacity of all images to 0
            $('div.rotator div img').css({ opacity: 0.0 }).css('display', 'none !important');

            //Get the first image and display it (gets set to full opacity)
            $(firststr).children('img').css({ opacity: 1.0 }).css('display', 'block !important');

            //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
            if (typeof (play) != 'undefined') {
                clearTimeout(play);
            }
            play = setTimeout('rotate()', 6000);
        }
        else {
            //Set the opacity of all images to 0
            $('div.rotator div').css({ opacity: 0.0 });

            //Get the first image and display it (gets set to full opacity)
            $('div.rotator div[id$=1]').css({ opacity: 1.0 });

            //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
            if (typeof (play) != 'undefined') {
                clearTimeout(play);
            }
            play = setTimeout('rotate()', 6000);
        }
}

function rotate() {
    if ((ua.msie && (vchar == '8' || vchar == '7'))) {
        //Get the first image
        var current = ($('div.rotator div.show') ? $('div.rotator div.show') : $('div.rotator div:first'));

        if (current.length == 0) current = $(firststr);

        //Get next image, when it reaches the end, rotate it back to the first image
        var next;
        if (current.next().length == 0) {
            next = $(firststr);
        }
        else {
            next = current.next()
        }

        //Set the fade in effect for the next image, the show class has higher z-index
        current.removeClass('show');
        $('div.rotator div img').css({ opacity: 0.0 }).css('display', 'none !important');

        //Get the first image and display it (gets set to full opacity)
        next.addClass('show');
        next.children('img').css({ opacity: 1.0 }).css('display', 'block !important');

        var pushID = 'push' + next[0].id.substring(next[0].id.length - 1, next[0].id.length);
        $('div#push-container a').removeClass("push-page-selected");

        var push = $('div#push-container a#' + pushID);
        //for the dots
        push.addClass("push-page-selected");

        if (typeof (play) != 'undefined') {
            clearTimeout(play);
        }
        play = setTimeout('rotate()', 6000);
    }
    else {
	    //Get the first image
        var current = ($('div.rotator div.show') ? $('div.rotator div.show') : $('div.rotator div:first'));

        if (current.css('opacity') != 1.0) {
            return;
        }

        if ( current.length == 0 ) current = $('div.rotator div:first');

	    //Get next image, when it reaches the end, rotate it back to the first image
        var next;
        if(current.next().length == 0){
            next = $('div.rotator div[id$=1]');
        }
        else {
            next = current.next()
        }

	    //Set the fade in effect for the next image, the show class has higher z-index
	    next.css({opacity: 0.0})
	    .addClass('show')
	    .animate({opacity: 1.0}, 1000);

	    //Hide the current image
	    current.animate({opacity: 0.0}, 1000)
	    .removeClass('show');

	    var pushID = 'push' + next[0].id.substring(next[0].id.length - 1, next[0].id.length);
	    $('div#push-container a').removeClass("push-page-selected");

	    var push = $('div#push-container a#' + pushID);
	    //for the dots
	    push.addClass("push-page-selected");

	    play = setTimeout('rotate()', 6000);
    }
}

function set_index(index) {
    if ((ua.msie && (vchar == '8' || vchar == '7'))) {
        //Get the first image
        var current = ($('div.rotator div.show') ? $('div.rotator div.show') : $('div.rotator div:first'));

        if (current.length == 0) current = $(firststr);

        //Get next image, when it reaches the end, rotate it back to the first image
        var tempID = 'rotate_' + index;
        var next = $('div.rotator div#' + tempID + ', div.rotator div#cit' + tempID);

        //Set the fade in effect for the next image, the show class has higher z-index
        $('div.rotator div img').css({ opacity: 0.0 }).css('display', 'none !important');

        //Get the first image and display it (gets set to full opacity)
        next.children('img').css({ opacity: 1.0 }).css('display', 'block !important');
        if (typeof (play) != 'undefined') {
            clearTimeout(play);
        }
        play = setTimeout('rotate()', 12000);

        var pushID = 'push' + index;
        $('div#push-container a').removeClass("push-page-selected");
        var push = $('div#push-container a#' + pushID);
        //for the dots
        push.addClass("push-page-selected");

        document.getElementById('timer').style.display = "none";
    }
    else {
        //Get the first image
        var current = ($('div.rotator div.show') ? $('div.rotator div.show') : $('div.rotator div:first'));

        if (current.length == 0) current = $('div.rotator div:first');

        //Get next image, when it reaches the end, rotate it back to the first image
        var tempID = 'rotate_' + index;
        var next = $('div.rotator div[id$=' + tempID+ ']');
        if (next[0].id == current[0].id) {
            return;
        }

        //Set the fade in effect for the next image, the show class has higher z-index
        next.css({ opacity: 0.0 })
	    .addClass('show')
	    .animate({ opacity: 1.0 }, 1000);

        //Hide the current image
        current.animate({ opacity: 0.0 }, 1000)
	    .removeClass('show');

        if (typeof (play) != 'undefined') {
            clearTimeout(play);
        }
        play = setTimeout('rotate()', 12000);

        var pushID = 'push' + index;
        $('div#push-container a').removeClass("push-page-selected");
        var push = $('div#push-container a#' + pushID);
            //for the dots
        push.addClass("push-page-selected");

        document.getElementById('timer').style.display= "none";
    }
}

$(document).ready(function () {
    //Load the slideshow
    theRotator();
    $('div.rotator').fadeIn(1000);
    $('div.rotator div').fadeIn(1000); // tweak for IE

    $('div.rotator').fadeIn(1000);
    $('div.rotator div#citrotate_1').fadeIn(1000); // tweek for IE
});

$(document).blur(function () {
    if (typeof (play) != 'undefined') {
        clearTimeout(play);
    }
});

$(window).blur(function () {
    if (typeof (play) != 'undefined') {
        clearTimeout(play);
    }
});

$(window).focus(function () {
    if (typeof (play) != 'undefined') {
        clearTimeout(play);
    }
    play = setTimeout('rotate()', 6000);
});
