/**
 * open.js
 * script for products pages
 * lastupdate: 2009/09/09 19:39
 */

(function($){

	/**
	 * build product list
	 * open/close product list
	 * if having hash(location.hash), open product list has same value as id
	 */
	function buildProductList(){
		
		var $list = $("div.product_list");
		var classOpen = "open";
		var classClose = "close";
		var selectorBtn = "a.btn";

		//close all
		$list.removeClass(classOpen).addClass(classClose);

		//treat hash(target list id)
		var hashId = location.hash;
		if(hashId){
			var $target = $list.filter(hashId);
			//open target, scroll to target
			if($target.size()){
				$target.removeClass(classClose).addClass(classOpen);
				$(document).scrollTop($target.offset().top);
			}
		}

		//set btn action
		$list.each(function(idx){
			var $this = $(this);
			$this.find(selectorBtn).click(
				function(evnt){
					if($this.hasClass(classClose)){
						//open
						$this.removeClass(classClose).addClass(classOpen);
					} else {
						//close
						$this.removeClass(classOpen).addClass(classClose);
					}
				}
			);
		});
	}

	//on dom ready
	$(function(){
		//build product list
		buildProductList();
	});

})(jQuery);