/*************************
*  Smugmug variables
*************************/
//SM.PhotoBar.config.position = 'bottom'; 
/************************
*  End Smugmug variables
************************/ 

function redirectPath() { 
  re = /(majis.smugmug.com)/; 
  tmp = window.location.href; 
  if (re.test(tmp)) { 
    tmp = tmp.replace(re, 'www.majakorpi.net'); 
    window.location.href = tmp; 
  } 
}
redirectPath();
function stylebaari() {
  var header = null;
  var stylebarr = null;
  try {
    header = document.getElementById('stylebaarinpaikka'); 
    stylebarr = document.getElementById('stylebar');
    if (stylebarr) {
      header.innerHTML = header.innerHTML + stylebarr.innerHTML;
      stylebarr.innerHTML = "";
    }
  } catch (err) {
    alert(err);
  }
}
  
function onLoadHandler() {
   ContextualizeTitle();
   stylebaari();
   //initMyMap("category_Travel");
   var smugelement = document.getElementById("smugmug");
   if (smugelement) {
     var hc = document.getElementById("headercontainer");
     if (hc) hc.style.width = smugelement.style.width;
   }
   langManager = new I18nizer(['Fin', 'Eng'],  ['div', 'span'], 'Fin');
   var head = document.getElementsByTagName('head')[0];
   var deliciousstyle = document.createElement("style");
   deliciousstyle.setAttribute( "type", "text/css" );
   deliciousstyle.setAttribute( "id", "deliciousstyle" );
    head.appendChild(deliciousstyle);
   var stails = 
".delicious-blogbadge-line { \
    font: 12px arial; position: relative; display: block; clear: both; overflow: hidden; height: 3em; line-height: 3em; padding-top: 3px !important;\
} \
.delicious-blogbadge-line .save-to-link { \
    margin: none; \
    padding: 4px 7px 4px 20px; \
    text-decoration: none; \
    border: 0px !important; \
    background: url(http://images.del.icio.us/static/img/delicious.small.gif) no-repeat 6px 50%; \
    color: #fff !important; \
} \
.delicious-blogbadge-line a:hover { \
    text-decoration: underline !important; \
} \
.delicious-blogbadge-line .url-link { \
    border: none !important; \
    color: #999 !important; \
} \
.delicious-blogbadge-line .url-link a:hover { \
    text-decoration: underline !important; \
} \
.delicious-blogbadge-line .url-link .post-count { \
    background-color: transparent !important; \
    color: #fff !important; \
} \
.delicious-blogbadge-line .top-tags li a { \
    color: #CCC !important; \
}";

 if (deliciousstyle.styleSheet) { //IE
     deliciousstyle.styleSheet.cssText = stails;
  } else {
   deliciousstyle.appendChild(document.createTextNode(stails)); 
  }


}

/**********************************
* Internationalizationizer :)
* by Mika Majakorpi
* Version 0.5
* 5th August, 2006
***********************************/
function I18nizer(langs, tagNames, defaultLang) {
  this.showLanguage = function (langToShow) {
    for (i = 0; i < langs.length; ++i) {
      for (j = 0; j < tagNames.length; ++j) {
        var elements = this.getElementsByClassName(document.body, tagNames[j], "lang"+langs[i]);
        for (k = 0; k < elements.length; ++k) {
          var e = elements[k];       
          if (langs[i] == langToShow) {
            if (e.tagName.toLowerCase() == 'span') {
              e.style.display = 'inline';
            } else {
              e.style.display = 'block';
            }
          } else {
            e.style.display = 'none';
          }
        } //elements
      } //tagnames
    } //languages
    createCookie('I18nizerLang', langToShow, 365);
  };
  this.getElementsByClassName = function (oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
  };
  var langSetting = readCookie('I18nizerLang');
  if (langSetting && langSetting != defaultLang) this.showLanguage(langSetting);
}

