// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

(function($) {
  $.fn.set_current_page = function(root_url, current_url, locale) {
  
  if (root_url) {
    $("#nav > ul > li").each(function() {
      href = $(this).find("a").attr("href");
      if (href == "/" + root_url || href == "/" + locale + "/" + root_url) { 
        $(this).find("a:first").removeClass().addClass("current_nav"); 
      }
    })
  };
  if (current_url) {
    current_url
     $(".menu_column a").each(function() {
       href = $(this).attr("href");
       if (href == current_url || href == "/" + locale + current_url) { 
         $(this).addClass("unlink").click(function() {return false});
       };
     })
     // Hide all the sub items for Dangerous Goods Division in other pages.
     if (current_url == "/specialty-offerings" || current_url == "/specialty-offerings/freund-container" || current_url == "/specialty-offerings/qorpak") {
       $(".sidebar_level_three").hide();
     };
   };
  };
	
	var change_fields = function($form, form_type) {
    if (form_type != "") {
      $form.find("li").show().end()
        .find("li:contains('Relationship')").hide().end()
        .find("li:contains('File')").hide().end()
        .find("li:contains('Position')").hide().end()
    } else {
  	  $form.find("li:not(:first)").hide();
    }	  
    if (form_type == "Feedback") {
      $form.find("li").show().end()
        .find("li:gt(18):not(:contains('Relationship')):not(:last)").hide().end()
        .find("li:contains('File')").hide().end()
        .find("li:contains('Position')").hide();
    }   
    if (form_type == "Send Resume") {
      $form.find("li:not(:first)").hide().end().find("li::last").show();
      $form.find("li:lt(18):not(:eq(1))").show().end()
        .find("li:contains('Company')").hide().end()
        .find("li:contains('Title')").hide().end()     
        .find("li:contains('Website')").hide().end()
    } 
    if (form_type == "Request Qorpak Catalog" || form_type == "Dangerous Goods Catalog") {
      $form.find("li:contains('Annual')").hide().end()
        .find("li:contains('Relationship')").hide().end();            
    }    
    if (form_type == "Request Freund Catalog") {
      $form.find("li:contains('Relationship')").hide().end();            
    }
    // Show or Hide required * for each field.
    if (form_type != "Request Quote" && form_type != "Request Samples" && form_type != "Feedback") {
      $form.find("li:contains('Message')").find("abbr").hide();
    } else {
      $form.find("li:contains('Message')").find("abbr").show();
    }
    if (form_type == "Feedback") {
      $form.find("li:contains('Preferred')").find("abbr").show();
    } else {
      $form.find("li:contains('Preferred')").find("abbr").hide();
    }    
    if (form_type == "Request Quote" && form_type == "Request Samples") {
      $form.find("li:contains('Market')").find("abbr").show();
      $form.find("li:contains('Product')").find("abbr").show();
      $form.find("li:contains('Annual')").find("abbr").show();
    } else {
      $form.find("li:contains('Market')").find("abbr").hide();
      $form.find("li:contains('Product')").find("abbr").hide();
      $form.find("li:contains('Annual')").find("abbr").hide();   
    };
	}
	$.fn.show_contact_us_fields = function() {
	  var $form = $(this);
	  var form_type = $("#contact_us_form_type").val()
    change_fields($form, form_type);	  
    $("#contact_us_form_type").change(function() {
      var form_type = $(this).val();
      change_fields($form, form_type);
    })
	};		  
})(jQuery);


