$(document).ready(function(){
	$("#locations ul li:nth-child(2n-1)").css("width","183px");

	$( "#accordion" ).accordion();

  // Active menu
  var path = location.pathname;
  var home = "/";
  $("#nav a[href='" + [path || home] + "']").parent("li").each(function() {
    $(this).addClass("current");
  });	

	// Jcarousel slider
  $('#promobox').jcarousel({
		scroll: 1,
		auto: 5,
		wrap: "both",
		animation: 1
  });

	$('.personalize-group').jcarousel({
		scroll: 1,
		wrap: 'both',
		animation: 1
	});

	$(".jcarousel-skin-personalize .jcarousel-prev").hide();
	$(".jcarousel-skin-personalize .jcarousel-next").hide();
	// Personalize slide
	$(".jcarousel-skin-personalize").hover(function(){
		$(this).find('.personalize-group').stop().animate({
			height: "315px"
		});
		$(this).find('.jcarousel-prev').show();
		$(this).find('.jcarousel-next').show();
	},
	function(){
		$(this).find('.personalize-group').stop().animate({
			height: "250px"
		});
		$(this).find('.jcarousel-prev').hide();
		$(this).find('.jcarousel-next').hide();
	});

	// Photo overlay
	$(".photo a").live('hover',function(event){
			$(this).append("<div class='overlay'><img src='/images/zoom.png' alt='zoom'/></div>");
	});

	$(".photo a").live('mouseleave',function(event){
			$('.overlay').detach();
	});

	//Login box
	var box = $('#login');
  var link = $('.sign-in');
  
  link.click(function() {
      box.show(); 
      $('html, body').animate({scrollTop:0}, 'slow');
      $('#signin_username').focus();
			return false;
  });

  $(document).click(function() {
      box.hide();
  });

  box.click(function(e) {
      e.stopPropagation();
  });

	//Tooltip
	var content;
	$('.tooltip').hover(function(){
		content = $(this).next('.tooltip-content').html();
		$(this).removeAttr('title');
		$(this).append("<div class='hint'>"+content+"</div>");
	},
	function(){
		$(".hint").detach();
	});

	$('.tooltip').click(function(){
		return false;
	});

	// Photos of Budapest tooltip
	$('.photo a').tooltip({
		bodyHandler: function() { 
        return $($(this).parent().children('.tip')).html(); 
    }, 
    showURL: false,
		track: true, 
    delay: 0
	});

	$(document).ajaxStop(function() { 
		$('.photo a').tooltip({
			bodyHandler: function() { 
					return $($(this).parent().children('.tip')).html(); 
			}, 
			showURL: false,
			track: true, 
			delay: 0
		});
	});

  // Autocomplete
  $(function() {
		$( "#venue-autocomplete" ).autocomplete({
			source: '/venues/autocomplete',
			html: true,
      select: function( event, ui ) { 
						if (ui.item.value == undefined){
							$("#search input[type='submit']").click();
						}
						else{
            	window.location.href = ui.item.value;
						}
        }
		});
	}); 
  
  // Venue tabs link
	var date = new Date();
 	date.setTime(date.getTime() + (5 * 60 * 1000));
	
	// Jquery ui tabs
	var $tabs = $( "#tabs" ).tabs({
		cookie: { expires: date },
		//collapsible: true
	});
  
	var num_tabs= $("#tabs").tabs( "length" );
	
  $('.to-review').click(function() { // bind click event to link
    $tabs.tabs('select', num_tabs-1); // switch to reviews
      $('html, body').animate({scrollTop:0}, 'slow');
    return false;
  });
  
  $('.to-map').click(function() { // bind click event to link
    $tabs.tabs('select', num_tabs-1); // switch to reviews
      $('html, body').animate({scrollTop:0}, 'slow');
    return false;
  });
  
  $('.to-photos').click(function() { // bind click event to link
    $tabs.tabs('select', 1); // switch to reviews
      $('html, body').animate({scrollTop:0}, 'slow');
    return false;
  });

	// Image galley title
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div class="overlay-title">' + (title && title.length ? '<b>' + 
				title + '</b><br/>' : '' ) + (currentIndex + 1) + ' / ' + currentArray.length + '</div>';
	}
  
  // Image gallery
  $("#tab-photos a").fancybox({
    'overlayColor'  : '#000',
		'titlePosition' : 'inside',
		'titleFormat'		: formatTitle
  });
  
  // Article image gallery show
  
  //$('#tab-photos').height('101px'); 
  
  $("#show-more").click(function() {
  	var photosHeight = $('.article-page #tab-photos').height();
		$(".article-page #photos-container").animate({height: photosHeight}, 500);
    return false;
  });

  // Tab profile - image gallery
  $("#tab-profile .profile-gallery a").fancybox({
    'overlayColor'  : '#000',
		'titlePosition' : 'inside',
		'titleFormat'		: formatTitle
  });
  
  // Article image gallery show
  
  var photosHeight = $('#tab-photos').height();
  
  $('#tab-profile').height('101px'); 
  
  $("#show-more").click(function() {
    $("#tab-profile").animate({height: photosHeight}, 500);
    return false;
  });

	// Remove from my budapest
	$("#tabs-my-budapest .remove").click(function(){
		$(this).parents('.venue').fadeOut('slow');
		deleteMarker(map, $(this).parents('.venue').attr('rel'));
	});

	// Remove from review
	$("#tabs-reviews .add").click(function(){
		$(this).parents('.comment').fadeOut('slow');
	});

	// Jqtransform - form styling
	$("form.order-by").jqTransform();

	// Option href follow
	$("div.jqTransformSelectWrapper ul li a").click(function(){
		var index = $(this).attr('index');
		var value = $('select.jqTransformHidden option:eq('+index+')').attr('value');
		window.location.href=value;
	});

	$(".sign-out").click(function(){
		//FB.logout(function(response) {
  		// user is now logged out
		//});
	});
 
	// sidebar search submit
	$('#custom-search form').change(function() {
  	$('#custom-search form').submit();
	});

	// venue rating
	venues = $("form[id^='ratings-']");
	
	$("form[id^='ratings-']").each(function(i){
		$(this).stars({
			oneVoteOnly: true,
			callback: function(ui, type, value)
			{
				slug = $(venues[i]).attr('id').substr(8);
				$("#venue-rater-"+slug+" .rate_"+value).click();
			}	
		});
	});

	// Xmas toggle
	$("#xmas-end .box").hover(function(){
		$(this).children(".layer").toggle();
	},function(){
		$(this).children('.layer').toggle();
	});	

	// Valentine toggle
	$("#valentine-end .box").hover(function(){
		$(this).children(".layer").toggle();
	},function(){
		$(this).children('.layer').toggle();
	});	

});

