initNewsArea = function() {
	initAllNewsArea("#newsArea");
}

initNewsQcArea = function() {
	initAllNewsArea("#newsAreaQc");
}

initArticleArea = function() {
	initAllNewsArea("#articleArea");
}

initAllNewsArea = function(parent) {
	$('a.articleTitreLien', parent).each(function() {
		var currentId = $(this).attr('id');
		var currentUrl = 'showArticle.php?id_article=' + currentId;
		$(this).attr('href','#');
		$(this).attr('href','javascript:displayarticle(' + currentId + ');');
		$(this).attr('onclick',"javascript:displayarticle(' + currentId + ');");
	});
}



initHeaderMenu = function() {
	$('.topButtonActive').mouseover(function() {
	   $(this).css("background-color", "#6F89A9");
	});
	$('.topButtonActive').mouseout(function() {
	   $(this).css("background-color", "#224774");
	});
}

getHtmlService = function(container, uri) {

    $.ajax({
        async: false,
        type: "GET",
        url: uri,
        dataType: "html",
        success: function(msg){
			msg = trim(msg);
				$(container).html(msg);
			}
    	});
    $.globalEval($('span.script',container).text());

    $(container).show();

};

getHtmlServiceWait = function(container, uri, loadingId, functionToCall) {

    $.ajax({
        async: true,
        type: "GET",
        url: uri,
        cache: false,
        dataType: "html",
        complete: function(XMLHttpRequest, textStatus){

			if (XMLHttpRequest.readyState!=4) {
				$(loadingId).show();
			} else if(XMLHttpRequest.readyState==4) {
				$(loadingId).hide();
				if(functionToCall != null)
					functionToCall();
			}
		},
        success: function(msg){
			msg = trim(msg);
				$(container).html(msg);
			}
    	});
    $.globalEval($('span.script',container).text());

    $(container).show();

};


initPage = function(section) {
	// we need to add the spip to the root as it is generated by the spip engine, and our current root doesn't include de spip in the root url...
	$("img", section).each(function() {
		var root = $(this).attr("src");
		var pos = root.indexOf("IMG/");
		var newRoot = root;
		if(pos != -1) {
			newRoot = root.substring(0,pos) + "/spip/" + root.substring(pos,root.length);
		}
		$(this).attr("src", newRoot);
	});
};


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
};

// The ids are hard coded and should be the same in the realization page with no regards to the language.
// the language code is there in case the slide shows differes based on the language code.
initSlideShows = function(languageCode) {
	$('#viewByLocation').click(function() {
		var locationSelection = $('#locationSelection').attr("value");
		if(locationSelection != "") {
			if(locationSelection == "MISCFR001") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/MISCFR001/MISCFR001.swf" /><param name="quality" value="high" /><param name="base" value="flash/MISCFR001" /><embed src="flash/MISCFR001/MISCFR001.swf?fs_path=flash/MISCFR001" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			}
			if(locationSelection == "RUSSIA") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/RUSSIA/russia.swf" /><param name="quality" value="high" /><param name="base" value="flash/RUSSIA" /><embed src="flash/RUSSIA/russia.swf?fs_path=flash/RUSSIA" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			}
		}
		self.location.href = '#slideShowAreaAnchor';
	});

	$('#viewByProject').click(function() {
		var projectSelection = $('#projectSelection').attr("value");
		if(projectSelection != "") {
			if(projectSelection == "ETS") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/ETS/ETS.swf" /><param name="quality" value="high" /><param name="base" value="flash/ETS" /><embed src="flash/ETS/ETS.swf?fs_path=flash/ETS" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			} else if(projectSelection == "CDR3EX") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/CDR3EX/CDR3EX.swf" /><param name="quality" value="high" /><param name="base" value="flash/CDR3EX" /><embed src="flash/CDR3EX/CDR3EX.swf?fs_path=flash/CDR3EX" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			} else if(projectSelection == "RESQC001") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/RESQC001/RESQC001.swf" /><param name="quality" value="high" /><param name="base" value="flash/RESQC001" /><embed src="flash/RESQC001/RESQC001.swf?fs_path=flash/RESQC001" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			} else if(projectSelection == "PREFAB01") {
				$('#slideShowArea').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="560" height="420" id="tech" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/PREFAB01/PREFAB01.swf" /><param name="quality" value="high" /><param name="base" value="flash/PREFAB01" /><embed src="flash/PREFAB01/PREFAB01.swf?fs_path=flash/PREFAB01" quality="high" width="560" height="420" name="tech" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
			}
		}
		self.location.href = '#slideShowAreaAnchor';
	});

	// Adding options to the combo boxes of either By Projects or By Location, all based on the language code.
	if(languageCode != '') {
		$('#projectSelection').append("<option value='PREFAB01'>Murs pr&eacute;usin&eacute;s / Prefab walls (Quebec Canada)</option>");
		$('#locationSelection').append("<option value='MISCFR001'>Projets de France</option>");
		$('#locationSelection').append("<option value='RUSSIA'>Projets de Russie / Russia Projects</option>");
		$('#projectSelection').append("<option value='RESQC001'>Projet R&eacute;sidentiel (Qu&eacute;bec Canada)</option>");
		$('#projectSelection').append("<option value='ETS'>R&eacute;sidences de l'&Eacute;cole de Technologie Sup&eacute;rieure (&Eacute;TS) (Montr&eacute;al, Qu&eacute;bec Canada)</option>");
		$('#projectSelection').append("<option value='CDR3EX'>Triplex (Montr&eacute;al, Qu&eacute;bec Canada)</option>");
	}

};

