// JavaScript Document
$(document).ready(function(){
	$('#login').find('.btnInput').click(function(){
		if($(this).parent().find('.txtInput').eq(0).attr('value')==''){
			$(this).parent().find('.txtInput').eq(0).attr('value').focus;
			return false;
		}else if($(this).parent().find('.txtInput').eq(1).attr('value')==''){
			$(this).parent().find('.txtInput').eq(1).attr('value').focus;
			return false;
		}

	});		
	
	setTimeout(data,500);
	if($('.printPage').is('div')){
		window.print();
	}
	imgMove();
	demo();
	fileUp();
})

function fileUp(){
	if($('.fileType').length){
		$('.fileType').change(function(){
			var name=$(this).attr('value');
			var ll=name.split('\\');
			if(ll.length<2){
				ll=name.split('/');
			}
			name=ll[ll.length-1];
			var inx=$('#fileInputBox').find('input').index(this);
			$('#info_'+inx).find('.fileName_').text(name);
			$('#info_'+inx).fadeIn('slow');
			$(this).hide();
			showFile();
		});
		$('.fileInfoTab').find('a').click(function(){
			$('.fileInfoTab').eq($('.fileInfoTab').find('a').index(this)).css('display','none');
			$('#fileInputBox').find('input').css('display','none');
			showFile();
		});
	}
	
	if($('.piceInput').length){
		$('.piceInput').focus(function(){
			var val='';
			val=$(this).attr('value');
			if(val=='Length' || val =='Width'){
				$(this).attr('value','');
			}
		});
	}
}


function showFile(){
	var nq=1;
	for(var n=0;n<3;n++){
		if(nq){
			if($('.fileInfoTab').eq(n).css('display')=='none'){
				nq=0;
				$('#fileInputBox').find('input').eq(n).after('<input type="file" name="file_'+n+'" class="fileType" id="file_'+n+'" />');
				$('#fileInputBox').find('input').eq(n).remove();
				$('#fileInputBox').find('input').eq(n).show();
				$('#fileInputBox').find('input').eq(n).change(function(){
						var name=$(this).attr('value');
						var ll=name.split('\\');
						if(ll.length<2){
							ll=name.split('/');
						}
						name=ll[ll.length-1];
						var inx=$('#fileInputBox').find('input').index(this);
						$('#info_'+inx).find('.fileName_').text(name);
						$('#info_'+inx).fadeIn('slow');
						$(this).hide();
						showFile();
					});
			}
		}
	}
}


function imgMove(){
	$('.imgOff').parent().mousemove(function(){
		$(this).find('.imgOn').hide();									 
		$(this).find('.imgOff').show();
	});
	$('.imgOff').parent().mouseout(function(){
		$(this).find('.imgOn').show();									 
		$(this).find('.imgOff').hide();
	});
}

function demo(){
	$('.demoImg').click(function(){
			var txt='';
			//if($.browser.msie && $.browser.version =='6.0'){
				txt='<div class="imgInfoDemo" style="background-image:url(./'+$(this).attr('value')+'_text.png)"></div>';
			//}else{
			//	txt='<div class="imgInfoDemo"><img src="./'+$(this).attr('value')+'_text.png"/></div>';
			//}
			if($(this).find('.imgInfoDemo').length && $(this).find('.imgInfoDemo').attr('value')){
				$(this).find('.imgInfoDemo').fadeIn("slow");
				$(this).find('.imgInfoDemo').attr('value',0);
			}else if($(this).find('.imgInfoDemo').length){
				$(this).find('.imgInfoDemo').fadeOut("slow");
				$(this).find('.imgInfoDemo').attr('value',1);
			}else{
				$(this).append(txt);
				$(this).find('.imgInfoDemo').hide();
				$(this).find('.imgInfoDemo').fadeIn("slow") ;
			}
			return false;
	});
}

function sortFun(){
	if($('#sBId').is('a')){
		$('#accTabSort').find('a').css('display','block');
		$('#sBId').click(function(){
			if($(this).is('.down')){
				$(this).removeClass('down');
				sortDo('.td1',1);
			}else{
				$(this).addClass('down');
				sortDo('.td1',0);
			}
			return false;
		});
		
		$('#sBEtd').click(function(){
			if($(this).is('.down')){
				$(this).removeClass('down');
				sortDo('.td10',1);
			}else{
				$(this).addClass('down');
				sortDo('.td10',0);
			}
			return false;
		});
		
	}	
};


function sortDo(id,type){
	var table='#accountTab';
	var arr=new Array();
	var n=0;
	$(table).find('tbody').find('.trData').each(function(){
		arr[n]=$(this).find(id).attr('value');
		n++;
	});
	if(type){
		arr=arr.sort(function compare(a,b){return b-a;});
	}else{
		arr=arr.sort();
	}
	for(var i=0;i<arr.length;i++){
		//alert($(table).find('tbody').find('.trData').find('td.td10[value="'+arr[i]+'"]').parent().html());
		$('#accTabSort').after($(table).find('tbody').find('.trData').find(id+'[value="'+arr[i]+'"]').parent());
	}
	
}
function data(){
	if($('.trData').is('tr')){
		var l=new Array();
		$('.trData:eq(0)').find('td').each(function(){
			var ro=0+$('.trData:eq(0)').find('td').index(this);
			l[ro]=$(this).width()+parseInt($(this).css('paddingLeft'))+parseInt($(this).css('paddingRight'));
		
			
		});
		$('.trData').each(function(){
			var d=$(this).attr('value');
			var dArr=d.split('_');
			var len=0;
			var n=0;
			for(var i=0;i<dArr.length;i++){
				if(dArr[i]==1)
					n=i+1;
			}
			for( var i=1;i<=n;i++){
				len+=l[i];
				
			}
			
			//if($.browser.msie){
			//	len+=n;
			//}else{
				len+=n;
				if(n<4)  len++;
			//}
			//
			$(this).find('td').eq(1).find('.thisBox').append('<div class="dataImgBoxWrap"><div class="dataImgBox"><img src="/images/gradient.png"/></div></div>');
			var pl=parseInt($(this).find('td:last').css('paddingTop'))+parseInt($(this).find('td:last').css('paddingBottom'))+1;
			var h=$(this).find('td:last').height()+pl+1;
			if($.browser.msie &&  $.browser.version =='7.0')
				len--;
			else
				len++;
			$(this).find(' .dataImgBoxWrap').height(h);
			$(this).find(' .dataImgBoxWrap').css({'left':'-5px','top':'-6px','width':'0'});
			//$(this).find('td > .dataImgBox').css({'position':'absolute'});
			$(this).find(' .dataImgBoxWrap > .dataImgBox').height(h);
			$(this).find(' .dataImgBoxWrap > .dataImgBox').width(len);
			//alert(len);
		});
		$('.trData').find(' .dataImgBoxWrap').animate({width: "500px"}, 3000, sortFun());
		
	}
}
