var licenses = [
  {id:'gds', url:'/sitetools/data_software_license_plain.html', printUrl:'/sitetools/data_software_license_print.html'},
  {id:'cg', url:'/sitetools/community_guidelines_plain.html', printUrl:'/sitetools/community_guidelines_print.html'}
];

var dest=null;
var target='self';

var maskCSS = {
  'z-index': '2000',
  'position': 'absolute',
  'top': '0px',
  'left': '0px',
  'width': '100%',
  'height': '100%',
  'overflow': 'hidden',
  'opacity': '0.75',
  'filter': 'alpha(opacity=75)',
  'background-color': '#fff'
};

var licenseCSS = {
  'z-index': '2001',
  'position': 'absolute',
  'text-align': 'left',  
  'width': '600px',
  'height': '400px',
  'padding': '5px',
  'border': '2px solid black',
  'background-color': '#fff',
  'overflow': 'auto'
}

function whichLicense(id) {
  for(var i=0; i<licenses.length; i++) {
    if(licenses[i].id == id) 
      return licenses[i]
  }

  return null;
}

function showPlainLicense(a, b) {
  var license = whichLicense(a);
  if(!license) {
    alert('Invalid license id.');
    return;
  }

  jQuery.get(license.url, function(data) {
    jQuery(b).html(data);
  });
}

function previewLicense(a) {
  var license = whichLicense(a);
  if(!license) {
    alert('Invalid license id.');
    return;
  }

  var buttonsHtml = "<p><div style=\"text-align:center\"><input type=\"button\" onclick=\"hideLicense()\" value=\" Close \" />";
  buttonsHtml += "<input style=\"margin-left:20px\" type=\"button\" onclick=\"window.open('" + license.printUrl + "')\" name=\"print\" value=\" Printable Version \" /></div></p><br />";

  jQuery.get(license.url, function(data) {
    var div1 = jQuery("<div id='mask'></div>");
    var div2 = jQuery("<div id='license'></div>").html(data + buttonsHtml);

    div1.css(maskCSS);

    div2.css(licenseCSS);
    div2.fixedCenter().fadeIn(500);

    window.scroll(0,0);

    jQuery("body").css('overflow', 'hidden');
    jQuery("body").prepend(div1);
    jQuery("body").prepend(div2);
  });
}

function showLicense(a, b, c) {
  dest = b;
  target = c;

  var license = whichLicense(a);
  if(!license) {
    alert('Invalid license id.');
    return;
  }

  var buttonsHtml = "<p><div style=\"text-align:center\"><input type=\"button\" onclick=\"acceptLicense()\" name=\"agree\" value=\" I Agree \" /> ";
  buttonsHtml += "<input type=\"button\" onclick=\"declineLicense()\" name=\"disagree\" value=\" I Disagree \" /> ";
  buttonsHtml += "<input style=\"margin-left:20px\" type=\"button\" onclick=\"window.open('" + license.printUrl + "')\" name=\"print\" value=\" Printable Version \" /></div></p><br />";

  jQuery.get(license.url, function(data) {
    var div1 = jQuery("<div id='mask'></div>");
    var div2 = jQuery("<div id='license'></div>").html(data + buttonsHtml);

    div1.css(maskCSS);

    div2.css(licenseCSS);
    div2.fixedCenter().fadeIn(500);

    window.scroll(0,0);
    
    jQuery("body").css('overflow', 'hidden');
    jQuery("body").prepend(div1);
    jQuery("body").prepend(div2);  
  });
}

function hideLicense() {
  jQuery("#mask").remove();
  jQuery("#license").remove();
  jQuery("body").css('overflow', 'visible');
}

function acceptLicense() {
  if(target == 'new')
    window.open(dest);
  else
    window.location.href = dest;
  hideLicense();
}

function declineLicense() {
  hideLicense();
}

