/**
*	@name							Lazy
*	@descripton						Lazy is Jquery plugin that lazy loads Jquery plugins
*	@version						1.3.1
*	@requires						Jquery 1.2.6+
*	@author							Jan Jarfalk jan.jarfalk@unwrongest.com
*
*	@param {String} src				Path to the plugin you want to load
*	@param {String} name			Function name of the plugin
*	@param {Hash} dependencies		Hash with the keys js and css
*		@param {Array} js			Array of paths to javascript dependencies
*		@param {Array} css			Array of paths to css dependencies
*	@param {Bool} cache				Enable or disable caching
*/

(function(jQuery) {
	
	jQuery.lazy = function(options)
	{
		
		// Create local variables
		var src = options.src,
			name = options.name,
			cache = options.cache,
			self, arg, object = {};
		
		// Add plugin to the archive
		jQuery.lazy.archive[src] = {'status':'unloaded','que':[]};
		
		
		// Add a CSS file to the document
		function loadCSS(src,callback,self,name,arg){
			
			jQuery.lazy.archive[src].status = "loading";
			
			var node = document.createElement('link');
			node.type = 'text/css';
			node.rel = 'stylesheet';
			node.href = src;
			node.media = 'screen';
			document.getElementsByTagName("head")[0].appendChild(node);
			
			jQuery.lazy.archive[src].status = 'loaded';

			if(callback)
				callback(self,name,arg);
		}
		
		// Add a JS file to the document
		function loadJS(src,callback,self,name,arg){
			
			jQuery.lazy.archive[src].status = "loading";
				
			jQuery.ajax({
				type: "GET",
			  	url: src,
			  	cache: cache,
			  	dataType: "script",
			  	success: function(){
			  		jQuery.lazy.archive[src].status = 'loaded';
					if(callback) {
						callback(self,name,arg);
					}
				}
			});
			
		}
		
		// Wrapper for loadJS for the actual plugin file
		function loadPlugin(self, name, arg){
			
			function callback(){
				if(typeof self == 'object'){
					self.each(function(){
						if(arg.length > 0){
							jQuery(this)[name].apply(self,arg);
						} else {
							jQuery(this)[name]();
						}
					});
				} else {
					jQuery[name].apply(null,arg);
				}
				
				jQuery.each(jQuery.lazy.archive[src].que,function(i){
					var queItem = jQuery.lazy.archive[src].que[i];
					object[queItem.name].apply(queItem.self,queItem.arguments);
				});
				jQuery.lazy.archive[src].que = [];
			}
			
			loadJS(src,callback,self,name,arg);
		}
		
		// Proxy function
		object[name] = function () {
			
			
			
			var self = this;
			arg = arguments;
				
			if( jQuery.lazy.archive[src].status === 'loaded' ) {
			
				jQuery.each(this,function(){
					jQuery(this)[name].apply(self,arg);
				});
				
			} else if ( jQuery.lazy.archive[src].status === 'loading' ) {
				
				jQuery.lazy.archive[src].que.push({'name':name,'self':self,'arguments':arg});
										
			} else {
			
				jQuery.lazy.archive[src].status = 'loading';
				
				if ( options.dependencies ) {
					
					var css = options.dependencies.css || [],
						js = options.dependencies.js || [];
					
					var total = css.length + js.length;
					
					function loadDependencies(array, callback, callbackCallback){
									
						var length = array.length,
							src;
						
						array = array.reverse();
						
						while( length-- && total-- ){
							
							
							src = array[length];
							
							if(typeof jQuery.lazy.archive[src] == 'undefined') {
								jQuery.lazy.archive[src] = {'status':'unloaded','que':[]};
							}
							
							if(jQuery.lazy.archive[src].status === 'unloaded'){
								
								if(!total) {
								
									callback(src,function(){
										loadPlugin(self,name,arg);
									});
									
								} else {
								
									callback(src);
									
								}
								
							} else if( !total ) {
							
								loadPlugin(self,name,arg);
								
							}
						}
					}
					
					loadDependencies(css, loadCSS);
					loadDependencies(js, loadJS);
				
				} else {
					loadPlugin(self,name,arg);
					
				}
			}
				
			return this;
		};
		
		jQuery.fn.extend(object);
		jQuery.extend(object);
		
	};
	
	jQuery.lazy.archive = {};

})(jQuery);

function tm_1675initCallback(carousel) {
    jQuery('.carousel_controls a.next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('.carousel_controls a.prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function tm_1675itemFirstInCallback(carousel,slide,index,state,evt) {
    jQuery('.tm_1675linkbox li').css('display','none');
    if (index < 0) {index = Math.abs(index -2);}
    if (index < tm_1675carouselimgs.length) {
        var idx = Math.floor(index/2);
    } else {
        var idx = index;
        while (idx >= tm_1675carouselimgs.length) {
            idx = idx - (tm_1675carouselimgs.length);
        }
        idx = Math.floor(idx/2);
    }
    
    jQuery('.tm_1675linkbox li').eq(idx).css('display','block');
}

function tm_1675itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, tm_1675carouselimgs.length);
    carousel.add(i, tm_1675getItemHTML(idx - 1));
};

function tm_1675itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};


function tm_1675getItemHTML(item)
{
    var pic = tm_1675carouselimgs[item];
    return '<img src="' + pic + '" width="320" />';
};