$(function() {
  // Packaging Perspectives Comment submit by ajax.
  $(".comment_form input[type=image]").click(function() {
    $form = $(this).parents("form");
    url = $form.attr("action");
    serialize = $form.serialize();
    $form.find(".ajax_loader").show();
    $.post(url, serialize)
    return false;
  })  
  
  $(".answer_form input[type=image]").click(function() {
    $form = $(this).parents("form");
    url = $form.attr("action");
    serialize = $form.serialize();
    $form.find(".ajax_loader").show();
    $.post(url, serialize)
    return false;
  })
    
  // Packaging Perspectives sign in/up link.
  notice_signin ="<div class='notice_box'>Please sign in using your email address and password.  If you do not yet have an account, please create one using the Create an Account box below.</div>"
  notice_signup ="<div class='notice_box'>Please provide the information required below to create an account, which will allow you to post comments, questions, answers as well as save products and searches.</div>"
  notice_default = "<div class='notice_box'>Please use the Sign in or Sign up forms on the right first, thank you!</div>"
  $(".widget_link").click(function() {
    widget_box = $(this).attr("href");
    if (widget_box == "#signup_widget_box") {
      notice = notice_signup
    } else if (widget_box == "#signin_widget_box") {
      notice = notice_signin
    } else {
      notice = notice_default
    }
    $(widget_box).find("p:first").before(notice);
    $('.notice_box').delay(10000).slideUp(400);
    return true;
  });
  
  
  $('.notice_box').delay(5000).slideUp(500);
  $('.error_box').delay(5000).slideUp(500);
  
  
      
  $.fn.imageSelector = function( options ) {  

    var settings = {
      'width'  : 606,
      'height' : 446
    };

    return this.each(function() {        
      // If options exist, lets merge them
      // with our default settings
      if ( options ) { 
        $.extend( settings, options );
      }
      result = $(this).attr('id').match(/(\S+)_button/);
      input_id = result[1];
      dialog_id = input_id + "_dialog";
      // imageSelector plugin code here
      $(this).click(function() {
        $("#" + dialog_id).dialog("open");
        return false;
      });
      //alert("#" + dialog_id);
      //alert($("#" + dialog_id).length);
      $("#" + dialog_id).dialog({
        autoOpen: false,
        resizable: false,
        width: settings['width'],		
        height: settings['height'],
        modal: true,
        buttons: {
          "Select": function() {
            var select_image_id = $("input#select_image_id").val();
            var select_image_src = $("input#select_image_src").val();			  
            $("input#"+input_id).val(select_image_id);
            $("img#" + input_id + "_preview").attr("src", select_image_src);			  
            $(this).dialog("close");
          },
          'Cancel': function() {
            $(this).dialog("close");
          }
        }
      });
      
      $("#" + dialog_id + " > ul").css('list-style-type', 'none');
      $("#" + dialog_id + " > ul > li").css('float', 'left');
      $("#" + dialog_id + " ul > li > img").css('border', '3px solid #FFF');
      
      $("#" + dialog_id + " ul li img").click(function() {
        if ($("input#select_image_id").val() != '') {
          $("#" + $("input#select_image_id").val()).css('border', '3px solid #FFF');
        }
        $(this).css('border', '3px solid #1D8895');
        $("input#select_image_id").val(this.id);
        $("input#select_image_src").val(this.src);
      });
    });

  };
  
  // the functions below are used for the text_field_enumerable form helper
  createAddFieldButton = function (objectName, fieldName, enumerable_type) {
    $('#add-field-' + fieldName).button().live('click', function() {
      if (enumerable_type == 'Hash')
        hash_key_field = '<input type="text" size="10" class = "hash-value-key" name="' + objectName + '_' + fieldName + '_key_"' + 'value="" />';
      else
        hash_key_field = '';
      $newField = $('<p>' + hash_key_field + ' <input type="text" size="20" name="' + objectName + '['+ fieldName + '][]" value="" placeholder="' + fieldName + '" /> <a href="#" class="remove-field">Remove</a></p>');
      $newField.find('.remove-field').button();
      $newField.appendTo($('#' + fieldName + '_div')).hide().slideDown();
      return false;
    });
  }
  
  $('.remove-field').live('click', function() {
    if( $(this).parents('div').children('p').size() > 1 ) {
      $(this).parents('p').slideUp(function() {
        $(this).remove()});
      }
      return false;
  });
  
  $('.hash-value-key').live('change', function() {
    result = $(this).next().attr('name').match(/(\S+\[\S+\])\[\S*\]$/);
    if (result) {
      $(this).next().attr('name', result[1] + '[' + $(this).attr('value') + ']')
    }
    return false;
  });
  
  $('.remove-field').button();
});
