/**
 * @fileoverview This file configure site attribute like the user site Id
 * must be import after all javascript file, and configure some site behavior like pub or statistic
 */
//prod site_userid = 12053 et media of the day groupd id = 525
//preprod site_userid = 676 et media of the day groupd id = 150
CW_Action_Site.SITE_USER_ID = 12053;
CW_Action_Site.MEDIA_OF_THE_DAY_GROUP_ID = 525;
ComwebPager_allmember.nbElement = 16;
ComwebPager_categorylist.nbElement = 15;
ComwebPager_media_comments.nbElement = 6;
ComwebPager_medialist.nbElement = 15;
ComwebPager_mycount_video.nbElement = 15;
ComwebPager_user_comments.nbElement = 6;

//determine the "abus" which are displayed for a media
CW_Action_Site.ArrayAbusId_Media = new Array();
CW_Action_Site.ArrayAbusId_Media.push(1);
CW_Action_Site.ArrayAbusId_Media.push(2);
CW_Action_Site.ArrayAbusId_Media.push(3);
CW_Action_Site.ArrayAbusId_Media.push(4);

//determine the "abus" which are displayed for a comment
CW_Action_Site.ArrayAbusId_Comment = new Array();
CW_Action_Site.ArrayAbusId_Comment.push(2);
CW_Action_Site.ArrayAbusId_Comment.push(3);
CW_Action_Site.ArrayAbusId_Comment.push(4);

//indicates that the random filter is not active
CW_Filter.Filter_ORDER_RANDOM_active = false;
CW_Filter.ORDER_MOSTNOTATION = 8;

var MainElementsLoader = {
	/**
	 * @return {void}
	 */
	loadMainElement:function(){
		CW_comwebAjax.loadHtml("html/login/register.html","registerContainer");
		CW_Action_User.loginCheckAutoConnect("loginCheck","registerHome");
		//

		//Load the search box
		CW_comwebAjax.loadHtml("html/search/search1.html","menuSearch");
		//load the menu
		initializeMenuMainSub();

	},

	/**
	 * The following function allow to display the 6 most viewed video (of the actual day) in the HtmlDivElement
	 * identified by "pubRightContainerR2" where html/home/homeListingViewed.html  html template was loaded.
	 * In order to realise this, this function used CW_comwebAjax to load the html template, and
	 * CW_Action_Media.displayMediaList with _displayMostViewedVideoCallback callback to retrieve
	 * and display the 6 most viewed videos
	 *
	 * @see CW_comwebAjax#loadHtml
	 * @see CW_Action_Media#displayMediaList
	 * @see #_displayMostViewedVideoCallback
	 * @return {void}
	 */
	 _displayMostViewedVideo:function(){
	 	var callback = "MainElementsLoader._displayMostViewedVideoCallback";
	 	CW_Action_Media.displayMediaList(callback,6,CW_Action_Media.Media_TYPE_VIDEO, CW_Filter.SINCE_ALL, 7, false);
	 },
	/**
	 * The following function is the _displayMostViewedVideo callback. So the comwebBean is the result of the
	 * CW_Action_Media.displayMediaList action and in this case contain the 6 most viewed videos (of the actual day). after
	 * has retrieve the videos , categories are videos in the htmldivelement identified by
	 * "mostViewedListHomeContent" with loadBrick_media_mediaElement3 function
	 *
	 * @see GLOBALS#loadBrick_media_mediaElement3
	 * @param {ComwebBean} comwebBean xml bean result of CW_Action_Media.displayMediaList launched by _displayMostViewedVideo
	 * @param {RequestResume} requestResume request resume of CW_Action_Media.displayMediaList launched by _displayMostViewedVideo
	 * @return {void}
	 */
	 _displayMostViewedVideoCallback:function(comwebBean, requestResume){
		var uid=0;
		var href="";
		var listingParam = requestResume.queryString;
		if(comwebBean!=null){
			document.getElementById("mostViewedListHomeContent").innerHTML = "";
			var comwebBeanMediaArray = comwebBean.getSpecificChildren("list")[0].getSpecificChildren("media");
			if(comwebBeanMediaArray.length>0){
				for(var i=0;i<comwebBeanMediaArray.length;i++){
					var comwebBeanMedia = comwebBeanMediaArray[i];
					uid = comwebBeanMedia.getUid();
					href =  "goToMediaPlay('action=alone&listing.page=1',"+uid+");";
					loadBrick_media_mediaElement3(comwebBeanMedia,"mostViewedListHomeContent",href,true);
				}
			}
		}
	 }
}
/**
 * set the function invoked by CW_config, allow to load main element which are not in the ajax page ""masterPage"
 * @return {void}
 */
