/**
 * Returns valid background position
 * @param position (e.g. TL)
 * @return position (e.g. top left)
 */
function getBackgroundPosition(bgPos) {
  if (bgPos == 'L') {
    bgPos = 'left';
  } else if (bgPos == 'R') {
    bgPos = 'right';
  } else if (bgPos == 'B') {
    bgPos = 'bottom';
  } else if (bgPos == 'T') {
    bgPos = 'top';
  } else if (bgPos == 'TL') {
    bgPos = 'top left';
  } else if (bgPos == 'TR') {
    bgPos = 'top right';
  } else if (bgPos == 'BL') {
    bgPos = 'bottom left';
  } else if (bgPos == 'BR') {
    bgPos = 'bottom right';
  } else if (bgPos == 'LT') {
    bgPos = 'top left';
  } else if (bgPos == 'RT') {
    bgPos = 'top right';
  } else if (bgPos == 'LB') {
    bgPos = 'bottom left';
  } else if (bgPos == 'RB') {
    bgPos = 'bottom right';
  }
  return bgPos;
}

/**
 * If the link is an ILWWCM link, rips
 * out the host name
 * @param link
 * @param formatted link
 */
function formatLink(link) {
  // Exit if it's not an ILWWCM link
  if (link.indexOf('/connect/') < 0) {
    return link;
  }
  // Exit if there's no host name
  if (link.indexOf('://') < 0) {
    return link;
  }
  pos = link.indexOf('://');
  link = link.substring(pos + 3);
  pos = link.indexOf('/');
  link = link.substring(pos);
  pos = link.indexOf('?id');
  if (pos > -1) {
    link = link.substring(0, pos);
  }
  return link;
}

/**
 * Returns the img tag for the introduction images
 * in a list
 * @param image
 * @param alt alternate text
 * @return img tag
 */
function getIntroImage(image, alt) {
  if (image != '') {
    return '<img border="0" alt="' + alt + '" src="' + image + '"/>';
  } else {
    return '';
  }
}


/*********** START PAGE FUNCTIONS ************/
/**
 * Gets the HTML for displaying the main image.
 * Creates a link of the image if it is applied
 * @return HTML for main image
 */
function getMainImage() {
  var href = '';
  if (mainImageFile != '') {
    href = mainImageFile;
  } else if (mainImageURL != '') {
    href = mainImageURL;
  }
  var htmlTD = '';
  if (image1 == '' && image2 == '' && image3 == '') {
    htmlTD = '<td width="591" height="343" colspan="10"';
  } else {
    htmlTD = '<td width="591" height="257" colspan="10"';
  }
  var bgPos = '';
  if (mainImagePosition != '') {
    bgPos = getBackgroundPosition(mainImagePosition);
  }
  var styleImage = '';
  var styleLink = '';
  var TDOnClick = '';
  if (mainImage != '') {
    styleImage = 'background-image: url(' + mainImage + ');background-repeat: no-repeat;background-position: ' + bgPos + ';';
  }
  if (href != '') {
    styleLink = 'cursor:pointer;';
    TDOnClick = ' onclick="location.href=\'' + formatLink(href) + '\'" ';
  }
  htmlTD = htmlTD 
           + TDOnClick
           + ' style="vertical-align: top;' + styleImage + styleLink + '">';

  return htmlTD;
}


function getLogo(image, alt, link) {
  logoHTML = '<a href="' + formatLink(link) + '"><img alt="' + alt + '" border="0" src="' + image + '"/></a>';
  logo.innerHTML = logoHTML;
}

/*********** END START PAGE FUNCTIONS ************/


/*********** ARTICLE ************/

/*loads article image from document*/
function loadArticleImage(){
	if(artImgURL != ""){
		document.write('<img src="' + artImgURL + '" alt="' + artImgAltTxt + '" class="articleimage">');
	}
}

/*loads ingress from document*/
function loadIngress(){
	if(introText != ""){
		document.write('<p class="ingress">' + introText + '</p>');
	}
}

function loadBackgroundIfFirefoxOpera(){
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera") < 0){
	
	}else{
		document.write('<div id="ArticleBGImageContainerFirefoxOpera">&nbsp;</div>');
	}
	
}


/**
 * Returns the introduction HTML
 * @return HTML
 */
