/**
 * @namespace The FBN object provides a namespace for all things FBN
 */
var FBN = window.FBN || {};

/**
 * @namespace Holds global functionality
 */
FBN.global = FBN.global || {};


window.LANE = window.LANE = {};

(function() { 
var Lane = {

  // storage for additional things to be run at page init
  //initItems: [],
  
  // yeah, this is to store carousels, if we so desire.
  // guessing i'll remove this soon.
  carousels: {},

  /**
  * Suckerfish method for nav dropdowns
  */
  sfHover: function() {
	$('#nav li').each(function() {
	  $(this).hover(
	    function() {
        	$('.sorter select').blur();
	    	$(this).addClass('sfhover');
	    },
	    function() {
	    	$(this).removeClass('sfhover');
	    }
	  );
		
	});
  }, // sfHover


  /**
  * Removes label from text fields    
  */
  removeLabel: function(field) {
    var node = $(field);
    var defaultText = node.val();
    
    node.focus(function() {
  	  if($(this).val()== defaultText) {
	    $(this).val('');
	  }
      $(field).blur(function() {
        var userInput = $(this).val();
        if(userInput=='') {
          $(this).val(defaultText);
        }
      }); // blur
    }); // focus
  }, // removeLabel


  /**
  * Directs user to the selected page  
  */
  selectNav: function(field) {
    var node = $(field);

    node.change(function() {
        if(!$(this).val() == '') {
            location.href = $(this).val();
        }
    }); // change
  }, // selectNav


  /**
  * Activates dropdowns on page
  */
  activateDropDowns: function() {
    $('ul.dropdown').each(function() {
      $(this).find('a.ddLink').click(function(e) {
        e.preventDefault();
        $(this).next().toggleClass('hide');
        $(this).blur();
      });
    });
  }, // activateDropDowns

  /**
  * manages standard lightbox forms
  */
  lbForm: {
    launch : function(url,params) {
            
        // manipulate url for full page v. ajax response
        url = url.replace(/format=nojs/,"format=ajax");
        
        $.openDOMWindow($.extend({
            borderSize:'0',
            windowBGColor: '#fff',
            width: 505,
            windowSource: 'ajax',
            windowSourceURL: url,
            functionCallOnOpen: Lane.lbForm.handleForm
        },params));
    },
    handleForm : function() {
        // hijack form inside response

        $("#DOMWindow form").bind("submit",function(e) {
        
            // manipulate url for full page v. ajax response
            var formUrl = $(this).attr("action").replace(/format=nojs/,"format=ajax");
        
        
            if ("post" == $(this).attr("method").toLowerCase()) {
                    $.post(formUrl,$(this).serializeArray(),function(data) {
                    $("#DOMWindow").html(data);
                    Lane.lbForm.handleForm();
                },"html");
            } else {
                // make get request
                $.get(formUrl,$(this).serializeArray(),function(data) {
                    $("#DOMWindow").html(data);
                    Lane.lbForm.handleForm();
                },"html");
            }
            e.preventDefault();
        });
        $("#DOMWindow a.closeDOMWindow").bind("click",function(e) {
            $.closeDOMWindow();
            e.preventDefault();
        });
    }
  },
  
  /**
  * manages footer behavior
  */
  footer: {
    init : function() {
        $("#footer a.emailList").bind("click", function(e) {
            Lane.lbForm.launch(this.href,{height: '220'});
            e.preventDefault();
        });
    }
    
  },
  
  /**
  * Opens links in New Window
  */
   newWin : function() {
       $("a.newWin").bind("click",function(e) {
            window.open(this.href);
            e.preventDefault();
        });
   },

  /**
  * Initializes all global page interactions
  */
  _init: function() {
  	this.sfHover();
  	this.removeLabel('.searchTerm');
  	this.selectNav('#selectCollection');
  	this.activateDropDowns();
  	this.footer.init();
  	this.newWin();
  } // _init
  
}; // Lane Namspace

$(document).ready(function() {
  Lane._init();
});

window.LANE = Lane;

})();
