function $m(theVar){
    return document.getElementById(theVar)
}
function remove(theVar){
    var theParent = theVar.parentNode;
    theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
    if(obj.addEventListener)
        obj.addEventListener(evType, fn, true)
    if(obj.attachEvent)
        obj.attachEvent("on"+evType, fn)
}
function removeEvent(obj, type, fn){
    if(obj.detachEvent){
        obj.detachEvent('on'+type, fn);
    }else{
        obj.removeEventListener(type, fn, false);
    }
}
function isWebKit(){
    return RegExp(" AppleWebKit/").test(navigator.userAgent);
}
function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){
    var detectWebKit = isWebKit();
    form = typeof(form)=="string"?$m(form):form;
    var erro="";
    if(form==null || typeof(form)=="undefined"){
        erro += "The form of 1st parameter does not exists.\n";
    }else if(form.nodeName.toLowerCase()!="form"){
        erro += "The form of 1st parameter its not a form.\n";
    }
    if($m(id_element)==null){
        erro += "The element of 3rd parameter does not exists.\n";
    }
    if(erro.length>0){
        alert("Error in call ajaxUpload:\n" + erro);
        return;
    }
    var iframe = document.createElement("iframe");
    iframe.setAttribute("id","ajax-temp");
    iframe.setAttribute("name","ajax-temp");
    iframe.setAttribute("width","0");
    iframe.setAttribute("height","0");
    iframe.setAttribute("border","0");
    iframe.setAttribute("style","width: 0; height: 0; border: none;");
    form.parentNode.appendChild(iframe);
    window.frames['ajax-temp'].name="ajax-temp";
    var doUpload = function(){
        removeEvent($m('ajax-temp'),"load", doUpload);
        var cross = "javascript: ";
        cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
        $m(id_element).innerHTML = html_error_http;
        $m('ajax-temp').src = cross;
        if(detectWebKit){
            remove($m('ajax-temp'));
        }else{
            setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
    }
    addEvent($m('ajax-temp'),"load", doUpload);
    form.setAttribute("target","ajax-temp");
    form.setAttribute("action",url_action);
    form.setAttribute("method","post");
    form.setAttribute("enctype","multipart/form-data");
    form.setAttribute("encoding","multipart/form-data");
    if(html_show_loading.length > 0){
        $m(id_element).innerHTML = html_show_loading;
    }
    form.submit();
}
function documentOverlay() {
    // @version 0.12
    // @author James Padolsey
    // @info http://james.padolsey.com/javascript/the-perfect-document-overlay/
 
    // Shortcut to current instance of object:
    var instance = this;
 
    // Cached body height:
    bodyHeight = (function(){
        return getDocDim('Height','min');    
    })();
 
    // CSS helper function:
    function css(el,o) {
        for (var i in o) { el.style[i] = o[i]; }
        return el;
    };
 
    // Document height/width getter:
    function getDocDim(prop,m){
        m = m || 'max';
        return Math[m](
            Math[m](document.body["scroll" + prop], document.documentElement["scroll" + prop]),
            Math[m](document.body["offset" + prop], document.documentElement["offset" + prop]),
            Math[m](document.body["client" + prop], document.documentElement["client" + prop])
	);
    }
    // get window height: (viewport):
    function getWinHeight() {
        return window.innerHeight ||
                (document.compatMode == "CSS1Compat" && document.documentElement.clientHeight || document.body.clientHeight);
    }
 
    // Public properties:
 
    // Expose CSS helper, for public usage:
    this.css = function(o){
        css(instance.element, o);
        return instance;
    };
 
    // The default duration is infinity:
    this.duration = null;
 
    // Creates and styles new div element:
    this.element = (function(){
        return css(document.createElement('div'),{
            width: '100%',
            height: getDocDim('Height') + 'px',
            position: 'absolute', zIndex: 1,
            left: 0, top: 0
        });
    })();
 
    // Resize cover when window is resized:
    window.onresize = function() { resize_this(); };

    this.resize = function() { resize_this(); };

    function resize_this() {
 
        // No need to do anything if document['body'] is taller than viewport
        if(bodyHeight>getWinHeight()) { return; }
 
        // We need to hide it before showing
        // it again, due to scrollbar issue.
        instance.css({display: 'none'});
        setTimeout(function(){
            instance.css({
                height: getDocDim('Height') + 'px',
                display: 'block'
            });
        }, 10);
    }
 
    // Remove the element:
    this.remove = function(){
        this.element.parentNode && this.element.parentNode.removeChild(instance.element);
    };
 
    // Show element:
    this.show = function(){};
 
    // Event handling helper:
    this.on = function(what,handler){
        what.toLowerCase() === 'show' ? (instance.show = handler)
        : instance.element['on'+what] = handler;
        return instance;
    };
 
    // Begin:
    this.init = function(duration){
 
        // Overwrite duration if parameter is supplied:
        instance.duration = duration || instance.duration;
 
        // Inject overlay element into DOM:
        document.getElementsByTagName('body')[0].appendChild(instance.element);
 
        // Run show() (by default, an empty function):
        instance.show.call(instance.element,instance);
 
        // If a duration is supplied then remove element after
        // the specified amount of time:
        instance.duration && setTimeout(function(){instance.remove();}, instance.duration);
 
        // Return instance, for reference:
        return instance;
 
    };
 
}
var imgpath = 'http://www.erineng.com/images/';
var uppath = 'http://www.erineng.com/upload/';

var myImg = [];
var myImgU = [];

function preload() {
  for (var i=0; i<preload.arguments.length; i++) {
    myImg[i] = new Image();
    var src = imgpath + preload.arguments[i] + '.jpg';
    myImg[i].src = src;
  }
}

function preloadu() {
  for (var i=0; i<preloadu.arguments.length; i++) {
    myImgU[i] = new Image();
    var src = uppath + preloadu.arguments[i]; // + '.jpg';
    myImgU[i].src = src;
  }
}

preload('header', 'logo_top', 'logo_bottom', 'left_header', 'left_below_header', 'links_background', 
        'header_right_top', 'grey_bar', 'links_bar',
        'home_background_active', 'home_background_hover',
        'company_background_active', 'company_background_hover',
        'services_background_active', 'services_background_hover',
        'products_background_active', 'products_background_hover',
        'employment_background_active', 'employment_background_hover',
        'contact_background_active', 'contact_background_hover'
        );

var myactive = '';

var dropto = 100;
var droptime = 0;
var menuitem = '';
var menulink = '';
var menuname = '';
document.onclick = close_dropdown;

function make_tab_inactive() {
  menulink.style.backgroundImage = '';
  menulink.style.color = '#ffffff';
}

function make_tab_active() {
  var src = 'url('+imgpath+menuname+'_background_active.jpg)';
  menulink.style.backgroundImage = src;
  menulink.style.color = '#888f4a';
}

function open_dropdown(id) {
  stoptimer();

  if (menuitem) menuitem.style.visibility = 'hidden';
  if (menulink) {
    if (menuname == myactive) { make_tab_active(); } else { make_tab_inactive(); }
  }

  if (document.getElementById(id+'_dropdown')) {
    menuitem = document.getElementById(id+'_dropdown');
    menuitem.style.visibility = 'visible';
  }

  var src = 'url('+imgpath+id+'_background_hover.jpg)';
  menuname = id;
  menulink = document.getElementById(id+'_link');
  menulink.style.backgroundImage = src;
  menulink.style.color = '#888f4a';
}

function close_dropdown() {                                                                                       
  if (menuitem) { menuitem.style.visibility = 'hidden'; }
  if (menulink) {
    if (menuname == myactive) { make_tab_active(); } else { make_tab_inactive(); }
  }
}

function starttimer() {
  droptime = window.setTimeout(close_dropdown, dropto);
}

function stoptimer() {
  if (droptime) {
    window.clearTimeout(droptime);
    droptime = '';
  }
}

var spics = [];  //This is filled by each page for their own slideshow

var curpic = 0;
var curcont = 0;
var curopac = 0;
var scont = ['slideshow', 'slideshow2'];
var opac = [100, 0];
var fto;

function load_first() {
  var nextpic = (curpic+1)%(spics.length);
  var nextcont = (curcont+1)%(scont.length);
  var nextopac = (curopac+1)%(opac.length);
  var src = 'url('+uppath+spics[curpic]+')';
  var ss = document.getElementById(scont[curcont]);
  var src2 = 'url('+uppath+spics[nextpic]+')';
  var ss2 = document.getElementById(scont[nextcont]);

  if (ss) {
  ss.style.backgroundImage = src;
  ss.style.opacity = opac[curopac]/100;
  ss.style.filter = 'alpha(opacity='+opac[curopac]+')';
  }
  if (ss2) {
  ss2.style.backgroundImage = src2;
  ss2.style.opacity = opac[nextopac]/100;
  ss2.style.filter = 'alpha(opacity='+opac[nextopac]+')';
  }
  var tmp = opac[curopac];
  opac[curopac] = opac[nextopac];
  opac[nextopac] = tmp;
  curpic = nextpic;
  curcont = nextcont;
  if (spics.length > 1) { fade(); }
}

function fade() {
  var nextpic = (curpic+1)%(spics.length);
  var nextcont = (curcont+1)%(scont.length);
  var nextopac = (curopac+1)%(opac.length);
  var ss = document.getElementById(scont[curcont]);
  var ss2 = document.getElementById(scont[nextcont]);
  if (curopac == 0) {
    opac[curopac] -= 1;
    if (opac[curopac] < 0) { opac[curopac] = 0; }
    opac[nextopac] += 1;
    if (opac[nextopac] > 100) { opac[nextopac] = 100; }
  } else {
    opac[curopac] += 1;
    if (opac[curopac] > 100) { opac[curopac] = 100; }
    opac[nextopac] -= 1;
    if (opac[nextopac] < 0) { opac[nextopac] = 0; }
  }
  ss.style.opacity = opac[curopac]/100;
  ss.style.filter = 'alpha(opacity='+opac[curopac]+')';
  ss2.style.opacity = opac[nextopac]/100;
  ss2.style.filter = 'alpha(opacity='+opac[nextopac]+')';
  if (opac[curopac] == 0 || opac[curopac] == 100) {
    //swap opacity for next container
    var tmp = opac[curopac];
    opac[curopac] = opac[nextopac];
    opac[nextopac] = tmp;
    //set source of invisible container
    var src = 'url('+uppath+spics[curpic]+')';
    var ss = document.getElementById(scont[curcont]);
    ss.style.backgroundImage = src;
    //move to next pic
    curpic = nextpic;
    curcont = nextcont;
    clearTimeout(fto);
  }
}
                                                                                                                  
function run_slideshow() {
  fto = setInterval('fade()', 10);
}


var Ht = new Array();
var varHt = new Array();
var x = new Array();
var y = 10;
var z = 15;
var toggleid;

function setup() {
  var foo = document.getElementsByTagName('div');
  for (i=0;i<foo.length;i++) {
    if (foo[i].className == 'hidden') {
      Ht[foo[i].id] = document.getElementById(foo[i].id).offsetHeight;
      document.getElementById(foo[i].id).style.height = '0px';
      x[foo[i].id] = 0;
      varHt[foo[i].id] = 0;
    }
  }
}

function toggle() {
    if (x[toggleid] == 0) {
        document.getElementById(toggleid).style.position = 'relative';
        document.getElementById(toggleid).style.visibility = 'visible';
        document.getElementById(toggleid).style.height = varHt[toggleid]+'px';
        if (((Ht[toggleid]-varHt[toggleid]) < z) && (varHt[toggleid] !== Ht[toggleid])) {
            varHt[toggleid] = Ht[toggleid];
        } else {
            varHt[toggleid] = varHt[toggleid]+z;
        }
        if (varHt[toggleid] <= Ht[toggleid]) {
            setTimeout('toggle()',y);
        }
        if (varHt[toggleid] > Ht[toggleid]) {
            varHt[toggleid] = Ht[toggleid];
            x[toggleid] = 1;
            myOverlay.resize();
        }
    } else {
        document.getElementById(toggleid).style.height = varHt[toggleid]+'px';
        varHt[toggleid] = varHt[toggleid]-z;
        if ((Ht[toggleid]-varHt[toggleid]) <= Ht[toggleid]) {
            setTimeout('toggle(toggleid)',y);
        }
        if ((Ht[toggleid]-varHt[toggleid]) > Ht[toggleid]) {
            varHt[toggleid] = 0;
            document.getElementById(toggleid).style.height = varHt[toggleid]+'px';
            x[toggleid] = 0;
            myOverlay.resize();
        }
    }
}

function popup_toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	popup_toggle('blanket');
	popup_toggle(windowname);		
}