function loadMainElements(){
	MainElementsLoader.loadMainElement();
}
/**
 * set the function invoked by CW_config, allow to laod the home page
 * @return {void}
 */
function loadMainPage(){
	CW_AjaxPage_Manager.displayPage("home");
}


/**
 * The following object allow to manage the site pub loading or reloading
 */
var Pub_EveryDay = {
	/**
	 * Stores the iframe window which must contained the ad1 pub
	 * @type Window
	 */
	iframeAd1:null,
	/**
	 * Stores the path source of the ad1 pub iframe
	 * @type String
	 */
	iframeAd1Src : "./pub/ad1.html",
	/**
	 * Stores the iframe window which must contained the ad2 pub
	 * @type Window
	 */
	iframeAd2:null,
	/**
	 * Stores the path source of the ad2 pub iframe
	 * @type String
	 */
	iframeAd2Src : "./pub/ad2.html",
	/**
	 * Stores the iframe window which must contained the ad3 pub
	 * @type Window
	 */
	iframeAd3:null,
	/**
	 * Stores the path source of the ad3 pub iframe
	 * @type String
	 */
	iframeAd3Src : "./pub/ad3.html",
	/**
	 * Stores the iframe window which must contained the ad4 pub
	 * @type Window
	 */
	iframeAd4:null,
	/**
	 * Stores the path source of the ad4 pub iframe
	 * @type String
	 */
	iframeAd4Src : "./pub/ad4.html",
	/**
	 * Stores the iframe window which must contained the ad5 pub
	 * @type Window
	 */
	iframeAd5:null,
	/**
	 * Stores the path source of the ad5 pub iframe
	 * @type String
	 */
	iframeAd5Src : "./pub/ad5.html",
	/**
	 * Stores the iframe window which must contained the ad6 pub
	 * @type Window
	 */
	iframeAd6:null,
	/**
	 * Stores the path source of the ad6 pub iframe
	 * @type String
	 */
	iframeAd6Src : "./pub/ad6.html",
	/**
	 * Stores the iframe window which must contained the ad7 pub
	 * @type Window
	 */
	iframeAd7:null,
	/**
	 * Stores the path source of the ad1 pub iframe
	 * @type String
	 */
	iframeAd7Src : "./pub/ad7.html",
	/**
	 *@return {void}
	 */
	reload:function(){
		//alert("reload Pub");

		if (this.iframeAd1 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad1"){
		 			this.iframeAd1 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd1 != null){
			this.iframeAd1.location.replace(this.iframeAd1Src + "?cache=" + (new Date()).getTime());
		}
		//reload ad2
		if (this.iframeAd2 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad2"){
		 			this.iframeAd2 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd2 != null){
			this.iframeAd2.location.replace(this.iframeAd2Src + "?cache=" + (new Date()).getTime());
		}
		//reload ad3
		if (this.iframeAd3 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad3"){
		 			this.iframeAd3 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd3 != null){
			this.iframeAd3.location.replace(this.iframeAd3Src + "?cache=" + (new Date()).getTime());
		}
		//reload ad4
		if (this.iframeAd4 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad4"){
		 			this.iframeAd4 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd4 != null){
			this.iframeAd4.location.replace(this.iframeAd4Src + "?cache=" + (new Date()).getTime());
		}
		/*
		//reload ad5
		if (this.iframeAd4 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad5"){
		 			this.iframeAd5 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd5 != null){
			this.iframeAd5.location.replace(this.iframeAd5Src + "?cache=" + (new Date()).getTime());
		}
		* */
		//reload ad6
		if (this.iframeAd6 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad6"){
		 			this.iframeAd6 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd6 != null){
			this.iframeAd6.location.replace(this.iframeAd6Src + "?cache=" + (new Date()).getTime());
		}

		//reload ad7
		if (this.iframeAd7 == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "ad7"){
		 			this.iframeAd7 = parent.frames[i];
		 		}
			}
		}
		if (this.iframeAd7 != null){
			this.iframeAd7.location.replace(this.iframeAd7Src + "?cache=" + (new Date()).getTime());
		}
	}
}

/**
 * The following object allow to manage the everyday stats loading or reloading
 */
