/*
 *
 *Utility functions
 **/

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft,curtop];
  }
}


function get_left(ele){
  return ele.cumulativeOffset()[0];
    
/*
    if (ele.offsetParent)
        return ele.offsetLeft + get_left(ele.offsetParent);
    else
        return ele.offsetLeft;*/
}

function get_top(ele){    
  return ele.cumulativeOffset()[1];
    
/*if (ele.offsetParent)
        return (ele.offsetTop + get_top(ele.offsetParent));
    else
        return (ele.offsetTop);*/
}

function toggle_checkboxes(element, prefix, form){
  f = form || element.form;
  inputs = f.getInputs('checkbox').reject(function(i){
    return i.name.indexOf(prefix)!=0
  });
  inputs.each(function(i){
    i.checked = element.checked
  });
}

function togggle_single_checkbox(element, c_id){
  check_box = $(c_id);
  check_box.checked = element.checked
}

/*
 *Open an image in a popup window
 **/
function image_popup(domElement, url){ 
  
  var myImage = new Image();
  myImage.name =url;
  myImage.onload = open_window;
  myImage.src = url;
  
/*/var x = get_left(domElement) - (width/2);
  if (x<0){ x=0}
  var y = get_left(domElement) - (height/2);
  if (y<0){ y=0}*/
}

function open_window(){    
    
  x = this.width+20;
  y = this.height+20;
  imgwindow= window.open(this.name, "imgwindow","menubar=0,toolbar=0,resizable=0,location=0,status=0,scrollbars=0,width="+x+",height="+y);
  
  dx = 800 - this.width;
  if (dx<0) dx=0;
  dy = 600 - this.height;
  if (dy<0) dy=0;
  
  imgwindow.moveTo(dx,dy);
}

function print_window(urll, x, y, dx,dy){
  printwindow= window.open(urll, "printwindow","menubar=1,toolbar=0,resizable=0,location=0,status=0,scrollbars=1,width="+x+",height="+y);
  printwindow.moveTo(dx,dy);
}


/*
 *Functions for layered menus
 **/
  
/*Array containing all layers together with their properties like lifetime, etc..*/  
var active_links = [];
var layers = $H({});
//layers that contain links of pages currently contained in the breadcrumb
var breadcrumb_layers = $H({});
//active image in the first layer of navigation
var breadcrumb_image ='';
var delta_t = 100; //interval between 2 scheduled runs;

  
function set_old_link_inactive(level)  {
  if (active_links[level]){
    $(active_links[level]).className=$(active_links[level]).className.replace(" active_level"+level,"");
  }
}
  
function set_link_active(id, level)  {
    
  set_old_link_inactive(level);
    
  active_links[level] = id;
  $(id).className=$(id).className+" "+"active_level"+level;
}
  
function register_layer(id,level)  {
  layers.set(id, {
    "id":id,
    "lifetime":0,
    "level":level
  });
}

function register_breadcrumb_image(img_src)  {
  breadcrumb_image = img_src;
}

function register_breadcrumb_layer(id,level)  {
  breadcrumb_layers.set(id, {
    "id":id,
    "level":level
  });
}

function show_layer(id, lifetime,parent_id,delta_x, delta_y,level)  {
  parent_element = $(parent_id);

  var distance_from_top = 166;
  var total_menu_canvas_height = 295;
  var topVal;
  var leftVal;
  /*alert(get_top($('main_menu_container')));
    alert(findPos($('main_menu_container'))[0]);*/
  $$('#'+id+' .transparent_filler').each(function(s){
    s.remove()
    });
  if (level==1){
        
    deltaXBrowser = 0;
    deltaYBrowser = 0;

    /*helper funcitons for IE version detection*/
    Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
    Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
    Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

    if (Prototype.Browser.IE==true && Prototype.Browser.IE8==false){
      deltaXBrowser = 22;
      deltaYBrowser = 22;
    }
        
    leftVal = (get_left(parent_element)+delta_x+parent_element.getWidth()+deltaXBrowser);
    topVal = (get_top(parent_element)+delta_y+deltaYBrowser);
        
    $(id).style.top = distance_from_top+ "px";
    //$(id).style.paddingTop = (topVal -distance_from_top)+ "px";
    //$(id).style.top = "0px";
    topFiller = '<div class="transparent_filler", style="height:'+(topVal -distance_from_top)+'px;"></div>'
    $(id).insert({
      top: topFiller
    });
    
  }else{
    leftVal = (delta_x);
    topVal = (delta_y);

    $(id).style.top = "0px";
    
    //$(id).style.paddingTop = "0px";
    //$(id).style.paddingBottom = "0px";
        
    totalPadding = (total_menu_canvas_height - $(id).getHeight());

    //$(id).style.top = (parseInt($(id).style.top)-totalPadding)+"px";
    $(id).style.top = "0px";
    topFiller = '<div class="transparent_filler", style="height:'+totalPadding+'px;"></div>'

    //$(id).style.paddingTop = totalPadding + "px";
    //$(id).style.paddingBottom = "0px";
        
    if (totalPadding-topVal >0 || level==3){
      //$(id).style.paddingTop =  (totalPadding-topVal)+ "px";
      //$(id).style.paddingBottom = (topVal) + "px";
      topFiller = '<div class="transparent_filler", style="height:'+(totalPadding-topVal)+'px;"></div>'

      height = topVal;
      if (level==3 && totalPadding<topVal){
        height = totalPadding;
      }

      bottomFiller = '<div class="transparent_filler", style="height:'+(height)+'px;"></div>'
      $(id).insert({
        bottom: bottomFiller
      });
    }


    $(id).insert({
      top: topFiller
    });    
    
  }
    
    
  $(id).style.left = leftVal + "px";
    
    
  hide_all_layers(level);
  layers.get(id)["lifetime"] = lifetime
  //$(id).show();
  
  $(id).style.visibility ="visible";
}  

