/*
  NAME:
    Javascript.js 
  LAST UPDATE

    2009-04-03 David Schulz
      Replaced various sharePage functions with sharePagePopup()
    2009-03-26 David Schulz
      Added dummy function to handle WebTrends dscMultiTrack calls when stats are disabled
    2009-03-17 David Schulz
      Updated autoIframeResize function
    2009-03-02 David Schulz
      Added autoIframeResize function
    2009-02-19 Johan Mattsson
      New funtions for DownloadImages
    2009-02-13 Johan Anderson
      Modified privSetActiveSizeIcon: getting a-elements instead of span-elements (now removed) 
    2008-12-16 Sverker Olausson
      Added toolbox-functions
    2008-12-08 Johan Mattsson
      Modified function "commonLoad()" since it caused js-error on all pages.
    2008-12-03 Jenny Bengter
      Changed function changeBig. Removed calls for getBackReservedChars
      Changed function getBackReservedChars. Does not do anyting now, should be deleted...
    2008-12-02 David Schulz
      Added function adjustImage
    2008-12-01 Jenny Bengter
      Changed function changeBig. Added parameter this.
    2008-09-18 Ola Ljungars
      Changed function miniMaximixe()


*/


// -----------------------------------------------------------
// TOOLBOX FUNCTIONS
// -----------------------------------------------------------

function sharePagePopup(serviceName, serviceUrl, popupWidth, popupHeight)
{
    dcsMultiTrack('WT.sp', serviceName);
    serviceUrl = serviceUrl.replace("#pageUrl#", encodeURIComponent(window.location));
    serviceUrl = serviceUrl.replace("#pageTitle#", encodeURIComponent(document.title));
    window.open(serviceUrl, 'sharePage', 'toolbar=1,location=1,resizable=1,width=' + popupWidth + ',height=' + popupHeight);
    return false;
}

// triggered by user
function setTextSize(size,sMessage){
    privSetTextSize(size);
    privSetActiveSizeIcon(size);
    privSetSizeCookie('userTextSize',size,sMessage);
}
// triggered by body onload
function setTextSizeOnLoad(){
    var size = getCookie('userTextSize');
    if (size != null){
        privSetTextSize(size);
        privSetActiveSizeIcon(size);
    }
}

function privSetSizeCookie(cookieName, cookieValue, sMessage){
    setCookie(cookieName, cookieValue, 365, '/');
    if (getCookie(cookieName) == null){
        alert(sMessage);
    }
}

// set text size
function privSetTextSize(size){
    // switch alternate stylesheet
    setActiveStyleSheet('size' + size);
}

// method from alistapart
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function privSetActiveSizeIcon(size){
    var sizeListE = document.getElementById('size');
    if (sizeListE != null){
        var alAElement = sizeListE.getElementsByTagName('a');
        if (alAElement != null){
            for (i=1; i<=alAElement.length; i++){
                var sClass = 'icon-size' + i;
                if (i==size){
                    sClass += ' icon-size' + i + '-on';
                }
                //alert(sClass);
                alAElement.item(i-1).className = sClass;
            }
        }
    }
}

var emailLayerLoaded = false;
function showEmailLayer(url){
    var e = document.getElementById('contactForm');
    if (e != null) {
        if (url != null){
            //if (!emailLayerLoaded){
                var f = e.getElementsByTagName('iframe');
                if (f != null && f.item(0) != null){
                    // todo: 
                    //f.item(0).src = 'http://uk.tooldev4.scania.com/test/konajh/email.aspx';
                    f.item(0).src = url;
                    //setTimeout("showEmailLayer(null)",5000);
                    emailLayerLoaded = true;
                }
            //}
        }
        with (e.style){
            display = display=='block'?'none':'block';
        }
    }
}

function showBusyImage()
{
    var waitLayer = top.document.getElementById("layer-busy");
    waitLayer.style.display = '';
}

function showContactForm(strSessionVariableName){
  var objIFrame = document.getElementById("contactIFrame");
  showL("contactForm");
  showBusyImage();
  objIFrame.src = "/_inc/contactform.aspx?stdContactRcpt=" + strSessionVariableName;
}

