// find center position of screen
function findCenter(winWidth, winHeight) {
  var screenWidth = screen.availWidth;
  var screenHeight = screen.availHeight;

  var positions = new Array;
  positions[0] = Math.round(screenWidth / 2);
  positions[0] = positions[0] - (Math.round(winWidth / 2));
  positions[1] = Math.round(screenHeight / 2);
  positions[1] = positions[1] - (Math.round(winHeight / 2));

  return positions;
}


// show login window and requested file
function showFile(fileLink) {
  var winWidth = 700;
  var winHeight = 450;

  var positions = findCenter(winWidth, winHeight);

  var domain = "http://www.pdicorp.com/";
  if(fileLink.indexOf(domain) != -1) { fileLink = fileLink.slice(domain.length - 1); }

  var params = "width=" + winWidth + ", height=" + winHeight +", left=" + positions[0] + ", top=" + positions[1] + ", location=no, menubar=no, resizable=yes, status=no, toolbar=no, scrollbars=yes";

  window.open("/user_login.asp?doc="+fileLink, "showFile", params);
}


// show popup window
function showPopup(theURL) {
  var winWidth = 600;
  var winHeight = 450;

  var positions = findCenter(winWidth, winHeight);

  var params = "width=" + winWidth + ", height=" + winHeight +", left=" + positions[0] + ", top=" + positions[1] + ", location=no, menubar=no, resizable=yes, status=no, toolbar=no, scrollbars=yes";

  window.open(theURL, "popup", params);
}


// submit rep search form
function repSearch() {
  $('#repSearch').submit(function() {
    window.location = "http://www.pdicorp.com/index.php/sales_reps/view/C" + document.getElementById('selState').options[document.getElementById('selState').selectedIndex].value + "/";
    return false;
  });
}


// set link actions
function linkActions() {
  $('a').filter('[@rel=external]').click(function() { // open external links in new window
    window.open($(this).attr('href')); return false;
  }).end().filter('[@rel*=brochure]').click(function() { // open brochure links in popups
    showFile($(this).attr('href')); return false;
  }).end().filter('[@rel*=popup]').click(function() {
    showPopup('/cd_request.asp'); return false;
  });
}


// load actions
$(document).ready(function() {
  linkActions();
  repSearch();
});