(function($) {
  $.fn.imageGallery = function() {
	
	//Main Image Holder
	imageObjects = { index: 0, parent: this };
	imageObjects.objects = new Array();
	var parent = this;
	timer = jQuery.timer( 1235000, function () {} );
	
	
	
    imageObjects.Append = function(options){
		this.objects.push( options  );	
    }

	imageObjects.GetImage = function( index ){
		var img = new Image();
	
		for( var key in this.objects[index] ){
			$(img).attr( key, this.objects[index][key] );
		}
			
		if (this.objects[index]['isvideo'] == '1'){
			img = $('<a />');
			img.append( document.createTextNode(' ') );
		
			for( var key in this.objects[index] ){
				$(img).attr( key, this.objects[index][key] );
			}
			//$(img).attr('href', $(img).attr('src'));
			$(img).addClass('video');

		}else{
			$(img).width( $(img).attr('width') );
			$(img).height( $(img).attr('height') );
			//Debug( $(img).attr("width") + " " + $(img).attr("height") );
		}

		if ($(img).attr('url').length > 0){
			$(img).click( function(){
				window.open( $(img).attr('url') );
			});
			$(img).css('cursor','pointer');
		}




		return img;
	}
	
	imageObjects.GetImageAttribute = function( index, key ){
		return $(this.objects[index]).attr(key);
	}

	imageObjects.GetImageContainer = function( index ){
		var rootElement = GetImageGalleryContainer()
		var divID = 'div' + this.GetImageAttribute(index, 'id');
		var divElement = '';

		// Check to see if the image container already exists. If it does
		// Then there is no need to create a new one.
		if ( $("#"+divID).length > 0 ){
			divElement = $("#"+divID);
		}else{
			var img = this.GetImage(index);		
		
			var navElements = '';
						
			
			divElement = $.create('div',{ id: divID },[ img ]);

			$(divElement).append('<div class="imageGalleryImgObj"><a class="imageGalleryAObjBack" href=""><img src="http://g3.worldweb.com/arrow-sm-left.png" border="0" width="25" height="25"></a> <a class="imageGalleryAObjPause" href=""><img src="http://g3.worldweb.com/arrow-sm-pause.png" border="0" width="25" height="25"></a> <a class="imageGalleryAObjNext" href=""><img src="http://g3.worldweb.com/arrow-sm-right.png" border="0" width="25" height="25"></a></div>');			
			$(divElement).hide();					
			$(rootElement).append( divElement );
			
			//window.console.log( $(divElement).html() );

			
			$(".imageGalleryAObjNext").unbind("click");		
			$(".imageGalleryAObjBack").unbind("click");		
			//alert($(".imageGalleryAObjNext").length);

		    $(".imageGalleryAObjNext").bind("click", function(){
			  	ImageSlideStop();
		  	    ImageSlide(1);			
				return false;
	 	    });
		    
			$(".imageGalleryAObjPause").bind("click", function(){
			  	ImageSlideStop();
				return false;
	 	    });


		  $(".imageGalleryAObjBack").bind("click", function(){
			  	ImageSlideStop();
	  	    	ImageSlide(-1);			
				return false;
		  });


		}
		return divElement;
	}
	
	imageObjects.GetIndex = function(){
		return this.index;
	}
	
	imageObjects.Length = function( ){
		return this.objects.length;
	}

	imageObjects.SetIndex = function( index){
		if ( this.Length() == index){
			index = 0;
		}else if ( index < 0 ){
			index = this.Length() - 1;
		}
		return this.index = index;
	}

	
  	imageObjects.toString = function(){ return "Image Gallery - Image Objects"; }

	// Initial Display of the image with formating. 
	// Append the formatted element to a div so it
	// can be easily removed.
	function ImageDisplay(){
		
		var div = imageObjects.GetImageContainer( imageObjects.GetIndex() );
		$(div).fadeIn();

		var title = imageObjects.GetImageAttribute(imageObjects.GetIndex(), 'title' );

		if ( imageObjects.GetImageAttribute(imageObjects.GetIndex(), 'url' ).length > 0 ){
			$("#imageGalleryPhotoDescription").html( '<a href="' + 
				imageObjects.GetImageAttribute(imageObjects.GetIndex(), 'url' )	
			 	+ '" target="_blank">' + title +'</a>'  );			

		}else{
			$("#imageGalleryPhotoDescription").html( title  );			
		}

		$("#imageGalleryListingURL").attr("href", imageObjects.GetImageAttribute(imageObjects.GetIndex(), 'listingurl' ) );


		if ( imageObjects.GetImageAttribute(imageObjects.GetIndex(),'isvideo') == 1 ){
			$('.video').media();
			ImageSlideStop();
		}
		if (imageObjects.Length() == 1){
			ImageSlideStop();
			$('.imageGalleryImgObj').hide();
		}
	}
	
	function ImageSlide(slideIndex){
			var currentDivID = 'div' + imageObjects.GetImageAttribute( imageObjects.GetIndex(), 'id');			

			var test = imageObjects.GetIndex();
			
			imageObjects.SetIndex( imageObjects.GetIndex() + slideIndex );
			
			$("#" + currentDivID).fadeOut('slow', function(){ 
				ImageDisplay();
			 } );
			 
	}
	
	function ImageSlideStart(){
		this.timer = jQuery.timer( 5000, function (timer) {
			ImageSlide( 1 );
		});
			
	}

	function ImageSlideStop(){
		this.timer.stop();		
	}

	function GetImageGalleryContainer(){
		return this.rootContainer;
	}


	function SetImageGalleryContainer( container ){
		this.rootContainer = container;
	}
	

	function Debug( value ){
		if (window.console){
			window.console.log( value );
		}
	}

  	function toString(){ return "Image Gallery"; }

	
    return this.each(function() {
      // apply plugin functionality to each element

	  // Check the element for each image tag and
	  // append them to the imageObject element for 
	  // display
 
	  // Create a unique name for the element id's  
	  var name = "imageObjects" + $(this).attr('id');
	  
	  var index = 0;
	  $( $(this).find("input")  ).each( function() {
	  	//Send the img element to the storage object
		var options = { src: $(this).attr('src'),
						border: $(this).attr('border'),
						width: $(this).attr('imgwidth'),
						height: $(this).attr('imgheight'),
						title: $(this).attr('title'),
						url: $(this).attr('url'),
						listingurl: $(this).attr('listingurl'),
						isvideo: $(this).attr('isvideo'),
						id: name + index		
		}
		//jQuery(this).hide('fast');

		//Add the image attribute to the storage routine.
	    imageObjects.Append(options);	  
		index++;
	  });
      
	  //Send the containter element to the Display Routine
	  var container = this;	  
	  SetImageGalleryContainer(this);
      ImageDisplay();	  
	  ImageSlideStart();
    });


	
  }


  
   
  
  


 
 

  
  
})(jQuery);
