
// Plugin correct code
(function( $ ) {
	$.fn.extend({
		// PTC Slider
		ptcSlider: function( options ) {
			
            var defaults = {
                cycle: true,
                timer: 4000
            }
                 
            var options =  $.extend(defaults, options);
		
			// Iterate over elements plugin called upon
			return this.each(function() {
				var o,t,obj,oid,ic,iw,cw,controller,controlstart,controlend,button,b,bIX,pos;
				
				// Plugin Option Variables
				o = options;
				t = o.timer;
				
				//Plugin Scope Variables
				obj = $(this);
				oid = '#' + obj.attr('id');
				
				// Slide Content Variables
				ic = obj.children('.slide-content').children().length;
				iw = obj.children('.slide-content').children().width();
				cw = ic * iw;
				
				// Slide selector variables
				scontrol = obj.children('.slide-controller');
				scontent = obj.children('.slide-content');
				
				// BUILD DYNAMIC BUTTON HTML CODE FOR INSERT INTO CONTROLLER
				controlstart = '<div class="slide-buttons">\n\
							<div class="nav">\n\
								<ul>\n';
				controlend = '\n\
								</ul>\n\
							</div>\n\
						</div>';
				button = '';
				
				for (i=0; i<ic; i++) {
					button += '<li class="slide-button-' + i + '">&nbsp;</li>\n';
				}
				controller = controlstart + button + controlend;
				
				// INSTER CONTROLLER BUTTONS INTO SLIDE-CONTROLLER
				scontrol.append(controller);
				
				// SLIDE CONTENT SETUP
				scontent.wrapAll('<div class="slide-content-wrapper" />').append('<div class="floater"></div>').css('width',cw);
				obj.children('.slide-controller').children('.slide-buttons').children('.nav').children('ul').children('li:first-child').toggleClass('active');
				
				// SLIDER ACTIONS
				obj.children('.slide-controller').children('.slide-buttons').children('.nav').children('ul').children('li').click(function() {
					var bIX = $(this).index();
					var pos = (iw * bIX);
					obj.children('.slide-content-wrapper').children('.slide-content').animate({
						left: '-' + pos
					}, 1000);
					obj.children('.slide-controller').children('.slide-buttons').children('.nav').children('ul').children('li.active').toggleClass('active');
					obj.children('.slide-controller').children('.slide-buttons').children('.nav').children('ul').children('.slide-button-' + bIX).toggleClass('active');
				});
				if (!(o.cycle == false)) {
					ab = obj.children('.slide-controller').children('.slide-buttons').children('.nav').children('ul').children('li');
					cycle(ab,t);
					looptime = t*ic;
					//setInterval( function() { cycle(ab,ic,t); } , looptime ); *** ic argument is incorrect
					setInterval(function () { cycle(ab, t); }, looptime);
					
				} // end autocycle option
				
			}); // end return
			
			// end ptcSlider
		}
	});
})( jQuery );

function cycle(b, t) {
    if (bContCycle) {
        b.each(function (i) {
            delay = i * t;
            $(this).delay(delay).queue(function () {
                $(this).trigger('click');
                $(this).dequeue();
            });
        });
    }
}

bContCycle = true;
window.onblur = cancelcycle;
window.onfocus = allowcycle;

function cancelcycle() {
    //alert("cancelling scroll");
    bContCycle = false;
}
function allowcycle() {
    //alert("allowing scroll");
    bContCycle = true;
}
