$(function(){
	//initNavigation();
	
	$('div.gallery').fadeGallery({
		gallery : 'div.gall-hold > ul',
		prev : false,
		next : false,
		play : false,
		pause : false,
		thumbnails : 'ul.swicher',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 10000,
		duration : 700
	});
})


/*****
function initNavigation(){

	var _nav = $('#nav ul.navigation');
	
	var _decor = $('div.bg-drop');
	var _decor2 = $('div.bg-drop2');
	var _li = $('> li', _nav);
	
	_li.each(function(){
	
		var _this = $(this);
		var _drop = $('ul.drop', _this);
		var _drop2 = $('ul.drop2', _this);

	//	_this.removeClass('hover');
		
	//	if (_drop.length) _decor.hide();
	//	if (_drop2.length) _decor2.hide();
		
		_this.hover(function(){
		
			// _this.addClass('hover');
			
			// if (_drop.length) _decor.hide();
			// if (_drop2.length) _decor2.hide();
			
			// setTimeout( function() {
					
					// if (_drop.length) _decor.show();
					// if (_drop2.length) _decor2.show();
				
					// if (_drop.length) _drop.show();
					// if (_drop2.length) _drop2.show();
				
				// },500);
						
		},function(){	
			
			// if (_drop.length) _drop.show();
			// if (_drop2.length) _drop2.show();
			
			// setTimeout( function() {			
					
					
					// if (_drop.length) _drop.hide();
					// if (_drop2.length) _drop2.hide();
					
					// if (_drop.length) _decor.hide();
					// if (_drop2.length) _decor2.hide();
				
					// _this.removeClass('hover');
					
				// },500);
			
			//clearTimeout();
		});

	});
}
**/
jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(options.gallery, holder),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('li'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && $.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		// thumbnails
		thumbs.each(function(i){
			$(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}
/*
Autor: Nahuel José
http://www.nahueljose.com.ar
nahuel(at)nahueljose(dot)com(dot)ar
*/
$.fn.incrustar = function(opciones){
        //valores por defecto
        var defaults = {  
           izquierda : 0,
           arriba : 0,
           efecto : 'desvanecer',
           velocidad: 'fast'
          };          
        var opciones = $.extend(defaults, opciones); 
        var objEtiqConten = $(this);
        var objEtiqueta = objEtiqConten.children('label');
        var objInput = objEtiqConten.children('input');
        
        //aplicar estilos basicos        
        objEtiqConten.css({
            position: 'relative'
        });
        objEtiqueta.css({
            position: 'absolute',
            left: opciones.izquierda,
            top: opciones.arriba    
        });
         return this.each(function() {
        //eventos focusin, focusout
        objInput.focusin(function(){
            var vacio = $(this).val() == '';
            switch(opciones.efecto){
                    case 'desvanecer':
                    if(vacio){
                            $(this).siblings(objEtiqueta).fadeOut(opciones.velocidad);                        
                        }
                    break;    
                    
                    case 'deslizarVert':
                    if(vacio){
                            $(this).siblings(objEtiqueta).slideUp(opciones.velocidad);                        
                        }
                    break;    
                    
                    case 'deslizarHor':
                    if(vacio){
                            var currEtiqueta = $(this).siblings(objEtiqueta);
                            var anchoCurrEtiqueta = currEtiqueta.width();
                            currEtiqueta.animate({
                                left: -1*(anchoCurrEtiqueta + opciones.izquierda)
                            }, opciones.velocidad);                        
                        }
                    break;
                    
                    default: break;        
                }
        });
        objInput.focusout(function(){
            var vacio = $(this).val() == '';
            switch(opciones.efecto){
                    case 'desvanecer':
                    if(vacio){
                            $(this).siblings(objEtiqueta).fadeIn(opciones.velocidad);                        
                        }
                    break;        
                    
                    case 'deslizarVert':
                    if(vacio){
                            $(this).siblings(objEtiqueta).slideDown(opciones.velocidad);                        
                        }
                    break;    
                    
                    case 'deslizarHor':
                    if(vacio){
                            var currEtiqueta = $(this).siblings(objEtiqueta);
                            var anchoCurrEtiqueta = currEtiqueta.width();
                            currEtiqueta.animate({
                                left: opciones.izquierda    
                            }, opciones.velocidad);                        
                        }
                    break;    
                    default: break;            
                }
        });
        return false;
        });
    };

var test = new Array(0,0,0,0,0,0,0,0,0,0,0);

function activeEncart(object)
{
	object.className += ' hover';
}

function desactiveEncart(object)
{
	object.className = object.className.replace('hover',"");
}

function loadMenu(menu)
{
	if(test[menu]==1)
	{
		if(menu!='')
		{		
			if(document.getElementById("link"+menu))
			{
				if(document.getElementById("Sub"+menu+"link")) 
				{
					document.getElementById('bg-drop').style.display="block";	
					document.getElementById("Sub"+menu+"link").style.display="block";
				}
				else
				{
					document.getElementById('bg-drop').style.display="none";
				}
		
				if(document.getElementById("Sub"+menu+"link-2"))
				{
					document.getElementById('bg-drop2').style.display="block";	
					document.getElementById("Sub"+menu+"link-2").style.display="block";
				}
				else
				{
					document.getElementById('bg-drop2').style.display="none";
				}
				clearTimeout();
			}
		}
	}
}

function unloadMenu(menu)
{
	if(test[menu]==0)
	{
		if(menu!='')
		{		
			if(document.getElementById("link"+menu))
			{							
				if(document.getElementById("Sub"+menu+"link-2")) 
				{
					document.getElementById("Sub"+menu+"link-2").style.display="none";
					document.getElementById('bg-drop2').style.display="none";
					
				}	
		
				if(document.getElementById("Sub"+menu+"link")) 
				{
					document.getElementById("Sub"+menu+"link").style.display="none";
					document.getElementById('bg-drop').style.display="none";
				}
				clearTimeout();
				desactiveEncart(document.getElementById("link"+menu));
			}
		}
	}
}

function Allunactive(menu)
{
	//clearTimeout();
	
	for( var i = 1 ; i < 10 ; i++ )
	{
		if( i != parseInt(menu) )
		{
			if(document.getElementById("link"+i))
			{
				test[i]= 0;
				if(document.getElementById("Sub"+i+"link")) unloadMenu(i);
				desactiveEncart(document.getElementById("link"+i));
			}
		}
	}
	
	
	if( test[menu] == 1 )
	{
		if(document.getElementById("Sub"+menu+"link")) 
		{
			document.getElementById('bg-drop').style.display="block";
		}
		else
		{	
			document.getElementById('bg-drop').style.display="none";
		}
		
		if(document.getElementById("Sub"+menu+"link-2")) 
		{
			document.getElementById('bg-drop2').style.display="block";				
		}
		else
		{
			document.getElementById('bg-drop2').style.display="none";
		}
	}

	
	
}

function menuActive(menu)
{					
	//clearTimeout();
	
	if(menu!='')
	{
		setTimeout(function(){
		
			test[menu] = 1;
			
			Allunactive(menu);
			
			if(document.getElementById("Sub"+menu+"link"))
			{			
				loadMenu(menu);
			}
			else
			{	
				document.getElementById('bg-drop').style.display="none";
				document.getElementById('bg-drop2').style.display="none";
			}
			
			activeEncart(document.getElementById("link"+menu));
			
		},500);
	}
}

function menuUnactive(menu)
{	
	//clearTimeout();
	
	if(menu!='')
	{		
		setTimeout( function(){
		
			if(document.getElementById("Sub"+menu+"link"))
			{
				Allunactive(menu);
				test[menu] = 0;
				unloadMenu(menu);
			}
			else
			{	
				document.getElementById('bg-drop').style.display="none";
				document.getElementById('bg-drop2').style.display="none";
			}
			
			desactiveEncart(document.getElementById("link"+menu));
			
		},500);	
	}
}	

function subMenuActive(menu)
{	
	if(menu!='')
	{		
		if(document.getElementById("Sub"+menu+"link"))
		{
			test[menu] = 1;
			Allunactive(menu);
			loadMenu(menu);
		}
		else
		{	
			document.getElementById('bg-drop').style.display="none";
			document.getElementById('bg-drop2').style.display="none";
		}
	}
}

function subMenuUnActive(menu)
{	
	if(menu!='')
	{		
		if(document.getElementById("Sub"+menu+"link"))
		{
			Allunactive(menu);
			test[menu] = 0;
			unloadMenu(menu);
		}
		else
		{	
			document.getElementById('bg-drop').style.display="none";
			document.getElementById('bg-drop2').style.display="none";
		}
	}
}

window.onLoad = Allunactive ;

