// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// default for search box
var DEFAULT_SEARCH_TEXT = 'Search for Design Templates';

// global page setup
$(function() {
  
	// search default text
  $('#layout_search_input').emptyVal(DEFAULT_SEARCH_TEXT);

  // localize any dates
  $('.utc_datetime').localizeDateTime();
  $('.utc_date').localizeDate();
	
	var submenu_showing = null;
	$('.layout_header_nav_main_item').each(function() {
				$('a.menu_arrow, a.menu_arrow2, a.menu_arrow3, a.menu_arrow4, a.menu_arrow5, a.menu_arrow6, a.menu_arrow7, a.menu_arrow8, a.menu_arrow9', this).mouseover(function() {
								$(this).siblings('div.layout_header_nav_sub_wrapper:hidden, div.layout_header_nav_sub_wrapper2:hidden, div.layout_header_nav_sub_wrapper3:hidden, div.layout_header_nav_sub_wrapper4:hidden,  div.layout_header_nav_sub_wrapper5:hidden, div.layout_header_nav_sub_wrapper6:hidden').slideDown(300);
				})
				$(this).hover(function() {
								
				},
				function() {
								$('div.layout_header_nav_sub_wrapper:visible, div.layout_header_nav_sub_wrapper2:visible, div.layout_header_nav_sub_wrapper3:visible, div.layout_header_nav_sub_wrapper4:visible, div.layout_header_nav_sub_wrapper5:visible, div.layout_header_nav_sub_wrapper6:visible', this)
												.slideUp(200);
				})
	});

  // focus first form field 
  $(function() {
//    $('#layout_content INPUT[type="text"]:first').focus();
  });

  //
  // fix ie select width
  //
  
  // for each select on page...
  $('select').each(function() {
    var select = $(this);
    
    // save current width
    select.data('width', select.outerWidth());
    
    // select is not opened
    select.data('open', false);
    
    // flag; indicates that select is opening
    var opening = false;
    
    // on mousedown
    select.mousedown(function(){
      if($.browser.msie) {
        var self = $(this);
        
        // if the select is not open
        if (!self.data('open')) {
          
          // give select auto width so that it fully shows all option text
          self.css({"width":"auto", position:'absolute'});
          
          // if auto-width is smaller than min-width, restore original width
          if (self.width() < parseInt(self.css('min-width').replace('px', '')) ) {
            self.css({"width":self.data('width'), position:''});          
          }
          
          // this will cancel the other click-related handlers while opening select
          opening = true;
        }
      }
    });
    
    // click blur select
    select.bind('click blur select', function(){
      if ($.browser.msie) {
        var self = $(this);

        // if this select is opening, complete operation here
        if (opening) {
          opening = false;
          self.data('open', true);
          
        // select is not opening; "close" it by restoring original width
        } else {
          if ( self.data('open') ) {
            self.css({"width":self.data('width'), position:''});
          
            self.data('open', false);
          }
        }
      }
    });

  });
  
  

})


// validation - any failing validator sets formValid = false; validators has the invalid validator functions to rerun.
var validators = {};
var formValid;

// sets up the target (textbox or textarea) to show a string when no other value has been typed in
jQuery.fn.emptyVal = function(emptyVal) {
	var target = this;
	
	this.parents('form').submit(function() {
		if (target.val() == emptyVal) {
			target.val('');
		}	
	})
	
	if ((this.val() == '') || (this.val() == emptyVal)) {
		this.val(emptyVal).addClass('empty');
	}
	return this
		.focus(function(){
			
			if (target.val() == emptyVal) {
				target
					.val('')
					.removeClass('empty');
			}
		})
		.blur(function() {
			if (target.val() == '') {
				target
					.val(emptyVal)
					.addClass('empty');
			}				
		})
}

// change utc dates on page to local browser date
jQuery.fn.localizeDateTime = function() {
				this.each(function() {
								dateString = $(this);
								var dt = Date.parse(dateString.text());
								dateString.text(dt.toString('h:mmtt') + ' on ' + dt.format('%x'));								
                dateString.show();
				})
}

jQuery.fn.localizeDate = function() {
				this.each(function() {
								dateString = $(this);
								var dt = Date.parse(dateString.text());
								dateString.text(dt.format('%x'));
                dateString.show();
				})
}

// "Remaining" support
function enforce_remaining(field, count) {
  var input = $('#' + field);
  var text = input.val();
  
  if (text.length >= count) {
    input.val(text.substr(0, count));
    update_remaining(field, count);
    return false;
  }
  update_remaining(field, count);
  return true;  
}
function update_remaining(field, count) {
  var text = $('#' + field).val();
  $('#' + field + '_remaining_count').text(count - text.length);
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}