var stats_EveryDay = {
 	/** Stores the iframe window which must contained the stats_tns
	 * @type Window
	 */	
	iframeAdStats:null,
	/**
	 * Stores the path source of the stats_tns iframe
	 * @type String
	 */	
	iframeAdStatsSrc : "./stats/tmv11.html",
	//reload stats_tns
	stats_reload:function(statId,menuId,pageName) {
		//reload iframeAdStats
		//alert("stats_reload 1");
		//alert(statId+" et "+menuId+" et "+pageName);
		if (this.iframeAdStats == null){
			for(var i=0; i < parent.frames.length; i++){
		 		if (parent.frames[i].name == "stats_tns"){
		 			this.iframeAdStats = parent.frames[i];
					//alert("stats_reload 2");
		 		}
			}
		}
		if (this.iframeAdStats != null){
			this.iframeAdStats.location.replace(this.iframeAdStatsSrc +"?stat_id="+ statId +"&pageName="+ pageName);
			//this.iframeAdStats.location.replace(this.iframeAdStatsSrc +"?stat_id="+ statId +"&menuId="+ menuId +"&pageName="+ pageName);			
			//alert("stats_reload 3");
		}
		//alert("stats_reload OK");		
	}
}
/**
 * The following object allow to manage the site stat
 * note that you must import the google unrchin.js file and set _uacct variable at the top of the document like this:
 * <br/>
 * <p>
 * 		&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript">&lt;/script&gt;
 * 		&lt;script type="text/javascript"&gt;
 * 			_uacct = "UA-2382180-1";
 * 		&lt;/script&gt;
 * <p/>
 **/
 
var StatObject = {
	/**
	 * the Following method allow to have a new statistic register and refresh the site pub
	 * @param {String} string
	 * @return {void}
	 */
	addStat:function(string){
		if (string != "player"){
			/*CW_logger.log(CW_logger.LEVEL_NOTICE,"CW_AjaxPage_Manager.addStat:" + string);
			if(document.getElementById('redSheriff'))
				document.getElementById('redSheriff').parentNode.removeChild(document.getElementById('redSheriff'));
			var rand = Math.ceil(Math.random() * 100000000);
			var img = document.createElement("img");
			img.setAttribute("width", "1");
			img.setAttribute("height", "1");
			img.setAttribute("alt", "Red Sheriff");
			img.setAttribute("id", "redSheriff");
			img.src = "http://&cg=0&si=http://" + string + "&rnd=" + rand;
			document.body.appendChild(img);
			// Google Analytics
			urchinTracker('/' + string);*/
			Pub_EveryDay.reload();
		}
	}
	,
	/**
	 * the Following method allow to have a new statistic register 
	 * @param {String} statId
	 * @param {String} menuId
	 * @param {String} pageName
	 * @return {void}
	 */
	addStat2:function(statId,menuId,pageName){
		if (statId != "player"){
			stats_EveryDay.stats_reload(statId,menuId,pageName);
		}
	}
}

/**
 * @return {void}
 */
CW_AjaxPage_Manager.displayUserNotConnected = function(){
	showDialog_connected_error();
}

CW_AjaxPage_Manager.currentPageIsSearch = false;

CW_AjaxPage_Manager.currentMenuId = null;

/**
 * @param {CW_AjaxPage_MODEL} ajaxPage
 * @return {void}
 */
