/**
* (c) InfoProjects bv. - www.infoprojects.nl
*
* $Revision: 1.29 $
* $Date: 2009/10/16 07:50:44 $
* $Author: Ad $
*/

var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

var _pageContent = new pageContent();

$(document).ready(function() {
  _pageContent.ProcessFlashMovies();
  _pageContent.ProcessExternalLinks();
  _pageContent.ProcessGoogleMapLinks();
  _pageContent.ProcessYouTubeLinks();
  _pageContent.ProcessMediaWidgets();
  _pageContent.ScrollToBookMark();

  try { initStijlset(); } catch (e) { } // standaard gematched in /views/shared/head/head_js.xsl
  try { initPagetype(); } catch (e) { } // standaard gematched in /views/shared/head/head_js.xsl
});

function pageContent() {
  // New window default settings
  this.newWindowDefaultWidth = '775';
  this.newWindowDefaultHeight = '475';
  this.newWindowDefaultPopupAttributes = "status=yes,toolbar=yes,menubar=no,location=no,scrollbars=yes,resizable=yes";
  this.newWindowLinkList = new Array();
}

pageContent.prototype.ScrollToBookMark = function() {
  $("#goto_bookmark[value]").each(function() {
    $("#" + $(this).attr("value")).scrollIntoView(true);
  });
}

pageContent.prototype.FindNewWindowLink = function(linkId) {
  for (var i = 0; i < this.newWindowLinkList.length; i++) {
    if (this.newWindowLinkList[i][0] == linkId) {
      return this.newWindowLinkList[i];
    }
  }
  return null;
}

pageContent.prototype.AddNewWindowLink = function(linkId, width, heigth, attribs) {
  this.newWindowLinkList[this.newWindowLinkList.length] = new Array(linkId, width, heigth, attribs);
  return this.newWindowLinkList.length - 1;
}

function openLinkInNewWindow(anchor) {
  var link = _pageContent.FindNewWindowLink($(anchor).attr("id"));
  var attributes = (link[3] != '') ? link[3] : _pageContent.newWindowDefaultPopupAttributes;

  if (link[1] != '') { attributes += ',width=' + link[1]; }
  else { attributes += ',width=' + _pageContent.newWindowDefaultWidth; }

  if (link[2] != '') { attributes += ',height=' + link[2]; }
  else { attributes += ',height=' + _pageContent.newWindowDefaultHeight; }

  return !window.open($(anchor).attr("href"), 'popup', attributes);
}

// on $(document).ready
pageContent.prototype.ProcessExternalLinks = function() {
  $("a[class*=Link]:not([id^=link])").each(function() {
    var linkTitle = $(this).attr("title");
    linkTitle += linkTitle == "" ? "Nieuw venster" : " (nieuw venster)";
    $(this).attr("title", linkTitle);
    var linkType = $(this).attr("class");
    if (linkType.indexOf("externLink") == -1) {
      $(this).attr("target", linkType.substring(0, linkType.indexOf("Link")));
    }
    else if ($(this).attr("href") && !$(this).attr("onclick")) {
      $(this).click(function() {
        return !window.open($(this).attr("href"));
      });
    }
  });

  $("form[class*=Link]").each(function() {
    var linkType = $(this).attr("class");
    if (linkType.indexOf("externLink") == -1) {
      $(this).attr("target", linkType.substring(0, linkType.indexOf("Link")));
    }
    else {
      $(this).attr("target", "popup");
    }
  });

  $("a[class*=print]").each(function() {
    $(this).click(function() { return !print(); })
  });

  $("a[id^=link]").each(function() {
    var linkTitle = $(this).attr("title");
    linkTitle += linkTitle == "" ? "Nieuw venster" : " (nieuw venster)";
    $(this).attr("title", linkTitle);
    $(this).click(function() {
      return !window.open($(this).attr("href"));
      // zou eigenlijk moeten zijn: return openLinkInNewWindow($(this)); (maar dat wil men niet vanwege de default width en height)
    });
  });
}

// on $(document).ready
pageContent.prototype.ProcessGoogleMapLinks = function() {
  $("a.GoogleMaps").each(function() {
    var result = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + $(this).attr("href") + '"></iframe>';
    $(this).replaceWith(result);
  });
}

// on $(document).ready
pageContent.prototype.ProcessYouTubeLinks = function() {
  $("a.YouTube").each(function() {
    $(this).createFlashObject();
  });
}

pageContent.prototype.ProcessMediaWidgets = function() {
  $("a.MediaWidget").each(function() {
    if (MediaWidgets[$(this).attr("id") + '_type'] == 'flash') {
      var x = MediaWidgets[$(this).attr("id")];
      $(this).createFlashObject(x['href'], x['width'], x['height'], x['args']);
    }
    else {
      $(this).replaceWith(MediaWidgets[$(this).attr("id")]);
    }
  });
}

// on $(document).ready
pageContent.prototype.ProcessFlashMovies = function() {
  $("a.flashMovie").each(function() {
    var dims = $(this).attr('title').split('Flash : ')[1];
    var width = (dims && dims != '') ? dims.split(',')[0] : 249;
    var height = (dims && dims != '') ? dims.split(',')[1] : 249;
    var id = $(this).attr('id');
    $(this).createFlashObject($(this).attr('href'), width, height, { 'quality': 'high', 'wmode': 'transparent', 'play': 'true' });
  });
}

// for dateformats (04, 07) etc.
function parseDigitString(s) {
  while (s.length > 1 && s.substring(0, 1) == "0") {
    s = s.substring(1, s.length);
  }
  return (isNaN(parseInt(s)) ? 0 : parseInt(s));
}

// for dateformats etc.
function putDigitString(num, digits) {
  num = "" + num;
  while (num.length < digits) {
    num = "0" + num;
  }
  return (num);
}

// mouse events

var curMouseX = 0;
var curMouseY = 0;

function updateXY(e) {
  curMouseX = e.pageX;
  curMouseY = e.pageY;
}

function fieldDateCheck(input, nextEltId, length) {
  try {
    var regex = new RegExp("[^0-9]", "g");
    input.value = input.value.replace(regex, "");

    if (input.value.length == length) {
      var elt = document.getElementById(nextEltId);
      if (elt) {
        elt.focus();
      }
    }
  }
  catch (e)
  { }
}

function backgroundToSrc(bgImg) {
  bgImg = bgImg.indexOf('url(') == -1 ? bgImg : bgImg.substring(4, bgImg.length - 1);
  bgImg = bgImg.replace('"', '');
  bgImg = bgImg.replace("'", "");
  return bgImg;
}

function extendUrl(url, args) {
  var sep = url.indexOf("?") < 0 ? "?" : "&";
  return (url + sep + args);
}