/*
--------------------------------------------------
    customize page title settings
--------------------------------------------------
*/
var titleSettings = new Array();
titleSettings.separator   = " : "; // text to insert between parts of title
titleSettings.maxLength   = -1;   // limits length of title (-1 == no limit)
titleSettings.doPhotos    = false; // true == append photo captions
titleSettings.doAlbums    = true; // true == append album names
titleSettings.doGalleries = true; // true == append gallery names

function ContextualizeTitle() {
  var re1 = /([\s\S]*)( - powered by smugmug)/;
  var re2 = /([\s\S]*) (galleries|sub-categories)/;
  var newTitle = document.title;
  var newText = null;
  if(!IsHomePage()) 
  {
    // add gallery title
    if(titleSettings.doGalleries)
    {
      var galleryTitle = document.getElementById("galleryTitle");
      if(galleryTitle)
      {
        var galleryTitleText = GetTextContent(galleryTitle);
        if(galleryTitleText.length > 0)
        {
          galleryTitleText = galleryTitleText.replace(re2, "$1")
          newText = titleSettings.separator + galleryTitleText;
          newTitle = newTitle.replace(re1, "$1" + newText + "$2");
        }
      } 
    }

    // add album title
    if(titleSettings.doAlbums)
    {
      var albumTitle = document.getElementById("albumTitle");
      if(albumTitle)
      {
        var albumTitleText = GetTextContent(albumTitle);
        if(albumTitleText.length > 0)
        {
          newText = titleSettings.separator + albumTitleText;
          newTitle = newTitle.replace(re1, "$1" + newText + "$2");
        }
      }
    }

    // add photo title
    if(titleSettings.doPhotos)
    {
      var photoTitleText = GetPhotoCaption();
      if(photoTitleText.length > 0)
      {
        newText = titleSettings.separator + photoTitleText;
        newTitle = newTitle.replace(re1, "$1" + newText + "$2");
      }
    }

    if(titleSettings.maxLength > -1)
      newTitle = newTitle.substr(0, titleSettings.maxLength);

    document.title = newTitle;
  }
}

function GetPhotoCaption() {
  var photoTitle = document.getElementById("caption_bottom");

  if(!photoTitle)
    photoTitle = document.getElementById("caption_top");

  if(!photoTitle)
    return "";
         
  return GetTextContent(photoTitle);
}

function GetTextContent(node) {
  var text = "";
  if(node) {
    if(node.children) {
      text = GetTextContent(node.firstChild);
    } else {
      if(node.nodeValue) {
        text = node.nodeValue; // IE
      } else {
        text = node.textContent; // mozilla
      }
    }
  }
  text = Trim(text);
  return text;
}

function Trim(text) {
    var regexp = /^\s+|\s+$/g;
    text = text.replace(regexp, "");
    return text;
}


function IsHomePage() {
  var classStr = document.body.className;
  var re = /homepage /;

  if (!classStr)
    return false;

  return re.test(classStr);
}
/*
--------------------------------------------------
  End customize page title settings
--------------------------------------------------
*/



function isClass(sClass) {
  sClassName = document.body.className;
  re = new RegExp(sClass + "( |$)") 
  if (!sClassName)
    return false;
  return re.test(sClassName);
}

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}


/*******************
Add map to travel gallery
********************/

function initMyMap(classToCheck) {
  if (!isClass(classToCheck)) {
    return;
  } else {
    var container = document.getElementById("galleriesBox");  
//include_dom2("http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAkvQxsHThXF-82RO3XcPLSxSx8lUHj4cbN9sgQWQFMtHDViOCexR0vYh8WK1tQxayqUXcV3VwDjNpWA");
        
    var map = document.createElement("div");
    map.setAttribute("id", "map");
    map.setAttribute("style", "width: 750; height: 400; display: block; z-index: 50;");
    container.insertBefore(map, container.firstChild);
    loadSmugMaps();
  }
}