CW_AjaxPage_Manager.refreshElement = function (ajaxPage){
	var pageName = ajaxPage.getPageName();

	if (pageName == "player"){
		return;
	}

	var statId = ajaxPage.getStatId();
	var menuId = ajaxPage.getMenuId();

	//process the stat identifier
	StatObject.addStat(statId);
	
	//process the stat identifier	
	StatObject.addStat2(statId,menuId,pageName);
	
	//process the menu identifier
	if (this.currentMenuId != menuId){
		this.currentMenuId = menuId;
		try{
			var charSeparator = menuId.indexOf("_");
			var firstLevel = menuId.substring(0,charSeparator);
			var secondLevel = menuId.substring(charSeparator + 1);

			if (menuId=="X_X"){
				menuSubMyScroon.style.display = "none";
				menuSubMember.style.display = "none";
				tabOngletMenuGen.deselectTab();
			}else{
				switch (firstLevel) {
					case "0":
						menuSubMyScroon.style.display = "block";
						menuSubMember.style.display = "none";
						tabOngletMenuGen.selectTab(0,false);
						if (secondLevel != "X"){
							tabOngletMenuSubMyScroon.selectTab(parseInt(secondLevel),false);
						}
						break;
					case "3":
						menuSubMyScroon.style.display = "none";
						menuSubMember.style.display = "block";
						tabOngletMenuGen.selectTab(3,false);
						if (secondLevel != "X"){
							tabOngletMenuSubMember.selectTab(parseInt(secondLevel),false);
						}
						break;
					default:
						if (firstLevel!="X"){
							menuSubMyScroon.style.display = "none";
							menuSubMember.style.display = "none";

							tabOngletMenuGen.selectTab(parseInt(firstLevel),false);
						}
						break;
				}
			}
		}catch(e){alert(e)}
	}
	//set the search backlink
	if (pageName.indexOf("search") == 0){
		this.currentPageIsSearch = true;
	}else{
		if (this.currentPageIsSearch){
			var curHash = window.location.hash;
			if (curHash!= null && curHash!=""){
				if (curHash.indexOf("#")==0){
					curHash = curHash.substr(1);
				}
			}
			document.getElementById("searchBackLink").innerHTML = "<a href='javascript:goToLastSearch(\""+curHash+"\" )' >"+ CW_internationalization.getValue("search.last")+"</a>"
		}
		this.currentPageIsSearch = false;
	}

	var refreshTag = "video";
	if (statId == "member"){
		refreshTag = "member";
	}else if (statId == "photo"){
		refreshTag = "photo";
	}

	loadBrick_tags_tagsHome(refreshTag);
}

/**
 * @ignore
 * Override the PlayerFlash.flashPlayMediaDisplayEvent in order to register the statistic
 * of the current played media
 * @param {ComwebBean_media} comwebBeanMedia  media which is playing
 * @param {RequestResume} requestResume
 * @return {void}
 */
PlayerFlash.flashPlayMediaDisplayEvent = function(comwebBeanMedia,requestResume){
	var categoryId = comwebBeanMedia.getCategoryId();
	StatObject.addStat("player:" + categoryId);


	menuSubMyScroon.style.display = "none";
	menuSubMember.style.display = "none";
	switch (comwebBeanMedia.getMediaType()) {
		case CW_Action_Media.Media_TYPE_IMAGE:tabOngletMenuGen.selectTab(2,false);break;
		default:tabOngletMenuGen.selectTab(1,false);break;
	}



}


CW_Action_User.validLogin= function (callback,loginForm) {
		if (CW_FormValidator.check(loginForm)){
			//set the action value
			var actionElement = CW_util.getFormElement(loginForm,"action");
			if (actionElement == null){
				loginForm.appendChild(CW_util.createElementInputHidden("action",this.ACTION_USER_LOGIN));
			}else{
				actionElement.value = this.ACTION_USER_LOGIN;
			}
			loginForm.appendChild(CW_util.createElementInputHidden("user.fullview",1));
			var hashTableStorage = new HashTable();
			hashTableStorage.put("originalCallback",callback);
			hashTableStorage.put("formName",loginForm.name);
			CW_comwebAjax.sendRequestForm("/comweb/ssoTele2", "CW_Action_User._loginCheck" , null, 'login', loginForm,hashTableStorage);
		}
}


CW_Action_User.loginCheckAutoConnect =function(callback,formRegisterName){
	var hashTableStorage = new HashTable();
	hashTableStorage.put("originalCallback",callback);
	hashTableStorage.put("formName",formRegisterName);

	var parameters = new Array();
	parameters.push('action=chk.sess');
	parameters.push('user.fullview=1');
	parameters.push('site.uid=' + CW_config.serviceId);
	parameters.push('lng=' +  CW_config.langId );

	var queryString = new String(parameters.join('&'));
	var url = CW_Action_User.SERVLET_URI;
	var strMD5 = MD5(queryString);
	if (CW_config.isProd && url.indexOf("/comweb/") == 0 ){
		url = url.replace("/comweb/","/comweb/MD5_" +strMD5+ "/");
	}
	var transaction = CW_comwebAjax.ajaxTransactionManager.getTransaction(strMD5);
	transaction.registerMD5(strMD5);
	var	requestResume = new RequestResume(url,"CW_Action_User._loginCheck" ,queryString,"nohistoryName",transaction.identifier,strMD5,hashTableStorage);
	var options = {	asynchronous:false,method:'post',encoding:'UTF-8',requestHeaders: ['X-Prototype-Version', Prototype.Version ],postBody: CW_util.encodeUTF8(queryString),onComplete: CW_comwebAjax._onRequestComplete.bind(options,requestResume,(new Date()).getTime())	};
	new Ajax.Request(url, options);
}

