var common = function(){

	var urllink = location.toString();

	var caseType = urllink.substring(urllink.lastIndexOf('\/')+1, urllink.lastIndexOf('.'));


	return {

		indexPoint: 0, 
		currentMenu: 0,
		
		
		init: function(){
			$('body').append('<div id="dialog" title="none" style="display:none"><p>empty</p></div>');
			
			$("img[name^='head_menu']").each(function() {	
				$(this).bind({
				  mouseover: function() {
					this.src = this.src.replace('images/index_','images/indexb_');
				  },
				  mouseout: function() {
					this.src = this.src.replace('images/indexb_','images/index_');
				  }
				});			
			})
			switch(caseType) {
				case 'aboutbbx':
					img = $("img[name='head_menu1']");
					break;
				case 'knowbarter':
					img = $("img[name='head_menu2']");
					break;
				case 'news':
					img = $("img[name='head_menu3']");
					break;
				case 'products':
					img = $("img[name='head_menu4']");
					break;
				case 'contact':
					img = $("img[name='head_menu5']");
					break;
			}
			origSrc = img.attr('src');
			img.attr('src', origSrc.replace('images/index_','images/indexb_'));
			img.unbind();

			
			
		},
		
		chgMenuCls: function(arg) {
			arg.addClass('pagenow');
			arg.children('a:first').replaceWith(function() {
				return $(this).html();
			})
		},
		

		msg: function(t,p) {
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title',t);
			$('#dialog > p').html(p);
			
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {}
			});
		},
		
		
		login: function() {
			var param1 = $('#Username').val();
			var param2 = $('#Password').val();
			if(param1=='' || param2=='') {
				common.msg('注意','請輸入會員帳號密碼!');
				return;
			} else {
				$.ajax('getdata.php?Type=Login',{
					dataType: 'json',
					type: 'POST',
					data: {paramID:[param1, param2]},
					error: function(rspn) {	alert(rspn);	},
					success: function(rspn) { 
						if(rspn=='true') {
							location.href = 'index.php'; //http://'+param1+'.eob8.com/
						} else {
							common.msg('通知','帳號/密碼錯誤或帳號尚未啟動!');
						}
					}
				});
			}
			
		},
		
		accCheck: function(arg) {
			param = $(arg).val();
			
			if(param!='') {
				$.ajax({
					url: 'getdata.php?Type=AccCheck',
					type: 'POST',
					dataType: 'json',
					data: {paramID: param},
					error: function() {	alert('Ajax Error');	},
					success: function(rspn) { 
						if(rspn=='true') {
							$('#accOK').show();
							$('#accNOK').hide();
							$('#accChkVal').val(1);
						} else {
							$('#accNOK').show();
							$('#accOK').hide();
							$('#accChkVal').val(0);
						}
					}
				});
			}
		},
		
		checkLogin: function(tar) {
			$.ajax({
				url: 'getdata.php?Type=checkLogin',
				type: 'POST',
				dataType: 'json',
				error: function() {	alert('Ajax Error');	},
				success: function(rspn) { 
					if(rspn=='true') {
						location.href = tar+'.php';
					} else {
						common.msg('提醒','請先登入會員！');
					}
				}
			});
			
		},
		

		addInquiry: function(uid, name){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html('Thank, Will be <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list !');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('YES', uid);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			
		},
		
		updateCart: function(box,uid){
			common.targetBox = {'num': box};
			common.clickBtn('UPD', uid);
			
		}, 

		

		delInquiry: function(box, name, uid){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html('Do you want delete <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list ?');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('DEL', uid);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});

			common.targetBox = {'box': box};

		}, 

		linkInquiry: function(target){
			$.ajax({
				url: 'getdata.php?Type=isInquiry',
				type: 'POST',
				dataType: 'json',
				success: function(rspn) { 
					if(rspn=='true') {
						location.href = 'inquiry.php';
					} else {
						common.msg('NOTICE','Sorry, you are not adding products to inquiry list !');
					}
				}			
			});
		}, 
		
		

		clickBtn: function(btn, uid, name){

			switch(btn){

				case 'YES':

					if(!uid) break;
					
					$.ajax({
						url: 'getdata.php?Type=addInquiry',
						type: 'POST',
						data: {paramID: uid}
					});

					break;

					

				case 'DEL':

					if(!uid) break;
					$.ajax({
						url: 'getdata.php?Type=delInquiry',
						type: 'POST',
						data: {paramID: uid},
						success: function(rspn) { 
							var pdt = $(common.targetBox.box).parent().parent();
							
							pdt.hide("Blind", function(){

								pdt.remove();

								$('#inqiryCount').val(parseInt($('#inqiryCount').val())-1);

								if($('#inqiryCount').val() == 0)  {
									//$('#dialog').dialog("destroy");
									$('#dialog').attr('title','Notice');
									$('#dialog > p').html('Sorry, you are not adding products to inquiry list !');
									$('#dialog').dialog({
										resizable: false,
										buttons: {
											OK: function() {
												location.href = 'index.php';
											}
										}
									});
									
								}

							});
						}
					});


					break;
					
				case 'UPD':

					if(!uid) break;
					var nums = $(common.targetBox.num).parent().parent().find("input[name='num']").val();
					
					$.ajax({
						url: 'getdata.php?Type=updateCart',
						type: 'POST',
						data: {paramID: uid, num: nums},
						success: function() {
							location.reload();
						}
					});
	
					break;

				default:

			}


		}, 
	
		
		gotosearch: function(target){

			var keyword = $('#'+target).val();
			
			if (keyword == ''){

				common.msg('NOTICE','Please Enter Keyword!!');

			}else{
				$.ajax({
					url: 'getdata.php?Type=Search',
					type: 'POST',
					dataType: 'json',
					data: {paramID: keyword},
					error: function() {	alert('Ajax Error');	},
					success: function(rspn) { 
						if(rspn=='true') {
							location.href="products.php?act=SRH&Keyword="+keyword;
						} else {
							common.msg('NOTICE','No any match products!');
						}
					}
				});
			}

		}
		

	}

}();



$(common.init);
