/*
*		===================  JOw   ==============
*	
*			Version 		: 	0.1.2
*			Desarrollador 	: 	Williams Medina Aguirre
*			Telefono		:	(511) 325 0287
*			Mobil			:	(511) 9895 50655
*			E-mail			:	will.medin@gmail.com
*								will.medin@yahoo.com
*								william.am@live.com
*
*			Creado en		:	10-dic-2009
*           Modificado en   :	19-ene-2010, 2:24:47 AM
*                       
*			Copyright (c) 2010 JOw - Williams Medina A.
* 			Condiciones de uso  | 	GPL (http://www.opensource.org/licenses/gpl-license.php)
*			
*			Dependencias : 
*				- jQuery 1.4 +
*			
*			Compatibilidad	: 
*				- IE 6,7,8+	
*				- Firefox 2.0 , 3.0 , 3.6 + 
*				- Google Chrome 2, 3 ,4 ,5 + 
*				- Opera 9, 10 +
*				- Safari
*				
*	       ==========================================================
*
*/
(function(){
	
	var window = this, 
	
	JOw = window.JOw = function(i,m,o){ 
		return new JOw[i][m](o); 
	};	
	JOw.Util   = JOw.prototype = {
		_include : function(script){
			ext  = script.substring(script.lastIndexOf('.')+1)
			head = $('head');
			switch(ext){
				case 'js' : {head.append('<script type="text/javascript" src="'+script+'"><\/script>');	}break;
				case 'css' : {head.append('<link rel="stylesheet" type="text/css" href="'+script+'" \/>');	}break;
			};
		},
		_error :  function(){
			window.onerror = function(msg,url,l){
			var m="Error en la Pagina:\n\n"
				m+="Error: " + msg + "\n"
				m+="URL: " + url + "\n"
				m+="Linea: " + l + "\n\n"
				m+="Click Aceptar para continuar.\n"
			alert(m)
			return true;
			}
		}
		
	}
	JOw.Widget = JOw.prototype = {
		_register : function(name,widget){
			return JOw.Widget[name] = widget;
		},
		_replace : function(Template,Data){
			var replaced = "";
			for(var item =0 ; item < Data.length ; item++){
					var format = Template;
						for( var column in Data[item] ){
							format = format.replace("#["+column+"]", Data[item][column] );
						};	
					replaced += (format);
			}
			return replaced;
		},
		_init : function(object,defaults,properties){
			for(var d in defaults){
				for(var p in properties){
					if( d == p ){ defaults[d]=properties[p]}
				}
				object[d]= defaults[d];
			}return object;	
		}
	}
	
	/* --------------------- Widget Anuncios --------------------- */
	
	JOw.Widget._register("Anuncios",
	function(Options){
	/* opciones predeterminadas */
		JOw.Widget._init( this,{
						
			Element : null,
			Delay   : 5000,
			Auto    : true,
			Data    : []
			
		}, Options);
	/* variables  */
		 this.Current  = $(".current-item"			,this.Element);
		 this.List     = $(".marquee-list"			,this.Element);
		 this.Template = this.List.html();
		 var self = this;
	/* funciones */
		 this.Show 	   = function(Index){
			    this.Item.removeClass("item-hover").css({ opacity : 0.8 });
				this.Item.eq(Index).addClass("item-hover").css({ opacity : 1 });
				this.Current.html(JOw.Widget._replace( this.Temp , [this.Data[Index]] )).find(".current-image").fadeOut().fadeIn();
				this.Mask     = $(".current-mask",this.Element);
				this.Mask
				 .css({ opacity : 0 , top : 250 })
				 .animate({ opacity : 0.2 , top : 190 } ,600 , 'swing' )
				 .animate({ opacity : 0.6 , top : 195 } ,150 , 'linear');
			    this.Start(Index);
		 }
		 this.Start =function(i){
			   self.Stop();
			   self.Move = setTimeout(function(){ 
					    go = i+1 ; 
						if( (go) == self.Data.length ){ go = 0 } self.Show(go); 
						self.Start(go);
				},self.Delay);		 
		 }
		 this.Stop = function(){
			clearTimeout(this.Move);	 
		 }
		 this.Run    = function(){
				/* Datos */
				this.List.html("");
				this.List.html( JOw.Widget._replace(this.Template,this.Data));
				/* Mostrar */
				this.Temp = this.Current.html();
				this.Item = $(".marquee-list-item",this.Element);
				
				this.Item.live("click",function(){
					index = $('.marquee-list-item', $(this).parent()).index($(this));
					self.Auto = false;
					self.Show(index); 
				});	
				
				this.Show(0);
		 }
	/* Iniciar */
		this.Element.css({ 'visibility' : 'visible' });
		this.Run();
		
	});
	
	/* End Anuncios */
	
	/* ---------------------   Widget Noticias  --------------------- */
	JOw.Widget._register("Noticias",function(Options){
	/* opciones predeterminadas */	
		JOw.Widget._init( this,{
			Element   : null,
			Delay     : 3000,
			Direccion : 'Up',
			Mostrar   : 4,
			Data      : []
		}, Options);
	/* variables */
		this.List  	  = $(".noticias-list", this.Element);
		this.Template = this.List.html();
		this.mTop	  = 0;
		var self 	  = this;
	/* funciones */
		this.Go    = function(Dir){
			this.Item.css({margin : '5px auto 5px auto'});
			switch(Dir){
				case 'Up' :{
					itemtop    = $(".noticias-item:first", this.Element);
					itemnext   = $(".noticias-item:eq("+this.Mostrar+")", this.Element);
					itembottom = $(".noticias-item:last", this.Element);
					//itemnext.css({opacity : 0.1 }).animate({ opacity : 1 }, (this.Delay/3) )
					itemtop.animate({  marginTop : [ -(this.iHeight) , 'linear' ]  }, (this.Delay/3)  , 'linear' ,function(){
							itemtop.css( {  margin : '5px auto 5px auto' } );
							itembottom.after(itemtop);
					});
				}break;
				case 'Down' :{
					var itemtop  	= $(".noticias-item:first", this.Element);
					var itemnext    = $(".noticias-item:eq("+(this.Mostrar - 1)+")", this.Element);
					var itembottom  = $(".noticias-item:last" , this.Element);
					itemtop.before(itembottom);
					itembottom.css( { marginTop : -(this.iHeight) } );
					itembottom.animate( {marginTop : 5 } , 1000 );
					//itemnext.animate({ opacity : 0.1 }, (this.Delay/3) );
				}break;
			}
		}
		this.Start = function(){
			self.Stop();self.Move = setTimeout(function(){self.Go(self.Direccion);self.Start()}, self.Delay );
		}
		this.Stop = function(){
			clearTimeout(this.Move);
		}
		this.Run = function(){
			this.List.html( JOw.Widget._replace(this.Template , this.Data) );
			this.Item	  = $(".noticias-item", this.Element);
			this.iHeight  = this.Item.height();
			this.Start();
		}
	/*Iniciar*/	
		this.Element
		.css({ 'visibility' : 'visible' })
		.mouseenter(function(){ self.Stop();  })
		.mouseleave(function(){ self.Start(); });
		
		this.Run();
	})
	
	/* End Noticias */
	
	
})();
