 // Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
 
var C1R = {
	enterSite : function(removeOverlay) {
		var removeOverlay = (removeOverlay == null) ? true : removeOverlay;
 
		// Set the expiry for the age verification cookie to seven days from now.
		var date = new Date();
		date.setTime(date.getTime()+(24*60*60*7*1000));
		document.cookie = 'c1r_age_verified=true; expires='+date.toGMTString()+'; path=/; domain=c1r.com';
 
		// Remove the overlay
		if(removeOverlay) {
			this.removeOverlay();
		}
 
		return false;
	},
 
	// Adds a black overlay with 65% opacity over the entire page.
	toggleTransparentOverlay : function(disableClickOut) {
		var overlay = $('overlay');
 
		if(overlay) {
			overlay.remove();
		}
		else {
			overlay = new Element('div', {id: 'overlay'});
 
			if(!disableClickOut) {
				overlay.onclick = function(e) {
					C1R.removeOverlay();
				}
			}
 
			$(document.body).insert(overlay);
		}
	},
 
	setupAgeVerification : function() {
		var verification = $("age-verification");
		if(verification) {
			this.toggleTransparentOverlay(true);
			this.ageVerifCenter(verification);
		}
		else {
			this.enterSite(false);
		}
	},
 
	setupKeywordOverlay : function() {
		var keywords = $("title_keywords")
		if (keywords) {
			this.toggleTransparentOverlay(true)
			this.absolutelyCenter(keywords)
		}
	},
 
	addExpandToSynopsis : function() {
		var synopsises = $$('.synopsis');
		synopsises.each(function(synopsis) {
			var synopsis_body = synopsis.getElementsBySelector(".body").first();
			if(synopsis_body.getHeight() > synopsis_body.up().getHeight()) {
				var more_link = new Element('a', { 'href':'#' }).update('(more)');
				more_link.observe('click', function() {
					C1R.displaySynopsisOverlay(this.up());
					return false;
				});
				synopsis.insert(more_link);
			}
		});
	},
 
	displaySynopsisOverlay : function(synopsis) {
		this.toggleTransparentOverlay();
		var synopsisContent = synopsis.getElementsBySelector('.body').first().innerHTML;
 
		var overlay = new Element('div', {'class': 'content-overlay'});
		overlay.update(synopsisContent);
		close_link = new Element('a', {'href': '#', 'class': 'close'}).update('close')
		close_link.observe('click', function() {
			C1R.removeOverlay();
			return false;
		});
		overlay.insert({'top' : close_link });
 
		overlay.setStyle({'display':'none'})
		$(document.body).insert(overlay);
		this.absolutelyCenter(overlay);
	},
 
	removeOverlay : function() {
		$$('.content-overlay').each(function(overlay) { overlay.remove(); });		    $$('.popup').each(function(overlay) { overlay.hide(); });
		},
 
 
	removeScreen : function() {
		if($('screen')) {
			$('screen').remove();
		}
	},
 
	absolutelyCenter : function(ele) {
		var window_dimensions;
 
		window_dimensions = Position.getWindowSize();
 
		ele.setStyle({
			left: '255px',
			top: '125px',
			display: 'block',
            position: 'absolute',
            border: '1px solid #fff'
		});
	},
    
      ageVerifCenter : function(ele) {
		var window_dimensions;
 
		window_dimensions = Position.getWindowSize();
 
		ele.setStyle({
			width: (window_dimensions.width) + 'px',
			height: (window_dimensions.height) + 'px',
			display: "block"
		});
	},
    
     
      
 
 
	closeCartPopup : function() {
		if($('cart-popup').getStyle('display') != 'none') {
			new Effect.BlindUp('cart-popup');
		}
	},
 
	displayLoginPopup : function() {
		if($('login-popup').getStyle('display') == 'none') {
			this.toggleTransparentOverlay();
			new Effect.Appear('login-popup');
		}
	},
 
	closeLoginPopup : function() {
		if($('login-popup').getStyle('display') != 'none') {
			new Effect.Fade('login-popup');
			this.toggleTransparentOverlay();
		}
	},
 
	displayExplorePopup : function() {
		if($('explore_popup').getStyle('display') == 'none') {
			this.toggleTransparentOverlay();
			new Effect.Appear('explore_popup');
		}
	},
 
	closeExplorePopup : function() {
		if($('explore_popup').getStyle('display') != 'none') {
			new Effect.Fade('explore_popup');
			this.toggleTransparentOverlay();
		}
	},
 
	collectCartItemsForAjax : function() {
    var items = $$('.line-item.active').map(function(item) {
			var sku = item.id;
			var quantity = $F(item.id+'_quantity');
      return 'line_items['+sku+']='+quantity;
    });
 
		return items.join("&");
	}
}
 
// Setup age verification
document.observe("dom:loaded", function() {
	C1R.setupAgeVerification();
	C1R.addExpandToSynopsis();
 
	$$('.purchase-options a').each(function(link) { link.observe('click', function(e) {
		stopBubble(e);
	}); });
 
	setTimeout(function() {
		C1R.removeScreen();
	}, 200);
 
	$('cart-popup').observe('click', function(e) {
		stopBubble(e);
	});
 
	$('login-popup').observe('click', function(e) {
		stopBubble(e);
	});
 
	$('explore_popup').observe('click', function(e) {
		stopBubble(e);
	});
 
	$(document.body).observe('click', function(e) {
		C1R.closeCartPopup();
	});
 
	$(document.body).observe('click', function(e) {
		C1R.closeLoginPopup();
	});
 
	$(document.body).observe('click', function(e) {
		C1R.closeExplorePopup();
	});
 
	if ($('browse_keywords')) {
	    $('browse_keywords').observe('click', function(e) {
	        C1R.setupKeywordOverlay();
	    });
    }
});
 
/*
  IE7 (and possible other browsers) does not properly close connections when you
  exit without closing out all processes. This code explicitly closes the 
  NetConnection so that Flash doesn't have to.
*/
// document.onbeforeunload = function() {
//   alert('killing!');
//   fl.call('killNetConnection');
//   alert('killed!');
// }
 
// IE has an issue where form (submit) buttons do not receive enter-to-submit if they are not visible.
// http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter
 
function addInputSubmitEvent(form, input) {
	input.onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            form.submit();
            return false;
        }
    };
}
 
window.onload = function() {
    var forms = document.getElementsByTagName('form');
 
    for (var i=0;i < forms.length;i++) {
        var inputs = forms[i].getElementsByTagName('input');
 
        for (var j=0;j < inputs.length;j++)
            addInputSubmitEvent(forms[i], inputs[j]);
    }
};
 