// Tell that HTML what's up -------------------------------------------------*/
$(function(){ 
    $('html').addClass('dom-ready').removeClass('dom-loading');
});

if ( ! TS) var TS = {};

/* Pageable Lists -----------------------------------------------------------*/
TS.pageable = {};
TS.pageable.init = function() {
	$('.pageable').each(
		function(i, el) {
			el.gallery_count = $('> ul > li', el).hide().length;

    		var initHeight = $('> ul > li', el).eq(i).height();
			initHeight = initHeight ? initHeight : 500;
			$('> ul', el).height(initHeight);
			
			// Fallback height adjustment for embedded images
			jQuery('> ul > li img:first', el).load(function(e){
				initHeight = jQuery('> ul > li', el).eq(i).height();
				initHeight = parseInt(initHeight) > 0 ? initHeight : this.height + 60;
				jQuery('> ul', el).height(initHeight);
			});
			
			
			if (el.gallery_count > 1) {
				el.cur_pos = 0;
				el.loop = false;
				
				jQuery(el).height('auto');
				
				jQuery(el).mouseout(function(){
					jQuery(this).addClass('disengaged');
				});
				jQuery(el).mouseover(function(){
					jQuery(this).removeClass('disengaged');
				});
				
				// Add controls
				if (jQuery('> .controls', el).length) {
					jQuery('> .controls', el).html('<a href="#" class="prev">prev</a> <a href="#" class="next">next</a><div class="count">1 of '+ el.gallery_count +'</div>');
              
                }
				
				// Add shortcuts
				if (jQuery('> .shortcuts', el).length) {
					for (i=0;i<el.gallery_count;i++) {
						jQuery('> .shortcuts', el).append('<a href="#">'+ i+1 +'</a> ');
					}
				}
				jQuery('.shortcuts a', el).each(
					function(index){
						jQuery(this).click(function(){
							jQuery('.shortcuts a', el).removeClass('active');
							jQuery(this).addClass('active');
							TS.pageable.show(index, el);
							this.blur();
							return false;
						});
					}
				);
				
				// Pager handlers
				jQuery('.controls .prev', el).click(function(){
					var target = el.cur_pos;
					if (target == 0) {
						target = el.gallery_count;
					}
					target--;
					TS.pageable.show(target, el);
					TS.pageable.pause(el);
					return false;
				});
				jQuery('.controls .next', el).click(function(){
					var target = el.cur_pos + 1;
					if (target == el.gallery_count) {
						target = 0;
					}
					TS.pageable.show(target, el);
					TS.pageable.pause(el);
					return false;
				});
				
				// Setup Timer
				if (jQuery('.timer', el).length) {
					el.loop = true;
				}
				
				// Hide all items but first
				TS.pageable.show(0, el);
				
			}
			else {
				jQuery('> ul > li', el).eq(0).show();
			}
		}
	);
};
//jQuery(TS.pageable.init);
TS.pageable.show = function(i, el) {
	// Update counters
	el.cur_pos = i;
	jQuery('.count', el).text(i + 1 +' of '+ el.gallery_count);
	
	// Update shortcuts
	jQuery('.shortcuts a', el).removeClass('active');
	jQuery('.shortcuts a', el).eq(i).addClass('active');
	
	// Fade one out...and the other in
	if ($.browser.msie && $.browser.version == '7.0' && jQuery(el).hasClass('maxcompat')) {
		jQuery('> ul > li:visible', el).hide();
		jQuery('> ul > li', el).eq(i).show();
	}
    else if ($.browser.msie) {
        jQuery('> ul > li:visible', el).hide();
        jQuery('> ul > li', el).eq(i).show();
    }
	else {
		jQuery('> ul > li:visible', el).fadeOut();
		jQuery('> ul > li', el).eq(i).fadeIn();
	}
	
	// Adjust height of pager to fit the content
	jQuery('> ul', el).filter(':visible').animate({height: jQuery('> ul > li', el).eq(i).height()}, 'fast');
	
	$('> .timer',el).css('display', 'block');
    $('> .controls',el).css('display', 'block');
	// Continue animation loop
	TS.pageable.setupTimer(el, i);
};
TS.pageable.setupTimer = function (el, i) {
	if (el.loop) {
		jQuery('> .timer', el).html('<div id="timer'+ i +'"><div class="progress"></div></div>');
        
        var duration = jQuery('#duration'+ i).val();
		var timer = jQuery('#timer'+ i);    
		jQuery('.progress', timer).width(5);
		jQuery('.progress', timer).animate({width: 43}, duration*1000, function() {
        
		var target = el.cur_pos + 1;
		if (target == el.gallery_count) {
			target = 0;
		}
		if (el.loop) {
			TS.pageable.show(target, el);
		}
	});
  }
};
TS.pageable.pause = function(el) {
	el.loop = false;
	jQuery('.timer .progress', el).remove();
};

