var imenu = false;
var iprod = false;

$(document).ready(function(){

			fix_firebug();
			init_menu();
			prods();
			init_search();
			show_search();
			//tooltips();
			//home_images();


});

/**
* Menu initializer
*/
function init_menu(){

	//Top Menu
	$('#nav ul li.parent').hover(function(){

		var submenu = $(this).css('background-color','#D4D3CE').css('color','#F29627').find('ul');
		//var submenu = $(this).addClass('.menu_active').find('ul');
		submenu.show();


	},function(){

     	var submenu = $(this).css('background-color','#000').css('color','#FFF').find('ul');
     	//var submenu = $(this).removeClass('.menu_active').find('ul');
		submenu.hide();

   });
	
	
	imenu = true;
}

function prods(){
	
	if (iprod) return false;

	/*$('ul#show_big li img').each(function(){
   
		var imgsrc = this.src;
		
		//$(this).attr('rel',imgsrc).removeAttr('src');	
		
	});*/
	
	var imgob = $('ul#show_big li:first').show().find('img');
	//console.log('El objeto imagen es: ',imgob);
	
	//Reemplazando el path por el que deber�a de ser
	var imgsrc = $(imgob).attr('rel');	
	//$(imgob).attr('src',imgsrc);
	
	var prev_item = '';

	$('#thumbs a').click(function(){

		$('#link_'+prev_item).parent().css('background-color','').end();
		var id_detail = $(this).parent().css('background-color','#FF9933').end().attr('rel');

		$('ul#show_big li:visible').hide();

		//console.log(id_detail);
		var imgob = $('#'+id_detail).show().find('img');
		//$(imgob).attr('src',$(imgob).attr('rel'));

		prev_item = id_detail;

	return false;

	});


	var total = $('ul#show_big li').size();
	var current = 1;
	var anterior = $('.prev_link').hide();
	var siguiente = $('.next_link');

	$('.prev_link').click(function(){

		if ( (current - 1) == 1 ){
			anterior.hide();
			console.warn('No mas imagenes atras!!');
		}
		console.log('%d < anterior',--current);
		siguiente.show();

		$('ul#show_big li:visible').hide().prev().show();//.find('img').attr('src',$(this).attr('rel'));

		return false;
	});

	$('.next_link').click(function(){


		if ( (current + 1) == total ){
			siguiente.hide();
			console.warn('No mas imagenes adelante!');
		}

		console.log('siguiente > %s',++current);
		anterior.show();
		
		$('ul#show_big li:visible').hide().next().show();//.find('img').attr('src',$(this).attr('rel'));
		return false;

	});
	
	
	/*$().ajaxStart(function(){
		   
		   $.growl('Loading', 'Wait while the information is loading....<br><img src="images/loading.gif" />', 'images/info.png', '...');
		   
	});*/
	
	$('#loading').ajaxStart(function(){
		   
		   $(this).css("display","block");
		   
	});
	
	$('#loading').ajaxSuccess(function(){
		   
		   $(this).css("display","none");
		   
	});
	
	$('#cat_thumbs a').click(function(){
		
		$('#cat_show_big').load('index.php?option=com_nvcatalog&view=nvcatviews&format=raw&id='+$(this).attr('rel'));
		
		return false;						   
	});

	iprod = true;

}


function home_images(){

	/*$('#home_flash_container').parent().css('position','absolute');
	console.log('Haciendo absoluto...');*/

}


/**
* Avoid js errors when firebug it's not installed
*/
function fix_firebug(){

    if (!("console" in window) || !("firebug" in console))
    {
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
	"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

	window.console = {};
	for (var i = 0; i < names.length; ++i)
	    window.console[names[i]] = function() {}
    }

}

/**
* Activate tooltips
**/
function tooltips(){
	
	console.log('iniciando tooltips');

	/*$('.img_show').Tooltip({
	    track: true,
	    delay: 0,
	    showURL: false,
	    showBody: " - ",
	    extraClass: "",
	    fixPNG: true,
	    opacity: 0.95,
	    left: -120
	});*/
	$('.img_show').Tooltip({ 
    delay: 0 
	});
}

function show_search(){

	//$(".msg_body").hide();
	//toggle the componenet with class msg_body
	$(".toggle_search").click(function(){
		$(this).next(".search_body").slideToggle(600);
	});
	
	//Loading clases_menu
	$('#contain_clases').load('index.php?option=com_nvcatalog&format=raw&type=clases');
	
}

function init_search(){
	
	$('.search_select').change(function(){
	
		//Si vale 0, es que no es opcion valida, asi que bye
		if ($(this).val() == 0){
		
			return false;
		
		}
		
		/*var select = $(this).parent().parent().siblings().find('.search_select');
		console.log(select);
		*/
		
		$(this).parent().parent().nextAll().find('.search_select').each(function(){
		
			$(this).empty();
		
		});
		
		var types = '';
		
		$(this).parent().parent().prevAll().andSelf().find('.search_select').each(function(){
		
			types += $(this).attr('id')+'_'+$(this).val()+'-';
		
		});
		
		//console.log(types);
		
		var get_select = $(this).parent().parent().next().find('.search_select');	
		
		var search_url = 'index.php?option=com_nvcatalog&format=raw&type='+types+$(get_select).attr('id');
		var thumbs_url = search_url+'&views=thumbs';
		
		//console.log(search_url);
		//console.log(thumbs_url);
		
		if ($(get_select).length > 0){
		
			$(this).unbind('change');
			console.log('Unbinding!');
		
		}
		
		$(get_select).parent().load(search_url);
		$('#cat_thumbs').load(thumbs_url);
					
	});

}


