﻿/*
* Google Docs DECK
*******************************************************************/
var iMontage = null;
var CurrentTab = 2;

(function ($) {

    $.fn.initDeck = function (o) {

        var opts = $.extend({}, {
            animation: 'fade',
            seconds: 8
        }, o);
        var playSettings = {
            pause: false
        };

        return this.each(function (target) {
            //Make sure all screen layers are faded out in all browsers
            $('#' + this.id + ' div.Tab').fadeOut(1);

			//Start a counter that will execute every so often.  Interval set above in Montage Settings
			iMontage = setInterval(function () { CycleTabs(playSettings.pause); }, opts.seconds * 1000);
			
            //Add Buttons
            var s = '<div id="Tabs">';
            var w = 0;
            for (var i = 0; i < $('#' + this.id + ' div.Tab').length; i++) {
                s += '<div id="TabBtn_' + i + '">&nbsp;</div>';
                w += 20;
            }
            s += "</div>";
            $('#' + this.id + ' div.Header').html(s + "<hr />");
            $('#' + this.id + ' div.Header #Tabs').css('width', w);

            //Select First Tab
            $('#' + this.id + ' div.Header #Tabs div:nth-child(1)').attr('class', 'selected');
            $('#' + this.id + ' div:nth-child(' + CurrentTab + ').Tab').css('visibility', 'visible');
            $('#' + this.id + ' div:nth-child(' + CurrentTab + ').Tab').fadeIn(1500);

            //Handle Button Actions
            $('#Tabs>div').mouseenter(function () {
                if ($(this).attr('class') != 'selected') {
                    $(this).attr('class', 'over');
                }
            });
            $('#' + this.id).mouseenter(function () { playSettings.pause = true; });
            $('#Tabs>div').mouseleave(function () {
                if ($(this).attr('class') != 'selected') {
                    $(this).attr('class', '');
                }
            });
            $('#' + this.id).mouseleave(function () { playSettings.pause = false; });
            $('#Tabs>div').click(function () { SelectTab(parseInt(this.id.replace("TabBtn_",""))); });

        });
    }
})(jQuery);

function CycleTabs(p) {
	if (!p)
	{
		$('#TabContent>div:nth-child(' + CurrentTab + ')').fadeOut(1500, function() { 
			$('#TabContent>div:nth-child(' + CurrentTab + ')').css('visibility','hidden');
			if (CurrentTab < $('#TabContent>div').length) CurrentTab += 1;
			else CurrentTab = 2;
			
			$('#Tabs>div').attr('class','');
			$('#Tabs>div:nth-child(' + (CurrentTab-1) + ')').attr('class','selected');
			
			$('#TabContent>div:nth-child(' + CurrentTab + ')').css('visibility','visible');
			$('#TabContent>div:nth-child(' + CurrentTab + ')').fadeIn(1500);
		});
	}
}

function SelectTab(t) {
	$('#TabContent>div:nth-child(' + CurrentTab + ')').fadeOut(250, function() { 
		$('#TabContent>div:nth-child(' + CurrentTab + ')').css('visibility','hidden');
		CurrentTab = t+2;
		$('#Tabs>div').attr('class','');
		$('#Tabs>div:nth-child(' + (CurrentTab-1) + ')').attr('class','selected');
		$('#TabContent>div:nth-child(' + CurrentTab + ')').css('visibility','visible');
		$('#TabContent>div:nth-child(' + CurrentTab + ')').fadeIn(250);
	});
}
