﻿// Copyright 2008 Google Inc.
// All Rights Reserved.
/**
 * @fileoverview Functions modifying presentation of elements on the page
 * @author xxxxx@google.com (Michal Drewniak)
 */

/**
 * Runs all onload functions
 */
function init() {
  resize();
}

/**
 * Resize elements on the page (images, videos)
 */
function resize() {
  var ATTR_NAME = 'resize';
  var resObjs = getElementsByAttribute(ATTR_NAME);
  var objWidth, objHeight, objRatio;
  var newObjWidth, newObjHeight;
  var browserWidth, browserHeight;
  var ratio;
  var resObj;

  for (var i = 0; i < resObjs.length; i++) {
    // Get browser width and height
    if (typeof window.innerWidth == 'number') {
      // Non IE
      browserWidth = window.innerWidth;
      browserHeight = window.innerHeight;
    } else if (document.documentElement && document.body.clientWidth && document.body.clientHeight) {
      // IE 6+
      browserWidth = document.documentElement.clientWidth;
      browserHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientWidth && document.body.clientHeight) {
      // IE 4 
      browserWidth = document.body.clientWidth;
      browserHeight = document.body.clientHeight;
    }
    
    // Get ratio of the currently modified element
    resObj = resObjs[i];
    ratio = resObj.getAttribute(ATTR_NAME);
    objWidth = resObj.width?resObj.width:resObj.style.width;
    objHeight = resObj.height?resObj.height:resObj.style.height;
    
    
    if (resObj.width) { 
      resObj.style.width = resObj.width + 'px';
    } else {
      objWidth = parseInt(objWidth.replace(/px/ig, ''));
    }
    if (resObj.height) {
      resObj.style.height = resObj.height +'px';
    } else {
      objHeight = parseInt(objHeight.replace(/px/ig,''));
    }
    
    // Calculate new dimensions
    objRatio = objWidth / objHeight;
    newObjWidth = browserWidth * ratio;
    newObjHeight = newObjWidth / objRatio;

    if (newObjWidth <= objWidth && newObjHeight <= objHeight) {
      resObj.style.width = newObjWidth +'px';
      resObj.style.height = newObjHeight +'px';
    }
    else {
      if (resObj.tagName == 'IMG') {
        // Resizing image
        resObj.removeAttribute('width');
        resObj.removeAttribute('height');
      } else if (resObj.tagName == 'EMBED') {
        // Resizing embedded object (video)
        resObj.width = newObjWidth;
        resObj.height = newObjHeight;
      } else if (resObj.tagName == 'OBJECT') {
        resObj.style.width = newObjWidth +'px';
        resObj.style.height = newObjHeight +'px';
      }
    }
  }
}


function setSupportLink() {
  var pathname = location.pathname;
  var supportLink = document.getElementById('supportLink');
  var galleryLink = document.getElementById('galleryLink');
  var re = RegExp(/intl\/([^\/]*)/i);
  var match = re.exec(pathname);

  if (match != null && supportLink != null) {
    supportLink.href = '/support/?hl=' +match[1];
  }
  if (match != null && galleryLink != null) {
    galleryLink.href += '&hl=' +match[1];
  }
}


function displayMessage(id) {
  var message = document.getElementById(id);
  var variables = parseURLVar();
  var tmp;

  if (variables['show'] && variables['show'] == id) {
    message.className = 'messagebox';
  }  
}

/**
 * Get elements by attribute
 * @param {String} attribute  name of the attribute
 * @return{Array} array of elements
 */
function getElementsByAttribute(attribute, value) {
  var attributes = Array();
  var tempTags;
  var tempAttr;
  var bWithValue = false;
  
  bWithValue = value?true:false;

  tempTags = document.getElementsByTagName('*');
        
  for (var i = 0; i < tempTags.length; i++) {
    tempAttr = tempTags[i].getAttribute(attribute);
  
    if (bWithValue && tempAttr && tempAttr == value) {
      attributes.push(tempTags[i]);
    } else if (!bWithValue && tempAttr) {
      attributes.push(tempTags[i]);
    }
    
  }
  return attributes;
}
