j$(document).ready(function()
{ 
	j$(".year").text( (new Date).getFullYear() );
	
	if (get_user_agent() == true || !FlashDetect.installed)
	{
	  j$('a.btn_expand').css('display','block');
	  j$('a.btn_replay').css('display','none');
	}
	 
	j$("#selectLang").selectBox();
   
    j$("div.carouselClients").carousel({ 
         loop: true,
         autoSlide: true,
         autoSlideInterval: 7000,
         effect: "fade",
         direction: "vertical"
      });
      
     j$("div.carouselClients ul").css("display", "block");
});


function writeFlash(swfName, width, height, wmode, flashvars) 
{
	AC_FL_RunContent(
		'id', swfName,
		'name',swfName,
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
		'width', width,
		'height', height,
		'src', swfName,
		'quality', 'high',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', wmode,
		'devicefont', 'false',
		'bgcolor', '#ffffff',
		'allowScriptAccess','sameDomain',
		'movie', 'swf/'+swfName,
		'flashvars', flashvars,
		'salign', ''
		); //end AC code
};

//Function called by the flash animation to open thickbox
function ShowFeature(id)
{
	var pseudoLinkId = "feature" + id
	var el = document.getElementById(pseudoLinkId);
	var loader = document.getElementById("flashLoader");
	loader.innerHTML = "";
	
	if(el != null) {
		var strOverlay = "<div onclick=\"closeFeature();\" class=\"div_overlay\" style=\"display:block;\" id=\"divOverlay\"></div><div class=\"overlay_msg_box\" style=\"display:block;\" id=\"msgBox\"></div>";
		var container = document.createElement("div");
		container.innerHTML = strOverlay;
		loader.appendChild(container);

		var pagesize = formatFlashSize(985, 765);
		var imageWidth = pagesize[0];
		var imageHeight = pagesize[1];
	
		var msgbox = document.getElementById("msgBox");
		if(msgbox != null) {
			msgbox.style.height = imageHeight+"px";
			msgbox.innerHTML = "<a onclick=\"closeFeature();\" class=\"btn_close\" href=\"javascript:void(0);\"></a><iframe id='frmFlash' src='so_flash.aspx?configUrl="+el.href+"&w="+imageWidth+"&h="+imageHeight+"' width='"+imageWidth+"' height='"+imageHeight+"' border='0' scrolling='no' style='margin:0; padding:0; border:0px none; ' frameborder='0'></iframe>";
			
			openOverLayPanel('divOverlay', 'msgBox', 1);
			positionFlash("msgBox", imageWidth, imageHeight);
//			var ifrm = document.getElementById("frmFlash");
//			if(ifrm != null)
//			{
//				if ((jQuery.browser.msie && jQuery.browser.version > 6 && jQuery.browser.version < 8))
//				ifrm.style.zoom = "100%";
//				//ifrm.src="so_flash.aspx?configUrl="+el.href+"&w="+imageWidth+"&h="+imageHeight;
//			}
		}
	}
}

//Formats the flash to fit in the window
function formatFlashSize (w, h)
{
	var pagesize = tb_getPageSize();
	var x = pagesize[0] - 20;
	var y = pagesize[1] - 20;

	var imageWidth = w;
	var imageHeight = h;
	if (imageWidth > x) {
		imageHeight = imageHeight * (x / imageWidth); 
		imageWidth = x; 
		if (imageHeight > y) { 
			imageWidth = imageWidth * (y / imageHeight); 
			imageHeight = y; 
		}
	} else if (imageHeight > y) { 
		imageWidth = imageWidth * (y / imageHeight); 
		imageHeight = y; 
		if (imageWidth > x) { 
			imageHeight = imageHeight * (x / imageWidth); 
			imageWidth = x;
		}
	}
	
	arrayPageSize = [Math.round(imageWidth), Math.round(imageHeight)];
	return arrayPageSize;
}