validateMandatory = function(parentId, mandatoryMessage) {
	var isValid = true;

	$('.mandatory',parentId).each(function() {
	    var currentType = $(this).attr('type');
	    if(currentType == 'text' || currentType == 'textarea') {
			var currentValue = $(this).attr('value');
			var parent = $(this).parent();
			if(currentValue == null || currentValue == '') {
				isValid = false;

				$('span.validateMessage',parent).html(mandatoryMessage);
			} else {
				$('span.validateMessage',parent).html("");
			}
	    } else if (currentType == 'radio') {
	        // we must consider all radio button that might be part of the same group as the current one (same name)
	        var radioName = $(this).attr("name");
	        var isOneChecked = false;
	        var parent = $(this).parent();
	        $('input',$(this).parent()).each(function() {
	            if($(this).attr("checked") == true) {
	                isOneChecked = true;
	            }
	        });
	        if(isOneChecked == false) {
				isValid = false;
				$('span.validateMessage',parent).html(mandatoryMessage);
			} else {
				$('span.validateMessage',parent).html("");
			}
	    }
	});

	return isValid;
};

buildEmail = function(parentId, mandatoryMessage) {
	var isValid = validateMandatory(parentId, mandatoryMessage);
	if(isValid == true) {
		// let's build a message for the email and put it as a hidden input into the parentId (most probably a form !)
		var message = $(parentId).serialize();

		$(parentId).append("<input type='hidden' name='message' value=" + message);
	}
	return isValid;
};

validateInputs = function(parentId, mandatoryMessage, errorMessage) {
	var isValid = true;

	isValid = validateMandatory(parentId, mandatoryMessage);

	if(isValid == true) {
		// let's check for email input
		$('.email',parentId).each(function() {
			var currentValue = $(this).attr("value");
			var parent = $(this).parent();
			if(currentValue.indexOf("@") == -1 || currentValue.indexOf(".") == -1) {
				isValid = false;
				$('span.validateMessage',parent).html(errorMessage);
			}
		});
	}

	return isValid;
};