// -----------------------------------------------------------
// DOWNLOAD IMAGES FUNCTIONS
// -----------------------------------------------------------
function showDownloadImage(path, alt, photographer, pixelSize, formatAndSize, title, description, originalPath){
  var theUL = document.getElementById("DILayerHide");
  if(originalPath == "")
  {
    theUL.className= "displayNone";
  }
  else
  {
    theUL.className ="";
  }
  showL("downloadImages");
  var theImg = document.getElementById("DILayerImage");
  theImg.alt=alt;
  theImg.title=alt;
  var thePhotographer=document.getElementById("DILayerPhotographer");
  thePhotographer.innerHTML=photographer;
  var thePixels=document.getElementById("DILayerPixels");
  thePixels.innerHTML=pixelSize;
  var theFormatAndSize=document.getElementById("DILayerFormatAndSize");
  theFormatAndSize.innerHTML=formatAndSize;
  var theTitle=document.getElementById("DILayerTitle");
  theTitle.innerHTML=title;
  var theDescription=document.getElementById("DILayerDescription");
  theDescription.innerHTML=description;
  var theLink = document.getElementById("DILayerHighres");
  if(originalPath != "")
  {
    theLink.onclick = new Function('showDisclaimer(\"' + originalPath + '\")');
  }
  theImg.src=path;
  document.getElementById("imgInfoRight").style.display = "inline";
  window.scrollTo(0,0);
}

function showDisclaimer(originalPath){
  var aClose = document.getElementById("DILayerClose");
  aClose.onclick = new Function("hideDisclaimer()");
  
  var divDisclaimer = document.getElementById("divDisclaimer");
  divDisclaimer.style.display = "inline";
  
  var btnEnter = document.getElementById("DIbtnEnter");
  btnEnter.onclick = new Function("openHiresImage('" + originalPath + "')");
}

function hideDisclaimer(){
  var aClose = document.getElementById("DILayerClose");
  aClose.onclick = new Function("hideL('downloadImages');");
  
  var divDisclaimer = document.getElementById("divDisclaimer");
  divDisclaimer.style.display = "none";
}

function openHiresImage(originalPath){
  open(originalPath);
  hideDisclaimer();
}

function getBackReservedChars(myString)
{
    return myString;
    if(myString == "")
        return "";
    return myString.replace(/&apos;/g,"'");
}

function showStreamingVideo(path, alt, photographer, pixelSize, formatAndSize, title, description, originalPath){
  var theUL = document.getElementById("DILayerHide");
  theUL.className= "displayNone";
  showL("downloadImages");
  var theImg = document.getElementById("DILayerImage");
  theImg.alt=alt;
  theImg.title=alt;
  var thePhotographer=document.getElementById("DILayerPhotographer");
  thePhotographer.innerHTML=photographer;
  var thePixels=document.getElementById("DILayerPixels");
  thePixels.innerHTML=pixelSize;
  var theFormatAndSize=document.getElementById("DILayerFormatAndSize");
  theFormatAndSize.innerHTML=formatAndSize;
  var theTitle=document.getElementById("DILayerTitle");
  theTitle.innerHTML=title;
  var theDescription=document.getElementById("DILayerDescription");
  theDescription.innerHTML=description;
  var theLink = document.getElementById("DILayerHighres");
  theLink.href="";
  theImg.style.display="none";
  var theVideoDiv = document.getElementById("DILayerVideoDiv");
  theVideoDiv.innerHTML = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0'  width='380px' height='380'><param name='movie' value='" + unescape(path) + "'></param><param name='allowFullScreen' value='true'></param><param name='wmode' value='transparent'></param><embed src='" + unescape(path) + "' width='380px' height='380' type='application/x-shockwave-flash' allowfullscreen='true' wmode='transparent'></embed></object>";
  document.getElementById("imgInfoRight").style.display = "none";
  window.scrollTo(0,0);
}


// -----------------------------------------------------------
// GENERAL FUNCTIONS
// -----------------------------------------------------------
window.onload = function() {
  if(document.getElementById('root')) {
	  document.getElementById('root').onmouseover = hideAllL;
  }
}

var prevL = null;

function showL(id) {
  if(id == "downloadImages")
    adjustImage();
  document.getElementById(id).style.display = 'block';
}

function hideL(id) {
  document.getElementById(id).style.display = 'none';
  if(id == "downloadImages")
    document.getElementById("DILayerVideoDiv").innerHTML = "";
}

function toggleL(id) {
  var theL = document.getElementById(id);
  var theDisplay = theL.style.display;
  if(theDisplay != 'block') {
    showL(id);
  } else {
    hideL(id);
  }
}

function hideAllL(){
}


