//	CUFON SETTINGS

var htext          = 'div.content h2, div.content h3';
var gblack         = 'div.content h4, div.content h3.subtitle, #prinfo th';
var gblack_shadow  = '#sidebar p.view a, #pic p, div#parea a';
var gblack_hover   = '#header li, #gallery p, #places li a';
var gbold          = '#home span.gotham';

Cufon.replace(htext,          { fontFamily: 'Hoefler-Text-Italic', hover: true });
Cufon.replace(gblack,         { fontFamily: 'Gotham-Black' });
Cufon.replace(gblack_shadow,  { fontFamily: 'Gotham-Black', textShadow: '#000 1px 1px' });
Cufon.replace(gblack_hover,   { fontFamily: 'Gotham-Black', hover: true });
Cufon.replace(gbold,          { fontFamily: 'Gotham-Bold' });

var base_url = window.location.host;

$(function(){

//	ENABLE STYLES FOR JS

	$('body').addClass('js');

//	FUNCTIONALITY FOR PLACES BAR

	$places = $('#places');
	$places_ul = $places.find('ul');
	$places_li = $places.find('li');
	$places_sc = $places.find('div');
	$places_br = $('#places span a');

	scwd = 0;
	stops = Array();
	stops[0] = 0;
	smark = 0;
	for(i=0; i<$places_li.length; i++){
		scwd += $places_li.eq(i).outerWidth();
		stops[i+1] = scwd;
	}
	
	oldmar = $places_sc.cssInt('marginLeft');
	$places_sc.width(scwd);

	i2 = stops.length - 1;
	max_j = 0;
		
	while(stops[i2] > $places.outerWidth() + 2 * oldmar)
	{
		max_j++;
		i2--;
	}
	max_j = max_j + 1;
	
	j = 0;
	end = false;
	
	$places_br.click(function(){
	
		// Clean search query
		$('#search_query').val('');
	
		if ($(this).parent().hasClass('left')){
			if (scwd - ($places.outerWidth() + 2 * oldmar) <= stops[j]){
				end = true;
				mleft = -(scwd - $places.outerWidth()) - oldmar;
			}
			else {
				j++;
				mleft = Math.round(oldmar - stops[j]);
			}
		}
		else {
			j--;
			if (j < 0){
				j = 0;
			}
			else if (end){
//				j++;
				end = false;
			}
			mleft = Math.round(oldmar - stops[j]);
		}
		$places_sc.animate({
			marginLeft : mleft
		}, 400);

		return false;
	});

	$links = $('#places div a');
	$handle = $('#places div span');
	hdwd = $handle.outerWidth();
	$handle.css('marginLeft', ($links.eq(0).parent().outerWidth() - hdwd)/2);

	$links.click(function(){
	
		// Clean search query
		$('#search_query').val('');
	
		var lid = this.id;
		var href = $(this).attr('href');
		
		num = $links.index(this);
		mleft = stops[num] + ($(this).parent().outerWidth() - hdwd)/2;
		
		$('ul.dk_options_inner li.dk_option_current').removeClass('dk_option_current');
		$('ul.dk_options_inner li a[data-dk-dropdown-value = ' + $('.default option[id=' + lid + ']').val() + ']').parent().addClass('dk_option_current');
		$('span.dk_label').html($('.default option[id=' + lid + ']').attr('name'));
		
		$handle.animate({
			marginLeft : mleft
		}, 400, function(){
			$links.removeClass('active').eq(num).addClass('active');
			Cufon.replace('#places div a', { fontFamily: 'Gotham-Black', hover: true });

			$.ajax({
				  url: href,
				  cache: false,
				  dataType: 'html',
				  type: 'POST',
				  data: {
				  	search_query: $('#search_query').val()
				  },
				  success: function(data) {
					$('div#ajax_content > *').fadeOut(400, function() {
						$('div#ajax_content').html(data);
						Cufon.replace('#gallery p', { fontFamily: 'Gotham-Black', hover: true });
						Cufon.replace('div.content h3', { fontFamily: 'Hoefler-Text-Italic', hover: true });
						$('div#ajax_content > *').fadeIn(400);
					});
				  }
			});			
			
			return false;			
		});
		return false;
	});

//	FUNCTIONALITY FOR GALLERY HIGHLIGHTING

	$('#gallery img').hover(
		function(){
			$t = $(this).parents('div').children('h3').children('a').css('color', '#df1b28');
			Cufon.replace('#gallery h3',  { fontFamily: 'Hoefler-Text-Italic', hover: true });
		},
		function(){
			$t = $(this).parents('div').children('h3').children('a').removeAttr('style');
			Cufon.replace('#gallery h3',  { fontFamily: 'Hoefler-Text-Italic', hover: true });
		}
	);
	
	// Pagination functionality
	$('a.pageno').live('click', function() {
		$.ajax({
			  url: $(this).attr('href'),
			  cache: false,
			  dataType: 'html',
			  type: 'POST',
			  data: {
			  	search_query: $('#search_query').val()
			  },
			  success: function(data) {
				$('div#ajax_content > *').fadeOut(400, function() {
					$('div#ajax_content').html(data);
					Cufon.replace('#gallery p', { fontFamily: 'Gotham-Black', hover: true });
					Cufon.replace('div.content h3', { fontFamily: 'Hoefler-Text-Italic', hover: true });
					$('div#ajax_content > *').fadeIn(400);
				});
				$('html, body').animate({scrollTop:350}, 'slow'); // Scroll at bottom of the header
			  }
		});
		return false;
	});
	
	// Open all external links and PDF in new window
	$("a[href^='http:']:not([href*='" + base_url + "']), a[href$='.pdf']:not([href*='" + base_url + "']), a[href$='.pdf']").live('click', function() {
		$(this).attr('target','_blank');
	});
});

(function($) {
	$.fn.cssInt = function(param){
		val = $(this).css(param);
		return parseInt(val);
	}
})(jQuery);