initContactTabs = function (languageCode) {

	$('#headOfficeLink').click(function(){
		$("#headOfficeAreaWait").show();

		// let's toggle the tabs
		$('#headOfficeTab').attr("class","activeTab first_activeTab");
		$('#headOfficeLink').attr("class","tabLink");
		$('#contactTab1').attr("src","images/active_tab.jpg");

		$('#manufacturerTab').attr("class","inactiveTab");
		$('#manufacturerLink').attr("class","inactiveTabLink");
		$('#contactTab2').attr("src","images/inactive_tab.jpg");

		$('#distributorTab').attr("class","inactiveTab");
		$('#distributorLink').attr("class","inactiveTabLink");
		$('#contactTab3').attr("src","images/inactive_tab.jpg");

		$('#installerTab').attr("class","inactiveTab");
		$('#installerLink').attr("class","inactiveTabLink");
		$('#contactTab4').attr("src","images/last_inactive_tab.jpg");

		getHtmlServiceWait("#mainBodyContactUsArea","content/contact-" + languageCode + ".php","#headOfficeAreaWait", null);
	});
	$('#manufacturerLink').click(function(){
		$("#manufacturerAreaWait").show();

		// let's toggle the tabs
		$('#headOfficeTab').attr("class","inactiveTab first_inactiveTab");
		$('#headOfficeLink').attr("class","inactiveTabLink");
		$('#contactTab1').attr("src","images/inactive_tab.jpg");

		$('#manufacturerTab').attr("class","activeTab");
		$('#manufacturerLink').attr("class","tabLink");
		$('#contactTab2').attr("src","images/active_tab.jpg");

		$('#distributorTab').attr("class","inactiveTab");
		$('#distributorLink').attr("class","inactiveTabLink");
		$('#contactTab3').attr("src","images/inactive_tab.jpg");

		$('#installerTab').attr("class","inactiveTab");
		$('#installerLink').attr("class","inactiveTabLink");
		$('#contactTab4').attr("src","images/last_inactive_tab.jpg");
		getHtmlServiceWait("#mainBodyContactUsArea","content/manufacturer-" + languageCode + ".php","#manufacturerAreaWait", null);

	});
	$('#distributorLink').click(function(){
		$("#distributorAreaWait").show();

		// let's toggle the tabs
		$('#headOfficeTab').attr("class","inactiveTab first_inactiveTab");
		$('#headOfficeLink').attr("class","inactiveTabLink");
		$('#contactTab1').attr("src","images/inactive_tab.jpg");

		$('#manufacturerTab').attr("class","inactiveTab");
		$('#manufacturerLink').attr("class","inactiveTabLink");
		$('#contactTab2').attr("src","images/inactive_tab.jpg");

		$('#distributorTab').attr("class","activeTab");
		$('#distributorLink').attr("class","tabLink");
		$('#contactTab3').attr("src","images/active_tab.jpg");

		$('#installerTab').attr("class","inactiveTab");
		$('#installerLink').attr("class","inactiveTabLink");
		$('#contactTab4').attr("src","images/last_inactive_tab.jpg");

		getHtmlServiceWait("#mainBodyContactUsArea","fr/content/distributor_installer-" + languageCode + ".php","#distributorAreaWait", null);
	});
	$('#installerLink').click(function(){
		$("#installerAreaWait").show();

		// let's toggle the tabs
		$('#headOfficeTab').attr("class","inactiveTab first_inactiveTab");
		$('#headOfficeLink').attr("class","inactiveTabLink");
		$('#contactTab1').attr("src","images/inactive_tab.jpg");

		$('#manufacturerTab').attr("class","inactiveTab");
		$('#manufacturerLink').attr("class","inactiveTabLink");
		$('#contactTab2').attr("src","images/inactive_tab.jpg");

		$('#distributorTab').attr("class","inactiveTab");
		$('#distributorLink').attr("class","inactiveTabLink");
		$('#contactTab3').attr("src","images/inactive_tab.jpg");

		$('#installerTab').attr("class","activeTab");
		$('#installerLink').attr("class","tabLink");
		$('#contactTab4').attr("src","images/last_active_tab.jpg");

		getHtmlServiceWait("#mainBodyContactUsArea","fr/content/installer_agent-" + languageCode + ".php","#installerAreaWait", null);
	});
};

