var js_var = {
	tabs_home : function(e){
		var $target = $(e.hash);
		$('a.tab').parent('li').removeClass("active");
		$(e).parent('li').toggleClass("active");
		if(navigator.userAgent.match(/MSIE 6/) || !$.id('#home')){
			$('.box_st').hide();
			$($target).show();
		} else {
			$('.box_st:visible').fadeOut('normal',function(){
				$($target).slideDown('normal');
			});
		}
	},
	show_restaurants : function(e){
		var $target = e.hash.split('_');
		var type = $target[0].replace('#','');
		var number = $.id("#home") ? 4 : 3 ;	
	
		$.get('/index.php',{ cats_type: type, cat_id: $target[1], number: number }, function(data){
			if(navigator.userAgent.match(/MSIE 6/) || !$.id('#home')){
				//if ( !$.id('#change-selection') ) $('#share').append('<li id="change-selection"><a href="#" class="back-type">Cambiar Selección</a></li>');
				
				$("#sub_content").html('');	
				$(".box_st").hide();
				$("#sub_content").html(data);	
				$("#sub_content").show("slow");	
				
				/*$('ul.tabs-side li a[href="#speciality"]').click();
				$('#back-'+type).show();
				$('a[href*="all-restaurants"]', '.nav-restaurants-map').click(function(){
					$(this).parents('p').next().toggle();
					$(this).toggleClass('colapse').toggleText("Ver listado completo de restaurantes","Ocultar listado completo de restaurantes");
					return false;
				});
				$('a[href*="show-info"]').click(function(){
					var id_post = this.hash.split('_');
					GeoMashup.clickMarker(id_post[1]);
				});
				$('.back-type').click(function(){
					if ($.id('#gm-map-1')){ 
						$('iframe').remove();
						$('.content_box',$target[0]).slideDown('normal');
					} else {
						$('#speciality_flow').show('normal');
						$('.restaurants').hide();
						//$('.restaurants').remove();
						//$('.content_box',$target[0]).slideDown('normal');
					$('#share').append('<li id="change-selection"><a href="#" class="back-type">Cambiar Selección</a></li>');
						
						//alert($target[0]);
						//$('.content_box',$target[0]).slideDown('normal');
					}
					if ( $.id('#page') ){
						$(this).remove();
					} else {
						$(this).hide();
					}
					return false;
				});*/
			} else {
				
				$("#sub_content").html('');	
				$(".box_st").hide();
				$("#sub_content").html(data);	
				$("#sub_content").show("slow");	
				
				/*
				$('.content_box',$target[0]).slideUp('normal', function(){
					$($target[0]).append(data);
					if ( !$.id('#change-selection') ) $('#share').append('<li id="change-selection"><a href="#" class="back-type">Cambiar Selección</a></li>');
					$('#back-'+type).slideDown('normal');
					$('a[href*="all-restaurants"]', '.nav-restaurants-map').click(function(){
						$(this).parents('p').next().toggle();
						$(this).toggleClass('colapse').toggleText("Ver listado completo de restaurantes","Ocultar listado completo de restaurantes");
						return false;
					});
					$('a[href*="show-info"]').click(function(){
						var id_post = this.hash.split('_');
						GeoMashup.clickMarker(id_post[1]);
					});
					$('.back-type').click(function(){
						if ($.id('#gm-map-1')){ 
							$('iframe').slideUp('normal', function(){
								$('iframe').remove();
								$('.content_box',$target[0]).slideDown('normal');
							})
						}
						else { 
							$('.restaurants').slideUp('normal', function(){
								$('.restaurants').remove();
								$('.content_box',$target[0]).slideDown('normal');
							});
						}
						$(this).hide();
						return false;
					});
				});*/
			}			
		});
	}
}

jQuery.fn.focus_search = function(settings) {
	settings = jQuery.extend({
		value_default : 'Ingresa tu búsqueda'
	},settings);
	// Ingresamos en valor que indica la accion
	jQuery(this).attr('value',settings.value_default);
	// Guardamos  el valor en una variable
	var search_value = jQuery(this).val();
	jQuery(this).click(function() {
		jQuery(this).select();
		if ( jQuery(this).val() == settings.value_default ) jQuery(this).val('');
	}).focus(function() {
		jQuery(this).select();
		if ( jQuery(this).val() == settings.value_default ) jQuery(this).val('');
	});
}