function keep_alive_layer(id, lifetime)  {
  if (layers.get(id)["lifetime"]< lifetime){
    layers.get(id)["lifetime"] = lifetime;
  }
}

function hide_layer(id, lifetime)  {
  layers.get(id)["lifetime"] = lifetime;
}

function hide_all_layers(level){
  layers.each(function(pair) {
    layer = pair.value;
    if (layer.level==level){
      layer.lifetime = 0;
      //$(layer.id).hide();
      $(layer.id).style.visibility ="hidden";
    //alert(pair.key + ' = "' + pair.value + '"');
    }
  });
}

//shows layers that ocntain the pages currently contained in the breadcrumb 
function show_breadcrumb_layers(level){
  /*set the breadcrumb image*/
  if ((breadcrumb_image!=null) && (breadcrumb_image.length>5) && ($('main_menu_img')!=null)){
    if ($('main_menu_img').src!= breadcrumb_image)
      $('main_menu_img').src = breadcrumb_image;
  }
    

  
  
  breadcrumb_layers.each(function(pair) {
    layer = pair.value;
    if (layer.level==level){
      $(layer.id).style.visibility ="visible";
    }
  });
}

//hides layers that ocntain the pages currently contained in the breadcrumb 
function hide_breadcrumb_layers(level){
  breadcrumb_layers.each(function(pair) {
    layer = pair.value;
    if (layer.level==level){
      $(layer.id).style.visibility ="hidden";
    }
  });
}

//hide "expired layers"  
function runner(){    
    
  atleast_one_layer_visible = false;
    
  layers_to_hide = [];
  layers_to_hide_count = 0;
    
  layers.each(function(pair) {
    layer = pair.value;
    if (layer.lifetime > 0){
      atleast_one_layer_visible = true;
      layer.lifetime -= delta_t;
      if (layer.lifetime <= 0){
        layer.lifetime = 0;
        //$(layer.id).hide();
        //just mark the layers to be hidden
        layers_to_hide[layers_to_hide_count] = layer.id;
        layers_to_hide_count++;
      //$(layer.id).style.visibility ="hidden";
      }
    }
  //alert(pair.key + ' = "' + pair.value + '"');
  });
    
  if (!atleast_one_layer_visible){
    set_old_link_inactive(0);
    show_breadcrumb_layers(1);
    show_breadcrumb_layers(2);
    show_breadcrumb_layers(3);
  }else{
  //hide_breadcrumb_layers();
  }
    
  //now hide the layers that have to be hidden
  for (k=0; k<layers_to_hide_count; k++){
    $(layers_to_hide[k]).style.visibility ="hidden";
  }
    
    
}
/*start the runner for the menu*/
setInterval(runner, delta_t);

/*declare the IframeShim*/
var iframeShim; 

function process_onload_tasks_common(){
  $('search_result_wrapper').style.left = "5px";//get_left($('body_container'))+"px";
  $('search_result_wrapper').style.width = (document.viewport.getWidth() -10) +"px";// $('body_container').getWidth()+"px";
  /*create and initialize the IframeShim*/
  iframeShim  = new IframeShim();
//iframeShim.hide(); 
}

function process_onload_tasks_performance_suspensions(){     
  maxHeight = [$('middle_body').getHeight(),$('left_body').getHeight(),$('right_body').getHeight()].max();
  
  //$('left_body_3').style.height = (maxHeight-$('left_body').getHeight()+$('left_body_3').getHeight()) + "px";
  $('left_login_area').style.height = (maxHeight -$('left_body').getHeight()+$('left_login_area').getHeight()) + "px";
  $('middle_body').style.height = maxHeight + "px";
  //$('news_box').style.height = (maxHeight -$('right_body').getHeight()+$('news_box').getHeight()) + "px";
  $('cms_content_right_placeholder').style.height = (maxHeight -$('right_body').getHeight()+$('cms_content_right_placeholder').getHeight()) + "px";

  

//$('left_body_3').style.height= ($('middle_body').getHeight() - $('left_body_1').getHeight() -$('left_body_2').getHeight() -20)+"px";
//$('news_box').style.height=($('middle_body').getHeight() - $('logo_links').getHeight())+"px"
}

