// main closure:
(function($){
  
  
  
  (function($){
  	$.fn.extend({
  		absolutize: function() {
  			return this.each(function() {
  				var offset = $(this).position();
  				var height = $(this).height();
  				var width = $(this).width();
  				$(this).
  					css('position','absolute')
  					.css('top',offset.top)
  					.css('left',offset.left)
  					.css('width',width)
  					.css('height',height)
  				;
  			});
  		}
  	});
  })(jQuery);
  
  
  var cartCollapsed = !Number($.cookie('cartExpanded'));
  
	var sidebarRightWidth = 280;
  
  
	// make sure there's a window.Drupal namespace:
	window.Drupal = window.Drupal || {};
	// remember Drupal.Phoenix (should be undefined)
	var _Phoenix = Drupal.Phoenix;
	// our primary object:
	var Phoenix =
	{
		// Put public API methods here:
		noConflict:function() {
			var __Phoenix = Drupal.Phoenix;
			Drupal.Phoenix = _Phoenix;
			return __Phoenix;
		},
		toggleCartDetail:function() {
			$.cookie('cartDetailsExpanded', !Number($.cookie('cartDetailsExpanded')) ? 1:0);
		},
		isCartDetailExpanded:function() {
			return !!Number($.cookie('cartDetailsExpanded'));
		},
		toggleTitleAttr:function() {
		  
		},
		showHideCart:function() {
		  // show the animation
			$('#sidebar-right').animate({width:cartCollapsed ? sidebarRightWidth : 0}, 750, 'easeOutCubic');
			// toggle the private state
			cartCollapsed = !cartCollapsed;
			// toggle the cookie (we remember track this as 'expanded' to cut down on initialization)
			$.cookie('cartExpanded', !cartCollapsed ? 1:0);
			// toggle the title attribute
			$('#phoenix-show-hide-cart').attr('title', (cartCollapsed ? 'Show' : 'Hide')+' Cart');
		},
	  settings:{}
	};
	// map the Drupal.Phoenix namespace to our primary object:
	Drupal.Phoenix = Phoenix;
	
	
	//  main jQuery ready event (entry point for DOM manipulation):
	$(document).ready(function() {
		
		//  play the movie the first time someone comes to the site (today).
		if(!$.cookie('visited') && !( $.browser.msie && $.browser.version < 7 ) )
			$('#phoenix-header .movie').flash({
				src: Drupal.settings.base_path + 'sites/all/modules/phoenix/movies/header.swf',
				width:900,
				height:150
			 });
	
		// set the cookie for one day (don't show the flash movie again until tomorrow)
		$.cookie('visited',1,1);
		
		$('#continue-shopping-link a').bind('click.phoenix', function(){
		  history.go(-2);
		  return false;
		});
		
		$('.phoenix-search-label').click(function() {
		  $('#edit-search_theme_form_keys').focus();
		  
		  return false;
		});
		
		$('.sf-menu').supersubs({ 
                minWidth:    12,   // minimum width of sub-menus in em units 
                maxWidth:    27,   // maximum width of sub-menus in em units 
                extraWidth:  2     // extra width can ensure lines don't sometimes turn over 
                                   // due to slight rounding differences and font-family
            }).superfish({
              animation:{height:'show',opacity:'show'},
              speed:'normal',
              dropShadows:true
            });
		
		// show the sidebar if it's not collapsed:
		if((location.href.indexOf('/cart') == -1) && !cartCollapsed)
		 $('#sidebar-right').width(sidebarRightWidth);
		
		if( Drupal.Phoenix.isCartDetailExpanded() ) {
		  $('#block-cart-contents').show();
		}
		
		//  assign the toggle event to the show/hide cart button:
		$('#phoenix-show-hide-cart').each(function(){
			// initialize the title attribute 
			Drupal.Phoenix.toggleTitleAttr(this);
		}).bind('click.phoenix', function() {
			Drupal.Phoenix.showHideCart();
		});
		
		$('#edit-attributes-6').parent().hide();
		
		
	  //  a setTimeout block that runs after-ish all the other code on this page has.  This way we can over-ride certain behaviors that are set obtrusively by ubercart, etc.
	  setTimeout(function () {
	    // this is the over-ride for the cvv_info_popup() function that ubercart obtrusively adds an onclick attribute that calls this function
	    // with no context.  We over-ride it in order to load the "what is cvv" popup as an ajax <div> instead of a popup-window
	    window.cvv_info_popup = function () {
	      if(!$('.info-popup.cvv').size())
	        $.get(Drupal.settings['base_path'] + '?q=cart/checkout/credit/cvv_info', function(data) {
  	        $('body').append('<div class="info-popup cvv">'+data+'</div>').find('.info-popup').each(function(index) {
  	         $(this).find('input').remove().end().bind('click.phoenix', function(){
   	           $('.info-popup.cvv').fadeOut('fast');
   	           $('#edit-cc-cvv').focus();
   	           return false;
   	         });
  	        }).hide().css({
   	           top : $('#edit-cc-number').offset().top - 200,
   	           left : $('#edit-cc-number').offset().left
   	        }).slideDown('fast');
  	      });
  	    else $('.info-popup.cvv:not(:visible)').slideDown('fast');
	      return false;
	    };
		  
  		$('.cart-block-toggle').bind( 'click.phoenix', function() {
  		  Drupal.Phoenix.toggleCartDetail();
  		}).prev().bind( 'click.phoenix', function (){
  		  //Drupal.Phoenix.showHideCart();
  		  return false;
  		}).css('cursor','default');

		},200);
		
		// and we're done!
	});
})(/*powered by*/ jQuery);