var resizeFirst = true;
var resizeProp = {hProp:0,wProp:0};

window.addEvent('load', function(){
	loadPhotos(np, 1, path, "photo")
	
	$$(".active").each(function(el) {
		if($E(".green_bar", el)) $E(".green_bar", el).addClass("hover")
		if($E(".title", el)) $E(".title", el).src = $E(".title", el).src.replace("_norm", "_hover")
	})
	
  	$$(".section", "#photo").addEvents({
		mouseenter: function() {
			if(!this.hasClass("active")) {
				if($E(".green_bar", this)) $E(".green_bar", this).addClass("hover")
				if($E(".title", this) && $E(".title", this).src) $E(".title", this).src = $E(".title", this).src.replace("_norm", "_hover")
			}
		},
		mouseleave: function() {
			if(!this.hasClass("active")) {
				if($E(".green_bar", this)) $E(".green_bar", this).removeClass("hover")
				if($E(".title", this) && $E(".title", this).src) $E(".title", this).src = $E(".title", this).src.replace("_hover", "_norm")
			}
		}
	});
	
	if (isdefined("contactPage") && GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(46.8481, -71.215525), 14);
		map.openInfoWindowHtml(new GLatLng(46.844166, -71.215525),"<strong>2485 boul. Ste-Anne #22</strong><br />Qu&eacute;bec, Qc. G1J 1Y4<br /><br />T&eacute;l. 418 658 8016<br />T&eacute;l&eacute;c. 418 658 0638");
		map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
	}
	
	var para = $$(".para")
	if(para.length > 0) para.getLast().addClass("last");
	
	rollOver(".rollOver");
	window.addEvent('resize', backgroundResize);
	backgroundResize();
})

function isdefined(variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function rollOver(el, search, replace) {
	if(search == null) search = "_norm";
	if(replace == null) replace = "_hover";
	$$(el).addEvents({
		mouseenter: function() {
			this.src = this.src.replace(search, replace)
		},
		mouseleave: function() {
			this.src = this.src.replace(replace, search)
		}
	});
}

function flashVisible(bool) {
//	arg = (bool) ? "visible" : "hidden" ;
//	$("background").setStyle("visibility", arg);
}

function loadPhotos(num, page, path, el) {
	var _box = (el == "lbThumbnail") ? _box = "&box=true" : "" ;
	new Ajax(path+"assets/php/photos.php?num="+num+"&page="+page+"&path="+path+_box, {
		onComplete: function(newHTML) {
			$(el).setHTML(newHTML)
			if(window.pngFix) pngFix();
			Lightbox.load(el);
		}
	}).request();
}

function print_r(theObj){
	if(theObj.constructor == Array || theObj.constructor == Object) {
		document.write("<ul>");
		for(var p in theObj){
			if(theObj[p].constructor == Array || theObj[p].constructor == Object) {
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>");
				print_r(theObj[p]);
				document.write("</ul>");
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>");
	}
}

function backgroundResize() {
	var sWidth = window.getWidth();
	var sHeight = window.getHeight();
	var img = $("background_img");
	if(resizeFirst) {
		resizeProp.hProp = img.height / img.width;
		resizeProp.wProp = img.width / img.height;
		resizeFirst = false
	}
	//*
	if(sHeight >= sWidth) {
		if(resizeProp.hProp > (sHeight/sWidth)) {
			img.width = sWidth
			img.height = img.width / resizeProp.wProp
		} else {
			img.height = sHeight
			img.width = img.height / resizeProp.hProp
		}
	} else {
		if(resizeProp.wProp > (sWidth/sHeight)) {
			img.height = sHeight
			img.width = img.height / resizeProp.hProp
		} else {
			img.width = sWidth
			img.height = img.width / resizeProp.wProp
		}
	}
	//*/
}


function validMail(strEmail) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail))
}

function contact_form_send() {
	var msg = "";
	if($("f_nom").getValue() == "") msg += "Vous devez écrire votre nom.\n";
	if(!validMail($('f_courriel').getValue())) msg += "Vous devez écrire un courriel valide.\n";
	if(msg != "") {
		alert(msg)
	} else {
		new Ajax(path+"assets/php/contact_form.php", {
			method: 'post',
			data: $('contact_form').toQueryString(),
			onComplete: function() {
				alert("Message envoyé avec succès.")
				$('f_message').update("Votre message")
			}
		}).request();
	}
	return false;
}