function getIntroduction() {
  if (introduction != '') {
    introduction = '<br><div class="introduction">' + introduction + '</div>';
  }
  return introduction;
}

/**
 * Gets the file name based on a url
 * @param url
 * @return the file name
 */
function getAttachmentName(url) {
  lastIndex = url.lastIndexOf('/');
  return url.substring(lastIndex + 1);
}

/**
 * Creates the HTML for displaying attachments
 * @return HTML displaying attachments
 */
function getAttachments() {
  var attachments = '';
  if (attachment1 != '') {
	 if(attachText1 == ''){attachText1 = getAttachmentName(attachment1);}  	
    attachments = '<li><a target="_blank" href="' + attachment1 + '">' + attachText1 + '</a></li>';
  }
  if (attachment2 != '') {
  	 if(attachText2 == ''){attachText2 = getAttachmentName(attachment2);} 
    attachments = attachments + '<li><a target="_blank" href="' + attachment2 + '">' + attachText2 + '</a></li>';
  }
  if (attachment3 != '') {
  	 if(attachText3 == ''){attachText3 = getAttachmentName(attachment3);} 
    attachments = attachments + '<li><a target="_blank" href="' + attachment3 + '">' + attachText3 + '</a></li>';
  }
  if (attachment4 != '') {
  	 if(attachText4 == ''){attachText4 = getAttachmentName(attachment4);} 
    attachments = attachments + '<li><a target="_blank" href="' + attachment4 + '">' + attachText4 + '</a></li>';
  }
  if (attachment5 != '') {
  	 if(attachText5 == ''){attachText5 = getAttachmentName(attachment5);} 
    attachments = attachments + '<li><a target="_blank" href="' + attachment5 + '">' + attachText5 + '</a></li>';
  }    
  if (attachments != '') {
    return '<ul class="linklist">' + attachments + '</ul>';
  } else {
    return '';
  }
}

/**
 * Gets any added links
 *@return HTML
 */
function getDocLinks() {
	var html = '';
  
  if (linktext1 != '') {
    html = html + getLinkHTML(linktext1, linkurl1);
  }
  if (linktext2 != '') {
    html = html + getLinkHTML(linktext2, linkurl2);
  }
  if (linktext3 != '') {
    html = html + getLinkHTML(linktext3, linkurl3);
  }
  if (linktext4 != '') {
    html = html + getLinkHTML(linktext4, linkurl4);
  }
  if (linktext5 != '') {
    html = html + getLinkHTML(linktext5, linkurl5);
  }
  
  if (html != '') {
    html = '<p></p><ul class="linklist">' + html + '</ul>';
  }
  
  return html;
}

/**
 * Gets HTML for one link
 * @param text
 * @param link
 * @param target
 * @return html
 */
function getLinkHTML(text, link) {
  return '<li><h2><a href="' + formatLink(link) + '">' + text + '</a></h2></li>';
}
/*********** END ARTICLE ************/


/*********** CATEGORY ARTICLE ************/
/**
 * Gets the Category Body if entered
 * @return formatted body (adds <br>)
 */
function getCategoryBody() {
  if (categorybody != '') {
    return categorybody + '<br><br>';
  } else {
    return '';
  }
}
/*********** END CATEGORY ARTICLE ************/

/*********** ARTICLE & CATEGORY ARTICLE ************/
/**
 * Gets the table tag
 * Includes the selected background image
 * @return table tag
 */
function getTableTag() {
  var bg = '';
  var bgPos = '';
  if (backgroundImage == '') {
    bg = backgroundImageSiteArea;
    bgPos = backgroundPositionSiteArea;
  } else {
    bg = backgroundImage;
    bgPos = backgroundPosition;
  }
  bgPos = getBackgroundPosition(bgPos);
  return '<table class="content" border="0" cellspacing="0" cellpadding="0" '
        + 'style="background-image : url(' + bg + ');background-position : ' + bgPos + ';">';
}

/**
 * Formats the image tag with alignment
 * @return formatted image
 */
function getImage() {
  if (contentImageFloat == 'L') {
    classStyle = 'class = "imgleft"';
  } else if (contentImageFloat == 'R') {
    classStyle = 'class = "imgright"';
  } else if (contentImageFloat == 'B') {
    classStyle = 'class = "imgboth"';
  } else {
    classStyle = 'class = "imgnone"';
  }
  if (contentImage != '') {
    contentImage = '<img border="0" alt="' + contentImageAlt + '" src="' + contentImage + '">';
    return '<div ' + classStyle + '>' + contentImage + '</div>';
  } else {
    return '';
  }
}
/*********** END ARTICLE & CATEGORY ARTICLE ************/

