/**
 *
 * @access public
 * @return void
 **/
 var img;
img=0;

CIMMO = {
	BienSellOnLoad: function(type) {
		type = type || 'B';

		if (type == 'B')
			this.doVisit();

		this.requestDoc();

		MG.Events.on("box_sendmail_closebox", "click", this.openCloseBox);
		MG.Events.on("box_sendmail_btn", "click", this.sendFormEmail);
	},

	sendFormEmail: function() {
		if (MG.$('nom').value == '' || MG.$('email').value == '')
		{
			MG.Style.show('box_sendmail_err_msg');
			return;
		}

		MG.Style.hide('box_sendmail_err_msg');
		MG.$('form_sendmail').submit();
	},

	openCloseBox: function(e, title, formtype) {
		var el = MG.$('box_sendmail'),
			boxID = 'box_sendmail';

		MG.$('box_sendmail_title').innerHTML = title;
		MG.$('fromtype').value = formtype;

		if (el.getAttribute("open") == "false") {
			el.setAttribute("open", "true");

			MG.Style.show(boxID);
			MG.Style.setStyle(boxID, "top", (MG.Window.pageYOffset()+((MG.Window.innerHeight()-MG.$(boxID).offsetHeight)/2))+"px");

			if (!MG.$("blurContainer"))
				MG.Dom.addElement("div", {parent:MG.$$("body")[0], id:"blurContainer"});

			MG.Effect.blurContainer.on("blurContainer", {
				zIndex: "8",
				color: "#FFF",
				opacity: "0.70"
			});
		}
		else {
			el.setAttribute("open", "false");
			MG.Style.hide(boxID);
			MG.Effect.blurContainer.off("blurContainer");
		}
	},

	doVisit: function() {
		MG.Events.on("btn_doVisit", "click", this.openCloseBox, CIMMO, ["Faire une visite", "dovisit"]);
	},

	requestDoc: function() {
		MG.Events.on("btn_requestDoc", "click", this.openCloseBox, CIMMO, ["Recevoir de la documentation", "requestdoc"]);
	}
};


CIMMO.txHypo = (function() {
	var buyPrice, fondPropre, yearGold, hypo, hypoR1, hypoR2, txH1, txH2, txA, IH1, IH2, amo, fraiAnnex, yearHouse, total,
		historyValue = {fondPropre:"", yearGold:""};

	var fn__click = function(e) {
		el = MG.$("btn_txHypo");
		if (el.getAttribute("open") == "false") {
			el.setAttribute("open", "true");
			MG.Style.setStyle("txHypo", "top", (MG.Window.pageYOffset()+30)+"px");
			MG.Style.show("txHypo");

			if (!MG.$("blurContainer"))
				MG.Dom.addElement("div", {parent:MG.$$("body")[0], id:"blurContainer"});

			MG.Effect.blurContainer.on("blurContainer", {
				zIndex: "8",
				color: "#FFF",
				opacity: "0.70"
			});
		}
		else {
			el.setAttribute("open", "false");
			MG.Style.hide("txHypo");
			MG.Effect.blurContainer.off("blurContainer");
		}
	}

	var get = function(el) {
		if (el.id == "txH1" || el.id == "txH2" || el.id == "txA")
			return (parseFloat(el.value)/100);

		return computerRead(el.value);
	}

	var set = function(el, v) {
		v = Math.round(parseFloat(v)*100)/100;

		switch (el.id)
		{
			case "total":
				if (v <= 33)
					MG.$("total_msg").innerHTML = "Dans cet exemple, ils sont inférieurs à <b>33%</b>. N'oubliez pas d'avoir une marge en cas d'augmentation des taux hypthécaires.";
				else
					MG.$("total_msg").innerHTML = "Dans cet exemple, ils sont supérieurs à <b>33%</b>. Refaites les calculs en intégrant plus de fonds propres et/ou en tenant compte d'un prix d'achat inférieur";

				break;
		}

		if (el.id == "txH1" || el.id == "txH2" || el.id == "txA")
			el.value = v;
		else
			el.value = humanRead(v);
	}

	var calcule = function() {
		set(hypo, (get(buyPrice)-get(fondPropre)));

		set(hypoR1, (get(buyPrice)*0.66));
		set(hypoR2, (get(hypo)-get(hypoR1)));

		set(IH1, (get(hypoR1)*get(txH1)));
		set(IH2, (get(hypoR2)*get(txH2)));

		set(amo, (get(hypo)*get(txA)));

		set(fraiAnnex, (get(buyPrice)*0.7/100));

		set(yearHouse, (get(IH1)+get(IH2)+get(amo)+get(fraiAnnex)));

		set(total, (get(yearHouse)/get(yearGold)*100));
	};

	var fn__calcule = function(e) {
		calcule.call();
	};

	var humanRead = function(v) {
		v = v.toString().split(".");
		var r = v[0];
			nbCut = r.length/3;

		nbCut = (nbCut > Math.floor(nbCut) && nbCut < Math.ceil(nbCut)) ? Math.floor(nbCut) : Math.floor(nbCut)-1;

		if (nbCut >= 1) {
			var coefAdd = 0;

			for (var i = 1, ilen = nbCut; i <= ilen; i++) {
				var r1 = r.slice(0, -((i*3)+coefAdd)),
					r2 = r.slice(-((i*3)+coefAdd));

				++coefAdd;
				r = r1+" "+r2;
			}
		}

		r = v.length == 2 ? r+","+v[1] : r;

		return r;
	};

	var computerRead = function(str) {
		while (str.search(/\s+/) != -1)
			str = str.replace(/\s+/, "");

		str = str.replace(/,+/, ".");

		return isNaN(str) ? "NaN" : parseFloat(str);
	};

	var fn__cleanNaN = function(e) {
		var v;

		if (computerRead(e.el.value) == "NaN")
			v = historyValue[e.el.id];
		else {
			v = computerRead(e.el.value);
			historyValue[e.el.id] = v;
		}

		e.el.value = humanRead(v);

		if (e.el.id == "fondPropre")
			check_fond_propre();
	};

	var check_fond_propre = function() {
		if (get(fondPropre) < (get(buyPrice)*20/100))
			MG.Style.show("msgFondPropre");
		else
			MG.Style.hide("msgFondPropre");
	};

	var init = function() {
		MG.Events.on("btn_txHypo", "click", fn__click);
		MG.Events.on("txHypo_closebox", "click", fn__click);

		buyPrice = MG.$("buyPrice");
		fondPropre = MG.$("fondPropre");
		yearGold = MG.$("yearGold");
		hypo = MG.$("hypo");
		hypoR1 = MG.$("hypoR1");
		hypoR2 = MG.$("hypoR2");
		txH1 = MG.$("txH1");
		txH2 = MG.$("txH2");
		txA = MG.$("txA");
		IH1 = MG.$("IH1");
		IH2 = MG.$("IH2");
		amo = MG.$("amo");
		fraiAnnex = MG.$("fraiAnnex");
		yearHouse = MG.$("yearHouse");
		total = MG.$("total");

		txH1.value = "2.50";
		txH2.value = "2.75";

		historyValue.fondPropre = fondPropre.value;
		historyValue.yearGold = yearGold.value;

		check_fond_propre();

		MG.Events.on(fondPropre, "blur", fn__calcule);
		MG.Events.on(yearGold, "blur", fn__calcule);
		MG.Events.on(fondPropre, "keyup", fn__cleanNaN);
		MG.Events.on(yearGold, "keyup", fn__cleanNaN);
		MG.Events.on(txH1, "change", fn__calcule);
		MG.Events.on(txH2, "change", fn__calcule);
		MG.Events.on(txA, "change", fn__calcule);

		calcule.call();
	}

	return {
		init: init
	}

})();



