/**
 * @package WordPress
 * @subpackage Custom_main_javascript
 */


$(document).ready(function(){
	// cufon replacements
	Cufon.replace('#nav a', { fontFamily: 'FairplexWideBold', hover: true });
	Cufon.replace('#sidebar ul li a', { fontFamily: 'FairplexWideBold', hover: true });	
	Cufon.replace('#sidebar.home h3', { fontFamily: 'FairplexWideBold' });		
	Cufon.replace('#sidebar.home p', { fontFamily: 'FairplexWideMedium' });			
	Cufon.replace('h2', { fontFamily: 'FairplexWideBold' });
	Cufon.replace('#shopp ul#categories-index li span.category-title', { fontFamily: 'FairplexWideBold' });
  Cufon.replace('#shopp ul.products li h4.name a', { fontFamily: 'FairplexWideBold' });
  Cufon.replace('.entry span.intro', { fontFamily: 'FairplexWideMedium' });
    
	// add prettyPhoto attribute to image links
	$('.page-gallery img').each(function() {
		$(this).parent().attr('rel', 'prettyPhoto[gallery]');
	});
	
	// init pretty photo
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: false,
		theme: 'dark_square'
	});	
	
  // map in contact page
	if($.find('#contact-gmap').length > 0) {
    gmaps.init_map({ container: 'contact-gmap', markers_selection: '#content a.geocode', zoom_level: 14}); 
	}
	
  // map in point de vent
  if($.find('#gmap').length > 0) {
    gmaps.init_map({ container: 'gmap', markers_selection: '#cities a.geocode'});
    collapse_menu.init({ scope_selection: "#cities ul li.region"});
  }
  
  // footer auto position
  position_footer();
  window.onresize = position_footer;
});


function position_footer() {
  var height = $("#header").height() + $("#content").height();
  var window_height = $(window).height(true) - $("#footer").height() + 20;
  if(window_height > height) {
    $("#footer").addClass('fixed');
  } else {
    $("#footer").removeClass('fixed');  
  }
}

/* ---------------------------------------------------------------------------
	Class: GMaps
	Description: various snippets for interacting with Google Maps API
------------------------------------------------------------------------------ */
gmaps = {

	settings : {
		default_lat: 45.953834,
		default_lng: -71.99170,
		zoom_level: 9,
		container: 'gmap',
		markers_selection: '.markers a'
	},

	init_map : function(settings) {
		// extend settings
		jQuery.extend(gmaps.settings, settings);						
				
		// init map object and center to the mountain
		this.map = new GMap2(document.getElementById(gmaps.settings.container));
		this.map.addControl(new GSmallMapControl());
		this.map.enableScrollWheelZoom();

		// default position
		var center = new GLatLng(this.settings.default_lat, this.settings.default_lng);

		// add default marker and center map
		this.map.setCenter(center, this.settings.zoom_level);

		// add loader
		$("<div id='gmap-loader'></div>").hide().appendTo("#" + gmaps.settings.container);

		// inject all markers
		gmaps.init_markers(gmaps.settings.markers_selection);			
									
		// ******************************
		// Firefox 3.5 Only feature
		// Ask for current user geocoordinate with geolocate api in Firefox
		// ******************************
		if(navigator.geolocation) {
			// show geo-locator call to call to action
			$("a#geo-locator").show().click(function() {
				gmaps.geo_locate();
				return false;
			});		
		}		
	},
	
	geo_locate : function() {
		gmaps.toggle_loader('show');		
		navigator.geolocation.getCurrentPosition(function(pos) {
			var geo_location = new GLatLng(pos.coords.latitude, pos.coords.longitude);
			gmaps.map.panTo(geo_location);			
			gmaps.toggle_loader('hide');
		});
	},
	
	toggle_loader : function(action) {
		if(action == 'show') {
			$("#gmap-loader").css('opacity', 0).show().animate({opacity: 0.5}, 250);
		}
		if(action == 'hide') {
			$("#gmap-loader").animate({opacity: 0}, 250, function(){ $(this).hide(); });
		}
	},
	
	init_markers: function(selection) {
		// clear all markers on map
		gmaps.map.clearOverlays();
					
		$(selection).each(function() {
			var type = this.className;
			var title = $(this).attr("title");
			var LatLng = gmaps.get_coordinates($(this).attr('rel')); // retreive lag&long from element rel attribute
			var i = gmaps.create_icon(type);
			var markerLatLng = new GLatLng(LatLng.lat, LatLng.lng);
			var marker = new GMarker(markerLatLng, {icon: i, title: title});
			var url = this.href;
			var a = $(this);

			// add marker on map
			gmaps.map.addOverlay(marker, url);

			// register click event on marker
			GEvent.addListener(marker, "click", function() {
				gmaps.display_info_window(marker, url, a);
			});			

			// unbind anything and register on click event on marker link
			a.unbind('click');
			a.click(function() {				
				// display info window
				gmaps.display_info_window(marker, url, a);
				gmaps.map.panTo(markerLatLng);						
				// prevent default click
				return false;
			});
		});
	},

	display_info_window : function(marker, url, a) {
		var address = $(a).find('.content').html();
		var title = $(a).find('.title').text();		
		var html = '';
		
		// build html
		html += "<div class='gmap-infowindow'>";
		html += "<h4>"+title+"</h4>";
		html += "<p>"+address+"</p>";
		html += "</div>";
		
		// open infoWindow
		gmaps.map.openInfoWindowHtml(marker.getPoint(), html, { maxWidth: 300 });
	},
	
	create_icon : function() {	
		var i = new GIcon();
		i.image = wp_theme_path + "/images/map-icn.png";			
    i.iconSize = new GSize(35, 37);
    i.iconAnchor = new GPoint(12, 37);
    i.infoWindowAnchor = new GPoint(1, -3);
		i.zIndex = 10000;
		return i;
	},

	// extrac geo-coding from a string
	// expected format : geo:lat=X.XXX,geo:lon=X.XXX
	get_coordinates : function(string) {
		raw_coordinates = string.split(',');
		LatLng = {
			lat: raw_coordinates[0].replace(/geo:lat=/, ''),
			lng: raw_coordinates[1].replace(/geo:lng=/, '')
		};
		return LatLng;
	}
	
};