//Position the flash centered
function positionFlash(id, w, h) 
{
	j$("div.overlay_msg_box").css({marginLeft: '-' + parseInt((w / 2), 10)-5 + 'px', width: w + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6 
		j$("div.overlay_msg_box").css({marginTop: '-' + parseInt((h / 2), 10)-2 + 'px'});
	} else { // IE6
		var winInHt = document.documentElement.clientHeight;
		var scrollPos = document.documentElement.scrollTop;
		var free_height = ((winInHt - h)/ 2) + scrollPos;
		
		with ((document.all? document.all[id] : document.getElementById(id)).style) {
			position='absolute';
			top=free_height + 'px';
		}
	}
}

//Close the feature
function closeFeature()
{
	var parentWindowFlashLoader = parent.document.getElementById("flashLoader");
	if( parentWindowFlashLoader != null) {
		parentWindowFlashLoader.innerHTML = "";
	}
}

function showText(id)
{
	var obj = document.getElementById(id);
	if(obj != null) {
		obj.style.display = 'block';
	}
}
function hideText(id)
{
	var obj = document.getElementById(id);
	if(obj != null) {
		obj.style.display = 'none';
	}
}
function setObjAttr(objId, attr, val)
{
	var obj = document.getElementById(objId);
	if(obj != null) {
			eval("obj."+attr+"='"+val+"';")
	}
}

function revertImgState(imgId, hidId)
{
	var img = document.getElementById(imgId);
	var hid = document.getElementById(hidId);
	if(img != null && hid != null) {
		setObjAttr(imgId, 'src', hid.value);
	} else {
		setObjAttr(imgId, 'src', 'img/buy_map_inac.jpg');
	}
}

function simulateLinkClick(elementId) {
	var el = document.getElementById(elementId); 
	if(el != null) {
		try { eval(el.href.replace(/%20/g, ' ')); } catch(e1){} //FF
		try { var str = el.href.replace(/%20/g, ' ').replace(/%22/g, '\'');  eval(str); } catch(e2){} //Safari, IE 
		try { eval(el.click()); } catch(e3){} //IE
	}
}
/*
function writeHomeAnim(lang) {
	var animName = "home_anim_" + lang;
	var parms = "";
	var innovCookie = getCookie('innovAnim');
	if(innovCookie != null) {
		parms = "skipintro=true";
		document.write("<img src='swf/"+animName+".jpg' alt='' />");
	} else {
		setCookie('innovAnim', "1", 1);
		writeFlash(animName, 630, 420, 'opaque', parms);
	}
}
*/
function writeHomeAnim(lang) {
	
	var animName = "chargement";
	var param_lang = "&anim_lang=english";	
	switch (lang)
	{
	   case "en":
	   param_lang = "&anim_lang=english";
	   break;
	   case "fr":
	   param_lang = "&anim_lang=french";
	   break;
	   case "cn":
	   param_lang = "&anim_lang=chinese";
	   break;	   
	   case "jp":
	   param_lang = "&anim_lang=japanese";
	   break;
	}		
	var parms = "skipintro=false" + param_lang;
	var innovCookie = getCookie('innovAnim');
	if (get_user_agent() == true || !FlashDetect.installed)
	{
	   document.write("<img src='swf/PW_Accueil_en.jpg' alt='InnovMetric' width='630px' height='420px' />");
	}
	else
	{
	   if(innovCookie != null) {
		   parms = "skipintro=true" + param_lang;
		   writeFlash(animName, 630, 420, 'opaque', parms);
	   } 
	   else {
		   setCookie('innovAnim', "1", 1);
		   writeFlash(animName, 630, 420, 'opaque', parms);
	   }
	}
}

//est appelée au clic dans le flash de l'accueil v12
function popupOverlay(caller, flash_anim_lang)
{
	//flash_anim_lang vient de l'animation flash - qui est soumise dans ce fichier par les flashvars du writeFlash
	//on la renvoit aux flash loadés dans l'overlay
	
	//section cliquables du flash
	//référence dans le flash: de gauche à droite; 
	//hs_01 = Laser Scanners
	//hs_02 = Fringe-based
	//hs_03 = Arms & CMMs
	//hs_04 = Laser & Optical
	var swfCallerRef = new Array();
	swfCallerRef["hs_01"] = "swf/LaserScanners.swf";
	swfCallerRef["hs_02"] = "swf/FringeBased.swf";
	swfCallerRef["hs_03"] = "swf/ArmsCMMs.swf";
	swfCallerRef["hs_04"] = "swf/Trackers.swf";

	
	//fadeIn de l'overlay
	//ajoute le flash avec les parametres
	j$('#home_overlay').fadeIn('slow').flash(
		{
			swf: swfCallerRef[caller],
			height:620,
			width:720,
			wmode: 'transparent',
			flashvars: {
				anim_lang: flash_anim_lang 
			}
		}
	)
	
	//ajout du bouton de 'close' dans le coin en haut et de son event pour fermer l'overlay au clic 
	 var close_btn = j$('<div style="cursor:pointer; position:absolute; top: 1px; right: 1px; background:transparent url(img/close.png) no-repeat scroll right top; width:50px; height:50px;"></div>').appendTo("#home_overlay");
	close_btn.click(function() { j$(this).parent().fadeOut('slow').html(""); });
} 

/*  Détecte les iphone, ipod, ipad */
function get_user_agent()
{
   var agent = navigator.userAgent.toLowerCase();
   
   if ((agent.match('iphone'))!= null || (agent.match('ipad'))!= null || (agent.match('ipod'))!= null)
   {
      return true;
   }
   else
   {
      return false;
   }
}

// nouveau sélecteur de langue pour les section Manufacturing et Surveying, fonctionne aussi sur umendemo
// la var section peut être "3D-scanners" ou "Surveying"; la var site peut être "in" (site de l'appel)
function switchLang(lang, section, site)
{
   var homePage = "/home.aspx";
   var paramFr = "lang=fr";
   var paramEn = "lang=en";  
   var siteDemo = "umendemo";
   var urlDemo = "http://innovmetric.s.umendemo.com/";
   var urlLive = "http://www.innovmetric.com/polyworks/";  
   
   if (site == "in")
	{
		 switch (lang)
		{
		   case "en":     
				window.location = urlLive + section + homePage + "?" + paramEn;
		   break;
		   
		   case "fr":
				window.location = urlLive + section + homePage + "?" + paramFr;
		   break;
		   
		   case "cn":
				window.location = "http://www.polyworks.com.cn/" + section + homePage;
		   break;	
		   
		   case "jp":
				window.location = "http://www.innovmetric.com/jp/" + section + homePage;
		   break;
		   
		   case "in":
			    window.location = "http://www.polyworks.in/";
		   break;
		}
		
		return;
	}
   
   var url = window.location.toString();
   var splitUrl = url.split(section);      
   var urlDomaine = splitUrl[0];
   var urlPage = splitUrl[1];
   
   if (urlDomaine.indexOf(siteDemo) != -1)
   {
      urlDomaine = urlDemo;
   }    
   switch (lang)
	{
	   // redirige sur la page équivalente en en-fr
	   case "en":
	      if (urlDomaine.indexOf(siteDemo) != -1)
         {
            urlDomaine = urlDomaine + "en-fr/";
         }
         else
         {
            urlDomaine = urlLive;
         } 
	      if (urlPage.indexOf(paramFr)!= -1)
         {
            urlPage = urlPage.replace(paramFr, paramEn);                        
            window.location = urlDomaine + section + urlPage;
         }
         else
         {      
            window.location = urlDomaine + section + homePage + "?" + paramEn;
         }  
	   break;
	   case "fr":
	      if (urlDomaine.indexOf(siteDemo) != -1)
         {
            urlDomaine = urlDomaine + "en-fr/";
         }
         else
         {
            urlDomaine = urlLive;
         }	   
	      if (urlPage.indexOf(paramEn)!= -1)
         {
            urlPage = urlPage.replace(paramEn, paramFr);        
            window.location = urlDomaine + section + urlPage;
         }
         else
         {
            window.location = urlDomaine + section + homePage + "?" + paramFr;
         }
	   break;
	   case "cn":
	      if (urlDomaine.indexOf(siteDemo) != -1)
         {
            window.location = urlDomaine + lang + "/" + section + homePage;
         }
         else
         {
	         window.location = "http://www.polyworks.com.cn/" + section + homePage;
	      }
	   break;	   
	   case "jp":
	      if (urlDomaine.indexOf(siteDemo) != -1)
         {
            window.location = urlDomaine + lang + "/" + section + homePage;
         }
         else
         {
	        window.location = "http://www.innovmetric.com/jp/" + section + homePage;
	      }
	      break;
	   case "in":
	      window.location = "http://www.polyworks.in/";
	   break;
	}
}
