/*
	Javascript pour College Lafleche - Google Search & Maps
	Tom - egzakt.com
	2009-02-20
*/
function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

jQuery.noConflict();
jQuery(document).ready(function() {
	if (isdefined('google')) {
    	jQuery("#go").click(function(){	
    		getDirections(jQuery("#fromAddress").val());
    		return false;
    	});
	
    	if (jQuery('#map_container').length > 0) {
    		loadMap();
    	}
    	aucun = '<div class="noresults">Aucun r&eacute;sultats.</div>';
    	google.load("search", "1");

        jQuery(".chercher").click(function() {
        	jQuery('#centre').empty('');
        	OnLoad();
        })
    }
});

function OnSearchComplete (sc, searcher) {
	if ((searcher.results == 0) && (searcher.results.length == 0)) {
		jQuery("#centre").empty();
	}
}

var map;
var directionsPanel;
var directions;
var cadIcon;
function loadMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var cadPoint1 = new GLatLng(46.35066011148575, -72.55727291107178);
		map.setCenter(cadPoint1, 15);
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		myKBHandler = new GKeyboardHandler(map);
		marker1 = new GMarker(cadPoint1);
		map.addOverlay(marker1);
		
		directionsPanel = document.getElementById("directions");
		directions = new GDirections(map, directionsPanel);

		map.addOverlay(marker1);

		GEvent.addListener(directions, "load", onGDirectionsLoad);
	}
}

function getDirections_init(a,from,to) {
	if (!a)
		return false;
	if (!from) {
		return code_postal_erreur();
	}
	if (!to) {
		return false;
	}
	getDirections(from,to);
	return code_postal_corrige();
}


function getDirections(from){
	dirString = "from: "+from+" to: 46.35066011148575, -72.55727291107178";
	directions.load(dirString);
}

function onGDirectionsLoad(){	
	jQuery("#directions").show();
	map.setMapType(G_NORMAL_MAP);
}

function onGDirectionsError(){
	$("#directions").show();
}


function OnLoad() {
	var searchControl = new google.search.SearchControl();
	var options = new google.search.SearcherOptions();
	var siteSearch = new google.search.WebSearch();
	var mots = document.getElementById("recherche").value;
	var gsc_source = "clafleche.qc.ca";
	options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
	siteSearch.setSiteRestriction(gsc_source);
	searchControl.addSearcher(siteSearch, options);
	searchControl.draw(document.getElementById("centre"));
	searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	searchControl.execute(mots);
	if (!mots) {
		jQuery("#centre").empty();
	}
	searchControl.setSearchCompleteCallback(this, googleSearchCallBack);
	jQuery("#breadcrumb").hide();
	jQuery("#gauche").html('&nbsp;');
	jQuery("#centre").append('<div id="resultats_recherche">Résultats de la recherche pour « <strong>'+mots+'</strong> »</div>')
}

var exec_once = 0;
function googleSearchCallBack(that)
{
    exec_once++;
	a = 0;
	jQuery('.gsc-webResult').each(function (i) { a++; });
	if ((a <= 1) && (exec_once == 1)) {
		jQuery('.gsc-control').remove();
		jQuery('#tipnprint').remove();
		jQuery("#centre").append(aucun);
	}
}


/*
    Tom - 2009-10-20 09:43:14
    Fonction pour le tipafriend
*/
function tipafriend (url) {
	window.location = 'envoyer-a-un-ami.html?tipUrl='+escape(url);
	return false;
}

jQuery(function(){
	if (jQuery('#graphique').html()) {
		jQuery('#graphique').hide();
		jQuery('#graphique').visualize({
			colors: ['#F89B2F'],
			height: 200,
			width: 400
		});
	}
	if (jQuery('#graphique2').html()) {
		jQuery('#graphique2').hide();
		jQuery('#graphique2').visualize({
			colors: ['#F89B2F'],
			height: 200,
			width: 400
		});
	}
});
