//	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' });

$(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(){
		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(){
		var lid = this.id;
		var href = $(this).attr('href');
		num = $links.index(this);
		mleft = stops[num] + ($(this).parent().outerWidth() - hdwd)/2;
		$handle.animate({
			marginLeft : mleft
		}, 400, function(){
			$links.removeClass('active').eq(num).addClass('active');
			Cufon.replace('#places div a', { fontFamily: 'Gotham-Black', hover: true });
			var psst = $('p.select select option#' + lid).text();
			$('p.select select option#' + lid).attr('selected', true);
			$('p.select span').html(psst);

			$.ajax({
				  url: href,
				  cache: false,
				  dataType: 'html',
				  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 });
		}
	);

//	FUNCTIONALITY FOR ENHANCED SELECT MENU

	ul = '<ul>';
	$browse = $('#browse');
	$opt = $browse.find('option');
	for (i=0; i<$opt.length; i++){
		ul += '<li>' + $opt.eq(i).text() + '</li>';
	}
	ul += '</ul>';
	$sel = $browse.find('p.select').append($(ul));
	$ul = $browse.find('ul');
	$span = $browse.find('span');
	var plow = $('#places div a').eq(0).parent().outerWidth();
	$span.click(function(e){
		e.stopPropagation();
		if ($ul.css('display') == 'none'){
			$ul.fadeIn(400);
		}
		else {
			$ul.fadeOut(400);
		}
	});
	hide_menu = function(){
		$ul.fadeOut(400);
	}
	$('body').click(hide_menu);
	$sel_li = $ul.find('li');
	$sel_li.click(function(){
		text = $(this).text();
		$span.text(text);
		num = $sel_li.index(this);
		$opt.eq(num).attr('selected', true);
		var lid = $('select#city option').eq(num).attr('id');
		
		$.ajax({
			  url: $opt.eq(num).val(),
			  cache: false,
			  dataType: 'html',
			  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);
				});
			  }
		});	

		$links = $('#places div a');
		$handle = $('#places div span');
//		num = $links.index($('#places div a#' + lid));
//		mleft = stops[num] + ($('#places div a#' + lid).parent().outerWidth() - hdwd)/2;
		mleft = stops[num] + (plow - hdwd)/2;

		$handle.animate({
			marginLeft : mleft
		}, 400, function(){
			$links.removeClass('active').eq(num).addClass('active');
			Cufon.replace('#places div a', { fontFamily: 'Gotham-Black', hover: true });
		});
		
		if(num >= max_j)
		{
			end = true;
			j = max_j;
		}
		else
		{
			j = num;
		}
		
		if (scwd - ($places.outerWidth() + 2 * oldmar) < stops[j]){
			end = true;
			mleftsc = -(scwd - $places.outerWidth()) - oldmar;
		}
		else {
			mleftsc = Math.round(oldmar - stops[j]);
		}

		$places_sc.animate({
			marginLeft : mleftsc
		}, 400);
		
	});
	
	$('a.pageno').live('click', function() {
		$.ajax({
			  url: $(this).attr('href'),
			  cache: false,
			  dataType: 'html',
			  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;
	});
});

(function($) {
	$.fn.cssInt = function(param){
		val = $(this).css(param);
		return parseInt(val);
	}
})(jQuery);