function getScreenCenterY() {
  var y = 0;
  y = getScrollOffset()+(getInnerHeight()/2);
  return(y);
} 

function getScreenCenterX() {
  return(document.body.clientWidth/2);
} 

function getInnerHeight() {
  var y; 
  if (self.innerHeight) // all except Explorer
  {
    y = self.innerHeight;
  } 
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  { 
    y = document.documentElement.clientHeight;
  } 
  else if (document.body) // other Explorers
  {
    y = document.body.clientHeight;
  } 
  return(y);
} 

function getScrollOffset() {
  var y; 
  if (self.pageYOffset) // all except Explorer
  {
    y = self.pageYOffset;
  } 
  else if (document.documentElement && document.documentElement.scrollTop)                                        
    // Explorer 6 Strict
  { 
    y = document.documentElement.scrollTop;                                                                       
  } 
  else if (document.body) // all other Explorers                                                                  
  {
    y = document.body.scrollTop;                                                                                  
  } 
  return(y);                                                                                                      
} 

function ShowContentCenter(d,xoffset,yoffset) {
  if(d.length < 1) { return; }
  document.getElementById(d).style.top = getScreenCenterY()-yoffset + 'px';
  document.getElementById(d).style.left = getScreenCenterX()-xoffset + 'px';
  document.getElementById(d).style.visibility = "visible";
}

function HideContent(d) {
  if(d.length < 1) { return; }
    document.getElementById(d).style.visibility = "hidden";
}