/********** FOOTER ************/
/**
 * Returns the HTML for the footer links
 * @return HTML
 */
function getFooterLinks() {
  return getFooterLink(footerText1, footerURL1, footerTarget1)
        + getFooterLink(footerText2, footerURL2, footerTarget2)
        + getFooterLink(footerText3, footerURL3, footerTarget3);
}
/**
 * Returns the HTML for a single footer link
 * @param text - the link text
 * @param link - the url
 * @param target - target window, current if blank
 * @return HTML
 */
function getFooterLink(text, link, target) {
  if (target == 'N') {
    target = 'target="_blank"';
  } else {
    target = '';
  }
  if (text != '') {
    return '<a href="' + formatLink(link) + '" ' + target + '>' + text + '</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  } else {
    return '';
  }
}
/********** END FOOTER **************/

/********** FILM ******************/

function loadFilm(){
	if(filmURL != ""){
		var pos = filmURL.lastIndexOf(".");
		var ext = filmURL.substr(pos);
		htmlcode = '<div id="player"><object width="' + filmWidth + '" height="' + filmHeight + '" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" style="padding-left: 0px; margin-left: -15px;">\n';
		htmlcode += '<param name="src" value="http://32.247.76.40:9081' + filmURL + '">\n';
		htmlcode += '<param name="controller" value="true">\n';
		htmlcode += '<param name="autoplay" value="true">\n';
		htmlcode += '<embed\n';
		htmlcode += 'src="http://32.247.76.40:9081' + filmURL + '"\n';
		htmlcode += 'width="' + filmWidth + '"\n';
		htmlcode += 'height=' + filmHeight + '\n';
		htmlcode += 'autoplay="true"\n';
		htmlcode += 'controller="true"\n';
		htmlcode += 'pluginspage="http://www.apple.com/quicktime/download/">\n';
		htmlcode += '</embed></object></div>\n';
		htmlcode += '<p>&nbsp;</p>\n';
		document.write(htmlcode);
	}
}

function loadFilmHelp(){
	if(filmURL != ""){
		var pos = filmURL.lastIndexOf(".");
		var ext = filmURL.substr(pos);	
		htmlcode = '<p>&nbsp;</p>\n';
		htmlcode += '<p>Kan du ikke se videoen ovenfor?<br />\n';
		htmlcode += '<a href="http://www.kavli.no/wps/wcm/connect/no_test/Aktuelt/Filmtest/Filmhjelp+Mac">G&aring; til hjelp for Macbrukere.</a><br />\n';
		htmlcode += '<a href="http://www.kavli.no/wps/wcm/connect/no_test/Aktuelt/Filmtest/Filmhjelp+Windows">G&aring; til hjelp for Windowsbrukere.</a></p>\n';
		document.write(htmlcode);
	}
}

/*
function loadFilmHelp(){
	if(filmURL != ""){
		var pos = filmURL.lastIndexOf(".");
		var ext = filmURL.substr(pos);	
		htmlcode = '<p>&nbsp;</p>\n';
		htmlcode += '<p>Kan du ikke se videoen ovenfor?<br />\n';
		htmlcode += '<a href="http://www.apple.com/no/quicktime/download/">Installer Apples gratis QuickTime player.</a></p>\n';
		document.write(htmlcode);
		if(ext == ".wmv"){
			htmlcode = '<p>Bruker du Safari eller Firefox p&aring; Mac?<br />\n';
			htmlcode += '<a href="http://www.microsoft.com/windows/windowsmedia/player/flip4mac.mspx">Denne plugin\'en lar deg se wmv-format i QuickTime.</a><br />\n';
			htmlcode += '<p>Bruker du Opera p&aring; Mac?<br />\n';
			htmlcode += '<a href="http://www.microsoft.com/windows/windowsmedia/player/flip4mac.mspx">Da trenger du Windows Media Player for Mac for &aring; se wmv-format</a></p>\n';			
			document.write(htmlcode);			
		}
	}
}
*/

/********** END FILM **************/