var objPopUpWin = null;

function popUp(strURL, width, height){
  
  if(objPopUpWin!=null){  // the window is previously created
    if(!objPopUpWin.closed){
      objPopUpWin.focus();
      objPopUpWin.location = strURL;
    }
    else{
      objPopUpWin = window.open(strURL,"rcPopUp","width="+width+",height="+height+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes");
    }
  }
  else{
    objPopUpWin = window.open(strURL,"rcPopUp","width="+width+",height="+height+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes");
  }
}

// set a cookie
function setCookie( name, value, expires, path, domain, secure ) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days.
    */
    if ( expires ){ expires = expires * 1000 * 60 * 60 * 24; }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// get a cookie 
function getCookie( name ) {
  var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

/******************************************************************************
	NAME:
		commonLoad
	DESCRIPTION:
		Function for later override, do not add code to this function.
******************************************************************************/
function commonLoad(){
	setTextSizeOnLoad();
	return false;
}

/*
function miniMaximize(objElement, isMinimized, itemIndex){
  var objMiniItem = document.getElementById("mini" + itemIndex);
  var objMaxiItem = document.getElementById("maxi" + itemIndex);

  if(isMinimized){
    objMiniItem.style.display="none";
    objMaxiItem.style.display="block";
  }
  else{
    objMiniItem.style.display="block";
    objMaxiItem.style.display="none";
  }
}
*/

function miniMaximize(objElement, isMinimized, itemIndex){

  var objRC = document.getElementById("rc" + itemIndex);
  var objLC = document.getElementById("lc" + itemIndex);
  var objLnk_open  = document.getElementById("lnk_open" + itemIndex);
  var objLnk_close = document.getElementById("lnk_close" + itemIndex);
  
  if(objRC.style.display=="block"){   // hide
    objRC.style.display="none";
    objLC.style.display="none";
    objLnk_open.style.display="block";
    objLnk_close.style.display="none";
  }
  else{ // show
    objRC.style.display="block";
    objLC.style.display="block";
    objLnk_open.style.display="none";
    objLnk_close.style.display="block";
  }
  
  /*
  var objMiniItem = document.getElementById("mini" + itemIndex);
  var objMaxiItem = document.getElementById("maxi" + itemIndex);

  if(isMinimized){
    objMiniItem.style.display="none";
    objMaxiItem.style.display="block";
  }
  else{
    objMiniItem.style.display="block";
    objMaxiItem.style.display="none";
  }
  */
}

function adjustImage()
{
    if(document.getElementById("DILayerImage") != null)
    {   
        
        var objImg = new Image();
        var objShownImg = document.getElementById("DILayerImage");
        objImg.src = objShownImg.src;      
        
        if(objImg.width > 380 || objImg.height > 380)
        {
            if(objImg.width > objImg.height)
            {
                var imgScale = 380/objImg.width;
                objShownImg.width = objImg.width*imgScale;
                objShownImg.height = objImg.height*imgScale;
            }
            else
            {
                var imgScale = 380/objImg.width;
                objShownImg.height = objImg.height*imgScale;
                objShownImg.width = objImg.width*imgScale;
            }        
        }
    }   
}

/******************************************************************************
	NAME:
		autoIframeResize
	DESCRIPTION:
		Function for setting the proper iframe height depending on it´s content
******************************************************************************/

function autoIframeResize(frameId){
    
    try{
        frame = document.getElementById(frameId);
        innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
        formDiv = innerDoc.getElementById('iframeForm');

        if(formDiv != null){
                objToResize = (frame.style) ? frame.style : frame;
                objToResize.height = (formDiv.offsetHeight-1) + "px";
        }
    }
    catch(err){
        //window.status = err.message;
    }
}

/******************************************************************************
	NAME:
		dcsMultiTrack
	DESCRIPTION:
		Dummy function to handle WebTrends dscMultiTrack calls when stats are disabled
******************************************************************************/

function dcsMultiTrack(str1, str2){}

function ShowOtherInfo(ItemID) {
    document.getElementById(ItemID).style.display = 'block';
}
function HideOtherInfo(ItemID) {
    document.getElementById(ItemID).style.display = 'none';
}

function HideShowOtherInfo(ItemID) {

    if (document.getElementById(ItemID).style.display == 'none')
        document.getElementById(ItemID).style.display = 'block';
    else
        document.getElementById(ItemID).style.display = 'none';
}
