if(!CORP) { var CORP = {}; }

//création du bloc News
CORP.blocNewsSet = {
	count: 0,
	getJSONFragment: function(json, type) { 
		return (typeof json == 'object' && json[type] && typeof json[type] != 'undefined') ? json[type] : null;
	},
	getRandomNumber: function(min, max) {
		return Math.floor(Math.random() * max + min);
	},
	makeTemplateLink: function(datas) {
		var tpl = '<a href="' + datas.url + '" target="_top"';
		if (datas.title) {
			tpl += ' title="' + datas.title + '"';
		}
		if (datas.xiti) {
			tpl += ' onclick="xt_med(' + datas.xiti + ');"';
		}
		if (datas.cls) {
			tpl += ' class="' + datas.cls + '"';
		}
		tpl += '>' + datas.content + '</a>';
		return tpl;
	},
	makeTemplateImage: function(datas) {
		var tpl = '<img src="' + datas.src + '" alt="';
		if (datas.alt) {
			tpl += datas.alt;
		}
		tpl += '" />';
		return tpl;
	},
	//création du DOM avec les données JSON
	compileTemplate: function(tplType, datas) {
		if ($('tpl_'+tplType)) {
			var tpl = '', tmp, lg, xiti;
			//bloc offers-noh
			if (tplType == 'offers-partners') {
				lg = $('tpl_offers-partners').className.replace('lang:', '');
				xiti = "'C'," + lg + ",'corporate::navigation::homepage_specials','N'";
				tpl = '<div class="partners-noh">';
				tpl = '<div class="contenu-noh">';
				tpl += '<h2>'+$$('#tpl_offers-partners h2')[0].innerHTML+'</h2>';
				if (datas.titre) {
					tpl += '<p class="intro">';
					if (datas.lien1) {
						tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien1, 'content': datas.titre, 'xiti': xiti});
					} else {
						tpl += datas.titre;
					}
					tpl += '</p>';
				}
				if (datas.texte) {
					tpl += '<p>';
					if (datas.lien1) {
						tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien1, 'content': datas.texte, 'xiti': xiti});
					} else {
						tpl += datas.texte;
					}
					tpl += '</p>';
				}
				
				if (datas.lien2) {
					tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien2, 'content': $$('#tpl_offers-partners a.more')[0].innerHTML, 'xiti': xiti, 'cls':'more'});
				}
				tpl += '</div></div>';
			//bloc open-noh
			} else if (tplType == 'opens') {
				lg = $('tpl_opens').className.replace('lang:', '');
				xiti = "'C'," + lg + ",'corporate::navigation::homepage_network','N'";
				tpl = '<div class="network-noh">';
				if (datas.image) {
					tpl+='<div class="visuel-noh">';
					tmp = CORP.blocNewsSet.makeTemplateImage({'src': datas.image, 'alt': datas.titre});
					if (datas.lien1) {
						tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien1, 'content': tmp, 'xiti': xiti, 'title': datas.titre});
					} else {
						tpl += tmp;
					}
					tpl += '</div>';				
				}				
				tpl += '<div class="contenu-noh">';
				tpl += '<h2>'+$$('#tpl_opens h2')[0].innerHTML+'</h2>';
				if (datas.mention) {
					tpl += '<p class="intro">' + datas.mention + '</p>';
				}
				if (datas.date) {
					tpl += '<p>' + datas.date + '</p>';
				}			
				if (datas.titre) {
					tpl += '<p>';
					/*if (datas.lien1) {
						tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien1, 'content': datas.titre, 'xiti': xiti});
					} else {*/
						tpl += datas.titre;
					//}
					tpl += '</p>';
				}
				if (datas.lien1) {
					tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.lien1, 'content': $$('#tpl_opens a.more')[0].innerHTML, 'xiti': xiti, 'cls':'more'});
				}
				tpl += '</div></div>';
			}
			//bloc offres-push
			else if (tplType == 'offres-push') {
				lg = $('tpl_offres-push').className.replace('lang:', '');
				xiti1 = "'C'," + lg + ",'corporate::navigation::homepage_push1','N'";
				xiti2 = "'C'," + lg + ",'corporate::navigation::homepage_push2','N'";
				if (datas.titre) {
					tpl += '<h2>' + datas.titre + '</h2>';
				}			
				if (datas.image) {
					tpl += CORP.blocNewsSet.makeTemplateImage({'src': datas.image, 'alt': datas.titre});
				}
				if (datas.texte) {
					tpl += '<p>' + datas.texte + '</p>';
				}
				if (datas.url_lien1) {
					tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.url_lien1, 'content': datas.libelle_lien1, 'xiti': xiti1, 'cls':'more'});
				}
				if (datas.url_lien2) {
					tpl += CORP.blocNewsSet.makeTemplateLink({'url': datas.url_lien2, 'content': datas.libelle_lien2, 'xiti': xiti2, 'cls':'more'});
				}	
				
			}
		}
		return tpl;
	},
	//gestion de l'affichage des blocs en fonction 
	manageView:function(){
		if($('open-noh') && $('offers-noh') && $('open-noh').childElements().length < 1 && $('offers-noh').childElements().length < 1){
			$('sdan-noh').setStyle({display:'none'});
		} 
		if($('open-noh') && $('open-noh').childElements().length < 1){
			$('offers-noh').setStyle({background:'none'});
		}
		if($('offers-noh') && $('offers-noh').childElements().length < 1){
			$('offers-noh').setStyle({display:'none'});
		}
		
		if($('offres-push') && $('offres-push').childElements().length < 1){
			$('homeDiscoverBrand').setStyle({display:'none'});	
		}

		if (auto_height_iframe) {
			auto_height_iframe.defer();
		}
	},
	//chargement du JSON et insertion du DOM dans le template
	addTemplateToPage: function(options) {
		document.observe("dom:loaded",function() {
			CORP.blocNewsSet.count = CORP.blocNewsSet.count + 1;
			var dom = $(options.insertTo);
			if (dom) {
				new Ajax.Request(options.url + '?time=' + new Date().getTime(), {
					method: 'get',
					onSuccess: function(request) {
						var json = request.responseText;
						if (typeof json == 'string' && json != '') {
							json = eval('(' + json + ')');
						}
						if (typeof json == 'object') {
							var fragment;
							if (options.code_pays) {
								fragment = CORP.blocNewsSet.getJSONFragment(json, options.code_pays);
								if (!fragment){
								fragment = CORP.blocNewsSet.getJSONFragment(json, 'DEF'); 
								}
							} else {
								fragment = CORP.blocNewsSet.getJSONFragment(json, 'DEF');
							}
							fragment = CORP.blocNewsSet.getJSONFragment(fragment || json, options.type);
							if (fragment.length) {
								options.number = options.number || 1;
								if (fragment.length < options.number) {
									options.number = fragment.length;
								}
								for (i = 0; i < options.number; i++){
									var n = CORP.blocNewsSet.getRandomNumber(0, fragment.length);
									var tpl = CORP.blocNewsSet.compileTemplate(options.type, fragment[n]);								
									fragment = fragment.without(fragment[n]);
									dom.insert(tpl);
									if(options.insertTo == 'offers-noh' || options.insertTo == 'open-noh'){
										$$('#'+options.insertTo+' div.visuel-noh').each(function(e) {
											e.next().addClassName('withVisuel');
										});
									}
								}
							}
						}
						if(CORP.blocNewsSet.count = 3){
							CORP.blocNewsSet.manageView.defer();
						}
					}
				});
			}
		});
	}
}