
//this makes the main image clickable to go to the next one. 
//$(function(){  $('.indexedImagesImgWrap').click( function(){ Billboards.nextArrow(); }); }); 

var Billboards={ 
	play:true,
	playInterval:7500,
	loopPlayback:0,
	loadCheckInterval:10,
	tmr:null,
	init:function(){
		
		if(!this.wrap) this.wrap=$('.tonyBillboardsWrap');
		this.imageTitle=this.wrap.find('.indexedImage_imageTitle');
		this.imagesWrap=this.wrap.find('.indexedImages_ImagesWrap');
		this.linkURL=this.wrap.find('#billboardsLinkTargetUrl').val(); 
		
		try{
			this.wrap.find('img').bind('load', function (e) { $(this).attr('imgLoaded',1); }); 
		}catch(e){
			this.wrap.find('img').each( function () { $(this).attr('imgLoaded',1); });
		}
		
		
		this.imagesWrap.animate( { height: (parseInt($('.currentIndexedImg').attr('imageHeight'))+2) },100, "linear", function(){} );
		
		if(this.play && this.playInterval){
			clearTimeout(this.tmr); 
			this.tmr = setTimeout( 'Billboards.imgLoaded()',this.loadCheckInterval );
		}
	},
	imgLoaded:function(){ 
		var fID = parseInt(this.wrap.find('.currentNumLink').attr('FID') ); 
		if( this.wrap.find('#indexedImagesImgWrap'+fID+' img').attr('imgLoaded') ){ 
			clearTimeout(this.tmr); 
			this.tmr = setTimeout( 'Billboards.nextImage()',this.playInterval );
		}else{
			//alert('not loaded');
			clearTimeout(this.tmr); 
			this.tmr = setTimeout( 'Billboards.imgLoaded()',this.loadCheckInterval );
		}
	},
	nextImage:function(){
		if(this.autoPlayStopped) return false;
		var currentFound=0; 
		var imageClicked=0;
		var firstImage;
		this.wrap.find('.indexedImageNumLink').each( function(i){ 
			if(!firstImage) firstImage=this;
			if(currentFound){  
				Billboards.showImageFID(this,$(this).attr('FID'),false);
				currentFound=0;
				imageClicked=1;
			}else if( $(this).hasClass('currentNumLink') ) currentFound=1;
		})
		if( !imageClicked && this.loopPlayback){ 
			Billboards.showImageFID(firstImage,$(firstImage).attr('FID'),false);
			imageClicked=1;
		}
		if(imageClicked && !this.autoPlayStopped && this.playInterval) {
			clearTimeout(this.tmr); 
			this.tmr=setTimeout( 'Billboards.imgLoaded()',this.loadCheckInterval );
		}
	},
	nextArrow:function(){ 
		var nextLink, currentFound=0, firstLink;
		this.wrap.find('.indexedImageNumLink').each( function(i){ 
			var el = $(this);
			if(!firstLink) firstLink=el;
			if(currentFound && !nextLink){
				nextLink=el;
				return;	
			}
			if(el.hasClass('currentNumLink')) currentFound=1;
		}) 
		if(this.loopPlayback && !nextLink) nextLink=firstLink;  
		if(!nextLink) return;  
		
		Billboards.showImageFID(nextLink.get(0), nextLink.attr('FID'), true); 
	},
	prevArrow:function(){
		var prevLink, currentFound=0, lastLink;
		this.wrap.find('.indexedImageNumLink').each( function(i){ 
			var el = $(this); 
			if(el.hasClass('currentNumLink')) prevLink=lastLink;
			lastLink = el;
		})
		if(this.loopPlayback && !prevLink) prevLink=lastLink; 
		if(!prevLink) return; 
		
		Billboards.showImageFID(prevLink.get(0), prevLink.attr('FID'), true); 
	},
	showImageFID:function(aLink,FID, clicked){ 
	
		if(clicked){ 
			clearTimeout(this.tmr);
			this.autoPlayStopped=1;			
		}
	
		if(aLink) $(aLink.parentNode).find('.indexedImageNumLink').removeClass('currentNumLink');
		if(aLink) $(aLink).addClass('currentNumLink');
		
		$('.indexedImagesImgWrap').fadeOut(500);
		if(this.imageTitle) this.imageTitle.fadeOut( 500 );
		this.pendingImg=$('#indexedImagesImgWrap'+FID); 	
		this.pendingTitle=this.pendingImg.attr('imageName');
		this.pendingFID=FID;
		
		//track event on tony_stats traffic monitoring package
		if( clicked && typeof(ccmStatsTracker)!='undefined' && typeof(ccmStatsTracker.trackEvent)=='function')
			ccmStatsTracker.trackEvent( Billboards.clickNumberEventName+' '+this.pendingTitle);			
		
		this.imagesWrap.animate( { height: (parseInt(this.pendingImg.attr('imageHeight'))+2) },1000, "linear", function(){} );
				
		//imageName
		setTimeout( 'Billboards.delayedShow()',500);
	},
	delayedShow:function(){ 
		var linkURL=''
		var a = this.pendingImg.find('a').get(0);
		//alert(a.innerHTML); 
		if(a && a.href) linkURL=a.href;
		if(this.imageTitle){ 
			if(linkURL) this.imageTitle.html( '<a href="'+linkURL+'">'+this.pendingTitle+'</a>' );
			else this.imageTitle.html( this.pendingTitle );
			this.imageTitle.fadeIn(500);	
		}
		this.pendingImg.fadeIn(500)
	}
}
$(function(){Billboards.init()});


// MIT License
// Paul Irish | @paul_irish | www.paulirish.com
// Andree Hansson | @peolanha | www.andreehansson.se  
// function to detect when images are loaded 
(function ($) {
	$.event.special.load = {
		setup: function(data, namespaces, hollaback) {
			var retVal = false;
			if (this.tagName.toLowerCase() === 'img' && this.src !== "") {
				 
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if (this.complete || this.readyState === 4) { 
					//if(typeof(hollaback)=='undefined' || !hollaback) hollaback=function(){ };
					$(this).bind('load', data || {}, hollaback).trigger('load');
					retVal = true;
				} 
				// Check if data URI images is supported, fire 'error' event if not
				else if (this.readyState === 'uninitialized' && this.src.indexOf('data:') >= 0) {
					$(this).trigger('error');
					retVal = true;
				} 
			} 
			return retVal;
		}
	}
}(jQuery));
