
$(document).ready(function() {
	catalog.initialize();

	$('#quick_search').focus(function() {
		if(this.value===this.defaultValue) {
			this.value = '';
			$(this).addClass('text');
		}
	}).blur(function() {
		if(this.value==='') {
			this.value = this.defaultValue;
			$(this).removeClass('text');
		}
	});
	$('#product_list').children().mouseenter(function(){
		$(this).addClass('hover');
	}).mouseleave(function(){
		$(this).removeClass('hover');
	}).click(function(event) {
    	if(event.target.type!=='checkbox' && event.target.type!=='text') {
    		$(':checkbox', this).trigger({type: 'click', manual: true});
		}
	}).find(':checkbox').click(function(event) {
		var li = $(this).parents('li:first');
		var qty = li.find(':text');
		if((event.manual && !$(this).is(':checked')) || (!event.manual && $(this).is(':checked'))) {
			if(!li.hasClass('selected')) {
				li.addClass('selected');
			}
			if(qty.val()=='') {
				qty.val(1);
				qty.select();
			}
			if(li.hasClass('incart')) {
				alert('This product is in your shopping cart already.');
			}
		} else {
			li.removeClass('selected');
			qty.val('');
		}
	});
	$('#product_list').children(':odd').addClass('odd');
	$('#product_list').find(':text').click(function(event) {
		if(this.value==='') {
			$(':checkbox', $(this).parents('li:first')).trigger({type: 'click', manual: true});
		}
	}).focus(function(event) {
		this.select();
	});
	$('#product_list').find('img').css('cursor', 'pointer').click(function(event) {
		event.stopPropagation();
		var p = $(this).offset();
		$('body').append('<img id="__enlarged" src="' + this.src + '" border="0"/>');
		$('#__enlarged').css({
			'z-index': 99999,
			position: 'absolute',
			left: p.left,
			top: p.top
		}).click(function(event) {
			$(this).remove();
		});
	});
	$('#product_list').find(':checkbox').each(function() {
		if(this.checked) {
			if(!$(this).hasClass('selected')) {
				$(this).parents('li:first').addClass('selected');
			}
		} else {
			$(this).parents('li:first').removeClass('selected');
		}
	});
	$('#member_login_userCode').focus();
});


var catalog = {

	shimobj: null,
	zIndex: 1000,

	initialize: function() {
		$('.catalog-icon', '#catalog_navigation').each(function() {
			var icon = $(this).children('div').children('a:last');
			var cl = $('.catalog-list', $(this));
			if(cl.length==1) {
				$(document.body).append(cl);
				var list = {
					instance: cl,
					timer: null
				};

				icon.bind('mouseenter', function(e) {
					clearTimeout(list.timer);
					catalog.showlist(icon, list);
				});
				icon.bind('mouseleave', function(e) {
					if (e.relatedTarget!=list.instance.get(0)) {
						list.timer = setTimeout(function() {
							catalog.hidelist(icon, list);
						}, 20);
					}
				});
				list.instance.bind('mouseenter', function(e) {
					clearTimeout(list.timer);
				});
				list.instance.bind('click mouseleave', function(e) {
					list.timer = setTimeout(function() {
						catalog.hidelist(icon, list);
					}, 20);
				});
			}
		});

		this.addshim();
	},

	showlist: function(icon, list) {
		icon.addClass('hover');
		var anchor = icon.children('img');
		anchor.addClass('catalog-image-active');

		var offsetx = 0, offsety = 0;
		var anchorw = 0, anchorh = 0;
		if(/Safari/i.test(navigator.userAgent)) {
			offsetx = anchor.offset().left;
			offsety = anchor.offset().top;
			anchorw = anchor.outerWidth();
			anchorh = anchor.outerHeight();
		} else {
			offsetx = icon.offset().left;
			offsety = icon.offset().top;
			anchorw = icon.outerWidth();
			anchorh = icon.outerHeight();
		}

		var left = 0, top = 0;
		if($(window).width() - (offsetx - $(document).scrollLeft())>list.instance.outerWidth()) {
			left = offsetx;
		} else {
			left = offsetx - list.instance.outerWidth() + anchorw;
		}
		top = offsety + anchorh;

		list.instance.css({
			left: left+'px', 
			top: top + 'px',
			'z-index': ++this.zIndex
		});
		
		if(list.instance.outerHeight()>500) {
			list.instance.css({
				width: list.instance.outerWidth() + 20 + 'px',
				height: '500px',
				overflow: 'scroll'
			});
		}
		this.shimobj.css({
			width: list.instance.outerWidth() + "px", 
			height: list.instance.outerHeight() + "px", 
			left: left + "px", 
			top: top + "px", 
			display: "block"
		});

		list.instance.show();
	},

	hidelist: function(icon, list) {
		icon.removeClass('hover');
		icon.children('img').removeClass('catalog-image-active');
		this.shimobj.css({display:"none", left:0, top:0});
		list.instance.hide();
	},

	addshim: function() {
		$(document.body).append('<iframe id="outlineiframeshim" src="'+(location.protocol=="https:"? 'blank.htm' : 'about:blank')+'" style="display:none; left:0; top:0; z-index:999; position:absolute; filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></iframe>')
		this.shimobj = $("#outlineiframeshim");
	}

};

var portal = {
	serviceCtx : '/ebiz-online',
	
	setCookie : function(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toUTCString()) + "; path=/";
	}
};

var utils = {
	isInt : function(v) {
		if(isNaN(v)) {
			return false;
		} else {
			return (v.indexOf('.')<0);
		}
	}
};