/* ---------------------------------------------------------------------------
	Class: Collapse Menu
	Description: show/hide child menu items
------------------------------------------------------------------------------ */

collapse_menu = {
	
	settings : {
		scope_selection: 'ul li h4',
		closed_height: 20
	},
	
	init : function(settings) {
		// extend settings
		jQuery.extend(collapse_menu.settings, settings);
				
		// faq toggles
		$(collapse_menu.settings.scope_selection).each(function(){
			// find child object to open
			var parent = $(this);
			var trigger = $(this).find('h4 a');
			var child = $(this).find('ul');
						
			$(trigger).click(function() {				
				if(!$(parent).hasClass('open')) {
					collapse_menu.openSlide(child, parent);					
				} else {
					collapse_menu.closeSlide(child, parent);
				}
				return false;
			});									
		});		
		
		// open first element in list
		var first = $(collapse_menu.settings.scope_selection + ":first");
		var first_child = $(first).find('ul');		
		collapse_menu.openSlide(first_child, first);		
	},
	
	openSlide : function(child, parent) {		
		var new_height = $(child).outerHeight(true) + $(parent).outerHeight(true);			
		
		// hide any opened faq
		$(collapse_menu.settings.scope_selection).filter('.open').animate({'height': collapse_menu.settings.closed_height}, 250, function() { $(this).removeClass('open'); })
		
		// open slide and scrollTo item
		$(parent).animate({'height': new_height}, 250, function() { $(parent).addClass('open'); });
	}

}

/* ---------------------------------------------------------------------------
	Class: Toggle overlay
	Description: show/hide dark overlay in catalog listing
------------------------------------------------------------------------------ */
toggle_overlay = {	
	init : function(selection) {		
		$(selection).mouseenter(function() {
		    var overlay = $(this).find('.overlay');
		    $(overlay).css('opacity', 0.5);
		    $(overlay).animate({opacity: 0}, 250);
		});
		$(selection).mouseleave(function() {
		    var overlay = $(this).find('.overlay');
		    $(overlay).animate({opacity: 0.5}, 250);
		});		
	}	
}
