$(function(){
  var fullurl = 'http://www.idealfastener.com/';
   var fullurl2 = 'http://www.idealfastener.com/';
  var loaded = true;
  
  $(".update_pager").bind("click",function(){
    var href = $(this).attr('href');
    $(this).attr('href','javascript:void(0)');
    $('#returnurl').val(href);
    $('#UpdateForm').submit();

    return false; 
  })

  
  
  $(".boxy").bind("click",function(){
    if(loaded){
      loaded = false;
      $('#opaco').height($(document).height());
      if($.browser.msie){
        document.getElementById("opaco").style.display = 'block';
        //$('#opaco').height($(document).height()).toggleClass('hidden');
        $("#SearchType").hide();
        $("#SearchSize").hide();
        $("#SearchLock").hide();
        $("#SearchPull").hide();
        $("#select_size").hide();
        $("#select_type").hide();
      }else{
        $('#opaco').toggleClass('hidden').fadeTo('slow', 0.7);
      }
      var href = $(this).attr('href');
      var marginLeft = parseInt($(window).width()/2 - 370) + 'px';
      var marginTop = '50px';
      if(href == '/zippers/zippers')
        var marginTop = '40px';
      $('#popup').html($('#loading').html());
      $('#popup').css({'margin-left':marginLeft, 'margin-top':marginTop});
      document.getElementById("popup").style.display = 'block';
      $('#popup').load( href, {}, function(){
        loaded = true;
      });
    }
    //$('#popup').toggleClass('hidden');
    return false; 
  })
  
  $(".tip").bind("mouseover",function(){
    $('#tip')
    .html($("#" + $(this).attr('id') + '_data').html())
    .toggleClass('hidden');
  });
  
  $(".tip").bind("mousemove",function(e){
    var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
    //Find out how close the mouse is to the corner of the window
    var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
    var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
    var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge< document.getElementById('tip').offsetWidth)
      var left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
    else if (curX<leftedge)
      var left="5px"
    else
      var left=curX+offsetxpoint+"px"
    
    //same concept with the vertical position
    if (bottomedge < document.getElementById('tip').offsetHeight){
      var top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
      
    }else{
      var top=curY+offsetypoint+"px"
    }
    
    $('#tip').css({left: left, top: top});
  });

  $(".tip").bind("mouseout",function(){
    $('#tip').toggleClass('hidden');
    return false;
  })  


  var offsetxpoint = 20;
  var offsetypoint = 20;
  var ie = document.all;
  var ns6 = document.getElementById && !document.all;
  var tipobj = $("#tip");  
  
  $(".header_button").bind("click",function(){
    var img = 'url(data/home/' + $(this).attr("img") + '.jpg)';
    $("#home_img").css({background: img});
    $(".header_button_active").addClass("header_button").removeClass("header_button_active");
    $(this).addClass("header_button_active");
  });

  $(".page_header_button").bind("click",function(){
    var img = 'url(data/home/' + $(this).attr("img") + '.jpg)';
    $("#page_img").css({background: img});
    $(".page_header_button_active").addClass("page_header_button").removeClass("page_header_button_active");
    $(this).addClass("page_header_button_active");
  });
  
  var pScroll = 32;
  
  $(".header_next").bind("click",function(){
    $("#header_button_holder").animate({scrollLeft: '+=' + pScroll + 'px'}, 100);
  });
  $(".header_prev").bind("click",function(){
    $("#header_button_holder").animate({scrollLeft: '-=' + pScroll + 'px'}, 100);
  });
  
  //menus
  
  $('.dropmenu').bind("click",function(){
    var id = $(this).attr("id");
    if($(this).is(".minus")){
      $(this).removeClass("minus").addClass("plus");
      $("#" + id + "_list").addClass("hide");
    }else{
      $(this).addClass("minus").removeClass("plus");
      $("#" + id + "_list").removeClass("hide");
      
    }
  });
  
  $('.dropsubmenu').bind("click",function(){
    var id = $(this).attr("id");
    if($(this).is(".minus2")){
      $(this).removeClass("minus2").addClass("plus2");
      $("#" + id + "_list").addClass("hide");
    }else{
      $(this).addClass("minus2").removeClass("plus2");
      $("#" + id + "_list").removeClass("hide");
      
    }
  });
  
  $('.showbutton').attr('value','');
  
  $("#search_limit").bind('change',function(){
    $("#limitForm").submit();
  })
  
  
  $(".overed").bind("mouseover",function(){
    $(this).attr('src',fullurl + 'data/' + $(this).attr('id') + '_h.gif');
  });
  $(".overed").bind("mouseout",function(){
    $(this).attr('src',fullurl + 'data/' + $(this).attr('id') + '.gif');
  });
  
  $("#select_type").bind("change",function(){

   $("#select_size").load("/zippers/update_size",{type: $(this).val()},function(){
     return true;
   });
  });
  $("#select_size").load("/zippers/update_size",{type: $("#select_type").val(),size: $("#updated_size").val()},function(){
    return true;
});

  

  
})


  function ietruebody(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
  }
    

function closePopup(){
  if($.browser.msie){
   document.getElementById("opaco").style.display = 'none';
  }else{
    $('#opaco').toggleClass('hidden').fadeTo('slow', 0);
  }
  document.getElementById("popup").style.display = 'none';
      $("#SearchType").show();
      $("#SearchSize").show();
      $("#SearchLock").show(); 
      $("#SearchPull").show(); 
      $("#select_size").show();
      $("#select_type").show();
      
      
   return false;
}

//additional properties for jQuery object
$(document).ready(function(){
   //align element in the middle of the screen
   $.fn.alignCenter = function() {
      //get margin left
      var marginLeft = Math.max(40, parseInt($(window).width()/2 - 720)) + 'px';
      //get margin top
      var marginTop = '160px';

      //return updated element
      return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
   };

});

function validate_form () {
  x = document.add_slider.elements.length;
  var thisName = "";
  var tempName = "";
  var count = 1;
  var isValid = true;
  
 	if(document.getElementById('pull_material').value == '')
 	{	alert("Please Select Pull Material."); isValid = false; }
	else if(document.getElementById('lock').value == '')
	{	alert("Please Select Pull Type."); isValid = false;	}
  else if(document.getElementById('pull').value == '')
	{	alert("Please Select Slider Function."); isValid = false;}	
 
  for(i=0; i<x; i++) {
    if(document.add_slider.elements[i].name) {
      thisName = document.add_slider.elements[i].name;
    } else {
      thisName = "none";
    }
    tempName = "size";
    
    if(thisName.search(tempName) != -1) {
      if(!(document.add_slider.elements[i].value)) {
        isValid = false;
        alert("Please enter value for size " + count);
      }
      count++;
    }
  }
  return isValid;
}
function validate_zipper () {
var string = "";
 valid = true;

    if ( document.add_zipper.type.value == "" )
    {
        string += "Please select the Type.\n" ;
    }
    if ( document.add_zipper.size.value == "" )
    {
        string += "Please select the Size.\n" ;
    }
    if ( document.add_zipper.code.value == "" )
    {
        string += "Please add a Code.\n" ;
    }
     if (string != "") {
       alert(string);
       valid = false;
    }


    return valid;

}
 function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  }
