/*********************************************************
      Versao: 2008.1
     Projeto: Sessions Tools
  Criado por: alex.sevilla@gmail.com
Data criacao: 4/05/2008
Alterado por: 
   Descricao: javascript functions for tools objects
*********************************************************
                 http://www.solucionator.com
*********************************************************/	
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

var sToolsSrc = 'http://classes.sessions.edu/angeluploads/MMTools/tools/tools';//path for the tool.swf - without the .swf
var sPrintableDir = 'http://classes.sessions.edu/angeluploads/MMtools/printcovers/';//printable covers directory

var iCount = 0;//how many gadgets have being created
var aTools = new Array();//this is the tools container

var sRootID = 'toolGadget';//id radical for the objects and divs
var sContainerClass = 'toolGadget';// is the class for the div container
var sWrapperClass = 'toolGadget';// is the class for the wrapper, recomended using the same as the container
var sPrintableClass = 'toolPrintable';// is the class for the printable image

var iWidth = '100%';// will use the hole extension of the div wrapper
var iHeight = '100%';// will use the hole extension of the div wrapper

var aFlashVars = new Array();//sets the variables for the tool

function newTool(sFileUrl){
	iCount ++;
	aFlashVar = new Array();
	sContainerID = sRootID + 'Container' + iCount;
	sWrapperID = sRootID + 'Wrapper' + iCount;
	sObjectID = sRootID + 'Obj' + iCount;
	
	setContainer(sContainerClass, sContainerID,sWrapperClass, sWrapperID);
	
        aFlashVars = new Array();
	aFlashVars.addVar('sid', iCount);
	aFlashVars.addVar('file', sFileUrl);

	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', iWidth,
			'height', iHeight,
			'src', sToolsSrc,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'noscale',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', sObjectID,
			'name', sObjectID,
			'menu', 'false',
			'allowFullScreen', 'false',
			'allowScriptAccess','always',
			'movie', sToolsSrc,
			'salign', 'lt',
			'flashVars', aFlashVars.getVars()
			); //end AC code
	}
	closeContainer();
	aTools.push(sObjectID);
	try{
	  document.getElementById(sObjectID).focus();
	  document.getElementById(sObjectID).blur();
    }catch(e){
	  eval(sObjectID).focus();
	  eval(sObjectID).blur();
	  window.scrollTo(0,0);
	}
}

function setContainer(sContClass, sContID,sWrapClass, sWrapID){
  sHtml = '<div class="' + sPrintableClass + '" id="print_' + sWrapID + '"></div>' + "\r";
  sHtml += '<div class="' + sContClass + '" id="' + sContID + '">' + "\r";
  sHtml += '<div class="' + sWrapClass + '" id="' + sWrapID + '" style="position: absolute">' + "\r";
  document.write(sHtml);
}

function closeContainer(){
  sHtml = '</div>' + "\r";
  sHtml += '</div>' + "\r";
  document.write(sHtml);
}

Array.prototype.addVar = function(sName, sValue){
  this.push({name: sName, value:sValue});
}

Array.prototype.getVars = function(){
  sGet = '';
  for(i = 0; i < this.length; i++){
    sGet += this[i].name + '=' + this[i].value + '&';
  }
  return sGet; 
}

Array.prototype.inArray = function(sVal){
  bResp = false;
  for(i = 0; i < this.length; i++){
    bResp = (this[i] == sVal)?true:false;
  }
  return bResp;
}

function resize(width, height, iID, scroll){  
  sContID = sRootID + 'Container' + iID;
  sWrapID = sRootID + 'Wrapper' + iID;
  sObjectID = sRootID + 'Obj' + iID;
  
  document.getElementById(sWrapID).style.width = width;
  document.getElementById(sWrapID).style.height = height;
  document.getElementById(sContID).style.height = height;
  if(scroll){document.getElementById(sContID).scrollIntoView(true)};
}

function setPrintable(iID, sImg){  
  sWrapID = sRootID + 'Wrapper' + iID;
  sPrintID = 'print_' + sWrapID;
  
  //sPrintImg = sImg.replace(/(covers\/\w*)(\.\w{3})\b/,  'print$2');
  sPrintImg = sImg.replace(/(\w*[\/\.:])*(\w*[^\/])(\w*\.)(xml)\b/,  '$2$3jpg');
  document.getElementById(sPrintID).innerHTML = '<img src="' + sPrintableDir + sPrintImg + '"\>';
}

// generic flash documents starts here
// openFlash(fileName,[params])
// params: width, height, quality, align, play, loop, scale, wmode, salign

function openFlash(sFileUrl, sParams){
	iCount ++;
	aFVars = new Array();
	sContainerID = sRootID + 'Container' + iCount;
	sWrapperID = sRootID + 'Wrapper' + iCount;
	sObjectID = sRootID + 'Obj' + iCount;
	
	setContainer(sContainerClass, sContainerID,sWrapperClass, sWrapperID);
	
	aFVars = getFlashParams(sParams);

	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', aFVars.getValue('width','100%'),
			'height', aFVars.getValue('height','100%'),
			'src', sFileUrl,
			'quality', aFVars.getValue('quality','high'),
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', aFVars.getValue('align','middle'),
			'play', aFVars.getValue('play','true'),
			'loop', aFVars.getValue('loop','true'),
			'scale', aFVars.getValue('scale','noscale'),
			'wmode', aFVars.getValue('wmode','transparent'),
			'devicefont', 'false',
			'id', sObjectID,
			'name', sObjectID,
			'menu', 'false',
			'allowFullScreen', 'false',
			'allowScriptAccess','always',
			'movie', sFileUrl,
			'salign', aFVars.getValue('salign','lt'),
			'flashvars', aFVars.getValue('flashvars','id='+iCount)
			); //end AC code
	}
	closeContainer();
	aTools.push(sObjectID);
	setFlashPrintable(iCount, sFileUrl);
	
	resize(aFVars.getValue('width','100%'), aFVars.getValue('height','100%'), iCount);
	try{
	  document.getElementById(sObjectID).focus();
	  document.getElementById(sObjectID).blur();
    }catch(e){
	  eval(sObjectID).focus();
	  eval(sObjectID).blur();
	  window.scrollTo(0,0);
	}
}

function getFlashParams(sParams){
	aFlashParams = Array();
	aTempPar = Array();
	sParams = sParams.replace(/\s/gi,  '');

	if(!sParams){
	  return aFlashParams;
	}
	aPar = sParams.split(';');
	
	for(i = 0; i < aPar.length; i++){
		aTempPar = aPar[i].split(':');
		
		aFlashParams.addVar(aTempPar[0], aTempPar[1]);
	}
	
	return aFlashParams;
}

function setFlashPrintable(iID, sImg){  
  sWrapID = sRootID + 'Wrapper' + iID;
  sPrintID = 'print_' + sWrapID;

  sPrintImg = sImg + ".jpg";
  document.getElementById(sPrintID).innerHTML = '<img src="' + sPrintImg + '"\>';
}

Array.prototype.getValue = function(sName, sDefault){
  sVal = sDefault;
  for(i = 0; i < this.length; i++){
    sVal = (this[i].name == sName)? this[i].value : sVal;
  }
  return sVal; 
}