﻿// Kenzan Technologies web site - javascript code.
// Copyright (c) 2009 Kenzan Technologies
// Author: Sebastian Gerlach

// Require dojo stuff.
dojo.require("dojo.fx");

/*! A simple function for the hard way to start flash files, used to get around that
    silly limitation in IE. I would love to write a dissertation about patent trolls here,
    but this is company code, so I shall not.
		\param filename URL of animation to include.
		\param width Width of animation in pixels.
		\param height Height of animation in pixels.
*/
function showFlashFile(filename, width, height)
{
	document.write('<object type="application/x-shockwave-flash" data="'+filename+'" width="'+width+'" height="'+height+'">');
	document.write('  <param name="play" value="true" />');
	document.write('  <param name="movie" value="'+filename+'" />');
	document.write('  <param name="menu" value="false" />');
	document.write('  <param name="quality" value="high" />');
	document.write('  <param name="wmode" value="transparent" />');
	document.write('  <param name="scalemode" value="noborder" />');
	document.write('</object>');
}

/*! Load an element into the page
    \param itemURL URL of the element to load.
*/
function expandItem(itemURL)
{
  dojo.xhrGet({
    url: itemURL,
    load: function(response, ioArgs) {
      dojo.byId("div"+id).innerHTML = response;
      return response;
    },
    error: function(response, ioArgs) {
      dojo.byId("div"+id).innerHTML = "An error occurred, with response: " + response;
      return response;
    },
    handleAs: "text"
  });
}

/*! Load an element into a div
    \param itemURL URL of the element to load.
		\param divid id of the DIV to update.
*/
function file2div(itemURL,divid)
{
  dojo.xhrGet({
    url: itemURL,
    load: function(response, ioArgs) {
      dojo.byId(divid).innerHTML = response;
      return response;
    },
    error: function(response, ioArgs) {
      dojo.byId(divid).innerHTML = "An error occurred, with response: " + response;
      return response;
    },
    handleAs: "text"
  });
}

/*! increase/decrease thumbnail image on mouse ouver/out
    \param n image
*/
function InitImgBounce(n)
{
	bo=0;
	mO=10;	// marge de base

	wCur=parseInt(n.style.width);					// largeur de l'image d'origine
	hCur=parseInt(n.style.height);				// hauteur de l'image d'origine
	mCur=parseInt(n.style.margin);				// margin de l'image d'origine
	
	if (mCur==0)	// Agrandissement
	{
		n.style.cursor='pointer';
		var wFinal=wCur-mO*2;
		var hFinal=hCur-mO*2;
		var mFinal=mO;
	}
	else					// Rapetissement
	{
		var wFinal=wCur+mO*2;
		var hFinal=hCur+mO*2;
		var mFinal=0;
	}
	n.style.height		=hFinal+"px";
	n.style.width			=wFinal+"px";				
	n.style.margin  	=mFinal+"px";
}

/*! Popups a big image from a thumbnail 
    \param 			src: 		thumbnail filename
		\param 			wFinal: final Width
    \param 			hFinal: final Height
*/
var nbImg=0;
function popupImg(src,wFinal,hFinal,e)
{
	// How many images do we have in the gallery ?
	nbImg=0;
	
	var lookingfor=src.substring(0,src.lastIndexOf('/',src.length));
	dojo.query("img.imgBounce").forEach(function(item)
	{
		var path=item.src.substring(0,item.src.lastIndexOf('/',item.src.length));
		if(path==lookingfor)
		{
			nbImg++;
		}
  });

  // Disable video
  if(document.getElementById("videoDIV"))
  	document.getElementById("videoDIV").style.visibility="hidden";
  
	// Disable page
	document.getElementById("GreyIMGDIV").style.width="100%";
	document.getElementById("GreyIMGDIV").style.height="100%";
	document.getElementById('PopupIMGDIV').backgroundImage = 'url(files/img/ajax-loader.gif)';
	
	// Generate big image filename
	var keyThumb='-thumb';	// thumbnail name: https://www.kenzantech.com/cms3/files/img/projet/place_affaire_2007/1-thumb.jpg
	var bigImgSrc;					// big image name  https://www.kenzantech.com/cms3/files/img/projet/place_affaire_2007/1.jp
	bigImgSrc = src.substring(0,src.indexOf(keyThumb)) ;
	extention =  src.substring(src.indexOf(keyThumb));
	extention = extention.substring(keyThumb.length)
	bigImgSrc+=extention;	
	//resizePopupDIV(); // Chrome skips onload() if this image is also the last clicked, so force it here
	document.getElementById('PopUpIMG').src = bigImgSrc;

}

