/* $Id: CK_db_proc.js,v 1.11 2011/03/16 21:01:37 Mario Exp $ */
/*
  www.clubkart.it - Copyright (c) 2002-2011 Mario Cimento - cimario@tiscali.it - http://creativecommons.org/licenses/by-nc-nd/2.5/it/
  Reverse engineering of this file is strictly prohibited. File protected by copyright law and provided under license.
  - Script per costruire il database piloti -
*/
function pilot_t(A){this.nome=A.nome;this.foto=A.foto;this.tot_pti=0;this.tot_pti_str="";this.tot_part=0;this.tot_vinte=0;this.tot_podi=0;this.tot_pole=0;this.tot_gv=0;this.palmDivHTML=this.nome}function pilots_t(){this.pilotH=new Hash();this.nome_sort={field:"nome",names:[],ind:[]};this.tot_part_sort={field:"tot_part",names:[],ind:[]};this.tot_gv_sort={field:"tot_gv",names:[],ind:[]};this.tot_pole_sort={field:"tot_pole",names:[],ind:[]};this.tot_podi_sort={field:"tot_podi",names:[],ind:[]};this.tot_vinte_sort={field:"tot_vinte",names:[],ind:[]};this.tot_pti_sort={field:"tot_pti",names:[],ind:[]}}function team_t(A){this.nome=A.nome;this.foto=A.foto;this.tot_pti=0;this.tot_pti_str="";this.tot_part=0;this.tot_vinte=0;this.tot_podi=0;this.tot_pole=0;this.tot_gv=0}function teams_t(){this.teamH=new Hash();this.nome_sort={field:"nome",names:[],ind:[]};this.tot_part_sort={field:"tot_part",names:[],ind:[]};this.tot_gv_sort={field:"tot_gv",names:[],ind:[]};this.tot_pole_sort={field:"tot_pole",names:[],ind:[]};this.tot_podi_sort={field:"tot_podi",names:[],ind:[]};this.tot_vinte_sort={field:"tot_vinte",names:[],ind:[]};this.tot_pti_sort={field:"tot_pti",names:[],ind:[]}}function season_t(A){this.year=A.year;this.year_str=A.year_str;this.name=A.name;this.div_name="div_"+A.name;this.t_div_name="div_team_"+A.name;this.title=A.title;this.title2=A.title2;this.titleTeams=A.titleTeams;this.champ_url=A.champ_url;this.table_w=A.table_w;this.races=A.races;this.pointsMap=A.pointsMap;this.n_discard=A.n_discard;if(this.n_discard>0){this.discard_on=1}else{this.discard_on=0}this.n_discard_team=A.n_discard_team;if(this.n_discard_team>0){this.discard_team_on=1}else{this.discard_team_on=0}this.wght_idx=A.races.wght_idx;if($type(this.wght_idx)=="array"){this.wght_on=1}else{this.wght_on=0}this.use_wght_bonus=A.use_wght_bonus;if($type(A.use_wght_bonus)=="number"){this.wght_bonus_on=1}else{this.wght_bonus_on=0}this.race_link=0;this.season_pilH=new Hash();this.class_sort=[];this.season_teamH=new Hash();this.team_class_sort=[]}function season_pil_t(C,B){function A(){this.nome=B.nome;this.foto=B.foto;this.year=C;this.tot_pti=0;this.tot_pti_str="";this.wght_bonus=0;this.wght_bonus_str="";this.pti=[];this.pti_str=[];this.disc=[];this.pos=0;this.part=0;this.vinte=0;this.podi=0;this.pole=0;this.gv=0;this.b_piaz=1000;this.b_piaz_n=0;this.b_race=0;this.n_pos=[];this.n_race=[];this.team_name=""}A.prototype=B;return new A()}function season_team_t(A,B){this.year=A;this.nome=B;this.tot_pti=0;this.tot_pti_str="";this.pti=[];this.pti_str=[];this.disc=[];this.pos=0;this.part=0;this.vinte=0;this.podi=0;this.pole=0;this.gv=0;this.b_piaz=1000;this.b_piaz_n=0;this.b_race=0;this.n_pos=[];this.n_race=[];this.races=[]}function my_tt(C,B,A){this.elem=C;this.opacity=B;this.pil="";this.opacify=function(){this.elem.style.filter="alpha(opacity="+B*100+")";this.elem.style.opacity=B};this.show=function(){this.elem.style.visibility="visible"};this.hide=function(){this.elem.style.visibility="hidden"};this.hidden=function(){return(this.elem.style.visibility=="hidden")};if(A){this.fx=new Fx.Tween(this.elem,"opacity",{duration:250,wait:false}).set(0)}}Array.prototype.indexOfMin=function(){var A=[];for(var B=0;B<this.length;B++){if(this[B]!=null){A.push(this[B])}}return this.indexOf(Math.min.apply(Math,A))};Array.prototype.indexOfMax=function(){var A=[];for(var B=0;B<this.length;B++){if(this[B]!=null){A.push(this[B])}}return this.indexOf(Math.max.apply(Math,A))};Hash.prototype.FieldSort=function(G){var A=[];var F=[];var N=this.getKeys();var S=N.length;var O=0;var I=[];var C=[];var L=[];var M=[];var K=[];var H=[];var J=[];var Q=[];var E=G.field;for(var P=0;P<S;P++){pil=this.get(N[P]);Q[P]={};Q[P].pos=P;if(($type(pil[E])=="undefined")||($type(pil[E])=="false")||(pil[E]=="undefined")){alert("proprietà --- '"+E+"' --- NON TROVATA!!!\n\n"+pil.tot_pole+" --- "+pil[E]);return }Q[P].values=pil[E];Q[P].tot_pti=pil.tot_pti;Q[P].tot_vinte=pil.tot_vinte;Q[P].tot_podi=pil.tot_podi;Q[P].tot_pole=pil.tot_pole;Q[P].tot_gv=pil.tot_gv;Q[P].tot_part=pil.tot_part}for(var D=0;D<S;D++){for(var B=(D+1);B<S;B++){O=0;if(Q[D].values>Q[B].values){O=1}else{if(Q[D].values==Q[B].values){if(Q[D].tot_pti>Q[B].tot_pti){O=1}else{if(Q[D].tot_pti==Q[B].tot_pti){if(Q[D].tot_vinte>Q[B].tot_vinte){O=1}else{if(Q[D].tot_vinte==Q[B].tot_vinte){if(Q[D].tot_podi>Q[B].tot_podi){O=1}else{if(Q[D].tot_podi==Q[B].tot_podi){if(Q[D].tot_pole>Q[B].tot_pole){O=1}else{if(Q[D].tot_pole==Q[B].tot_pole){if(Q[D].tot_gv>Q[B].tot_gv){O=1}else{if(Q[D].tot_gv==Q[B].tot_gv){if(Q[D].tot_part>Q[B].tot_part){O=1}}}}}}}}}}}}}if(O){var R=Q[D];Q[D]=Q[B];Q[B]=R}}}for(var P=0;P<S;P++){G.ind[P]=Q[P].pos;G.names[P]=N[Q[P].pos]}};Hash.prototype.AlphaSort=function(){var A=this.getKeys();return A.sort()};Hash.prototype.ClassSort=function(){var C=this.getValues();var D=this.getKeys();var W=C.length;var Q=0;var S=100;var M;var L;var P;var U;var K;var I;var G;var R;var N;var T;var J;var F;var E;var O;if($type(C[0])=="object"){for(var B=0;B<W;B++){for(var A=(B+1);A<W;A++){M=this.get(D[B]);L=this.get(D[A]);P=M.tot_pti;U=M.b_piaz;K=M.b_piaz_n;I=M.b_race;G=M.n_pos;R=M.n_race;N=L.tot_pti;T=L.b_piaz;J=L.b_piaz_n;F=L.b_race;E=L.n_pos;O=L.n_race;Q=0;if(P<N){Q=1}else{if(P==N){if(U>T){Q=1}else{if(U==T){for(var H=0;H<S;H++){if(!G[H]&&E[H]){Q=1}if((E[H])&&(G[H]<E[H])){Q=1}if(!E[H]){}if(G[H]!=E[H]){break}}if(H==S){if(I>F){Q=1}}}}}}if(Q){var V=D[B];D[B]=D[A];D[A]=V;M.pos=(A+1);L.pos=(B+1)}}}}return D};var pilots1=new pilots_t();var teams1=new teams_t();var seasonH=new Hash();function CK_db_Init(){for(var A=0;A<CK_db.length;A++){season_init(CK_db[A].year)}}function season_init(R){var i;for(var S=0;S<CK_db.length;S++){if(CK_db[S].year==R){var h=CK_db[S];break}}if(!h){var K="stagione '"+R+"' non trovata all'interno del database\n\n";K+="Le stagioni disponibili sono:\n";for(var S=0;S<CK_db.length;S++){K+=" - '"+CK_db[S].year+"'\n"}alert(K);return }var e=seasonH.get(h.year);if(e!=null){e.season_pilH.each(function(k,d){var l=pilots1.pilotH.get(k.nome);if(l==null){return }l.tot_part-=k.part;l.tot_vinte-=k.vinte;l.tot_podi-=k.podi;l.tot_pole-=k.pole;l.tot_gv-=k.gv;l.tot_pti-=k.tot_pti;l.tot_pti_str=num2str(l.tot_pti);pilots1.pilotH.set(k.nome,l)});e.season_teamH.each(function(l,k){var d=teams1.teamH.get(l.nome);if(d==null){return }d.tot_part-=l.part;d.tot_vinte-=l.vinte;d.tot_podi-=l.podi;d.tot_pole-=l.pole;d.tot_gv-=l.gv;d.tot_pti-=l.tot_pti;d.tot_pti_str=num2str(d.tot_pti);teams1.teamH.set(l.nome,d)});seasonH.erase(h.year);e.season_teamH.empty();i=e}else{i=new season_t(h)}var b=h.races.label.length;for(var Z in i.races.link){if(Z.length){i.race_link=1;break}}i.team_discard_pilots_discards=0;i.team_discard_own_minimums=1;for(var W=0;W<h.piloti.length;W++){var D=h.piloti[W];var E=new season_pil_t(h.year,D);E.pos=(W+1);if(($type(E.peso)=="string")&&i.wght_bonus_on){var L=parseFloat(E.peso);var Q=Math.round(((((L-50)*2)/12)*100));var I=((Q%100)/100);var F=Math.floor(Q/100)+I;E.wght_bonus_per_race=(((L-50)*2)/12);var c=Math.round(I*100);E.wght_bonus_per_race_str=Math.floor(F)+"."+((c<10)?"0":"")+c}E.tot_pti=0;for(var U=0;U<b;U++){var G=E.piaz[U];var J=E.col[U];E.disc[U]=false;if(G>0){E.part++;if((G-1)<i.pointsMap.length){if(($type(E.wght)=="array")&&i.wght_on){var X=parseFloat(E.wght[i.wght_idx[U]]);var M=Math.round(i.pointsMap[G-1]*(X*100));var B=((M%100)/100);var H=Math.floor(M/100)+B;E.pti[U]=H;var C=Math.round(B*100);E.pti_str[U]=Math.floor(H)+"."+((C<10)?"0":"")+C}else{if(($type(E.peso)=="string")&&i.wght_bonus_on){E.pti[U]=i.pointsMap[G-1];var M=Math.round((i.pointsMap[G-1]+E.wght_bonus_per_race)*100);var B=((M%100)/100);var H=Math.floor(M/100)+B;var C=Math.round(B*100);E.pti_str[U]=Math.floor(H)+"."+((C<10)?"0":"")+C}else{E.pti[U]=i.pointsMap[G-1];E.pti_str[U]=""+E.pti[U]}}}else{E.pti[U]=0;if(($type(E.peso)=="string")&&i.wght_bonus_on){var M=Math.round(E.wght_bonus_per_race*100);var B=((M%100)/100);var H=Math.floor(M/100)+B;var C=Math.round(B*100);E.pti_str[U]=Math.floor(H)+"."+((C<10)?"0":"")+C}else{if(($type(E.wght)=="array")&&i.wght_on){E.pti_str[U]="0.00"}else{E.pti_str[U]="0"}}}if(!E.n_pos[G]){E.n_pos[G]=1}else{E.n_pos[G]++}if(!E.n_race[G]){E.n_race[G]=U}}else{if(G==-1){E.part++;E.pti[U]=0;if(($type(E.peso)=="string")&&i.wght_bonus_on){E.pti_str[U]="//.//"}else{if(($type(E.wght)=="array")&&i.wght_on){E.pti_str[U]="//.//"}else{E.pti_str[U]="/"}}}else{E.pti[U]=0;if(($type(E.wght)=="array")&&i.wght_on){E.pti_str[U]="--.--"}else{if(($type(E.peso)=="string")&&i.wght_bonus_on){E.pti_str[U]="--.--"}else{E.pti_str[U]="-"}}}}E.tot_pti+=E.pti[U];if(($type(E.peso)=="string")&&i.wght_bonus_on&&(G>0)){E.tot_pti+=E.wght_bonus_per_race;E.wght_bonus+=E.wght_bonus_per_race}if(G==1){E.vinte++}if(G>0&&G<4){E.podi++}if((G>0)&&(G<E.b_piaz)){E.b_piaz=G;E.b_piaz_n=1;E.b_race=U}else{if((G>0)&&(G==E.b_piaz)){E.b_piaz_n++}}var Y=0;var T=0;if(typeof (J)=="number"){if(J==1){Y=1;T=1}else{if(J==2){Y=1}else{if(J==3){T=1}}}}if(Y){E.pole++}if(T){E.gv++}var P=null;if((typeof (E.team)=="object")&&(typeof (E.team[U])!="undefined")&&(E.team[U].length>0)){P=E.team[U];E.team_name=P}var a=null;if(P!=null){a=i.season_teamH.get(P);if(a==null){a=new season_team_t(h.year,P)}a.tot_pti+=E.pti[U];if(i.wght_bonus_on&&(G!=-1)){a.tot_pti+=E.wght_bonus_per_race}if(i.wght_on||i.wght_bonus_on){a.tot_pti_str=num2str(a.tot_pti)}else{a.tot_pti_str=""+a.tot_pti}if(!a.pti[U]){a.pti[U]=E.pti[U]}else{a.pti[U]+=E.pti[U]}if(i.wght_bonus_on&&(G!=-1)){a.pti[U]+=E.wght_bonus_per_race}if(i.wght_on||i.wght_bonus_on){a.pti_str[U]=num2str(a.pti[U])}else{a.pti_str[U]=""+a.pti[U]}if($type(a.races[U])!="object"){a.races[U]={tot_pti:0,tot_pti_str:"",pilots:new Hash()}}if(a.races[U].pilots.getLength()==0){if((G>0)||(G==-1)){a.part++}}var g={pil_name:"",piaz:0,pti:0,pti_str:"0",disc:false};g.pil_name=D.nome;g.pti=E.pti[U];if(i.wght_bonus_on&&(G!=-1)){g.pti+=E.wght_bonus_per_race}if(i.wght_on||i.wght_bonus_on){g.pti_str=num2str(g.pti)}else{g.pti_str=""+g.pti}g.disc=false;g.piaz=G;a.races[U].pilots.set(D.nome,g);a.races[U].tot_pti+=g.pti;if(i.wght_on||i.wght_bonus_on){a.races[U].tot_pti_str=num2str(a.races[U].tot_pti)}else{a.races[U].tot_pti_str=""+a.races[U].tot_pti}if(!a.n_pos[G]){a.n_pos[G]=1}else{a.n_pos[G]++}if(!a.n_race[G]){a.n_race[G]=U}if(G==1){a.vinte++}if(G>0&&G<4){a.podi++}if((G>0)&&(G<a.b_piaz)){a.b_piaz=G;a.b_piaz_n=1;a.b_race=U}else{if((G>0)&&(G==a.b_piaz)){a.b_piaz_n++}}if(Y){a.pole++}if(T){a.gv++}i.season_teamH.set(P,a)}}if(i.discard_on){var A=E.pti;for(var f=0;f<i.n_discard;f++){var U=A.indexOfMin();E.disc[U]=true;E.tot_pti-=A[U];if((E.piaz[U]>0)&&($type(E.peso)=="string")&&i.wght_bonus_on){var M=Math.round(E.wght_bonus_per_race*100);var B=((M%100)/100);var H=Math.floor(M/100)+B;var C=Math.round(B*100);E.pti_str[U]=Math.floor(H)+"."+((C<10)?"0":"")+C}if(i.team_discard_pilots_discards){var P=null;if((typeof (E.team)=="object")&&(typeof (E.team[U])!="undefined")&&(E.team[U].length>0)){P=E.team[U]}var a=null;if(P!=null){a=i.season_teamH.get(P);if(a!=null){a.tot_pti-=A[U];if(i.wght_on){a.tot_pti_str=num2str(a.tot_pti)}else{a.tot_pti_str=""+a.tot_pti}if($type(a.races[U])=="object"){a.races[U].tot_pti-=A[U];if(i.wght_on){a.races[U].tot_pti_str=num2str(a.races[U].tot_pti)}else{a.races[U].tot_pti_str=""+a.races[U].tot_pti}var N=a.races[U].pilots.get(E.nome);if(N!=null){N.disc=true;a.races[U].pilots.set(E.nome,N)}}}}}delete (A[U])}}if(($type(E.peso)=="string")&&i.wght_bonus_on){var j=Math.round(E.wght_bonus*100);var V=((j%100)/100);var O=Math.floor(j/100)+V;E.wght_bonus=O;E.wght_bonus_str=num2str(E.wght_bonus)}if(i.wght_on||i.wght_bonus_on){E.tot_pti_str=num2str(E.tot_pti)}else{E.tot_pti_str=""+E.tot_pti}i.season_pilH.set(D.nome,E)}if(i.discard_team_on&&i.team_discard_own_minimums){i.season_teamH.each(function(l,k){var n=l.pti;for(var m=0;m<b;m++){if(!n[m]){n[m]=0}l.disc[m]=false}for(var o=0;o<this.n_discard_team;o++){var m=n.indexOfMin();l.disc[m]=true;l.tot_pti-=n[m];if(i.wght_on||i.wght_bonus_on){l.tot_pti_str=num2str(l.tot_pti)}else{l.tot_pti_str=""+l.tot_pti}delete (n[m])}}.bind(i))}i.season_pilH.each(function(k,d){var l=pilots1.pilotH.get(k.nome);if(l==null){l=new pilot_t(k)}l.tot_part+=k.part;l.tot_vinte+=k.vinte;l.tot_podi+=k.podi;l.tot_pole+=k.pole;l.tot_gv+=k.gv;l.tot_pti+=k.tot_pti;l.tot_pti_str=num2str(l.tot_pti);pilots1.pilotH.set(k.nome,l)});i.season_teamH.each(function(l,k){var d=teams1.teamH.get(l.nome);if(d==null){d=new team_t(l)}d.tot_part+=l.part;d.tot_vinte+=l.vinte;d.tot_podi+=l.podi;d.tot_pole+=l.pole;d.tot_gv+=l.gv;d.tot_pti+=l.tot_pti;d.tot_pti_str=num2str(d.tot_pti);teams1.teamH.set(l.nome,d)});i.class_sort=i.season_pilH.ClassSort();i.team_class_sort=i.season_teamH.ClassSort();seasonH.set(h.year,i)}function num2str(C){var B=(Math.round(C*100)/100);var A=(Math.round(B*100)%100);return(Math.floor(B)+"."+((A<10)?"0":"")+A)}function trim(A){return A.replace(/^\s*|\s*$/g,"")}