function process_onload_tasks_industrial_springs(){
  if ($('right_column_image_wrapper')) $('right_column_image_wrapper').style.height = "650px";
    
  maxHeight = [$('middle_body').getHeight(),$('left_body').getHeight(),$('right_body').getHeight()].max();
  
  //$('left_body_3').style.height = (maxHeight-$('left_body').getHeight()+$('left_body_3').getHeight()) + "px";
  $('left_login_area').style.height = (maxHeight -$('left_body').getHeight()+$('left_login_area').getHeight()) + "px";
    
  $('middle_body').style.height = maxHeight + "px";
  //$('right_column_image_wrapper').style.height = maxHeight + "px";
    
  $('cms_content_right_placeholder').style.height = (maxHeight -$('right_body').getHeight()+$('cms_content_right_placeholder').getHeight()) + "px";

//$('right_column_image_wrapper').style.height =+ "650px";

//$('left_body_3').style.height= ($('middle_body').getHeight() - $('left_body_1').getHeight() -$('left_body_2').getHeight() -20)+"px";
// $('right_column_image_wrapper').style.height=($('middle_body').getHeight())+"px"
}

function process_onload_tasks_oem(){     
  maxHeight = [$('middle_body').getHeight(),$('left_body').getHeight(),$('right_body').getHeight()].max();
    
  //$('left_body_3').style.height = (maxHeight-$('left_body').getHeight()+$('left_body_3').getHeight()) + "px";
  $('left_login_area').style.height = (maxHeight -$('left_body').getHeight()+$('left_login_area').getHeight()) + "px";
    
  $('middle_body').style.height = maxHeight + "px";
  //$('news_box').style.height = (maxHeight -$('right_body').getHeight()+$('news_box').getHeight()) + "px";
  $('cms_content_right_placeholder').style.height = (maxHeight -$('right_body').getHeight()+$('cms_content_right_placeholder').getHeight()) + "px";


//$('left_body_3').style.height= ($('middle_body').getHeight() - $('left_body_1').getHeight() -$('left_body_2').getHeight() -20)+"px";
//$('news_box').style.height=($('middle_body').getHeight() - $('logo_links').getHeight())+"px";
  
}




/*product search results related functions*/

function show_search_result_wrapper(position_beneath){
  var position_beneath_element = $(position_beneath);
        
  if ((position_beneath_element) && ($('search_result_wrapper').style.position !="relative")) {
    $('search_result_wrapper').style.top = (get_top(position_beneath_element) + $(position_beneath_element).getHeight() + 10)+"px";
  }
      
  $('search_result_wrapper').show();
  if (iframeShim) {
    iframeShim.positionUnder($('search_result_wrapper'));
  }
    
}

function hide_search_result_wrapper(){
  if (iframeShim) {
    iframeShim.hide();
  }
  $('search_result_wrapper').hide();
}
  
function show_product_tab(product_id, selected_tab){
  /* highlight the selected tab */
  $$('#search_result_wrapper td.header_background a').each(function(s){
    s.removeClassName('selected');
  });
  $(selected_tab).addClassName('selected');
  $(selected_tab).blur();
  

  /* highlight only the corresponding product thumbnail */
  $$('#product_search_result_header_thumbs li').each(function(s) {
    s.removeClassName('selected');
  });
  $$('#product_search_result_header_thumbs li#product_' + product_id).each(function(s) {
    s.addClassName('selected');
  });


  /*hide all divs containing titles , header+models*/
  $$('#search_result_wrapper div.title','#search_result_wrapper div.header_and_models','#search_result_wrapper div.spacer').each(function(s) {
    s.hide();
  });


  /*unhide ony the title spacer + header and models for the given product id*/
  $('search_result_product_spacer_'+product_id).show();
  $('search_result_product_table_'+product_id).show();
  
}

function show_all_product_tabs(selected_tab){
  /* remove highlight from all tabs and all product thumbnails */
  $$('#search_result_wrapper td.header_background a').each(function(s){
    s.removeClassName('selected');
  });
  $(selected_tab).addClassName('selected');
  $(selected_tab).blur();
  $$('#product_search_result_header_thumbs li').each(function(s) {
    s.removeClassName('selected');
  });
  
  
  /*hide all divs containing titles , header+models*/
  $$('#search_result_wrapper div.title','#search_result_wrapper div.header_and_models','#search_result_wrapper div.spacer').each(function(s) {
    s.show();
  });
}

function toggle_product_columns(table_id){
  $$('#'+table_id+' .invisible').each(function(s) {
    s.toggle();
  });
}