/*! Dispay next/previous image of the gallery
    \param direction : forward=1;  backward=-1
*/
function nextPopupIMG(direction)
{
	var curSrc=document.getElementById('PopUpIMG').src;
	var nextSrc=curSrc.substring(0,curSrc.lastIndexOf('.',curSrc.length));
	var pathSrc= curSrc.substring(0,curSrc.lastIndexOf('/',curSrc.length)+1);
	var extention =  curSrc.substring(curSrc.lastIndexOf('.',curSrc.length));
	
	var noImg =  curSrc.substring(curSrc.lastIndexOf('/',curSrc.length)+1 ,curSrc.lastIndexOf('.',curSrc.length) );
	noImg=parseInt(noImg)+1*direction;
	
	// Checke if we are on first image
	if(noImg<1)
		noImg=nbImg;	// we loop
	
	// Checke if we are on last image
	if(noImg>nbImg)
		noImg=1;	// we loop
	
	bigImgSrc=pathSrc+noImg+extention;
	document.getElementById('PopUpIMG').src = bigImgSrc;//	document.getElementById('PopupIMGDIV').setAttribute("src",src)
}

/* Resize the popup image background according to the image size 
*/
function resizePopupDIV()
{
	// resize div
	hDivFinal=parseInt(document.getElementById('PopUpIMG').height)+35;
	wDivFinal=parseInt(document.getElementById('PopUpIMG').width)+40;
	document.getElementById('PopupIMGDIV').style.height=hDivFinal+"px";
	document.getElementById('PopupIMGDIV').style.width=wDivFinal+"px";
	
	xFinal=document.body.clientWidth/2  - wDivFinal/2;
	yFinal=document.body.clientHeight/2 - hDivFinal/2;
	document.getElementById('PopupIMGDIV').style.left=xFinal+"px";
	document.getElementById('PopupIMGDIV').style.top=yFinal+"px";	
	
	// update 'Photo x/x'
	var curSrc = document.getElementById('PopUpIMG').src;
	var noImg =  curSrc.substring(curSrc.lastIndexOf('/',curSrc.length)+1 ,curSrc.lastIndexOf('.',curSrc.length) );
	document.getElementById('PopupIMGphotoNo').innerHTML='Photo ' + noImg + '/' + nbImg ;
}

/*! Dispay html content into floating div
		\param htmlContent : 	html code to put in the div
    \param evt :					mouse event
		\param sideH:					horizontal side; > 0 display div on the right hand side of the mouse pointer; < 0 display div on the left  hand side of the mouse pointer
		\param sideV:					vertical side
															
*/
function popupTxt(htmlContent,evt,sideH,sideV)
{
	var mousemargin=20;
	
	if (evt)
	{	
		mouseX=evt.clientX;
		mouseY=evt.clientY;
	}
	else	if (event)
	{
		mouseX=event.x;
		mouseY=event.y;
	}
	/*
	sideH=1;
	if (mouseX>document.getElementById('MainTabCont').offsetWidth/2)
		sideH=-1;
	sideV=1;
	if (mouseY>document.getElementById('MainTabCont').offsetHeight/2)
		sideV=-1;
	*/
	document.getElementById('PopupTXTDIV').style.width='';
	if (document.getElementById('PopupTXTDIV').offsetWidth > document.getElementById('MainTabCont').offsetWidth*40/100)
		document.getElementById('PopupTXTDIV').style.width='40%';
		
	if (sideH>0)		document.getElementById('PopupTXTDIV').style.left = parseInt(mouseX+mousemargin)+'px';
	else						document.getElementById('PopupTXTDIV').style.left=  parseInt(mouseX - document.getElementById('PopupTXTDIV').offsetWidth-mousemargin)+'px';
	
	if (sideV>0)		document.getElementById('PopupTXTDIV').style.top = parseInt(mouseY)+'px';
	else						document.getElementById('PopupTXTDIV').style.top=  parseInt(mouseY - document.getElementById('PopupTXTDIV').offsetHeight+mousemargin)+'px';
	
	document.getElementById('PopupTXTDIV').style.visibility='visible';
	
	if (htmlContent!='')	// update div content only when mouseover, not mousemove
		document.getElementById('PopupTXTDIVcontent').innerHTML=htmlContent;
}

/*! Toggle a TitlePane located in an other tab
		\param idtab:		id of the tab
		\param idtile:  id of titlepane
*/
function selectTitlePane(idtab,idtile)
{
	var t;
	dijit.byId('MainTabCont').selectChild(idtab);
	if (dijit.byId(idtile))
	{
		if (dijit.byId(idtile).open==false)
		{
			dijit.byId(idtile).toggle();
		}
		if (t) clearTimeout(t);
		timer_is_on=0;
	}
	else
	{
		t=setTimeout("selectTitlePane('"+idtab+"','"+idtile+"')",1);
	}
}

// post data form kenzan website
function postFormData(htmcontent,divid,formid)
{
	var kw = {
	url: htmcontent,
	load: function(data)
	{
		dojo.byId(divid).innerHTML = data;
		},
	error: function(data){
		alert("An error occurred: " + data);
		},
	timeout: 2000,
	form: formid
	};
	dojo.xhrPost(kw);
}

console.log("Let's Go !!!");
