
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};

// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,clear,count,debug,dir,dirxml,error,exception,firebug,group,groupCollapsed,groupEnd,info,log,memoryProfile,memoryProfileEnd,profile,profileEnd,table,time,timeEnd,timeStamp,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());


// place any jQuery/helper plugins in here, instead of separate, slower script files.

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};



// Seeded by a komodomedia article:
// http://www.komodomedia.com/blog/2008/07/using-jquery-to-save-form-details/
      
// Written by Alex Wreschnig
// Started 3/11/2009
// Generalized and turned into a plugin on 12/12/2009
      
jQuery.fn.saveToCookie = function(options){
  var options = jQuery.extend({
    prefix: "form-"
  }, options);

  this.each(function(){
    // if we have a checkbox

    if($(this).attr("type") == "checkbox") {
      // Get "checked" status and set name field
      var isChecked = $(this).is(':checked'); 
      var name = options['prefix'] + $(this).attr('id');

      //if this item has been cookied, restore it
      
      if($.cookie( name ) == "true") {
        $(this).attr( 'checked', 'checked' );
      }else{
        $.cookie( name ) =="false" ? $(this).attr( 'checked', false ): '';
      }
      
      //log($.cookie('form-jobtype-8'));
      //assign a change function to the item to cookie it
      $(this).change(
        function(){
          $.cookie( name, $(this).is(':checked'), { path: '/', expires: 365 });
        }
      );
    }
  
    // if we have a radio button
    else if($(this).attr("type") == "radio") {
      /* We need to do something special for radio buttons.
         We'll identify radio buttons not only by name
         but by value. */
      var isSelected = $(this).attr('selected');
      var name = options['prefix'] + $(this).attr('id');
      var value = $(this).val();

      // Unlike the others, we have to test against the value here.
      // After we make sure the cookie exists.
      if($.cookie( name )) {
        if ($.cookie ( name ) == value) {
          $(this).attr( 'checked', 'checked' );
        }
      }
      // Assign a change function to the item to cookie it
      $(this).change(
        function(){
          $.cookie( name, $(this).val(), { path: '/', expires: 365 });
        }
      );
    }
  
    // otherwise it's probably a text input, a textarea element, or a select element.
    else {
      //if this item has been cookied, restore it
      var name = options['prefix'] + $(this).attr('id');

      if($.cookie( name )) {
        $(this).val( $.cookie(name) );
      }
      //assign a change function to the item to cookie it
      $(this).change(
        function() {
          $.cookie(name, $(this).val(), { path: '/', expires: 365 });
        }
      );
    }
  });
  return this;
};


/* 
// List Ticker by Alex Fish 
// www.alexefish.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
*/

(function($){
  $.fn.list_ticker = function(options){
    
    var defaults = {
      speed:4000,
	  effect:'slide',
	  run_once:false,
	  random:false
    };
    
    var options = $.extend(defaults, options);
    
    return this.each(function(){
      
      var obj = $(this);
      var list = obj.children();
      var count = list.length - 1;

      list.not(':first').hide();
      
      var interval = setInterval(function(){
        
        list = obj.children();
        list.not(':first').hide();
        
        var first_li = list.eq(0)
		var second_li = options.random ? list.eq(Math.floor(Math.random()*list.length)) : list.eq(1)
		
		if(first_li.get(0) === second_li.get(0) && options.random){
			second_li = list.eq(Math.floor(Math.random()*list.length));
		}
	
		if(options.effect == 'slide'){
			first_li.slideUp();
			second_li.slideDown(function(){
				first_li.remove().appendTo(obj);
				
			});
		} else if(options.effect == 'fade'){
			first_li.fadeOut(function(){
				obj.css('height',second_li.height());
				second_li.fadeIn();
				first_li.remove().appendTo(obj);
			});
		}
		
		count--;
		
		if(count == 0 && options.run_once){
			clearInterval(interval);
		}
		
      }, options.speed)
    });
  };
})(jQuery);