initInstallerAgentMorePages = function (languageCode)  {

	$('#bsgProductsLlcMore').click(function() {
		callModal('#basicModalContent');
		getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=66","#basicModalContentWait",null);
	});

	$('#icfBuildingSolutionSALlcMore').click(function() {

		callModal('#basicModalContent');
		getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=67","#basicModalContentWait",null);
	});

	$('#ecoConstructionEnergiaMore').click(function() {

		callModal('#basicModalContent');
		if(languageCode == 'fr') {
			getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=83","#basicModalContentWait",null);
		} else {
			getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=85","#basicModalContentWait",null);
		}
	});


};

initDistributorMorePages = function (languageCode)  {


	$('#constructionsErnestVeilleuxFilsMore').click(function() {

		callModal('#basicModalContent');
		getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=84","#basicModalContentWait",null);
	});

};


initManufacturerMorePages = function (languageCode)  {

	$('#troisRivieresPlantMore').click(function() {
		callModal('#basicModalContent');
		getHtmlServiceWait("#scrollableContactMoreArea","spip/spip.php?page=showArticle&id_article=68","#basicModalContentWait",null);
	});

};

callModal = function(modalAreaToCall) {
	$('#polycreteFlash').hide();	// hide it cause the modal will show itself under the flash.

	$(modalAreaToCall).modal({onClose: function (dialog) {
	  dialog.data.fadeOut('slow', function () {
		dialog.container.slideUp('slow', function () {
		  dialog.overlay.fadeOut('slow', function () {
			$.modal.close(); // must call this!
			$('#polycreteFlash').show();	// show back the polycrete flash object.
		  });
		});
	  });
	}});
};


initTechnicalLibTree = function() {

	// BEGIN : SECTION FOR THE CAD DESIGN TREE LIST

	$('#rootClick').click(function() {
		if($('#rootClick').attr("src").indexOf("down") == -1) {
			$('#rootClick').attr("src","../images/tree_root_down.gif");
			// showing all other main nodes below
			$('.treeMainNodeRow').each(function() {
				$(this).show();
			});
		} else {
			$('#rootClick').attr("src","../images/tree_root_up.gif");
			// hidding all other nodes below
			$('.treeNodeRow').each(function() {
				$(this).hide();
			});
			$('.treeMainNodeRow').each(function() {
				$(this).hide();
			});

			$('#treeFXNode').attr("src","../images/tree_node_up.gif");
			$('#treeBBNode').attr("src","../images/tree_node_up.gif");
		}
	});

	$('#treeFXNode').click(function() {
		if($('#treeFXNode').attr("src").indexOf("down") == -1) {
			$('#treeFXNode').attr("src","../images/tree_node_down.gif");
			// showing all other main nodes below
			$('.flex850CAD').each(function() {
				$(this).show();
			});
		} else {
			$('#treeFXNode').attr("src","../images/tree_node_up.gif");
			// hidding all other nodes below
			$('.flex850CAD').each(function() {
				$(this).hide();
			});
		}
	});

	$('#treeBBNode').click(function() {
		if($('#treeBBNode').attr("src").indexOf("down") == -1) {
			$('#treeBBNode').attr("src","../images/tree_node_down.gif");
			// showing all other main nodes below
			$('.bbCAD').each(function() {
				$(this).show();
			});
		} else {
			$('#treeBBNode').attr("src","../images/tree_node_up.gif");
			// hidding all other nodes below
			$('.bbCAD').each(function() {
				$(this).hide();
			});
		}
	});

	// END: SECTION FOR THE CAD DESIGN TREE LIST


	$('img.rootLevelNode').each(function() {
		$(this).click(function() {
			if($(this).attr("src").indexOf("down") == -1) {
				$(this).attr("src","../images/tree_root_down.gif");
				$('tr.leafNode', $(this).parent().parent().parent()).each(function() {
					$(this).show();
				});
			} else {
				$(this).attr("src","../images/tree_root_up.gif");
				$('tr.leafNode', $(this).parent().parent().parent()).each(function() {
					$(this).hide();
				});
			}
		});
	});


};
