/* share onclick event handlers */
function facebook_click() {
  u = location.href;
  t = document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function twitter_click(obj) {
  u = location.href;
  t = document.title;
  obj.href = 'http://twitter.com/home?status='+encodeURIComponent('Currently reading http://anythingproject.uvic.ca/');
  //window.open('http://twitter.com/home?status='+encodeURIComponent('Currently reading http://anythingproject.uvic.ca/')+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  //return false;
  return true;
}

function digg_click(obj) {
  u = location.href;
  t = document.title;
  obj.href = 'http://digg.com/submit?url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t) +
    '&bodytext=' + encodeURIComponent('The Anything Project. If you could do anything, what would you do?') +
    '&media=news&topic=' + encodeURIComponent('Arts & Culture');
  return true;
}

function email_click(obj) {
  u = location.href;
  t = document.title;
  obj.href = 'mailto:?subject=' + encodeURIComponent(t) + '&body=' + encodeURIComponent('Check out this page:\n\n') +
    encodeURIComponent(u);
  return true;
}

/* this function is called from flash to change the url hash, update page title, and re-initialize share urls and titles */
function callback_onchange_urlhash(hash, param1, param2) {
  var href = 'http://www.addthis.com/bookmark.php?pub=' + addthis_publisher_id + '&v=250&source=tbx-250&s=%1&url=';
  window.location.hash = hash;

  document.title = 'Hash-specific page title ' + param1;
  var title = 'Check out ' + document.title;
  var url = share_url();

  document.getElementById('share_facebook').href = href.replace('%1', 'facebook') + encodeURIComponent(url) + "&title=" + encodeURIComponent(title) + "&content=";
  document.getElementById('share_facebook').onclick = function() { return true; };
  document.getElementById('share_facebook').target = '_BLANK';

  //document.getElementById('share_twitter').href = "http://www.addthis.com/bookmark.php?pub=xa-4b0a2a7700c5e7c6&v=250&source=tbx-250&s=twitter&url=" + encodeURIComponent(url) + "&title=" + encodeURIComponent(title) + "&content=&template=" + encodeURIComponent('Watching video on {{url}}');
  document.getElementById('share_twitter').href = href.replace('%1', 'twitter') + encodeURIComponent(url) + "&title=" + encodeURIComponent(title)  + "&content=";
  document.getElementById('share_twitter').onclick = function() { return true; };
  document.getElementById('share_twitter').target = '_BLANK';

  document.getElementById('share_digg').href = href.replace('%1', 'digg') + encodeURIComponent(url) + "&title=" + encodeURIComponent(title)  + "&content=";
  document.getElementById('share_digg').onclick = function() { return true; };
  document.getElementById('share_digg').target = '_BLANK';

  $('share_email').share.title = title;
  $('share_email').share.url = url;

  $('share_addthis').share.title = title;
  $('share_addthis').share.url = url;
}

/* this function is really need only to get facebook crawler pulling the correct title but we use it 
  for all other shares as well because $('share_addthis').share.url sets the facebook url as well
*/
function share_url() {
  thePage = decodeURIComponent(location.href);
  if (thePage.indexOf('?') !== -1) {
    thePage = thePage.substring(0,thePage.indexOf('?'));
  }
  thePage = thePage.substr(thePage.lastIndexOf('/')+1);
  thePage = thePage.substr(0,thePage.lastIndexOf('.'));
  return(location.protocol + '//' + location.host + location.pathname.replace(thePage, 'share_target') +
    '?hash=' + window.location.hash.substring(1));
}

//Adds a query string to URL
function setQueryStringParameter(name,value) {      
  var url = window.location.href;
  var queryfound=false;
  var queries = new Array();
  var urlSegment = new Array();         
  var queryName = name.toLowerCase();
  var query = queryName+"="+encodeURIComponent(value);
  
  if (url.indexOf('?')!=-1) {
    urlSegment = url.split('?');   
    (urlSegment[1].indexOf('&')!= -1) ? queries=urlSegment[1].split("&") : queries[0]=urlSegment[1];
   
   //if query exist, update it 
    for (i=0;i<queries.length;i++) {
      if (queries[i].substring(0,queries[i].indexOf("=")) == queryName) {
        queries[i] = query;
        queryfound = true;
        break;
      } 
    } 

    if (!queryfound) queries.push(query);         
    urlSegment[1]="";
    
    for (j=0;j<queries.length;j++) {      
      urlSegment[1] += queries[j];
      if (j<queries.length-1) urlSegment[1]+="&";
    }
    
    url = urlSegment[0]+"?"+urlSegment[1];    
  } else {
    url += "?" + query;    
  }
     
  window.location=url;    
}

//Adds taget="_blank" to external links
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

//returns the position of the element inside its parent
function returnPosition(element) {
  var elementName = element.nodeName;  
  var element_position = 0;    
  
  while (element.previousSibling) {
    if ((element.previousSibling.nodeType == 1) && (element.previousSibling.nodeName == elementName)) {
      element_position += 1;
    }
    element = element.previousSibling;
  } 
  return(element_position);
} 

//returns the width or height of the viewport
function viewportSize(side) {
  var viewportWidth;
  var viewportHeight;

  // for standards compliant browsers (mozilla/netscape/opera/IE7)     
  if (typeof window.innerWidth != 'undefined') {
    viewportWidth = window.innerWidth,
    viewportHeight = window.innerHeight
  }        
  //for IE6     
  else {
    viewportWidth = document.documentElement.clientWidth,
    viewportHeight = document.documentElement.clientHeight
  }      
  return (side == 'width' ? viewportWidth : viewportHeight);
}

//puts an absolutely-positioned element at the center of viewport
function centerAlign(element) {
  var elementHeight = element.offsetHeight;
  var elementWidth= element.offsetWidth;
  
  if (viewportSize('height')>elementHeight) {
    element.style.top = (viewportSize('height')-elementHeight)/2+"px";
  } else {
    element.style.top = "100px"; 
  }
  element.style.left = (viewportSize('width')-elementWidth)/2+"px";        
}

//set the form focus on first input element of type text
function setFormFocus() {
  if ($$('form').length>0) {
    var inputs = $$('form')[0].getElementsByTagName('input');
    for (i=0;i<inputs.length;i++) {
      if (inputs[i].getAttribute('type') == "text") {    
        inputs[i].focus();
        break;
      }
    }
  }
}

