/*
  @author: Leysam Rosario
  @description: For private project use only.
  @notes:
  	- Please dont remove the copyright
	- Please add a documentation when adding/removing/editing some script
	- enjoy!
  @copyright addscrave 2010
  
  cForceGallery version 0.4
*/
(function($){
    $.fn.extend({
        //plugin name - cForceGallery
        cForceGallery: function(options) {
			
            var defaults = {
                animateDuration: 1000,
				autoPlay: false,
				autoPlayTimer: 2000
            };
             
            var options = $.extend(defaults, options);
			
			var cForceGallery = function(target,settings){
				this.target = $(target);
				this.settings = settings;
				
				var that = this;
				var self = this;
				
				this.current = 0;//0 = first(1)
				
				this.construct = function(){
					this.target.addClass("cforcegallery_container");
					this.create_navigator(this.settings.nav);
					this.construct_images(this.target.children("div"));
					if(this.settings.autoPlay){
						this.autoPlayTimer();//start the auto play
					}
				};
				
				/*-validator function-*/
				this.isArray = function(obj) {
					return (obj.constructor.toString().indexOf("") != -1);
				}
				
				this.construct_images = function(targets){
					image = targets;
					image.each(function(i){
						obj = $(this);
						if(i!=0){
							obj.hide();
						}
						obj.css({
							position: "absolute"
						});
					});
				};
				
				this.create_navigator = function(nav){
					if(this.isArray(nav)){
						container_child = this.target.children();
						for(j=0;j<nav.targets.length;j++){
							if(nav.types[j]){
								if(nav.types[j] == "number"){
									navContainer = $(nav.targets[j]);
									navContainer.addClass("cforceNavText");
									navContainer.html("");//empty navigator content
									for(i=0;i<container_child.length;i++){	
										_nav = document.createElement('span');
										if(i==0){$(_nav).attr("class","active_item");}
										$(_nav).addClass("cforceNavItem").html(i+1).bind("click",i,function(events){
											count = events.data;
											that.navigate(count);		
										});
										navContainer.append(_nav);
										//alert($(navContainer).attr("class"));
									}
								}
								
								if(nav.types[j] == "thumb"){
									navContainer = $(nav.targets[j]);
									navContainer.addClass("cforceNavText");
									navContainer.html("");//empty navigator content
									for(i=0;i<container_child.length;i++){	
										thumb = $(container_child[i]).attr("thumb");
										_nav = new Image();
										if(i==0){$(_nav).attr("class","active_item");}
										$(_nav).addClass("cforceNavItem").attr('src',thumb).bind("click",i,function(events){
											count = events.data;
											that.navigate(count);		
										});
										navContainer.append(_nav);
										//alert($(navContainer).attr("class"));
									}
								}
								
							}else{//default
								navContainer = $(nav.targets[j]);
								navContainer.addClass("cforceNavText");
								navContainer.html("");//empty navigator content
								for(i=0;i<container_child.length;i++){	
									_nav = document.createElement('span');
									if(i==0){
										$(_nav).attr("class","active_item");
									}
									$(_nav).addClass("cforceNavItem").html(i+1).bind("click",i,function(events){
										count = events.data;
										that.navigate(count);		
									});
									navContainer.append(_nav);
									//alert($(navContainer).attr("class"));
								}
							}
						}
					}
				}
				
				this.updateNav = function(){
					nav = this.settings.nav;
					for(j=0;j<nav.targets.length;j++){
						navContainer = $(nav.targets[j]);
						if(nav.types[j]){
							if(nav.types[j] == "number"){
								targets = navContainer.children();
								targets.each(function(i){
									obj = $(this);
									if(obj.hasClass("active_item")){
										obj.removeClass("active_item");
									}
									$(targets[that.current]).addClass("active_item");
								});
							}
						}else{//default
						}
					}
				}
				
				this.autoPlayTimer = function(){
					setInterval(function(){
						that._autoPlay();//Run the auto play
					},that.settings.autoPlayTimer);
				};
				//this.next();
				this.construct();
			}
			
			cForceGallery.prototype = {
				next: function(){
					alert("next");
				},
				prev: function(){
					alert("prev");
				},				
				_autoPlay: function(){
					var duration = this.settings.animateDuration;
					images = this.target.children("div");
					id = this.current+1;
					if(this.current >= images.length-1){
						id = 0;
					}
					$(images[this.current]).css({zIndex:0});
					$(images[id]).css({zIndex:0});
					if(this.current > id){
						images.each(function(i){
							img_obj = $(this);
							if(i!=id){
								img_obj.fadeOut(duration);
							}else{
								img_obj.show();
							}
						});
					}else{
						$(images[this.current]).css({zIndex:1}).fadeOut(duration);
						$(images[id]).css({zIndex:0}).show();
					}
					this.current = id;
					this.updateNav();
				},
				
				navigate: function(id){
					if(id!=this.current){
						var duration = this.settings.animateDuration;
						if(id != this.current){
							images = this.target.children("div");
							$(images[this.current]).stop().css({zIndex:0,opacity:1});
							$(images[id]).stop().css({zIndex:0,opacity:1});
							if(this.current > id){
								images.each(function(i){
									img_obj = $(this);
									if(i!=id){
										img_obj.stop().css({opacity:1}).fadeOut(duration);
									}else{
										img_obj.show();
									}
								});
							}else{
								$(images[this.current]).stop().css({zIndex:1,opacity:1}).fadeOut(duration);
								$(images[id]).css({zIndex:0}).show();
							}
							this.current = id;
							this.updateNav();
						}
					}
				},
			};
          
		   return this.each(function() {
				new cForceGallery(this,options);
		   });
			
        }
    });
})(jQuery);
