/**
 * This is the default javascript file for the Frieslandbank website
 * It is loaded on every page, after the jquery library.
 * 
 * Contents:
 * - FB_Global object, global settings
 * - Document ready functions that occur on every page
 * - jQuery easing plugin, custom easing (transitional) effects
 * - Panel toggler, provides functionality to open and close panels / components
 * 
 * Global settings
 */
var Base = {
  debug: true,
  
  /**
   * Either console.debug or alerts a message
   * @param {Object} message
   */
  log: function(message) {
		this._notify(message, 'debug');
	},
  
  /**
   * Either console.warn or alerts a warning message
   * @param {Object} message
   */
  warn: function(message) {
		this._notify(message, 'warn');
	},
  
  /**
   * Either console.error or alerts an error message
   * @param {Object} message
   */
  error: function(message) {
		this._notify(message, 'error');
	},
  
  /**
   * Sets the actual message
   * @param {Object} message
   * @param {Object} lvl
   */
  _notify: function(message, lvl){
    if( this.debug == true ){
      if(window.console) {
        switch( lvl ){
          case 'debug':
            console.debug(message); break;
          case 'warn':
            console.warn(message); break;
          case 'error':
            console.error(message); break;
        }
  		} else {
        switch( lvl ){
          case 'debug':
            lvl = ''; break;
          case 'warn':
            lvl = 'Warning: '; break;
          case 'error':
            lvl = 'ERROR: '; break;
        }
  			alert(lvl + message);
  		}
    }
  }
}


/**
 * Execute on document ready on every page
 */
$(function(){
  // IE 6 fix for mouseovers
  
  if( $.browser.msie && $.browser.version.substring(0,1) == 6 ){
    // Main menu & Tabs
    $('div#main-nav li, div.tabs li').not('li.active').hover(
      function(){ $(this).addClass('hover'); }, // over
      function(){ $(this).removeClass('hover'); } // out
    );
  }
  
  // Accordion
  // first show hidden tabs or else infoCordion will not init right
  var hidden_tabs = $('.ppzone-tab-content:hidden');
  $(hidden_tabs).show();
  $('div.bigfaq dl').infoCordion();
  $('div.lightbox .faq-content dl').infoCordion({printContent:true});
  $('div.box-fullwidth .faq-content dl').infoCordion({printContent:true});
  $(hidden_tabs).hide();
  
  
  
  // Form information tooltips
	$('.tooltip').live('mouseover',function(e) { // built and show the tooltip div
		var html = $(this).parent().children('.tooltip_content').html();
		var offset = $(this).offset();
		$('body').prepend('<div class="tooltip_box"><div class="tooltip_text">' + html + '</div></div>');
		
		var new_top = Math.round(offset.top-1);
		var new_left = Math.round(offset.left+20);

		var win_height  = $(window).height(); 
		var tooltip_bottom = new_top + $('body > div.tooltip_box').height() - $(window).scrollTop();	
			
		if (tooltip_bottom > win_height) {
			new_top += (win_height - tooltip_bottom)-10;
		}
		
		$('body > div.tooltip_box').css({
			top: new_top, left: new_left
		});
	}).live('mouseout',function() { // remove the tooltip div
		$('body div.tooltip_box').remove();
	});


  
  // Visually enhance accordion mouseover state
  $('.bigfaq dt').hover(
    function(){ $(this).find('span').addClass('hover'); }, // over
    function(){ $(this).find('span').removeClass('hover'); } // out
  );
  
  
  // Contact panel
  $('.contactpanel.flex').togglePanel(34, {
    open_easing: 'easeOutQuad',
    close_easing: 'easeOutQuad'
  });
  $('.contactpanel.mini .bottom').unbind('click'); //exclude contactpanel midi  
  
  
  // Mouse overs for buttons
  $('input[type=submit]').hover(
    function(){ $(this).addClass('hover'); }, // over
    function(){ $(this).removeClass('hover'); } // out
  );
  
  $('button.route').hover(
    function(){ $(this).addClass('hover'); }, // over
    function(){ $(this).removeClass('hover'); } // out
  );
});




/**
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright Â© 2008 George McGinley Smith
 * All rights reserved.
 */
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
  def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});


function validateDate(date) {
	var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
	
	if (!validformat.test(date)) {
		return false;
	} else {
		var date_arr = date.split('/'); 
		var dayfield=date_arr[0];
		var monthfield=date_arr[1];
		var yearfield=date_arr[2];
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
	
	if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) {
			return false;
		} else {
			return true;
		}
	}
}

// function to get url from <a> on <.bg> in teaser 

	$('.teaser.link').click(function() {
		document.location= $(this).find('a').attr('href');
	});

/**
 * Panel toggler
 * 
 * @param {int} height_closed Height of elem when closed WITHOUT padding
 * @param {Object} opts Set these if you want to deviate from default behaviour
 * @return {Object} jQuery
 */
(function($){
  $.fn.togglePanel = function(height_closed, opts){
    opts = $.extend({
      click_selector : '.bottom',
      open_class   : 'open',
      open_speed   : 500,
      open_easing  : 'easeOutElastic',
      close_speed  : 300,
      close_easing : 'easeInQuart',
      hide_content : false
    }, opts || {});
    
    var obj = this;
    var bgElem = $(obj).find('.bg');
    var height_open = bgElem.height();
    var newH, speed, easeing;
    
    // Collapse the block on page load
    bgElem.height(height_closed);
    
    // Bind the click event
    obj.find(opts.click_selector).unbind('click').click(slide);
    
    // Return for chaining
    return obj;
    
    
    /**
     * Opens or closes the panel
     */
    function slide()
    {
      // Set relevant variables
      if(! obj.hasClass('open') ){
        newH = height_open;
        speed = opts.open_speed;
        easing = opts.open_easing;
        obj.addClass('open');
      }
      else {
        newH = height_closed;
        speed = opts.close_speed;
        easing = opts.close_easing;
        obj.removeClass('open');
      }
      
      // animate bg div and return elem for chaining
      bgElem.animate({height: newH+'px'}, speed, easing);
      return false;
    }
  }
})(jQuery);