// Tooltip
jQuery.fn.tooltip = function(settings) {
	settings = jQuery.extend({
		attr : 'title'
	},settings);
	var s = settings;
	jQuery(this).mousemove(function(e){
		var mouseX = e.pageX;
		var mouseY = e.pageY;
		var text = '<p title="'+jQuery(this).attr(s.attr)+'">'+jQuery(this).attr(s.attr)+'</p><span class="indi"></span>'
		jQuery(this).attr(s.attr,'');
		if(!document.getElementById("toolTip")){
			toolTip = document.createElement('div');
			jQuery(toolTip).hide().attr('id','toolTip').html(text);
			document.getElementsByTagName('body')[0].appendChild(toolTip);
		} else {
			var toolTipHeight = jQuery(toolTip).height();
		}
		if(parseInt(document.documentElement.clientWidth) < parseInt(toolTip.offsetWidth + mouseX)){
			if(navigator.userAgent.match(/MSIE/)) jQuery(toolTip).css({ 'top' : (mouseY-toolTipHeight-40)+'px'});
			else jQuery(toolTip).css({ 'top' : (mouseY-toolTipHeight-30)+'px'});
			jQuery(toolTip).css({ left : (mouseX-(toolTip.offsetWidth-20))+'px'}).addClass('alt-position');
		} else {
			jQuery(toolTip).css({ 'top' : (mouseY-toolTipHeight-33)+'px' , 'left' : (mouseX-29)+'px' }).removeClass('alt-position');
		}
		jQuery(toolTip).css({ 'position' : 'absolute', 'z-index' : 1000 }).show();
	});
	jQuery(this).mouseout(function(){
		jQuery(this).attr(s.attr,jQuery('#toolTip p').attr('title'));
		jQuery('#toolTip').remove();
	});
}

$(document).ready(function(){
	// hide
	//$('.hide').hide();

	// focus input search
	$('#s').focus_search();

	// tooltip
	if($('.table')) $('.table td[title]').tooltip();

	// colapsables
	$('.tab').click(function(e){ js_var.tabs_home(this); return false; });
	if ( $.id('#tags') ){
		tagLinks = $('a', '#tags .content_box');
		tagLinksLength = tagLinks.length;
		for ( i = 0; i < tagLinksLength; i++ ) {
			tagHrefParts = $(tagLinks[i]).attr('href').split('/');
			tagName = tagHrefParts[tagHrefParts.length - 2];
			$(tagLinks[i]).attr('href', '#tags_'+tagName);
		}
	}
	$('a[href*="location_"], a[href*="speciality_"], a[href*="tags_"], a[href*="zone_"]').click(function(e){ js_var.show_restaurants(this); return false; });	

	// Print
	$('.print').click(function(){ print(); });

	// Target
	$('.out, .pdf').attr('target','_blank');

	// Back history
	$('.previous-link').attr('href','javascript:history.go(-1)');

	// Styles
	$('dd:last','#finance').css({ borderRight: 'none' });
	$('ul li:last a','.menu_sidebar').css({ backgroundImage : 'none' });
	if(!navigator.userAgent.match(/MSIE 6/)){
		$('.widget:last','#sub_content').css({ marginBottom : '.2em' });
		$('.widget_banners:last').css({ marginBottom : '0' });
		$('.over').mouseover(function(){ $(this).css('opacity','.85'); }).mouseout(function(){ $(this).css('opacity','1'); });
	}

    if($.id('#home')){
		$('.specialities', '#speciality').jcarousel({
			visible : 5,
			buttonNextHTML: '<a></a>',
			buttonPrevHTML: '<a></a>'
		});
	}
	if($.id('#slider')){
		$('#slider').easySlider({
			orientation: 'horizontal'
		});
	}
	if($.id('#share')){
		$('#share').append('<li><a id="print" href="javascript:printContent();">Imprimir</a></li>');
	}	
});

jQuery.extend({
	id : function(id) {
		var id = id.split("#");
		return document.getElementById(id[1])?true:false;
	}
});

jQuery.fn.toggleText = function(a, b) {
	return this.each(function() {
		jQuery(this).text(jQuery(this).text() == a ? b : a);
	});
};

function printContent(){
	//var title = $('ul.tabs-side li.active a').attr("title");
	//var codigo = $(".content-main").html();
	//$("#divprint").html("<h1>"+title+"</h1><br><br>"+codigo);
	$.jPrintArea("#main","http://buenamesa.digitaria.cl/");
}

