(function($){
  $(window).load(function() {
    var footerHeight = 0,
      windowHeight = 0,
      pageHeight = 0;
      
    footerHeight = $('#footer-wrapper').outerHeight();
    pageHeight = $('#page').height();
    windowHeight = $(window).height();
    
    if (pageHeight + footerHeight <= windowHeight) {
      $('#bg-container').css({height: windowHeight - footerHeight});
    }
  });
  $(document).ready(function() {
    
    $('#topnav .announcement').each(function() {
    	$link = $(this);
    	$(this).attr('href','#announcement');
    	$(this).click(function() {
    		return false;
    	});
    	mlid = $(this).parent('li').attr('rel');
    	announcement = eval('announcement_'+mlid);
    	
    	$(this).qtip({
  			content: {
  				text: announcement.body,
  				title: {
  					text: announcement.title,
  					button: true
  				}
  			},
  			position: {
  				my: 'top center',
  				at: 'bottom center',
  				adjust: {
  	         y: 5
  	      }
  			},
  			show: {
  				target: $link,
  				delay: 0,
  				effect: function(offset) {
  					$(this).fadeIn(400); // "this" refers to the tooltip
  	      }
  			},
  			hide: {
  				target: $('#main-wrapper, #main-menu-links'),
  				fixed: true,
  				effect: function() {
  					$(this).fadeOut(400);
  				}
  			},
  			style: {
  				width: {
  					min: 300
  				},
  				classes: 'ui-tooltip-shadow ui-tooltip-plain'
  			}
  		});
    });
  //	preloaded objects
//	  $('#callout-cycles').bind('load',function() {
//	  
//	  	if (cyclesLoaded == false ) {
		  	if ($('#callout-cycles').get(0)) {
			  	$('#page-loading').fadeOut('fast').remove();
			  	$('.main-stage').animate({opacity:1},'fast');
		  	/* homepage cycler */
		  	  $('#callout-cycles').cycle({
		  	    timeout:6500, // time between cycles
		  	    speed:1500, // transition speed
		  	    pager:'#cycle-nav'
		  	  });
		  	}
//		  }
//	  });
	  	
    /* image overlay for collections etc... */
	  $("div.overlayed").hover(
		  function(){
        $overlay = $(".hover-overlay", this);
     
        if (typeof $overlay.data("base") == 'undefined') { 
          $overlay.data("base", {opacity: $overlay.css('opacity')}); 
        }
        
  		  $overlay.stop().animate({opacity: $overlay.data("base").opacity}, 500);
			  $overlay.removeClass('hidden');			
		  },
		  function () {
        overlay = $(".hover-overlay", this);
        
			  overlay.stop().animate({opacity:0.0}, 500);
			  $("img", this).stop().animate({opacity:1.0}, 500);
		  }
	  );
    
    /* 
      attach on click to overlayed divs if there's
      an anchor inside the overlayed div
    */
    $("div.overlayed").click(function(event) {
      if (typeof event.target.href == 'undefined') {      
        anchor = $($(".content a", this).get(0));
        
        if (typeof anchor.attr('href') != 'undefined') {
          if (anchor.attr('target') == '_blank') {
            window.open(anchor.attr('href'));
          }
          else {
            window.location = anchor.attr('href');
          } 
        }
        return false;
      }
    });
    
    
    
    /* ui tabs */
    if ($("#ui-tabs").get(0)) {
	    $("#ui-tabs").tabs();
    }
    
    /* blog archive year expander/collapser */
    if($('li.archive-year').get(0)) {
      $('.archive-year').click(function(){
        if ($(this).next().css('display') == 'none') {
          $(this).removeClass('collapsed');
          $(this).addClass('expanded');
          $(this).next().css('display', 'block');
        }
        else {
          $(this).next().css('display', 'none');
          $(this).removeClass('expanded');
          $(this).addClass('collapsed');
        }
      });
    }
    
    /* blog archive year expander/collapser */
    if($('li.archive-months').get(0)) {
      $('li.archive-months').each(function(index){
        if (!index) {
          $(this).css('display', 'block');
          $(this).prev().removeClass('collapsed');
          $(this).prev().addClass('expanded');
        }
      })
    }
    
    /* brand search keyword default value text */
    if ($('#views-exposed-form-brands-page-2 input[name$="title_1"]').get(0)) {
      $('#views-exposed-form-brands-page-2 input[name$="title_1"]').each(function(index){
        if ($(this).val() == '') {
          $(this).val('Enter keywords...'); 
        }
      });
      
      $('#views-exposed-form-brands-page-2 input[name$="title_1"]').focusin(function(index){
        if ($(this).val() == 'Enter keywords...') {
          $(this).val(''); 
        }
      });
      
      $('#views-exposed-form-brands-page-2 input[name$="title_1"]').focusout(function(index){
        if ($(this).val() == '') {
          $(this).val('Enter keywords...'); 
        }
      });
    }
    
    /* add target blank to all files anchors */
    $('span.file a').attr('target', '_blank');
    
    /* attach form reset to reset buttons */
    $('.input-reset').click(function(event){
      clearForm($(this).closest('form'));
    })
    
    function clearForm(form) {
      // iterate over all of the inputs for the form
      // element that was passed in
      $(':input', form).each(function() {
        var type = this.type;
        var tag = this.tagName.toLowerCase(); // normalize case
        // it's ok to reset the value attr of text inputs,
        // password inputs, and textareas
        if (type == 'text' || type == 'password' || tag == 'textarea') {
          this.value = "";
        // checkboxes and radios need to have their checked state cleared
        // but should *not* have their 'value' changed
        }
        else if (type == 'checkbox' || type == 'radio') {
          this.checked = false;
        // select elements need to have their 'selectedIndex' property set to -1
        // (this works for both single and multiple select elements)
        }
        else if (type == 'select-one') {
          this.selectedIndex = 0;
        }
      });
    }  
    
     /* beer gear functionality */
   $('#beer-gear').click(function(e) {
      e.preventDefault();
      $.confirm({
      'title'   : 'DISCLAIMER - You are now leaving www.thebeerstore.ca',
      'message' : 'By entering https://shop.thebeerstore.ca/ ("the Site"), you are ' +   
                  'leaving www.thebeerstore.ca and entering a website that is neither ' +   
                  'affiliated with nor controlled by Brewers Retail Inc., d.b.a. The ' +   
                  'Beer Store ("The Beer Store"). The Site is externally hosted and ' +
                  'operated by an independent third party, and may have separate terms ' +  
                  'and conditions of use and privacy and security policies, including ' +  
                  'those governing your personal information and credit card ' + 
                  'information. Please review any such terms and conditions and ' +   
                  'policies when you use and make purchases through the Site. The Beer ' +  
                  'Store does not receive a copy of nor has any control over any ' +  
                  'personal information or credit card information disclosed by you in ' +  
                  'using the Site, and is not responsible for the acts or omissions of ' +  
                  'the third party operating the Site or its use of your personal ' +  
                  'information and credit card information. The Beer Store cannot ' +  
                  'ensure, protect or warrant the security of any information you ' +  
                  'transmit using the Site. THE BEER STORE IS NOT RESPONSIBLE FOR ANY ' +  
                  'DAMAGES OR LOSSES ARISING FROM AND EXPRESSLY DISCLAIMS ANY LIABILITY ' +  
                  'ASSOCIATED WITH YOUR USE OF THE SITE, INCLUDING ANY PURCHASES MADE ' +  
                  'USING THE SITE OR THE PRODUCTS PURCHASED ON THE SITE, AND IS NOT ' +  
                  'RESPONSIBLE FOR ANY DAMAGES OR LOSSES ARISING FROM AND EXPRESSLY ' + 
                  'DISCLAIMS ANY LIABILITY ASSOCIATED WITH HOW SUCH THIRD PARTY SITE ' +  
                  'MANAGER OR OPERATOR COLLECTS, STORES, USES OR DISCLOSES YOUR ' +  
                  'PERSONAL INFORMATION AND CREDIT CARD INFORMATION.',
      'buttons' : {
        'I Accept' : {
          'class' : 'blue',
          'action': function(){
            window.open('https://shop.thebeerstore.ca');
          }
        },
        'Cancel'  : {
          'class' : 'gray',
          'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
        }
      }
    }); // end $.confirm

   }); // end #beer-gear click function
    
  });
})(jQuery);
;
(function($){
	
	$.confirm = function(params){
		
		if($('#confirmOverlay').length){
			// A confirm is already shown on the page:
			return false;
		}
		
		var buttonHTML = '';
		$.each(params.buttons,function(name,obj){
			
			// Generating the markup for the buttons:
			
			buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'<span></span></a>';
			
			if(!obj.action){
				obj.action = function(){};
			}
		});
		
		var markup = [
			'<div id="confirmOverlay">',
			'<div id="confirmBox">',
			'<h1>',params.title,'</h1>',
			'<p>',params.message,'</p>',
			'<div id="confirmButtons">',
			buttonHTML,
			'</div></div></div>'
		].join('');
		
		$(markup).hide().appendTo('body').fadeIn();
		
		var buttons = $('#confirmBox .button'),
			i = 0;

		$.each(params.buttons,function(name,obj){
			buttons.eq(i++).click(function(){
				
				// Calling the action attribute when a
				// click occurs, and hiding the confirm.
				
				obj.action();
				$.confirm.hide();
				return false;
			});
		});
	}

	$.confirm.hide = function(){
		$('#confirmOverlay').fadeOut(function(){
			$(this).remove();
		});
	}
	
})(jQuery);;

