document.addEventListener("DOMContentLoaded", function(){
	categoryDropdown.init();
	imagePreloader.init();
},false);

var categoryDropdown = {
	blockerId : "categoryDropdownBlocker",
	isOpen : 0,
	
	init : function(){
		this.dropdown = document.getElementById('category_dropdown');
		this.list = this.dropdown.getElementsByTagName('ul')[0];
		this.trigger = document.getElementById('header_button');
		
		//Don't animate for older iphones
		var appReg = /iphone os (\d+)/i;
		if (appReg.test(navigator.appVersion))	{
			var version = parseInt(appReg.exec(navigator.appVersion)[1]);
			if (version >= 3) addClass(this.list, 'animate');
		}
		this.openHeight = this.getOpenHeight();
		this.bindEvents();
	},
	
	bindEvents : function(){
		var self = this;
		this.trigger.addEventListener('click', function(e){
			self.toggle.call(self, e);
		}, false);
	},
	
	toggle : function(e){
		e.preventDefault();
		this.isOpen ? this.close() : this.open();
		this.toggleBlocker();
	},
	
	open : function(){
		this.list.style.height = this.openHeight;
		this.isOpen = 1;
	},
	
	close : function(){
		this.list.style.height = "0px";
		this.isOpen = 0;
	},
	
	toggleBlocker : function(){
		var 
			cont = document.getElementById('page-container'),
			blocker = document.getElementById(this.blockerId);
			
		if (blocker) cont.removeChild(blocker);
		else {
			blocker = document.createElement('div');
			blocker.id = this.blockerId;
			blocker.style.height = this.getBlockerHeight();
			cont.appendChild(blocker);
		}

	},
	
	getBlockerHeight : function(){
		var height = "100%";
		if (document.body.clientHeight > window.innerHeight) height = document.body.clientHeight + "px";
		
		return height;
	},
	
	getOpenHeight : function(){
		var ret = "";
		this.list.style.left = "-999em";
		this.list.style.height = "auto";
		ret = this.list.clientHeight + "px";
		this.list.style.height = "0px";
		this.list.style.left = "0px";
		return ret;
	}
};

var imagePreloader = {
	globalPrefix : "/nikeos/mobile/global/images/iphone/",
	images : ["load.gif", "product_grid_bg.png", "product_large_bg.jpg"],
	init : function(){
		this.preload();
	},
	
	preload : function(){
		for (var i = 0; i < this.images.length; i++){
			var img = new Image();
			img.src = this.globalPrefix + this.images[i];
		}
	}
};
