// JavaScript Document
var common = function(){
	var urllink = location.toString();
	var obj = Ext.urlDecode(urllink.substring(urllink.lastIndexOf('?')+1, urllink.length));
	var alertWin = new Ext.Window({
		title: '請輸入帳號密號', 
		width: 250, 
		height: 180, 
		layout: 'fit', 
        bodyStyle: 'padding:15px;',
		plain: true, 
		closeAction: 'hide', 
		buttonAlign: 'center', 
		html: ['<div>帳號： <input id="username" name="username" size="22" /></div>', 
			'<div>密碼： <input id="passname" name="passname" type="password" size="22" /></div><br>', 
			'<div id="errorMsg" style="color:#FF0000"></div>'].join(''),
		buttons: [{text: 'OK', handler: function(){common.regLogin()}}, {text: 'Cancel', handler: function(){alertWin.hide()}}]
	});
	
	function flashCode(uid){
		html = (Ext.isIE) ? ['<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="480" height="360" id="main_movie" name="main_movie" type="application/x-shockwave-flash">', 
			'<param name="allowFullScreen" value="false" />',
			'<param name="movie" value="../vcastr/vcastr3.swf" />', 
			'<param name="FlashVars" value="xml=vcastr.php?UID='+uid+'" />',
			'</object>'] : 
			['<embed src="../vcastr/vcastr3.swf" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="480" height="360" name="main_movie" FlashVars="xml=vcastr.php?UID='+uid+'"></embed>'];
		return html.join('');
	}
	
	return {
		backupPic: null,
		dorun: .01,
		extField: [], 
		
		init: function(){
			var display = Ext.query('div[ID^=menulist]');
			for(i=0;i<display.length;i++){
				Ext.get(display[i]).hide();
//				display[i].style.height = 0;
//				display[i].style.display = 'none';
			}
			
			if(Ext.get('keyword')){
				var textf = Ext.getDom('keyword');
				var thename = new Ext.form.TextField({
					emptyText: textf.value,
					fieldClass: '',
					invalidClass: '',
					emptyClass: '',
					focusClass: '',
					value: '',
					id: textf.id,
					applyTo: textf.id
				});
				this.searchField = thename;
				thename.getEl().removeClass('x-form-text')
				thename.getEl().removeClass('x-form-empty-field')
				thename.getEl().addKeyListener({key:13},function(){
					this.gotosearch();
				},this);
			}
			
			if(Ext.get('advertisement')){
				var carousel = new Ext.ux.Carousel({
					numVisible: 1,
					animationSpeed: 0.5,
					autoPlay: 5000, 
					scrollInc: 1,
					//animationMethod: 'backBoth', 
					renderTo: 'advertisement', 
					prevElement: "profit_prev",
					nextElement: "profit_next"
				});
				var cc = function(a, b){
					if(b){
						a.dom.src = a.id == 'profit_prev' ? 'images/components_more_25.jpg' : 'images/components_more_26.jpg';
					}else{
						a.dom.src = a.id == 'profit_prev' ? 'images/components_more_31.jpg' : 'images/components_more_32.jpg';
					}
				}
				carousel.on('prevButtonStateHandler', cc);
				carousel.on('nextButtonStateHandler', cc);
				carousel.show();
			}
			
			if(Ext.get('menulist1')){
				var list = Ext.get('menulist1').parent();
				i = list.getHeight()/600;
				list.dom.style.height = 0;
				list.dom.style.display = 'block';
				if(Ext.isIE6){
					list.slideIn('t',{
						afterStyle: {height:"auto"},
						block: false,
						useDisplay: true,
//						stopFx: true, 
						duration: i, 
						callback: function(){
							this.theFadeOut(Ext.get('menulist1'));
						},
						scope: this
					});
				}else{
					list.autoHeight(true, i, function(){
						common.theFadeOut(Ext.get('menulist1'));
					}, 'easeOutStrong');
				}
				
//				task = new Ext.util.DelayedTask(this.theSlideOut, this, [Ext.get('menulist1')]);
//				task.delay(500);
			}
			
			if(Ext.getDom('request')){
				fields = Ext.query('[bvalue]');
				var ln = fields.length;
				for(var i=0;i<ln;i++){
					var text = (Ext.isIE) ? fields[i].bvalue : getAbrite(fields[i], 'bvalue');
					var type = (fields[i].type == 'text') ? 'TextField' : 'TextArea';
					var ec = (fields[i].id == 'Verify') ? '' : fields[i].className+'-2';
					var indexField = new Ext.form[type]({
						emptyClass: ec, 
						emptyText: text, 
						fieldClass: '',
						focusClass: '',
						applyTo: fields[i].id
					});
					indexField.getEl().removeClass('x-form-text')
					indexField.getEl().removeClass('x-form-empty-field')
					this.extField.push(indexField);
				}
			}
		},
		
		theFadeOut: function(el){
			if(!el) return;
			el.show({
				duration: .00025, 
				callback: function(){
					this.theFadeOut(el.next());
				},
				scope: this
			});
		}, 
		
		loginParam: function(uid){
			twofloor = ((obj.ID) ? obj.ID : '0')+'|';
			twofloor = (uid.indexOf('|') != -1) ? '' : twofloor;
			var cb = function(rspn, a){
				var json = Ext.util.JSON.decode(rspn.responseText);
					Ext.getDom('detailParam').value = (obj.KindID) ? obj.KindID+'|'+twofloor+uid : uid;;
					Ext.getDom('detailPdt').submit();
			}
			var cn = new Ext.data.Connection({url:"getdata.php?Type=isLogin"}).request({
				scope: this, 
				method: "POST", 
				success:cb
			});
		}, 
		
		regLogin: function(){
			Ext.get('errorMsg').update('<div class="x-mask-loading">&nbsp;</div>')
			var cb = function(rspn, a){
				var json = Ext.util.JSON.decode(rspn.responseText);
				(!eval(json.isLogin)) ? Ext.get('errorMsg').update('您的帳號密碼有誤') : this.loginParam(this.backupID);
			}
			var cn = new Ext.data.Connection({url:"getdata.php?Type=login"}).request({
				scope: common, 
				method: "POST", 
				params: {'paramID': Ext.getDom('username').value, 'passID': Ext.getDom('passname').value}, 
				success:cb
			});
		},
		
		gotosearch: function(){
			var s1 = Ext.getDom('select');
			var s2 = Ext.getDom('select2');
			var keyword = Ext.getCmp('keyword').getRawValue().trim();
			if(s1.value == ''){
				alert('請'+s1.options[0].text);
				return;
			}
			if(s2.value == ''){
				alert('請'+s2.options[0].text);
				return;
			}
			if(keyword == ''){
				alert('Please '+Ext.getDom('keyword').value);
				return;
			}
			keyword = encodeURIComponent(keyword);
			var to = s1.value == 'pdt' ? 'products' : 'components';
			var cate = s2.value.split('|');
			var str = '&KindID='+cate[0]+(cate.length == 1 ? '' : '&ID='+cate[1]); 
			var strURL= to+'.php?Type=search&Keyword='+keyword+str;
			location.href=strURL;
		}, 
		
		showFM: function(uid){
			var win = Ext.getCmp('movieShow');
			if(!win){
				win = new Ext.Window({
					html: flashCode(uid), 
					id: 'movieShow',
					width: 494, 
					height: 377
				});
			}
//			win.on('show', function(obj){obj.setHeight(337)});
			win.show();
		}
	}
}();

function changeMenu(droplist, changeDrop){
	var drop = Ext.getDom(changeDrop);
	for (var m=drop.options.length;m>0;m--) {
		drop.options[m] = null;
	}
	var cb = function(rspn, a){
		var value = eval(rspn.responseText);
		for(var i=0;i<value.length;i++){
			drop.options[i+1] = new Option(value[i].Name, value[i].ID);
		}
	}
	if(droplist.value == '') return;
	var cn = new Ext.data.Connection({url:"getdata.php?Type=showMenu"}).request({
		scope: this, 
		method: "POST", 
		params: {"paramID": droplist.value.toString()}, 
		success:cb
	});
}

Ext.onReady(common.init, common);
