var common = function(){
	var urllink = location.toString();
	var caseType = urllink.substring(urllink.lastIndexOf('\/')+1, urllink.lastIndexOf('.'));
	var obj = Ext.urlDecode(urllink.substring(urllink.lastIndexOf('?')+1, urllink.length));
	var winBox = null;
	var nextBtn, prevBtn;
	var carousel, shelter;
	
	function changeToExtField(fields){
		var text = (Ext.isIE) ? fields.bvalue : getAbrite(fields, 'bvalue');
		var type = (fields.type == 'text') ? 'TextField' : 'TextArea';
		var indexField = new Ext.form[type]({
			emptyClass: '', 
			emptyText: text, 
			fieldClass: '',
			focusClass: '',
			applyTo: fields.id
		});
		indexField.getEl().removeClass('x-form-text');
		indexField.getEl().removeClass('x-form-empty-field');
	}

	function getTemplate(xtpl){
		var word = (Ext.getDom(xtpl)) ? Ext.getDom(xtpl).innerHTML : xtpl;
		word = word.replace(/\%7B/g, '{').replace(/\%7D/g, '}');
		return word.replace(/TPL/g, 'tpl').replace(/ > /g, ' &gt; ').replace(/ < /g, ' &lt; ');
	}
	
	return {
		nowImg: 1, 
		backupPdtLink: [], 
		indexPoint: 0, 
		startPoint: 0, 
		indexM: 0,
		
		init: function(){
			if(Ext.getDom('request')){
				fields = Ext.query('[bvalue]');
				for(x in fields){
					if(isNaN(parseInt(x))) continue;
					changeToExtField(fields[x]);
				}
				this.verifyCode = function(){Ext.getDom('request').submit()}
			}
			
			if(Ext.get('index_div2')){
				Ext.get('index_div2').hide();
				this.tplMarquee = getTemplate('index_div2');
				var key = this.tplMarquee.indexOf('visibility') != -1 ? 'visibility' : 'VISIBILITY';
				this.tplMarquee = this.tplMarquee.replace(key, '');
				this.createMarquee();
			}
				
			switch(caseType){
				case 'index':
					if(Ext.get('advertisement')) {
						Ext.get('advertisement').hide();
						Ext.get('advertisement').show.defer(1000, Ext.get('advertisement'), [Ext.get('advertisement'), true]);
					}
					break;
					
				case 'newscenter':
//					if(Ext.get('advertisement')){
//						carousel = new Ext.ux.Carousel({
//							numVisible: 1,
//							animationSpeed: 0.5,
//							scrollInc: 1,
//							//animationMethod: 'backBoth', 
//							orientation: 'vertical', 
//							renderTo: 'advertisement'
//						});
//						carousel.show();
//						this.addShelter.defer(1000, this.addShelter, ['images/newscenter_10.jpg']);
//					}
					break;
					
				case 'products':
					if(Ext.get('pdtList')) {
						this.backupTemp = getTemplate('tplList');
						if(obj.Type == 'list') this.getPdtMore();
					}
					if(Ext.get('imgArray')) this.imageData = Ext.getDom('imgArray').value.split(',');
					if(obj.Type == 'more') this.createMsg('inquiryMsg');
					if(obj.Type == 'Inquiry') this.createMsg('deleteMsg');
					if(Ext.get('advertisement')){
						carousel = new Ext.ux.Carousel({
							numVisible: 1,
							animationSpeed: 0.5,
							scrollInc: 1,
							//animationMethod: 'backBoth', 
							renderTo: 'advertisement'
						});
						carousel.show();
						this.addShelter.defer(1000, this.addShelter, ['images/products_15.jpg']);
					}
					break;
				
				case 'mail':
					if(!winBox) this.createWinMsg(Ext.getBody().dom.innerHTML);
					Ext.getBody().update('');
					winBox.show();
			}
		}, 
		
		addShelter: function(img){
			shelter = Ext.DomHelper.append(Ext.getBody(), {tag: 'img', src: img}, true);
			shelter.setStyle({'z-index': '1000', 'cursor': 'pointer'});
			shelter.anchorTo('bar1', 'c-c');
		},
		
		moveTo: function(area){
			if(carousel.moving) return;
			var num = parseInt(area.id.replace('bar', ''));
			shelter.anchorTo(area.id, 'c-c', [0, 0], true);
			carousel.moveTo(num);
		},
		
		getPdtMore: function(uid, target){
			if(this.indexPoint) return;
			this.indexPoint = 1;
			var uid = (uid) ? uid : 0;
			var inx = this.backupPdtLink.indexOf(uid);
			if(inx == -1) this.backupPdtLink.push(uid); else this.backupPdtLink.splice(inx, 1);
			if(uid == 0) this.backupPdtLink = [];
			if(Ext.get('point_'+uid)) Ext.get('point_'+uid)[(inx == -1 ? 'hide' : 'show')]();
			var type = (target) ? target : 'findPdt';
			var target = (target) ? Ext.getDom(target).value : obj.ID.replace(/\#/g, '');
			var cb = function(rspn, option){
				this.indexPoint = 0;
				var JSON = Ext.util.JSON.decode(rspn.responseText);
				var tpl = new Ext.XTemplate(this.backupTemp);
				Ext.get('pdtList').update(JSON.length ? tpl.apply(JSON) : 'NO DATA');
			}
			var cn = new Ext.data.Connection({url:'getdata.php?Type='+type}).request({
				scope: this, 
				method: 'POST', 
				params: {'paramID': this.backupPdtLink.join(','), 'ID': target}, 
				success: cb
			});
		},
		
		showSelect: function(box, uid){
			if(this.indexPoint || this.clickBox == box.id) return;
			this.clickBox = box.id;
			if(!this.backupTemp) this.backupTemp = getTemplate('tplCombo');
			var tpl = new Ext.XTemplate(this.backupTemp);
			if(!this.backupPdtLink[uid]){
				var cb = function(rspn, option){
					this.backupPdtLink[uid] = Ext.util.JSON.decode(rspn.responseText);
					this.openSelect(tpl.apply(this.backupPdtLink[uid]));
				}
				var cn = new Ext.data.Connection({url:'getdata.php?Type=Cate'}).request({
					scope: this, 
					method: 'POST', 
					params: {'paramID': uid}, 
					success: cb
				});
			}else{
				this.openSelect(tpl.apply(this.backupPdtLink[uid]));
			}
		},
		
		openSelect: function(html){
			if(!winBox) {
				var create = true;
				winBox = Ext.DomHelper.append(Ext.getBody(), {tag: 'div', style: 'float:left;z-index:12'}, true);
			}
			var isShow = create ? false : winBox.isVisible();
			if(isShow){
				winBox.slideOut('b', {callback: function(){this.fixedToShow(html)}, scope: this});
			}else{
				this.fixedToShow(html)
			}
			Ext.getDoc().on('mousedown', this.closeSelect, this, true);
		},
		
		fixedToShow: function(html){
			winBox.update(html).alignTo(this.clickBox, 'bl-tl', [0, -2]);
			winBox.slideIn('b');
		},
		
		closeSelect: function(e){
			if(winBox && e.within(winBox)) return;
			var target = e.getTarget();
			if(target.id == this.clickBox) return;
			if(target.src && target.src.indexOf('images/products_32.jpg') != -1){ 
				Ext.getDoc().un('mousedown', this.closeSelect, this, true);
				return;
			}
			this.indexPoint = 1;
			this.clickBox = 0;
			winBox.slideOut('b', {callback: function(){this.indexPoint = 0;}, scope: this});
			Ext.getDoc().un('mousedown', this.closeSelect, this, true);
		}, 
		
		getScopeBtn: function(scope){
			return Ext.query('input[type=image]', scope);
		},
		
		createMarquee: function(){
			var box = Ext.get('index_div2').parent('table');
			var btn = this.getScopeBtn(box.dom);
			prevBtn = btn[0], nextBtn = btn[1];
			var cb = function(rspn, option){
				this.marqueeData = Ext.util.JSON.decode(rspn.responseText);
				if(this.marqueeData.length){
					var tpl = new Ext.XTemplate(this.tplMarquee);
					Ext.get('index_div2').update(tpl.apply(this.marqueeData[0])).show(true);
					if(this.marqueeData.length > 1){
						nextBtn.src = 'images/index_07-05.jpg';
						nextBtn.onclick = function(){common.changeMarquee(1)};
						prevBtn.onclick = function(){common.changeMarquee(-1)};
					}
				}else{
					box.hide();
				}
			}
			var cn = new Ext.data.Connection({url:'getdata.php?Type=marquee'}).request({
				scope: this, 
				success: cb
			});
		},
		
		changeMarquee: function(num){
			var tpl = new Ext.XTemplate(this.tplMarquee);
			var inx = this.indexM+num;
			var max = this.marqueeData.length-1;
			if(inx.constrain(0, max) != inx) return;
			nextBtn.src = (inx == max) ? 'images/index_27.jpg' : 'images/index_07-05.jpg';
			prevBtn.src = (inx == 0) ? 'images/index_26.jpg' : 'images/index_07.jpg';
			Ext.get('index_div2').update(tpl.apply(this.marqueeData[inx])).show(true);
			this.indexM = inx;
		},
		
		changePic: function(num){
			var inx = this.startPoint+num;
			var max = this.imageData.length-1;
			if(inx.constrain(0, max) != inx) return;
			Ext.getDom('nextPic').src = (inx == max) ? 'images/products_more_36.jpg' : 'images/products_more_29.jpg';
			Ext.getDom('prevPic').src = (inx == 0) ? 'images/products_more_35.jpg' : 'images/products_more_28.jpg';
			Ext.getDom('pdtImage').src = this.imageData[inx];
			Ext.get('pdtImage').show(true);
			this.startPoint = inx;
		},
		
		createMsg: function(type){
			var cb = function(rspn, option){this.backupMsg = getTemplate(rspn.responseText)}; 
			var cn = new Ext.data.Connection({url:'getdata.php?Type='+type}).request({
				scope: this, 
				success: cb
			});
		},
		
		createWinMsg: function(html){
			winBox = new Ext.Window({
    			modal: true, 
    			baseCls: '', 
    			headerAsText: false, 
    			html: html, 
    			closeAction: 'hide',
    			closable: false, 
    			resizable: false, 
    			shim: false, 
    			shadow: false,
			    layout:'fit', 
			    closeAction:'hide', 
				animate: true
			});
			winBox.on('show', function(p){winBox.center()});
		},
		
		clickBtn: function(btn, uid){
			switch(btn){
				case 'YES':
					if(!uid) break;
					var cn = new Ext.data.Connection({url:'getdata.php?Type=addInquiry'}).request({
						scope: this, 
						method: 'POST', 
						params: {'paramID': uid}
					});
					break;
					
				case 'OK':
					var cb = function(rspn, option){
						var pdt = this.deleteBox.parent('div[class^=products_list_div2]');
						var next = pdt;
						while(next){
							if(next.hasClass('products_list_div2-2')){
								if(next.next()) next.next().replaceClass('products_list_div2', 'products_list_div2-2');
								next.replaceClass('products_list_div2-2', 'products_list_div2');
								next = next.next() ? next.next() : next;
							}
							next = next.next();
						}
						pdt.remove();
						if(Ext.getDom('inquiryBox').innerHTML.trim() == '') Ext.get('mainPage').update(rspn.responseText).show(true);
					}
					var cn = new Ext.data.Connection({url:'getdata.php?Type=delInquiry'}).request({
						scope: this, 
						method: 'POST', 
						params: {'paramID': uid}, 
						success: cb
					});
					break;
					
				default:
			}
			winBox.hide();
		},
		
		addInquiry: function(uid, name){
			var tpl = new Ext.XTemplate(this.backupMsg);
			var word = tpl.apply({'Name': name, 'YES': "common.clickBtn('YES', "+uid+")", 'NO': "common.clickBtn('NO')"});
			if(!winBox) this.createWinMsg(word);
			winBox.show();
		}, 
		
		delInquiry: function(box, uid){
			var name = Ext.get(box).prev('strong').dom.innerHTML;
			var tpl = new Ext.XTemplate(this.backupMsg);
			var word = tpl.apply({'Name': name, 'YES': "common.clickBtn('OK', "+uid+")", 'NO': "common.clickBtn('NO')"});
			this.deleteBox = Ext.get(box);
			if(!winBox) this.createWinMsg(word); else winBox.body.update(word);
			winBox.show();
		}, 
		
		endInquiry: function(btn){
			var del = Ext.query('div[onclick ^= common.delInquiry]');
			for(x in del){
				if(isNaN(parseInt(x))) continue;
				Ext.get(del[x]).remove();
			}
			Ext.getDom('extForm').style.display = 'block';
			Ext.get('extForm').slideIn('t');
			btn.onclick = null;
		}
	}
}();

Ext.onReady(common.init, common);