function isDigit(e) {
    if (e.ctrlKey == true) {
        return true;
    }
      var sKeyCode = e.keyCode;

    if (sKeyCode == 8 || sKeyCode == 9 || sKeyCode == 37 || sKeyCode == 39 || sKeyCode == 46 || 
        (sKeyCode >= 48 && sKeyCode <= 57) || (sKeyCode >= 96 && sKeyCode <= 105)
        ||sKeyCode == 110 ||sKeyCode == 190 ||
        sKeyCode == 189 || sKeyCode == 109) {
        window.event.returnValue = true;
    } else {
        window.event.returnValue = false;
    }
}

function enableInputNumber() {
	$(".input_number").keydown(function(event){isDigit(event);});
}

function isdefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function setComma(str) {
	str = ""+str+"";
	var retValue = "";
	for(i=0; i<str.length; i++) {
		if(i > 0 && (i%3)==0) {
			retValue = str.charAt(str.length - i -1) + "," + retValue;
		} else {
			retValue = str.charAt(str.length - i -1) + retValue;
		}
	}
	return retValue;
}

//오류 신고
function openErrorReport(obj) {
	open(obj.href, 'error_report', 'width=500, height=600'); 
	return false;
}
function openCommentReport(obj) {
	open(obj.href, 'comment_report', 'width=500, height=600'); 
	return false;
}

//sns
function go_facebook() {
    var url = document.URL;
    var strpath = "http://www.facebook.com/sharer.php?u="+url;
	window.open(strpath, 'Facebook', 'width=680, height=524 ,scrollbars=no, resizable=no');
}
function go_twitter() {
	var str = document.title;
	var enstr = encodeURIComponent(str);
	var tmphref = document.getElementById("tb").href;
	if (document.getElementById("tb").href.indexOf("?text") == -1) {
		document.getElementById("tb").href = tmphref + "?text=" + encodeURIComponent("[하림]") + enstr;
	}else{
		document.getElementById("tb").href = tmphref.substring(0, tmphref.indexOf("?text")) + "?text=" + encodeURIComponent("[하림]") + enstr;
	}	
	return true;
}
function go_me2day() {
	var strTitle;
	strTitle = document.title;         					
	strTitle = strTitle.replace(/\"/g,'\\\"');    // " --> \" 로 대체
	var enstr = encodeURIComponent(strTitle);
	var naverAddr  = 'http://me2day.net/posts/new';
	var strAddr = naverAddr + '?new_post[body]="'+ encodeURIComponent("[하림]") + enstr + '":'+ encodeURIComponent(document.URL) + '&new_post[tags]=' + encodeURIComponent('하림');
	window.open(strAddr);
	return false;
}
function go_yozm() {
	var prefix = encodeURIComponent("[하림]") + encodeURIComponent(document.title);
	var link = encodeURIComponent(document.URL);
	var url = "http://yozm.daum.net/api/popup/prePost?prefix=" + prefix + "&link=" + link;
	window.open(url);
}

//한눈에 보기
function resetAvTab(n) {
	$("#loadingDiv").show();
	$("#av_tab1").attr('src', '/user/images/common/av_tab1_' + (n == 1?'on':'off') + '.gif');
	$("#av_tab2").attr('src', '/user/images/common/av_tab2_' + (n == 2?'on':'off') + '.gif');
	$("#av_tab3").attr('src', '/user/images/common/av_tab3_' + (n == 3?'on':'off') + '.gif');
	$("#av_tab4").attr('src', '/user/images/common/av_tab4_' + (n == 4?'on':'off') + '.gif');
	$("#av_tab5").attr('src', '/user/images/common/av_tab5_' + (n == 5?'on':'off') + '.gif');
	$("#av_tab6").attr('src', '/user/images/common/av_tab6_' + (n == 6?'on':'off') + '.gif');
	$("#av_tab7").attr('src', '/user/images/common/av_tab7_' + (n == 7?'on':'off') + '.gif');
}

function showAllProducts() {
	resetAvTab(1);
	productService.getAllList(0, false, getAllListCb);
}
function showNewProducts() {
	resetAvTab(2);
	productService.getAllList(0, true, getAllListCb);
}
function showProducts(n) {
	resetAvTab(n + 2);
	if (n == 1) {
		productService.getAllList(3, false, getAllListCb);
	} else if (n == 2) {
		productService.getAllList(4, false, getAllListCb);
	} else if (n == 3) {
		productService.getAllList(5, false, getAllListCb);
	} else if (n == 4) {
		productService.getAllList(6, false, getAllListCb);
	} else if (n == 5) {
		productService.getAllList(7, false, getAllListCb);
	} 
}

//shocking shopping
function showPreCon1(idx) {
	$("#preCon1 > a > img, #preCon1 > p, #preCon1 > strong").hide();
	$(".preCon1_" + idx).show();
	$(".preCon1Txt").each(function() {
		$(this).html(this.id.substring(11));
	});
	$("#preCon1Txt_" + idx).html("<strong>" + idx+ "</strong>");
	return false;
}

function addBookmark(title, url) {
	window.external.AddFavorite(url, title);
}
$(document).ready(function(){
	enableInputNumber();
	if (typeof noticeService != 'undefined') {
		noticeService.getMainNotice(getMainNoticeCb);
	}
	$(".t_site").click(function() {
		showAllProducts();
	});
	
	$(".preCon > a > img, .preCon > p, .preCon > strong").hide();
	$(".preCon1_1, .preCon0_1").show();
	
	$('#familySites').change(function(){
		if($('#familySites').val() != ''){
			window.open($('#familySites').val(),'','');
		}
	});
	
	//quick
	var quickCount = 3;
	if ($("#quickInner > ul > li").length > quickCount) {
		$("#quickInner").css("height", (71+5)*quickCount);
	}
	$("#quickInner > ul > li > img").show();
	$("#quickDown").click(function() {
		var topPx = parseInt($("#quickInner > ul").css("margin-top"));
		if (topPx < -(parseInt($("#quickInner > ul").css('height')) - (71+5)*(quickCount+1))) {
			return false;
		}
		var topTo = topPx - (71 + 5);
		$("#quickInner > ul").animate({'margin-top' : topTo}, 'fast');
		return false;
	});
	$("#quickUp").click(function() {
		var topPx = parseInt($("#quickInner > ul").css("margin-top"));
		if (topPx > -(71 + 5)) {
			return false;
		}
		var topTo = topPx + (71 + 5);
		$("#quickInner > ul").animate({'margin-top' : topTo}, 'fast');
		return false;
	});
	//quick scroll
	$(window).scroll(function () {   
		if ($(document).scrollTop() < $(document).height() - (400 + $("#quick").height())) {
			var offset = $(document).scrollTop() + "px";
			$("#quick").animate({top:offset},{duration:500,queue:false});
		}
	 });
});