/**
 */
CIMMO.defileBar = (function() {

	var init = function() {
		MG.Effect.moveEffect(MG.$("defile"), {
			length: 1000,
			delay: 5000,
			intervalTime: 10,
			type: "line",
			deplacement: "right",
			callback: function() {
				MG.Style.hide("defile");
				MG.Style.show("message2");
			}
		});
	};

	return {
		init: init
	}
})();






function faireVisite(){
	document.getElementById("f2").style.display="block";
	document.getElementById("f1").style.display="none";
}

function demanderDocumentation(){
	document.getElementById("f2").style.display="none";
	document.getElementById("f1").style.display="block";
}

function homeimage(pic, picno)
{


var obj = document.getElementById("cadre2");
obj.style.backgroundImage= 'url( photos/' + pic + ')';
obj.style.backgroundRepeat="no-repeat";
obj.style.backgroundPosition= '0px 0px';


var obj = document.getElementById("im1");
obj.src="images/rond_blanc.png";
var obj = document.getElementById("im2");
obj.src="images/rond_blanc.png";
var obj = document.getElementById("im3");
obj.src="images/rond_blanc.png";
var obj = document.getElementById("im4");
obj.src="images/rond_blanc.png";
var obj = document.getElementById("im5");
obj.src="images/rond_blanc.png";


switch(picno)
{
case 1:
	var obj = document.getElementById("im1");
	obj.src="images/rond_actif.png";
	break;
case 2:
	var obj = document.getElementById("im2");
	obj.src="images/rond_actif.png";
	break;
case 3:
	var obj = document.getElementById("im3");
	obj.src="images/rond_actif.png";
	break;
case 4:
	var obj = document.getElementById("im4");
	obj.src="images/rond_actif.png";
	break;
case 5:
	var obj = document.getElementById("im5");
	obj.src="images/rond_actif.png";
	break;
}
}


/**
 *
 * @access public
 * @return void
 **/
function homegallery(){
	img++;
	if (img>4 ) {
		img=0;
	}
	homeimage(homepic[img],img+1);
//	alert(img);
}


