/*
 * @version 1
 * @dependicies mooTools 1.11
 */
var imageswitch = function(){

    //"private" variables:
    
    var linkgroups;
    var preload;
    var preload_ary = {};
    
    //"private" method:
    var bind_switch = function(){
        linkgroups.each(function(item, index){
            var linkstage = $ES('p#' + linkgroups[index].a + ' a');
            $each(linkstage, function(tag, index){
                tag.addEvent('click', function(event){
                    event = new Event(event);
                    var parent = $(this).getParent();
		            var parent_id = $(parent).getProperty('id');
		            var tag_href = $(this).getProperty('href');
					imageswitch.switchImages(tag_href,parent_id);
                    event.preventDefault();
                    event.stop();
                });
            });
            
        });
    };
	
	
	var getPreloadImage = function(tag_href,group_id)
	{
		var returnval = false;
		
		if(typeof(preload_ary[group_id]) !== 'undefined')
		{
		    $each(preload_ary[group_id], function(tag){
			    if($(tag).getProperty('src') === tag_href)
				{
					returnval = tag;
				}
			});
		}
		return returnval;
	};
	
	function inject_Element(element,src,image_group)
	{
		//get all the image elements in the group
		var image_tags = image_group.getChildren('img');
		
		//test if exists
		var index = 0;
		var found = image_tags.some(function(item, loopindex){
			 var thereturn =false;
			 if($(item).getProperty('src') === src)
			 {
			 	thereturn = true;
				index = loopindex;
			 }
			 return thereturn;
		});
		
		
		var mySlider;
		
		//hide visible image
		$each(image_tags, function(tag){
			 tag.setStyle('display','none');
		});
		
		if(!found)
		{
			element.injectTop(image_group.getProperty('id'));
			element.setStyle('display','block');
		}
		else
		{
			image_tags[index].setStyle('display','block');
		}
		
			
	}
    
    return {
        switchImages: function(tag_href,parent_id){
          
		  	var image_group = '';
		  
			linkgroups.each(function(item, index){
				if (linkgroups[index].a === parent_id) {
					image_group = $(linkgroups[index].img);
				}
				
			});	
			
			if(image_group === '')
			{
				throw new Exception('no Image GROUP found to swap on');
			}
			
			var img = false;
			
			//find the preload image
			if(preload)
			{
				var group_id = $(image_group).getProperty('id');
				img = getPreloadImage(tag_href,group_id);
			}
			
			if(!img)
			{
				//get the image normaly
				img = new Element('img', {src: tag_href});
			}
			
			//inject element 
			inject_Element(img,tag_href,image_group);
			
        },
        init: function(data){
            linkgroups = data.linkgroups || [];
            preload = preload || true;
            
            
            //if preload then build assets
            if (preload) {
                linkgroups.each(function(item, index){
                    var linkstage = $ES('p#' + linkgroups[index].a + ' a');
                    var href_ary = [];
                    $each(linkstage, function(tag, index){
                        href_ary.push(tag.getProperty('href'));
                    });
                    
                    new Asset.images(href_ary, {
                        onComplete: function(){
                            preload_ary[linkgroups[index].img] = [];
                            href_ary.each(function(im){
                                preload_ary[linkgroups[index].img].push(new Element('img', {
                                    src: im
                                }));
                            });
                            
                        }
                    });
                    
                    
                });
                
            }
            
            //bind the online to each link group
            bind_switch();
        
        
        
        } //end init()
    };
}(); // the parens here cause the anonymous function to execute and return

