﻿$(document).ready(function(){
	$('img[src$=png]').png();
	$('a[call]').mouseover(function(){
		var me=$(this);
		var call=me.attr('call');
		var offset=me.offset();
		if(!$('#'+call).is(':visible')){
			$('.childmenu:visible').hide().removeShadow();
			$('.menufocus').removeClass('menufocus');
			me.addClass('menufocus');
			$('#'+call).css({left:offset.left,top:offset.top+me.height()}).fadeIn(400);
		};
		//$('select').hide();
	});
	$('.err').livequery(function(){
		$(this).css({filter:'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true",sizingMethod="scale",src="images/err.png")'});
	});
	$(document).click(function(){
		$('.childmenu:visible:last').each(function(){
			$(this).hide();
			if($(this).parents('.childmenu').is(':visible'))$(this).parents('.childmenu').hide();
		});
		//$('select').show();
		$('.language').removeClass('langshow');
		$('.menufocus').removeClass('menufocus');
		$('#selectlist').hide().removeShadow();
	});
	$('.search span').click(function(event){
		window.event.cancelBubble=true;
	});
	$('.childmenu li').each(function(){
		var me=$(this);
		if(me.find('.childmenu').length>0)me.find('a:first').addClass('submenushow');
		me.mouseover(function(){
			var w=me.width();
			var ph=parseInt(me.closest('ul').css('borderTopWidth'))+parseInt(me.closest('ul').css('borderBottomWidth'));
			var position=me.position();
			//$('.childmenu').index($(window.event.toElement).parents('.childmenu:first'))
			me.siblings().find('.childmenu:visible').hide();
			me.find('.childmenu:first:hidden')
			.css({left:position.left+w,top:position.top-ph})
			.fadeIn(400);
		});
	});
	$('.childmenu').bind('mouseleave',function(){
		if($(this).find('.childmenu:visible').length==0)$(this).hide();
		if($('.childmenu:visible').length==0)$('.menufocus').removeClass('menufocus');
		$(this).find('.childmenu:visible').hide();
		//$('select').show();
	});
	$('.classchild').treeview({closed:'closed',persist:'cookie'});
	$('#chkcode_img').live('click',function(){
		$(this).attr('src','inc/chkcode.asp?timer='+new Date().getTime());
	});
	$('a[type]').live('click',function(){
		var me=$(this);
		var type=me.attr('type');
		var aid=me.attr('aid');
		$('.modal,#login__window,.err').remove();
		$.get(type+'.asp?aid='+aid+'&timer='+new Date().getTime(),function(html){
			var _modal=$('<div class="modal"></div>').appendTo('body').css({width:$(document).docWidth(),height:$(document).docHeight()});
			var _window=$('<div id="login__window" class="window">'+html+'</div>').appendTo('body').center();
			$(_window).find('.close').click(function(){
				_modal.remove();
				$('#login__window,.err').remove();
			})
			.end().find('input:first').focus();
		});
		return false;
	});
	//$('.language a[selected]').insertBefore('.language a').remove();
	if($('.language a[selected]').length>0){
		var langhref=$('.language a[selected]').attr('href');
		var lang=$.getParam('language',langhref)||langhref.split('/')[langhref.split('/').length-1];
		$('.loginstauts').load('login_stauts.asp?language='+lang+'&timer='+new Date().getTime());
	};
	$('.image img').wrap('<div></div>');
	//$('.images img').dropShadow({left:0,top:0,blur:1,opacity:0.4});
	$('form[ajax=true]').livequery('submit',function(){
		var me=$(this);
		var action=me.attr('action');
		$.ajax({
			type:'post',
			url:action,
			async:false,
			dataType:'json',
			data:me.serialize(),
			success:function(data){
				if(data.success){
					//$('a[type=login]').parent().css({paddingLeft:8}).html(data.html).find('a').css({zoom:'normal',background:'none',padding:0});
					$('a[type=register]').parent().remove();
					$('#login__window,.err,.modal').remove();
					me.find(':text,textarea').val('');
					me.find('button').after('<span class=success><img src=images/success.gif width=16 height=16 /></span>');
					setTimeout(function(){
						$('.success').fadeOut('slow');
					},3000);
				}else{
					$('.input_error').removeClass('input_error');
					$('.err').remove();
					var err=data.err.split(',');
					$.each(err,function(i,v){
						var val=v.split(':');
						$('*[name='+val[0]+']').addClass('input_error');
						var p=$('*[name='+val[0]+']').offset();
						$('<img src="images/blank.gif" class="err" />').appendTo('body').css({left:p.left-15,top:p.top}).attr('alt',val[1]);
					});
					$('.input_error:first').focus();
				};
			}
		});
		return false;
	});
	/*
	$('form[ajax=true]').submit(function(){
		var form=$(this);
		var action=form.attr('action');
		var data=form.serialize();
		$.ajax({
			url:action,
			type:'post',
			data:data,
			cache:false,
			dataType:'json',
			success:function(data){
				$('.input_error').removeClass('input_error');
				$('.msg').remove();
				if(!data.success){
					if(data.msg.indexOf('=>')>0){
						var msg=data.msg.split('=>');
						var put=$('*[name='+msg[0]+']').addClass('input_error').focus();
						var offset=put.offset();
						var msgbox=$('<span class=msg>'+msg[1]+'</span>')
							.appendTo('body')
							.css({top:offset.top,left:offset.left+put.width()+150,opacity:0})
							.animate({left:'-=130px',opacity:1},500,function(){
								setTimeout(function(){
									msgbox.fadeOut('slow',function(){
										$(this).remove();
									});
								},3000);
							});
					}else{
						//message(data.msg);
					};
				}else{
					if(data.url)window.location=data.url;
					if(data.load){
						var str=data.load.split(':');
						$(str[0]).load(str[1]);
					};
					if(data.refresh)$('.refresh').click();
					if(data.execute)eval(data.execute);
					$('a[function=refresh]').click();
					$().close();
				};
				form.find('button[type=submit]').attr('disabled',false);
			}
		});
		return false;
	});
	*/
	$('.marquee').each(function(i){
		var me=$(this);
		var m1=me.wrapInner('<span id="m1'+i+'"></span>').attr('id','m0'+i).contents();
		var m2=m1.after('<span id="m2'+i+'">'+m1.html()+'</span>');
		var speed=20;
		var marquee=function(){
			if(document.getElementById('m2'+i).offsetWidth-document.getElementById('m0'+i).scrollLeft<=0){
				document.getElementById('m0'+i).scrollLeft-=document.getElementById('m1'+i).offsetWidth;
			}else{
				document.getElementById('m0'+i).scrollLeft++;
			};
		};
		var mymar=setInterval(marquee,speed);
		me.hover(function(){
			clearInterval(mymar);
		},function(){
			mymar=setInterval(marquee,speed);
		});
	});
});