function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
  if (restore) selObj.selectedIndex=0;
}
// Javascript General

//JQuery Object
jQuery(document).ready(function() {
	
	//hide and show area de login
	$('#login_section').hide();
		$("#login_link").click(function(){
			$('#login_section').slideToggle('slow');
			return false;
		});
	
	//auto submit del formulairo de paypal
	if($('#paypal_form').length){
		$('#paypal_form').submit();
	}

	
	//boton de loading
	$('#send_bt').click(function(){
		var boton = $(this);
		var celda = boton.parent('td');
		boton.hide();
		$('<span><img src="imagenes/ajax-loader.gif"></span>').appendTo(celda);
		//$.jGrowl("Please Wait!",{sticky: true});
		//return false;
	})
	
	
	//hacer que los campos de billing concuerden con los de shipping
	function mismoShipp(){
		var billingBox = $('#billing_info');
		var shippingBox = $('#shipping_info');
		if($('#eC_SameAs').is(':checked')){
			billingBox.find('input').each(function(){
				var input = $(this);
				var inputID = input.attr('id');
				var inputShipp = '#shipping_'+inputID;
				//console.log(inputShipp);
				$(inputShipp).val(input.val());
			});
			billingBox.find('select').each(function(){
				var selectAct = $(this);
				//console.log(selectAct);
				var selectActID = selectAct.attr('id');
				var selectActShipp = '#shipping_'+selectActID;
				//console.log(selectActShipp);
				$(selectActShipp).val(selectAct.val());
			});
		}
		/*else {
			shippingBox.find('input').val('');
			shippingBox.find('select').val('');
		}*/
	}
	
	$('#eC_SameAs, #billing_info input, #billing_info select').change(function(){
		mismoShipp();
	})
	
	if($(".scrollable").length > 0){
		// initialize scrollable together with the navigator plugin
		$("#view1 .scrollable").scrollable({size: 1, clickable: false}).circular().navigator().autoscroll({ 
				interval: 8000         
			});	
		$("#view5 .scrollable").scrollable({size: 5, clickable: false, speed: 600}).navigator();
		
		//scrollable paralos productos asociados
		$("#product_asoc_div").scrollable({size: 1, clickable: false});
				
		
		//toggle 'enlaces' box
		$("#enlaces h1 img").toggle(function(){
			$('#enlaces .flexbox_height').slideToggle('slow');
			setTimeout("$('#enlaces h1 img').attr('src', 'images/toggle_open.png');", 550);
		}, function() {
			$('#enlaces .flexbox_height').slideToggle('slow');
			setTimeout("$('#enlaces h1 img').attr('src', 'images/toggle_close.png');", 550);
		});
	}
	
	
	//toggle 'newsletter' box
	$("#newsletter h1 img").toggle(function(){
		$('#newsletter .flexbox_height').slideToggle('slow');
		setTimeout("$('#newsletter h1 img').attr('src', 'images/toggle_open.png');", 550);
	}, function() {
		$('#newsletter .flexbox_height').slideToggle('slow');
		setTimeout("$('#newsletter h1 img').attr('src', 'images/toggle_close.png');", 550);
	});
	
	/*
	//Convert lists to treeview
	if($(".filetree").length){
		$(".filetree").treeview({
			animated: "fast",
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	*/
	//Functions for opening and closing flexbox panels
	function panelClose(panel, widget){			
		panel.find('.flexbox_width').animate({width: '65px'}, 'medium');
		panel.find('.flexbox_width h1').find('span').hide();
		panel.find('.flexbox_height').children().slideUp('medium');
		//console.log(panel.find('.flexbox_height').children());
		widget.find('.flexbox_width').animate({width: '875px'}, 'medium');
	};
	
	function panelOpen(panel, widget){
		widget.find('.flexbox_width').animate({width: '680px'}, 'medium');
		panel.find('.flexbox_width').animate({width: '260px'}, 'medium');
		panel.find('.flexbox_width h1').children().show();
		panel.find('.flexbox_height').children().slideDown('medium');
	};
	
	
	//Toggle "categorias" open/close
	$("#categorias h1 a").click(function() {
		if ($(this).hasClass('open')) {
			panelClose($("#categorias"), $("#browse"));
			$(this).removeClass('open').addClass('close');
			//results specific
			if($('#browse .product .cover_holder').length < 1){
				$('.cover_holder img').animate({width: "135px"}, 'medium' );
			}
			$('.info_holder').animate({paddingLeft: "155px"}, 'medium' );
			$('.small_desc').slideDown('medium');
		}
		else {
			panelOpen($("#categorias"), $("#browse"));
			$(this).removeClass('close').addClass('open');
			//results specific
			$('.info_holder').animate({paddingLeft: "115px"}, 'medium' );
			$('.small_desc').slideUp('medium');
			if($('#browse .product .cover_holder').length < 1){
				$('.cover_holder img').animate({width: "90"}, 'medium' );
			}
		}
	});
	
	
	//Toggle flexboxes open/close
	$(".widget h1 .toggle").click(function() {
		if ($(this).hasClass('open')) {
			$(this).closest('.flexbox_width').find('.flexbox_height').slideToggle('slow');
			$(this).removeClass('open').addClass('close');
		}
		else {
			$(this).closest('.flexbox_width').find('.flexbox_height').slideToggle('slow');
			$(this).removeClass('close').addClass('open')
		}
	});
	
	
	//////////////Tabs de productos
	if($("ul.tabs").length > 0){
		$("ul.tabs").tabs();
	}
	
	//////////////menu dependientes
	
	function makeSublist(parent,child,isSubselectOptional,childVal,sufijClass){
		$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
		$('#'+parent+child).html($("#"+child+" option"));
		
		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child + sufijClass + parentValue).clone());
		
		//childVal = (typeof childVal == "undefined")? "" : childVal ;
		//$("#"+child+' [value="'+ childVal +'"]').attr('selected','selected');
		
		$('#'+parent).change( 
			function()
			{
				var parentValue = $('#'+parent).attr('value');
				var contenidoHijo = $("#"+parent+child + sufijClass + parentValue);
				var html_cont = contenidoHijo.html();
				
				$('#'+child).html(contenidoHijo.clone());
				
				//if(isSubselectOptional) $('#'+child).prepend('<option value="todos" selected="selected"> '+ childVal +' </option>');
				//console.log(html_cont);
				
				if(isSubselectOptional && html_cont == null) {
					
					$('#'+child).prepend('<option value="todos" selected="selected"> '+ childVal +' </option>');
					$('#shipping_estado_sl').html('<option value="todos" selected="selected"> '+ childVal +' </option>');
				}
				
				if($('#eC_SameAs').is(':checked')){
					$('#shipping_estado_sl').html(contenidoHijo.clone());
				}
				
				$('#'+child).trigger("change");
				$('#'+child).focus();
			}
		);
	}
	if($('#paises_sl').length){
		makeSublist('paises_sl','estado_sl', true, 'Todos', " .sub_");
		makeSublist('shipping_paises_sl','shipping_estado_sl', true, 'Todos', " .sub2_");
	}
	
	
	if($('#eC_SameAs').is(':checked')){
		$('#shipp_info_tr').hide();
	}
	else {
		$('#shipp_info_tr').show();
	}
	
	$('#eC_SameAs').click(function(){
		if($('#eC_SameAs').is(':checked')){
			$('#shipp_info_tr').fadeOut();
		}
		else {
			$('#shipp_info_tr').fadeIn();
		}
	})
	
	//////////////////////////// autocomplete
	
	function split_j(val) {
		return val.split(/,\s*/);
	}
	function extractLast(term) {
		return split_j(term).pop();
	}
	if($(".autocomplete").length > 0){
		$(".autocomplete", "#form_add_adm").autocomplete({
			source: function(request, response) {
				var tipoEsp = $('#select_type').val();
				var valAutoC = "include/auto_compleate.inc.php?tipo=" + tipoEsp;
				
				$.getJSON(valAutoC, {
					term: extractLast(request.term)
				}, response);
			},
			search: function() {
				// custom minLength
				var term = extractLast(this.value);
				/*if (term.length < 2) {
					return false;
				}*/
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function(event, ui) {
				this.value = ui.item.value;
				$(this).parents('form').trigger('submit');
				return false;
			}
		});
	}
	
	
	/////////////////////// Contacto
	if($('#contact_mapa').length){
		$('#contact_mapa h3').tooltip({ effect: 'slide', relative:	true, offset: [-10, 0], delay: 15});
	}
	
});
