$.fn.gmFade = function(options) {
    var defaults = {
        imageWidth: 960,
        imageHeight: 350,
        cycle:true,
        controls: true
    }
        
    options = $.extend(defaults, options);  
    return this.each(function() {
    	$(this).css('display','block'); 

		var Obj = $(this)
		var FuncObj = this;
		
		uid = giveUnusedID();
		
		this.id = uid;
		this.JQ = Obj

    	lis = $(this).children('li');
    	Obj.num = lis.length;
    	
    	lis.wrapAll('<div class="gm-slider-container"></div>');
    	$(this).find('.gm-slider-container').append('<div class="canvas"></div>');
    	lis.wrapAll('<div class="wrapper"></div>');
    	lis.wrapAll('<div class="slide-box"></div>');
   
   		imgs = lis.find('img,iframe');
   		
   		lis.addClass('item');
   		$(lis[0]).show();
   		
   		Obj.slideBox = $(this).find('.slide-box');
   		   		
   		if($(lis[0]).children('a').length > 0){
   			Obj.css('cursor','pointer');
   			Obj.click(function(){
   				window.location.href = $(lis[0]).children('a')[0].href;
   			});
   		}
   		
   		if(Obj.num > 1){
   		
   		Obj.find('.gm-slider-container').append("<a href='javascript:void(0)' class='arw2next'></a>");
   		Obj.find('.gm-slider-container').append("<a href='javascript:void(0)' class='arw2prev'></a>");	
   		
   		}
  

		$(window).keydown((function(Obj,FuncObj,e){return function(e){
   			if(e.keyCode == 37){
   			e.stopImmediatePropagation()
   			FuncObj.slide2prev(Obj);
   			window.clearInterval(Obj.interval);
   			}
   			if(e.keyCode == 39){
   			e.stopImmediatePropagation()
   			FuncObj.slide2next(Obj);
   			window.clearInterval(Obj.interval);
   			}
   		}})(Obj,FuncObj))

   		Obj.find('.arw2next').click((function(Obj,FuncObj,e){return function(e){
   			e.stopImmediatePropagation()
   			FuncObj.slide2next(Obj);
   			window.clearInterval(Obj.interval);
   		
   		}})(Obj,FuncObj))
   		
   		Obj.find('.arw2prev').click((function(Obj,FuncObj,e){return function(e){
   			e.stopImmediatePropagation()
   			FuncObj.slide2prev(Obj);
   			window.clearInterval(Obj.interval);
   		
   		}})(Obj,FuncObj))
   		
   		Obj.total = 0;
   		
   		
   		
   		
   		$(lis[0]).find('img.preload').each(function(){
   			this.setAttribute('src',this.getAttribute('newsrc'));
   			$(this).removeClass('preload');
   		})
   		
   		$(lis[0]).next().find('img.preload').each(function(){
   			this.setAttribute('src',this.getAttribute('newsrc'));
   			$(this).removeClass('preload');
   		})
   		
   		Obj[0].slide2next = function(Obj){
   			
   			
   			
   			   			

   			if((Obj.slideBox.find('li').length-1) > Obj.total ){
   			
   			$(Obj.slideBox.find('li')[Obj.total+1]).find('img.preload').each(function(){
   				this.setAttribute('src',this.getAttribute('newsrc'));
   				$(this).removeClass('preload');
   			})   			
   			
   			if((Obj.slideBox.find('li').length) > Obj.total ){
   			
   			$(Obj.slideBox.find('li')[Obj.total+2]).find('img.preload').each(function(){
   				this.setAttribute('src',this.getAttribute('newsrc'));
   				$(this).removeClass('preload');
   			}) 
   			
   			
   			$(Obj.slideBox.find('li')[Obj.total]).fadeTo(600,0); 
   			$(Obj.slideBox.find('li')[Obj.total+1]).fadeTo(600,1); 
   			
   			  
   			
   			}
   			
   			Obj.total++;
   			
   			}else{
   			$(Obj.slideBox.find('li')[Obj.total]).fadeTo(600,0); 
   			$(Obj.slideBox.find('li')[0]).fadeTo(600,1);
   			Obj.total = 0;
   			
   			}
   		
   		
    		}
   		
   		Obj[0].slide2prev = function(Obj){  	
   		
   			if(Obj.total  > 0 ){
   			
   			$(Obj.slideBox.find('li')[Obj.total-1]).find('img.preload').each(function(){
   				this.setAttribute('src',this.getAttribute('newsrc'));
   				$(this).removeClass('preload');
   			}) 
   			
   			$(Obj.slideBox.find('li')[Obj.total]).fadeTo(600,0); 
   			$(Obj.slideBox.find('li')[Obj.total-1]).fadeTo(600,1); 
   			Obj.total--;
   			
   			}else{
   			
   			$(Obj.slideBox.find('li')[Obj.slideBox.find('li').length-1]).find('img.preload').each(function(){
   				this.setAttribute('src',this.getAttribute('newsrc'));
   				$(this).removeClass('preload');
   			}) 
   			
   			$(Obj.slideBox.find('li')[Obj.total]).fadeTo(600,0); 
   			$(Obj.slideBox.find('li')[Obj.slideBox.find('li').length-1]).fadeTo(600,1);
   			Obj.total = Obj.slideBox.find('li').length-1;
   			
   			
   			
   			
   			
   			}
   		
   		}
   		
    	    	
    	if(Obj.find('ul').length > 0){
    		Obj.find('ul').gmSlide();
    		Obj.find('ul').find('.arw2next').hide();
    		Obj.find('ul').find('.arw2prev').hide();
    	};
    });
};

 function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toStringe();
 }
 
 
function giveUnusedID(){

   var chars = '0123456789abcdef'.split('');

   var uuid = [], rnd = Math.random, r;
   uuid[8] = uuid[13] = uuid[18] = uuid[23] = '';
   uuid[14] = '4'; 

   for (var i = 0; i < 30; i++)
   {
      if (!uuid[i])
      {
         r = 0 | rnd()*16;

         uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
      }
   }

   return "lo" + uuid.join('');
}
