var common = function(){

	var urllink = location.toString();

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


	return {

		indexPoint: 0, 
		currentMenu: 0,
		
		
		init: function(){
			$('.catalog_sub').hide();
			$('.mainmenu').bind('mouseover', function() {
				if(!$(this).children('.catalog_sub').is(":visible")) {
					$('.catalog_sub').slideUp(600);
					$(this).children('.catalog_sub').slideDown(400);
				}
			});


			$('body').append('<div id="dialog" title="none" style="display:none"><p>empty</p></div>');
			
		},
		

		msg: function(t,p) {
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title',t);
			$('#dialog > p').html(p);
			
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {}
			});
		},
		
		getPageData: function(TID, IPP, NOWP, GOP, Target, Flag) {
			var myTPL = '<div class="switch_item"><a href="#none"><div class="switch_item_box" onclick="location.href=\'productsinfo.php?TypeID=${TypeID}&CateID=${CategoryID}&ID=${ID}\'"><img src="${Img}" width="159" height="125" /></div></a><div class="p_name"><span class="p_name_tag"><a href="productsinfo.php?TypeID=${TypeID}&CateID=${CategoryID}&ID=${ID}">${ProductName}</a></span></div></div>';
			
			$.ajax({
			  dataType: "json",
			  url: 'rtntpl.php',
			  type: 'POST',
			  data: {TopID: TID, ipp:IPP, nowp: NOWP, gop:GOP},
			  success: function(rspn){
				  if(rspn.State!='false') {
					  //alert(rspn.Items);
					  $('#'+Target).fadeOut(250, function(){
						 $('#'+Target).empty();
						 $.tmpl(myTPL, rspn.Items ).appendTo('#'+Target);
					  });
					  $('#'+Target).fadeIn(250);
					  $('#'+Flag).val(rspn.State);
					  
				  }
			  },
			  error: function(rspn) { alert(rspn); }
			});
			
		},
		
		KeyPass: function() {
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html("Please input password...<br><input type='password' id='pswd' size='15'>");
			$('#dialog').dialog({
				modal: true,
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Submit!': function() {
						var pswd = $('#pswd').val();
						if(pswd!='') {
							$.ajax({
								url: 'getdata.php?Type=Download',
								type: 'POST',
								dataType: 'json',
								data: {paramID: pswd},
								error: function() {	alert('Ajax Error');	},
								success: function(rspn) { 
									if(rspn=='true') {
										location.href="download.php";
										$(this).dialog('close');
									} else {
										common.msg('Sorry','Password illegal!');
									}
								}
							});
							
						} else {
							common.msg('','Please input Password!');
						}
					},
					'Cancel': function() {
						$(this).dialog('close');
					}
				}
			});
		},
		

		addInquiry: function(uid, name){
			//$('#dialog').dialog("destroy");
			var spec = $("#spec>option:selected").get(0);
			var number = spec.title;
			var flag = spec.value;
			$('#dialog').attr('title','確認');
			$('#dialog > p').html('是否確定將 <strong><font color="#f40000">'+name+' '+number+'</font></strong> 加入購物清單？');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('YES', uid, flag);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			
		}, 
		
		updateCart: function(box,uid, spec){
			common.targetBox = {'num': box};
			common.clickBtn('UPD', uid, spec);
			
		}, 

		

		delInquiry: function(box, name, uid, spec){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','確認');
			$('#dialog > p').html('是否確定將 <strong><font color="#f40000">'+name+' '+spec+'</font></strong> 從清單中移除？');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('DEL', uid, spec);
						$(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','抱歉，您的購物清單中沒有任何商品 !');
					}
				}			
			});
		}, 
		
		

		clickBtn: function(btn, uid, name){

			switch(btn){

				case 'YES':

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

					break;

					

				case 'DEL':

					if(!uid) break;
					$.ajax({
						url: 'getdata.php?Type=delInquiry',
						type: 'POST',
						data: {paramID: uid, spec: name},
						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('抱歉，您的購物清單中沒有任何商品 !');
									$('#dialog').dialog({
										resizable: false,
										buttons: {
											OK: function() {
												location.href = 'index.php';
											}
										}
									});
									
								} else {
									location.reload();
								}

							});
						}
					});


					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, spec: name, 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','Did not find any information!');
						}
					}
				});
			}

		}
		

	}

}();



$(common.init);
