// 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);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// 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,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});

var APP = (function($) {
    var app = {}, $el, on = 'on';
    // Public functions
    app.init = function() {
        $('a[href=#]').attr('href', 'javascript:;');
        $('#nav > li').mouseenter(onNavOver).mouseleave(onNavOut);
        // Set up the global ajax
        $.ajaxSetup({ cache: false, error: function errorLog(x, e) { log(x, e); }, type: 'POST' });
        if (!Modernizr.csstransitions) { $.getScript(STATIC_URL + 'javascripts/behaviors/css3.js'); }
        if (!Modernizr.input.placeholder) { placeholder(); }

	      $('.faq-module .question a').click(onFAQClick);
	      $("#datepicker").datepicker({ showOtherMonths: true });
	      $('.revol-now, .revol-then').hover(revolOver, revolOut);

        var home_selector = ".home-slider", slideshow_selector = ".slideshow",
            home_and_slideshow_selectors = home_selector + ", " + slideshow_selector, $homeslider = $(home_selector),
            $slideshow = $(slideshow_selector), $home_and_slideshow = $(home_and_slideshow_selectors),
            slideshow_props = {
              displaySlideQty:3, moveSlideQty:1, pager:true, pagerSelector:'#my-pager',
              onBeforeSlide: updateSlideCaption, onAfterSlide: fadeInText,
              auto: true, pause: 5000
            };
        if ($homeslider.length > 0) {
          $el = $homeslider.find("li.slider-element:last").remove();
          $el.prependTo(home_selector);
          $homeslider.bxSlider(slideshow_props);
        }
        if ($slideshow.length > 0) {
          $slideshow.bxSlider(slideshow_props);
        }
	      if ($('.slideshow2').length > 0){
	          $('.slideshow2').bxSlider({
              displaySlideQty: 3, moveSlideQty: 1, pager: true, pagerSelector: '#my-pager2'
            });
	      }
        $('li > label[for=id_birthday_month]').parent().addClass('birthday');
        if  ($('#id_billing_detail_first_name').is(':hidden')) {
            $('#id_billing_detail_country, #id_shipping_detail_country').hide();
        }
        $('#messages .info').delay(8000).fadeOut()
        infieldLabels();
    };
    // Private functions
    function fadeInText() {
        $(".single-slide *").fadeIn(200);
    }
    function fadeOutText(slide, total) {
        $(".single-slide *").fadeOut(200);
    }
    function revolOut(e) {
        $('.revol-now').css('background','#cf7019').css('color','#FFF');
		    $('.revol-then').css('background','#231f20').css('color','#FFF');
    }
    function revolOver(e) {
        $('.revol-now').css('background','#FFF').css('color','#cf7019');
		    $('.revol-then').css('background','#FFF').css('color','#231f20');
    }
    function updateSlideCaption(currentSlideNumber, totalSlideQty, currentSlideHtmlObject){
        fadeOutText();
        $el = $('.caption-slider');
        var $slide = $(currentSlideHtmlObject).next(),
            $img = $slide.find('img'),
            url = $img.data('url');
        $el.find('p').html('<a href="' + url + '">' + $img.attr('alt') + '</a>');
        $el.find('span').text($img.data('caption'));
    }
    function placeholder() {
        var d;
        $('footer input[placeholder!=""]').each(function(idx, el){
            $el = $(el);
            d = $el.attr('placeholder');
            $el.focus(function onFocus() { 
                if (this.value === d) { this.value = ''; }
            }).blur(function onBlur() { 
                if ($.trim(this.value) === '') { this.value = d; }
            });
            $el.blur();
        });
    }
    function onFAQClick(e) {
        $el = $(e.currentTarget);
        $el.parent().toggleClass('closed-question');
        $el.parent().toggleClass('open-question');
    }
    function onNavOut(e) {
        $el = $(e.currentTarget);
        $el.removeClass(on);
    }
    function onNavOver(e) {
        $el = $(e.currentTarget);
        $el.addClass(on);
    }
    function infieldLabels() {
        $(".infield-label").inFieldLabels();
    }
    // Call the init function on load
    $(app.init);
    return app;
} (jQuery));

