
// 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,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,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.

/* =========================================================
 * carosel.js v0.3
 * http://www.bamroberts.com
 * =========================================================
 * Copyright 2011 Ben Roberts.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================= */
$.fn.infiniteCarousel = function () {
    return this.each(function () {
      //create wrapper
      //crete window
      //ul is content
      //li is items
    
    
        var $carousel = $(this).css('position', 'relative'),
            $slider = $carousel.find('> ul'),
            window=$slider.wrap('<div class="window" />'),
            $wrapper = $carousel.find('> div.window',this).css('overflow', 'hidden'),
           
        
            $items = $slider.find('> li').css('float','left'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            totalWidth=$slider.css('width',singleWidth * ($items.length+1)),
            
            height= $slider.css('height',$single.outerHeight()),
           
            //visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = $items.length;            

			$slider.append($single.clone());
			
			wrap=true;

        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left=((page==1))?0:'+=' + singleWidth * dir * n;
            
			    if (page==0){
				  page=pages;
				  $wrapper.animate({scrollLeft : singleWidth*(pages)}, 0);
				}
			
                console.log('page ' + page);
                console.log('n ' + n);
                console.log('left ' + left);	
            $('input.page_'+page, this.parentNode).attr('checked', 'checked');
            $wrapper.animate({
                scrollLeft : left
            }, 1500, function () {
                 if (page > pages) {
                    console.log('start-');
					$wrapper.animate({scrollLeft : 0}, 0)
					$('input.page_1', this.parentNode).attr('checked', 'checked');
                    return gotoPage(1)
                    //$wrapper.scrollLeft(singleWidth );
                    // reset back to start position
                    //page = 1;
                } else if (page == 0) {
                    //$wrapper.scrollLeft(singleWidth  * (pages-1));
                    //page = pages;
					console.log('page-'+page);
					//$wrapper.animate({scrollLeft : singleWidth*(pages-2)}, 0);
					//$('input.page_'+pages, this.parentNode).attr('checked', 'checked');
                    return gotoPage(pages);
                    c//onsole.log('end-');
                }
                currentPage = page;
            }); 
            clearTimeout(wait);               
            wait = setTimeout(incrementPage, 5000);
            return false;
        }
        
        
        $carousel.append('<div class="control"></div>')
        $control=$carousel.find('.control');
        
        $carousel.append('<a class="arrow back">&laquo;</a>'); 
        $items.each(function (index) {$control.append('<input class="control page_'+(index+1)+'" type="radio" value="'+(index+1) + '" name="page" />')});
        $carousel.append('<a class="arrow forward">&raquo;</a>'); 
        
        $('input.page_1', this.parentNode).attr('checked', 'checked');
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        $('input.control', this).click(function () {
            gotoPage(Math.abs(this.value));
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
        var state=1;
        
        function incrementPage(){
          if (state==0) return gotoPage(currentPage);  
          return gotoPage(currentPage+1);
        };
        
        var wait = setTimeout(incrementPage, 5000);
        $wrapper.mouseenter(function(){state=0;});
        $wrapper.mouseleave(function(){state=1;});        
    });  
};

//$(document).ready(function () {
  $('.carousel').infiniteCarousel();
  $.fn.CloseButton = function () {
    return this.each(function () {
	  $(this).click(function (e,el) {
	    e.preventDefault();
	    $.get(this.href);
	    $(this).parent().slideUp();
	  });
	});
  };
	
  $('.alert-message a.close').CloseButton();
  
  
//});
$(document).ready(function() {
 $(document).keydown(function(e) {
   if (e.keyCode != 18 && !e.altKey)
        return;

   $("[accesskey]").each(function() {
   if ($("div#access_" + $(this).attr("accesskey")).length > 0)
        return;
        
   var content=$(this).attr("title") || $(this).text();
   $("<div id='access_" + $(this).attr("accesskey") + "' class='access'><span>" + $(this).attr("accesskey") + "</span> " + content +"</div>")
                .insertAfter($(this));

    $(this).addClass("accessactive");
   });
  });

  $(document).keyup(function(e) {
     if (e.keyCode != 18 && !e.altKey)
         return;

     $("[accesskey]").each(function() {
         $("div#access_" + $(this).attr("accesskey")).remove();
         $(this).removeClass("accessactive");
     });
  });
});

/* =========================================================
 * bootstrap-modal.js v1.3.0
 * http://twitter.github.com/bootstrap/javascript.html#modal
 * =========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================= */


(function( $ ){

 /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  * ======================================================= */

  var transitionEnd

  $(document).ready(function () {

    $.support.transition = (function () {
      var thisBody = document.body || document.documentElement
        , thisStyle = thisBody.style
        , support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined
      return support
    })()

    // set CSS transition event type
    if ( $.support.transition ) {
      transitionEnd = "TransitionEnd"
      if ( $.browser.webkit ) {
      	transitionEnd = "webkitTransitionEnd"
      } else if ( $.browser.mozilla ) {
      	transitionEnd = "transitionend"
      } else if ( $.browser.opera ) {
      	transitionEnd = "oTransitionEnd"
      }
    }

  })


 /* MODAL PUBLIC CLASS DEFINITION
  * ============================= */

  var Modal = function ( content, options ) {
    this.settings = $.extend({}, $.fn.modal.defaults)
    this.$element = $(content)
      .delegate('.close', 'click.modal', $.proxy(this.hide, this))

    if ( options ) {
      $.extend( this.settings, options )

      if ( options.show ) {
        this.show()
      }
    }

    return this
  }

  Modal.prototype = {

      toggle: function () {
        return this[!this.isShown ? 'show' : 'hide']()
      }

    , show: function () {
        var that = this
        this.isShown = true
        this.$element.trigger('show')

        escape.call(this)
        backdrop.call(this, function () {
          that.$element
            .appendTo(document.body)
            .show()

          setTimeout(function () {
            that.$element
              .addClass('in')
              .trigger('shown')
          }, 1)
        })

        return this
      }

    , hide: function (e) {
        e && e.preventDefault()

        var that = this
        this.isShown = false

        escape.call(this)

        this.$element
          .trigger('hide')
          .removeClass('in')

        function removeElement () {
          that.$element
            .hide()
            .trigger('hidden')

          backdrop.call(that)
        }

        $.support.transition && this.$element.hasClass('fade') ?
          this.$element.one(transitionEnd, removeElement) :
          removeElement()

        return this
      }

  }


 /* MODAL PRIVATE METHODS
  * ===================== */

  function backdrop ( callback ) {
    var that = this
      , animate = this.$element.hasClass('fade') ? 'fade' : ''
    if ( this.isShown && this.settings.backdrop ) {
      this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
        .click($.proxy(this.hide, this))
        .appendTo(document.body)

      setTimeout(function () {
        that.$backdrop && that.$backdrop.addClass('in')
        $.support.transition && that.$backdrop.hasClass('fade') ?
          that.$backdrop.one(transitionEnd, callback) :
          callback()
      })
    } else if ( !this.isShown && this.$backdrop ) {
      this.$backdrop.removeClass('in')

      function removeElement() {
        that.$backdrop.remove()
        that.$backdrop = null
      }

      $.support.transition && this.$element.hasClass('fade')?
        this.$backdrop.one(transitionEnd, removeElement) :
        removeElement()
    } else if ( callback ) {
       callback()
    }
  }

  function escape() {
    var that = this
    if ( this.isShown && this.settings.keyboard ) {
      $('body').bind('keyup.modal', function ( e ) {
        if ( e.which == 27 ) {
          that.hide()
        }
      })
    } else if ( !this.isShown ) {
      $('body').unbind('keyup.modal')
    }
  }


 /* MODAL PLUGIN DEFINITION
  * ======================= */

  $.fn.modal = function ( options ) {
    var modal = this.data('modal')

    if (!modal) {

      if (typeof options == 'string') {
        options = {
          show: /show|toggle/.test(options)
        }
      }

      return this.each(function () {
        $(this).data('modal', new Modal(this, options))
      })
    }

    if ( options === true ) {
      return modal
    }

    if ( typeof options == 'string' ) {
      modal[options]()
    } else if ( modal ) {
      modal.toggle()
    }

    return this
  }

  $.fn.modal.Modal = Modal

  $.fn.modal.defaults = {
    backdrop: false
  , keyboard: false
  , show: true
  }


 /* MODAL DATA- IMPLEMENTATION
  * ========================== */

  $(document).ready(function () {
    $('body').delegate('[data-controls-modal]', 'click', function (e) {
      e.preventDefault()
      var $this = $(this).data('show', true)
      $('#' + $this.attr('data-controls-modal')).modal( $this.data() )
    })
  })

})( window.jQuery || window.ender )
