Array.prototype.indexOf = function(a) {
	for (var i in this) {
		if (this[i]==a) {
			return i;
		}
	} 
	return false;
}

var opened = false;
function detail_view_init() {
	$('.stylishtable a.detail').each(function (i) {
		
		$(this).click(function () {
			
			var info = $(this).siblings('div');
			obj = info.find('span.product_info');
			obj.attr('id', 'panel_'+i);
			
			if (opened && opened.attr('id') != obj.attr('id')) {
				opened.hide();
				opened = false;
			}
			
			obj.slideToggle(function () {
				if (this.style.display == 'block') {
					opened = $(this);
				} else {
					opened = false;
				}
			});
			return false;
		});
	});
	
	$(document).click(function () {
		if (opened) opened.slideUp();
	});
}

$(document).ready(function(){
	
	/*
	 * jQuery: Change line height
	 */
	
	var line_heights = Array('1.3', '1.4', '1.5');
	
	if($.cookie('lineheight') == null) {
		$.cookie('lineheight', line_heights[0], { expires: 7, path: '/', domain: '', secure: false });
	}
	
	$(".center").css({
		'line-height': $.cookie('lineheight')
	});
	
	$("#b_line").click(function() {
		var line_height = parseFloat(line_heights.indexOf($.cookie('lineheight'))) + parseFloat(1);
		if(line_height == line_heights.length) {
			new_height = line_heights[0];
		} else {
			new_height = line_heights[line_height];	
		}
		$.cookie('lineheight', new_height, { expires: 7, path: '/', domain: '', secure: false });
		$(".center").css({
			'line-height': new_height
		});
		return false;
	});
	
	/*
	 * jQuery: Change line height
	 */
	
	var font_sizes = Array('12', '14', '16');
	
	if($.cookie('fontsize') == null) {
		$.cookie('fontsize', font_sizes[0], { expires: 7, path: '/', domain: '', secure: false });
	}
	
	$(".center").css({
		'font-size': $.cookie('fontsize')
	});
	
	$("#b_font").click(function() {
		font_size = parseFloat(font_sizes.indexOf($.cookie('fontsize'))) + parseFloat(1);
		if(font_size == font_sizes.length) {
			new_size = font_sizes[0];
		} else {
			new_size = font_sizes[font_size];	
		}
		$.cookie('fontsize', new_size, { expires: 7, path: '/', domain: '', secure: false });
		$(".center").css({
			'font-size': new_size + "px" 
		});
		return false;
	});

	
	/*
	 * jQuery: Print document
	 */
	
	$("#b_print").click(function(){
		print();
		return false;
	});
	
	/**
	 * jQuery: product detail view
	 */
	
	detail_view_init();
	
	/**
	 * jQuery: select and unselect all checkboxes
	 */
	
	if($("#selectall")) {
		$("#selectall").click(function(){
			$("#checkboxes input").each(function(){
				this.checked = true;
			});
			return false;
		});
	}
	
	if($("#unselectall")) {
		$("#unselectall").click(function(){
			$("#checkboxes input").each(function(){
				this.checked = false;
			});
			return false;		   
		});
	}
	
});
