// global cool namespace
var COOL = { }

COOL.print = function() {
    $('.print a').click(function() {
        window.print();
        return false;
    });
}

COOL.initialize = function() {
    COOL.print();
}

$(document).ready(function() {
    COOL.initialize();
    
    $("input").click( function () {
      this.select();
    });
    
    $(".dialog").click(function() { 
    var tmp = this.href.split("#")
    div_id = "#"+tmp[tmp.length-1]
    div_html = $(div_id).html()
    $("<div/>").html(div_html).dialog(
      { 
        modal: true,
        overlay: { 
            opacity: 0.5, 
            background: "black" 
        },
        title:"Disclaimer",
        width:400,
        height:200,
        minWidth:400,
        minHeight:200,
        maxWidth:500,
        maxHeight:500 
      }
    );
    return false;
  }); 
  
  $(".dialog-medium").click(function() { 
    var tmp = this.href.split("#")
    div_id = "#"+tmp[tmp.length-1]
    div_html = $(div_id).html()
    $("<div/>").html(div_html).dialog(
      { 
      modal: false,
      width:500,
      height:500,
      minWidth:500,
      minHeight:500,
      maxWidth:600,
      maxHeight:600,
      open: function(){
        loadVideo(div_id);
      }
      }
    );
    return false;
  }); 
});

// fill height function
$(function() {
    var max_height = 0;
    $('.fill-height').each(function(){
      $(this).children("div").each(function(){
        if ($(this).height()>max_height){
          max_height = $(this).height() 
        }  
      })
      $(this).children("div").each(function(){
        $(this).height(max_height) 
        if ($(this)[0].className != "vertical-height"){
          $($(this).children("div")[0]).height(max_height)
        }
      })
    });
});

// fill vertical height function
$(function() {
    var max_height = 0;
    $('.vertical-height').each(function(){
      total_height = 0;
      parent_height = $(this).parents("div").height()
      first_child = $(this).children("div")[0]
      second_child = $(this).children("div")[1]
      
      first_child_height = $(first_child).height() + parseInt($(first_child).css("margin-top").substring(0,$(first_child).css("margin-top").length-2)) + parseInt($(first_child).css("margin-bottom").substring(0,$(first_child).css("margin-bottom").length-2))
      second_child_height = $(second_child).height() + parseInt($(second_child).css("margin-top").substring(0,$(second_child).css("margin-top").length-2)) + parseInt($(second_child).css("margin-bottom").substring(0,$(second_child).css("margin-bottom").length-2))
      
      total_height = first_child_height + second_child_height
      if (total_height < parent_height){
        $(second_child).height(parent_height-first_child_height-4)
      }
    });
});

// fill pannels function
$(function() {
  if ($("#left-pannel")){    
    org_left = $("#left-pannel").height()
    org_body = $("#body-div").height()
    org_right = $("#right-pannel").height()
    fillPannels();
  }  
});

function createSlider(obj,max,step,inc,formName,fieldName,options,first,last){
  $(obj).slider({
  		stepping: step,
  		min: 0, 
  		max: max,
  		range: true,
  		slide: function(e, ui) {
  			
  			measure_value = $("input[@name='adv_measure_value']").val()
  			var minValue = $(obj).slider('value', 0); 
  			var maxValue = $(obj).slider('value', 1);
  			$("#"+fieldName+"_min").addClass("orange-field")
        $("#"+fieldName+"_max").addClass("orange-field")
  			if (measure_value == "mm"){
  			  $("#"+fieldName+"_min").val(((minValue/inc)*25.4).toFixed(1))
          $("#"+fieldName+"_max").val(((maxValue/inc)*25.4).toFixed(1))
          //$(obj).slider("moveTo", 0,0)
        }else{
          $("#"+fieldName+"_min").val(minValue/inc)
          $("#"+fieldName+"_max").val(maxValue/inc)      
        }          
  		},
  		stop: function(){
        var minValue = $(obj).slider('value', 0); 
  			var maxValue = $(obj).slider('value', 1);
  			$("#"+fieldName+"_min").removeClass("orange-field")
        $("#"+fieldName+"_max").removeClass("orange-field")
      },
  		change: function(){
  		  $("input[@name='page']").val(1)
        $('#'+formName).ajaxSubmit(options);
      },
  		handles: [
  			{start: first, min: 0, max: max},
  			{start: last, min: 0, max: max}
  		]
	 });
}

function addPagination(){
  $(".pagination_link").click(function() {
  	var tmp = this.href.split("=");
    page_number = tmp[tmp.length-1];
    return nextPage(page_number);
  });
}

var isOpen = false;
function returnDatasheets(cat,product,measurement,resultDiv){
  if (!measurement){
    measurement = "in"
  }

  search_url = "/search/datasheets/"
  if (cat){
    search_url += cat+"/"
  }
  if (product){
    search_url += product+"/"
  }    

  page_value = "measurement="+measurement

  $.ajax({
     type: "GET",
     url: search_url,
     data: page_value,
     success: function(html){
       $("#"+resultDiv).html(html)
       fillPannels()       
     }
   });
   return false;
}

function swivel(dir, pop) {
    if (!pop){
      var imgs = $('#gallery ul li');
      var cur = $('#gallery ul li.active');
      var num = $('#gallery .pagination-numbers a.selected').html();
    }else{
      var imgs = $('#large-slide-show ul li');
      var cur = $('#large-slide-show ul li.active');
      var num = $('#large-slide-show .pagination-numbers-pop a.selected').html();
    }
    
    if (num != dir){
      $(cur).removeClass("active");
      var len = imgs.size();
  
      
      if (dir == "next") {
          if ( (pos < len) && (pos != (len-1)) ) {
              pos += 1;
          } else if (pos == (len-1)) {
              pos = 0;
          } else {
              pos = 1;
          }
      } else if (dir == "prev") {
          if (pos == 0) {
              pos = (len-1);
          } else {
              pos = (pos-1);
          }
      } else {
          pos = parseInt(dir)-1;
      }
      
        $(imgs[pos]).fadeIn("fast");
        $(imgs[pos]).addClass("active");    
        $(cur).css({display:"none"});
  
        if (!pop){
          $(".pagination-numbers a").each(function(t){
            if (parseInt($(this).html()) == pos +1 ){
              $(this).addClass("selected")
            }else{
              $(this).removeClass("selected")
            }            
          });
        }else{          
          $(".ui-dialog-content .pagination-numbers-pop a").each(function(t){
            if (parseInt($(this).html()) == pos +1 ){
              $(this).addClass("selected")
            }else{
              $(this).removeClass("selected")
            }            
          });
        }
    }
}

function fillPannels(){
  left_pannel = "#left-pannel"
  body_pannel = "#middle-pannel"
  right_pannel = "#right-pannel"
  
  
  if (org_right>org_left && org_right>$(body_pannel).height()){
    $(left_pannel).height(org_right)
    $(right_pannel).height(org_right)
  }else if ($(body_pannel).height()>org_left && $(body_pannel).height()>org_right){
    $(left_pannel).height($(body_pannel).height()) 
    $(right_pannel).height($(body_pannel).height()) 
  }else if (org_left>org_right && org_left>$(body_pannel).height()){
    $(left_pannel).height(org_left)
    $(right_pannel).height(org_left)
  }
}



  
