var HBN = new Class({
	Implements								: Log,
	debug									: false,
	
	product_grid_selector					: 'product_grid',
	product_grid_el							: '',
	
	product_grid_items_selector				: '#product_grid li.product_grid_item a',
	product_grid_item_els					: '',
	product_grid_item_id_prefix				: 'product_grid_item-',
	
	product_details_selector				: 'product_details',
	product_details_el						: '',
	product_details_fx 						: '',
	
	product_details_items_selector			: '#product_details .product',
	product_details_item_els				: '',
	product_detail_id_prefix				: 'product_detail-',
	product_details_image_container_selector: '#product_details .images',
	product_details_image_container_els		: '',
	product_details_image_dots_selector		: '#product_details .images a',
	product_details_image_dots_el			: '',
	product_details_images_selector			: '.images img',
	product_details_images_els				: '',
	
	preloader_periodical					: '',
	
	hide_details_selector					: '.hide_details a',
	hide_details_els						: '',
	
	upload_video_toggle_selector			: '#submit .toggle',
	upload_video_toggle_els					: '',
	
	upload_video_form_selector				: 'uploadform',
	upload_video_form_el					: '',
	
	upload_image_form_selector				: 'upload_image_form',
	upload_image_form_el					: '',
	upload_image_toggler_selector			: 'upload_image_toggler',
	upload_image_toggler_el					: '',
	upload_image_submit_el					: '',
	upload_image_close_el					: '',
	
	quick_buy_selector						: '.quick_buy',
	quick_buy_els							: '',
	
	product_details_open					: false,
	
	upload_success							: '',
	upload_error							: '',
	
	scroll									: '',
	
	help_tips								: '',
	
	initialize : function() {
		
		if(this.debug == true) {
			this.enableLog();
		}
		
		this.log('Initializing HBN...');
		
		window.addEvent("domready", this.dom.bind(this));
		
	},
	dom : function(e) {
		
		this.log('The DOM is ready...');
		
		try{
			this.upload_success		= upload_success;
		}
		catch(e) {
			this.log('Upload Success not set!');
		}
		try{
			this.upload_error		= upload_error;
		}
		catch(e) {
			this.log('Upload Error not set!');
		}
		
		this.product_grid_el						= $(this.product_grid_selector);
		this.product_grid_item_els					= $$(this.product_grid_items_selector);
		this.product_details_el						= $(this.product_details_selector);
		this.product_details_fx 					= new Fx.Reveal(this.product_details_el, {duration: 500});
		this.product_details_item_els				= $$(this.product_details_items_selector);
		this.product_details_image_container_els	= $$(this.product_details_image_container_selector);
		this.product_details_images_els				= $$(this.product_details_images_selector);
		this.quick_buy_els							= $$(this.quick_buy_selector);
		this.hide_details_els						= $$(this.hide_details_selector);
		this.product_details_image_dots_el			= $$(this.product_details_image_dots_selector);
		this.upload_video_toggle_els				= $$(this.upload_video_toggle_selector);
		this.upload_video_form_el					= $(this.upload_video_form_selector);
		this.upload_image_form_el					= $(this.upload_image_form_selector);
		this.upload_image_toggler_el				= $(this.upload_image_toggler_selector);
		this.upload_image_submit_el					= $('upload_submit');
		this.upload_image_close_el					= $('upload_close');
		
		this.initProductDetails();
		this.initProductGrid();
		this.initUploadVideo();
		this.initUploadImage();
		
		this.checkForDeepLink();
		
		this.scroll = new Fx.SmoothScroll();
		
	},
	
	initUploadImage : function() {
		
		if(this.upload_image_toggler_el && this.upload_image_form_el) {
			if(this.upload_success == true || this.upload_error == true) {
				this.upload_image_form_el.removeClass('hidden');
				(function() {
					
					try {
						$$('#upload_image_form .success')[0].destroy();
					}
					catch(e) {
						this.log(e);
					}
					
					this.upload_image_form_el.addClass('hidden');
					
				}).delay(3000, this);
			}
			
			this.upload_image_toggler_el.set('href', 'javascript:void(0);');
			this.upload_image_toggler_el.addEvent('click', this.uploadImageToggleClickHandler.bind(this));
			this.upload_image_submit_el.addEvent('click', this.uploadImageSubmitClickHandler.bind(this));
			this.upload_image_close_el.addEvent('click', this.uploadImageToggleClickHandler.bind(this));
		}
		
	},
	
	uploadImageSubmitClickHandler : function(e) {
		$('upload_image_overlay').setStyle('display', 'block');
	},
	
	uploadImageToggleClickHandler : function(e) {
		
		this.upload_image_form_el.toggleClass('hidden');
		
	},
	
	initUploadVideo : function() {
		
		if(this.upload_video_toggle_els && this.upload_video_form_el) {
			this.upload_video_toggle_els.set('href', 'javascript:void(0);');
			this.upload_video_toggle_els.addEvent('click', this.uploadVideoToggleClickHandler.bind(this));
			this.upload_video_form_el.addEvent('submit', this.uploadFormSubmitHandler.bind(this));
			
			this.log(upload_success + " : " + upload_error);
			
			if(this.upload_success == true || this.upload_error == true) {
				var el		= $$('.toggle.upload_form')[0];
				el.set('text', 'less');
				el.getNext('ul').setStyle('display', 'block');
			}
			
			
		}
		else {
			this.upload_video_toggle_els.set('href', 'javascript:void(0);');
			this.upload_video_toggle_els.addEvent('click', this.uploadVideoToggleClickHandler.bind(this));
		}
	},
	
	uploadFormSubmitHandler : function(e) {
		
		$('upload_video_overlay').setStyle('display', 'block'); 
		
	},
	
	uploadVideoToggleClickHandler : function(e) {
		
		if(e.target.get('text') == 'more') {
			e.target.set('text', 'less');
			e.target.getNext('ul').setStyle('display', 'block');
		}
		else {
			e.target.set('text', 'more');
			e.target.getNext('ul').setStyle('display', 'none');
		}
		
	},
	
	initProductDetails : function() {
		
		this.hide_details_els.set('href', 'javascript:void(0);');
		this.hide_details_els.addEvent('click', this.hideDetailsClickHandler.bind(this));
		this.product_details_image_dots_el.set('href', 'javascript:void(0);');
		this.product_details_image_dots_el.addEvent('click', this.imageDotsClickHandler.bind(this));
		
		this.initProductDetailsImagePreloader();
		
		if(Browser.Platform.ipod) {
			this.quick_buy_els.addEvent('click', this.iphoneClickHandler.bind(this));
		}
		
	},
	
	initProductDetailsImagePreloader : function() {
		
		this.product_details_images_els.each(this.imageLoadedHandler, this);
		
		this.preloader_periodical	= this.checkImageContainers.periodical(2000, this);
		
	},
	
	imageLoadedHandler : function(image) {
		
		image.addEvent('load', this.imageLoaded);
		
	},
	
	imageLoaded : function(e) {
		
		this.set('rel', 'true');
		
	},
	
	checkImageContainers : function() {
		
		var counter			= 0;
		
		for(var i = 0; i < this.product_details_image_container_els.length; ++i) {
			
			if(this.checkImagesProgress(this.product_details_image_container_els[i])) {
				
				++counter;
				
			}
			
		}
		
		if(this.product_details_image_container_els.length == counter) {
			clearInterval(this.preloader_periodical);
			// this.log('All Images Loaded!!!!');
		}
		
	},
	
	checkImagesProgress : function(image_container) {
		
		var images					= image_container.getChildren('.detail_image');
		var loaded_counter			= 0;
		
		var images_overlay			= image_container.getChildren('.loader');
		
		for(var i = 0; i < images.length; ++i) {
			
			if(images[i].get('rel') == 'true') {
				++loaded_counter;
			}
			
		}
		
		if(images.length == loaded_counter) {
			// this.log('Set Loaded!');
			images_overlay.fade('out');
			return true;
		}
		else {
			return false;
		}
		
		
	},
	
	iphoneClickHandler : function(e) {
		$(e.target).getParent('.quick_buy').toggleClass('hover');
	},
	
	hideDetailsClickHandler : function(e) {
		
		this.product_details_el.dissolve();
		this.product_details_open = false;
		
	},
	
	imageDotsClickHandler : function(e) {
		
		var id						= e.target.get('rel');
		
		var great_grandparent		= e.target.getParent('.product');
		var grandparent				= e.target.getParent('.images');
		var images					= grandparent.getChildren('img');
		var parent					= e.target.getParent('dots');
		
		var image_id				= (images.length - 1) - id;
		
		$$("#" + great_grandparent.get('id') + " a.dot").removeClass('current');
		
		for(var i = (images.length - 1); i >= 0 ; --i) {
			
			// this.log(image_id + " : " + i);
			
			if(image_id == i) {
				images[i].fade('in');
				// this.log(images[i]);
				$$("#" + great_grandparent.get('id') + " a.dot[rel='" + id + "']")[0].addClass('current');
				
				// this.updateUrl({'view_product' : 'current', 'image' : id});
			}
			else {
				images[i].fade('out');
			}
			
		}
		
	},
	
	initProductGrid : function() {
		
		// this.product_grid_item_els.set('href', 'javascript:void(0);');
		this.product_grid_item_els.addEvent('click', this.productGridAnchorClickHandler.bind(this));
		this.product_grid_item_els.set('href', 'javascript:void(0);');
		
		// this.log(this.product_grid_item_els);
		
	},
	
	productGridAnchorClickHandler : function(e) {
		
		var parent			= $(e.target).getParent('.product_grid_item');
		var id				= parent.get('id').replace(this.product_grid_item_id_prefix, '');
		
		detail_images		= $$('#' + this.product_detail_id_prefix + id + ' img');
		// detail_images[0].fade('show');
		// detail_images[1].fade('hide');
		
		this.product_details_item_els.setStyle('display', 'none');
		
		$(this.product_detail_id_prefix + id).setStyle('display', 'block');
		$(this.product_detail_id_prefix + id).fade('in');
		
		if(this.product_details_open == false) {
			this.reveal_details();
		}
		
		// $$(" a.dot[rel='0']").addClass('current');
		// $$(" a.dot[rel='1']").removeClass('current');
		
		var product_details_scroll		= new Fx.Scroll(window).toElement('product_details');
		this.updateUrl({'view_product' : id});
		
		
	},
	
	reveal_details : function() {
		this.product_details_el.reveal();
		this.product_details_open = true;
	},
	
	//Help Tips Functions
	
	initHelpTips : function() {
		
		/*
			TODO Derrin - Add help tips?
		*/
		
		this.help_tips		= new Tips('.help_tip');
		
	},
	
	updateUrl : function(obj) {
		
		
		
		var url				= window.location.href;
		var uri				= new URI(url);
		var fragment		= uri.get('fragment');
		var query_obj		= fragment.parseQueryString();
		
		if(obj.view_product == 'current') {
			obj.view_product		= query_obj.view_product;
		}
		if(obj.image == 'current') {
			obj.image				= query_obj.image;
		}
		// 		
		// 		var query_string			= 'view_product=' + view_product + '&image=' + image;
		
		
		
		uri.setData(obj, true, 'fragment');
		uri.go();
		// this.log(uri);
		
	},
	
	checkForDeepLink : function() {
		var query_obj					= this.getHashData();
		if(query_obj.view_product) {
			
			var e					= {};
			e.target				= $$('#product_grid_item-' + query_obj.view_product + ' a img')[0];
			
			if(e.target) {
				this.productGridAnchorClickHandler(e);
				
				if(query_obj.image) {
					var e2				= {};
					e2.target			= $$('#product_detail-' + query_obj.view_product + ' .dots .dot[rel=' + query_obj.image + ']')[0];
					
					if(e2.target) {
						this.imageDotsClickHandler(e2);
					}
					
				}
				
			}
		}
		
	},
	
	getHashData : function() {
		
		var url				= window.location.href;
		var uri				= new URI(url);
		var fragment		= uri.get('fragment');
		var query_obj		= fragment.parseQueryString();
		// var query_obj		= uri.getData('fragment');
		
		this.log(query_obj);
		
		return query_obj;
		
	}
	
});

var hbn = new HBN();
