var intervalID;
var _LMHTML = new StringBuilder();
var _CMHTML = new StringBuilder();
var _liveRowIndex = 0;
var _tempLiveCount = 0;
var _completedRowIndex = 0;
var _newGoals = '';



function showCM(){
    
   cMatches = 1;
   seqVal = -1;
   f1('COMPLETEDMATCHES_LOADING_HOLDER').innerHTML = '<font face="Arial" size="3" color="#333333"><b>Yükleniyor...</b></font><br /><br /> <font face="Arial" size="5" color="#cccccc"><b>www.bahistr.com</b></font>';                                              
   getMatches();
   setCookie("CM",cMatches);
}



function getMatches(){
    new Ajax.Request(
         'CanliSkor.swf?seq='+seqVal+'&txtDate=' + sTxtDate + '&filter=' + sFilter + '&ddlGrup='+ sDdlGrup + '&CM='+ cMatches+'&st='+sportType, {
            method : 'get',
            asynchronous:true, 
            evalScripts:true, 
            onComplete:function(request){prepareHTML(request)},
            onFailure:function(request){}
            });
}


function prepareHTML(output){
	if(output.responseText.indexOf("Loading.")==-1 && output.responseText.indexOf("Yükleniyor.")==-1 && output.responseText.indexOf("Yukleniyor.")==-1){
		if(output.responseText != "NOP"){
			initStringBuilders();

			try{
				eval(output.responseText);
			}catch(e){
                //alert(e);
			}

            _tempLiveCount = _liveRowIndex;
			completeStringBuilders();
			
			_liveRowIndex = 0;
			_completedRowIndex = 0;
			loadLive();
		    
			if(cMatches == 1 || _tempLiveCount == 0)
			{
			    cMatches = 1;
			    loadCompleted();
            }
			try{
			    if(isRefreshed)
			    {
			        window.scrollTo(0,240);
			        isRefreshed = false;
			    }
			}
			catch(err)
			{}
		}
	}
}



function loadLive(){
		f1('LIVEMATCHES_LOADING_HOLDER').style.display = 'none';
		f1('LIVEMATCHES_HOLDER').innerHTML = _LMHTML.toString();
		_LMHTML.clear();
		initVariables();
		showHideMatches();
		playGoalSound(_newGoals);		
		_newGoals = '';		
}

function loadCompleted(){
		f1('COMPLETEDMATCHES_LOADING_HOLDER').style.display = 'none';
		f1('COMPLETEDMATCHES_HOLDER').innerHTML = _CMHTML.toString();
		_CMHTML.clear();
}


function SETSEQ(val){
	seqVal = val;
}

function SETLMC(val){
	if(val==0)
	{
		refreshScoresInterval(30);
	}
	else
	{
		refreshScoresInterval(iRefreshSecond);
	}
}

function L1M(altgId, altgAdi, altgKisaadi, altgEleme, grupAdi, macId, macDurum, macSezon, takim1ID, takim1Adi, takim2ID,takim2Adi,iySonuc,sonuc,macIddaaID,macSaat,kks1,kks2,u1,u2,imoMs1,imoMs0,imoMs2,info,altgGrup,kalanSaat,kalanDakika,imoUnder,imoOver, tahminId, tip,iyH1,iyH2, distMacId,H1,H2){
			
            	var rowBgColor = _liveRowIndex % 2 == 0 ? '#f0f0f4' : '#ffffff';
				if(u1 > 0 || u2 > 0){
					_newGoals += distMacId+'_'+sonuc.replace(' ','').replace('-','_') + "|";
					rowBgColor = '#bdf899';
				}
				var isLiveMatch = "1,2,3,5,7".indexOf(macDurum) != -1;
				_LMHTML.append('<tr style="background-color:' + rowBgColor + ';height:20px;" onmouseover="HLon(this)">');
				_LMHTML.append('<td style="border-left-width: 0px;" nowrap><img height=11 width=16 src="'+ _EXT_IMG_PATH +"images/groups/"+ altgGrup + '.gif" '+(document.all ? 'alt="'+grupAdi+'-'+altgAdi+'"':'')+'></td>');
				_LMHTML.append('<td class="linked_cell" nowrap>'+ (altgEleme == 0 || altgEleme==2 ?  '<a href="Puan-Durumu/'+grupAdi+'/'+altgAdi+'/'+altgId+'-'+altgEleme+'" onclick="PuanDurumu('+altgId+','+altgEleme+',\''+grupAdi+'\',\''+altgAdi+'\')"  title="Puan durumu" target="'+altgId+'" >'+altgKisaadi+'</a>':altgKisaadi)+'</td>'); 

    
				_LMHTML.append('<td class="table_header" align=center '+( macDurum  == 0 ? 'title="'+((kalanSaat>0 ? kalanSaat+' Saat,':'')+(kalanDakika>0 ? kalanDakika +' Dakika':''))+'"':'')+'><span '+( isLiveMatch ? 'class="green" id=D'+distMacId :'' )+'>' + macSaat.replace('@@','<blink>+</blink>').replace('@','<blink>\'</blink>') + '</span></td>' );    
    
                
				_LMHTML.append('<td class="linked_cell" align=center>&nbsp;<a href="javascript:Kc(' + macId + ',' + takim1ID + ',' + takim2ID + ',' + macSezon + ',' + altgId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')">' + macIddaaID + '</a></td>' );  


				_LMHTML.append('<td class="linked_cell" style="padding-left:3px" align="right">');
                for (i1=0;i1<kks1;i1++){
                   _LMHTML.append('<img src="images/red.jpg" style="vertical-align:baseline;" />&nbsp;'); 
                }
                i1=0;
                _LMHTML.append('<a href="javascript:Tc('+takim1ID+','+altgId+',\''+takim1Adi+'\')">' + takim1Adi + '</a>'+(parseInt(H1)>0 ? ' <span style="color:red;">(H '+parseInt(H1)+')</span>':'')+'&nbsp;</td>');
  


				if ( macDurum  != 0 )
				{
					_LMHTML.append('<td class="linked_cell" align=center> <a href="javascript:MSc(' + macId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')"><b><span '+( isLiveMatch ? 'id=S'+distMacId :'' )+'>' + ( u1 > 0 ? '<img src='+variables.imagePath+'ball.gif border=0>':'')+sonuc+(u2 > 0 ? '<img src='+ variables.imagePath +'ball.gif border=0>':'')+ '</span></b></a></td>' );
				}
				else
				{
					_LMHTML.append('<td class="linked_cell" align=center><a href="javascript:MSc(' + macId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')">vs</a></td>' );
				}
  
  
  
                
				_LMHTML.append('<td class="linked_cell" style="padding-left:3px" align="left"><a href="javascript:Tc('+takim2ID+','+altgId+',\''+takim2Adi+'\')">' + takim2Adi +'</a>'+(parseInt(H2)>0 ? ' <span style="color:red;">(H '+parseInt(H2)+')</span>':''));
                
 
                for (i2=0;i2<kks2;i2++){
                   _LMHTML.append('&nbsp;<img src="images/red.jpg" style="vertical-align:baseline;" />&nbsp;'); 
                }
                i2=0;     
                
                
                _LMHTML.append('</td>' );
                



                
				_LMHTML.append('<td class="table_header" align=center>&nbsp;' + iySonuc.replace(' ','').replace(' ','') + '</td>'); 
				

		

                


				_LMHTML.append('<td class="table_header" style="border-right-width:1px" align="center"><a href="javascript:Kc(' + macId + ',' + takim1ID + ',' + takim2ID + ',' + macSezon + ',' + altgId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')"><img height=11 src="images/Stat.gif"  border="0" alt="T" style="cursor:pointer"/></a></td>' ); 

                
				_LMHTML.append('<td><input type=checkbox  name=chkboxes value='+distMacId+' id=chkboxes  class=canlichkb></td>' );
                
                
				_LMHTML.append('</tr>');
 		
			_liveRowIndex++;
			


	
}




function C1M(altgId,altgKisaadi,altgAdi,altgEleme,altgGrup,grupAdi,takim1Id, takim2Id,macId, macSezon, macIddaaid, takim1Adi, takim2Adi, iySonuc,sonuc, macEledi, kks1, kks2, macDurum, durum, info,macSonuc1,macSonuc2,tahminId,tip){
			
			_CMHTML.append('<tr style="height:20px;background-color:' + (_completedRowIndex% 2 == 0 ? '#f0f0f4' : '#ffffff') + '" onmouseover="HLon(this)">' );
            
			_CMHTML.append('<td style="border-left-width: 0px;"><img height=11 width=16 src="'+ _EXT_IMG_PATH +"images/groups/"+ altgGrup + '.gif" '+(document.all ? 'alt="'+grupAdi+'-'+altgAdi+'"':'')+'></td>' );
            
			_CMHTML.append('<td class="linked_cell" nowrap>'+ (altgEleme == 0 || altgEleme==2 ?  '<a href="javascript:PuanDurumu('+altgId+','+altgEleme+',\''+grupAdi+'\',\''+altgAdi+'\')"  title="Puan durumu">'+altgKisaadi+'</a>':altgKisaadi)+'</td>'); 



			_CMHTML.append('<td class="linked_cell">&nbsp;<a href="javascript:Kc(' + macId + ',' + takim1Id +',' + takim2Id + ',' + macSezon + ',' + altgId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')">' + macIddaaid + '</a></td>' );

          


            
            _CMHTML.append('<td class="linked_cell" style="padding-left:3px" align="right">' );
            

                for (ii1=0;ii1<kks1;ii1++){
                   _CMHTML.append('<img src="images/red.jpg" style="vertical-align:baseline;" />&nbsp;'); 
                }
                ii1=0;


            _CMHTML.append('<a href="javascript:Tc('+takim1Id+','+altgId+',\''+takim1Adi+'\')">' + ( macSonuc1> macSonuc2 ? '<b>':'' )+( macEledi == 1 ? '<span style="color:Red;">':'' )+ takim1Adi +( macEledi == 1 ? '</span>':'' )+ ( macSonuc1> macSonuc2 ? '</b>':'' )+  '</a></td>' );





                                                                                                                                                                                                
 			if ( macDurum  != 0 && macDurum  != 9 && macDurum  != 10 && macDurum  != 11)
			{
				_CMHTML.append('<td class="linked_cell" align="center">&nbsp;<a href="javascript:MSc(' + macId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')"><b>' + sonuc + '</b></a>' ); 
			}
			else
			{
			 if(macDurum==9){var yazi='ERT';}
			 else if(macDurum==10){var yazi='HÜK';}
             else{var yazi='vs';}             
             
                                                    			 
				_CMHTML.append('<td class="linked_cell" align="center">&nbsp;<a href="javascript:Kc(' + macId + ',' + takim1Id + ',' + takim2Id + ',' + macSezon + ',' + altgId + ',\''+takim1Adi+'\',' + '\''+takim2Adi+'\')">'+yazi+'</a>' );
			}           
 _CMHTML.append('</td>' );   
    
    


			_CMHTML.append('<td class="linked_cell" style="padding-left:3px" align="left"><a href="javascript:Tc('+takim2Id+','+altgId+',\''+takim2Adi+'\')">' + ( macSonuc2> macSonuc1 ? '<b>':'' )+( macEledi == 2 ? '<span style="color:Red;">':'' )+ takim2Adi +( macEledi == 2 ? '</span>':'' )+ ( macSonuc2> macSonuc1 ? '</b>':'' )+  '</a>' ); 
            
for (ii2=0;ii2<kks2;ii2++){
   _CMHTML.append('&nbsp;<img src="images/red.jpg" style="vertical-align:baseline;" />&nbsp;'); 
}
ii2=0;  
            
 _CMHTML.append('</td>' ); 


            
            
            
			_CMHTML.append('<td class="table_header" align=center>&nbsp;' + iySonuc.replace(' ','').replace(' ','') + '</td>' );



			_CMHTML.append('<td class="table_header" align="center"><a href=javascript:Tahmin('+tahminId+')><img height=11 src="images/Stat.gif" alt="T" style="cursor:pointer" border="0"/></a>&nbsp;</td>' ); 
            
			_CMHTML.append('</tr>'); 	
			
			
			_completedRowIndex++;
}






function initStringBuilders(){

		_LMHTML.append( '<table class="bahistrcanli" cellpadding="0" cellspacing="0" border="0" id=QueryGrid_CanliMaclar width="596px" onmouseout="HLoff()">' );	
		_LMHTML.append( '<tr style="background-image: url('+ variables.imagePath +'canlisonuclar_tabs.gif); height:22px;">');
        
        
        
		_LMHTML.append( '<td style="border-left-width: 0px;" colspan="2" class="iddaa_white_txt" style="border-left-style:none;"  width="8%" >Lig</td>');

		_LMHTML.append( '<td class="iddaa_white_txt" width="3%">Saat</td>');         
		_LMHTML.append( '<td class="iddaa_white_txt" width="3%">Kod</td>');
		_LMHTML.append( '<td class="iddaa_white_txt" width="23%">Ev Sahibi</td>');
		_LMHTML.append( '<td class="iddaa_white_txt" width="6%" align="center">&nbsp;</td>');        
		_LMHTML.append( '<td class="iddaa_white_txt" width="23%">Misafir</td>');
		_LMHTML.append( '<td class="iddaa_white_txt" width="4%" align="center">IY</td>');
		_LMHTML.append( '<td style="border-right-width:1px" class="iddaa_white_txt" width="3%">&nbsp;</td>'); 
		_LMHTML.append( '<td align="left" width="2%"><input type="checkbox" name="chkall" value="ON" id="chkall" class="canlichkb" onclick="checkUnCheckAll()" title="Tümünü Seç"></td>');         
		_LMHTML.append( '</tr>' ); 




		_CMHTML.append('<table  class="bahistrcanli" cellpadding="0" cellspacing="0" border="0" width="596px" onmouseout="HLoff()">' );
		
        
        if(cMatches==1)
		{	
		_CMHTML.append('<tr style="background-image: url('+ variables.imagePath +'canlisonuclar_tabs.gif);" height="22px">');
		_CMHTML.append('<td class="iddaa_white_txt" style="border-left-style:none;" style="border-left-width: 0px;" colspan=2 width="12%">Lig</td>');
        
		_CMHTML.append('<td class="iddaa_white_txt" width="4%">Kod</td>');
		_CMHTML.append('<td class="iddaa_white_txt" width="32%">Ev Sahibi</td>');
		_CMHTML.append('<td class="iddaa_white_txt" width="8%" align="center">MS</td>');        
		_CMHTML.append('<td class="iddaa_white_txt" width="32%">Misafir</td>');
		_CMHTML.append('<td class="iddaa_white_txt" width="8%" align="center">IY</td>');
		_CMHTML.append('<td class="iddaa_white_txt" width="4%">&nbsp;</td>'); 
		_CMHTML.append('</tr>');
		}
}




function completeStringBuilders(){
		if ( _liveRowIndex == 0 ) 
		{ 
			_LMHTML.append( '<tr  style="background-color:#f0f0f4">' );
			_LMHTML.append( '<td class="iddaa_black_txt" colspan=17 align="center">Şu anda maç yok.</td>' );
			_LMHTML.append( '</tr>' ); 	 
		}
		_LMHTML.append('</table>' );
		
		if ( _completedRowIndex == 0 && (cMatches == 1 || _liveRowIndex==0)) 
		{ 
			_CMHTML.append('<tr style="background-color:#f0f0f4">' );
			_CMHTML.append('<td class="iddaa_black_txt" colspan=11 align="center">Biten maç yok.</td>' );
			_CMHTML.append('</tr>' ); 	 
		}
		
		_CMHTML.append( '</table>' ); 
		
}


function StringBuilder(initialValue)
{
    this.buffer         = new Array();
    this.bufferLength   = 0;
    
    if(initialValue != null)
        this.append(initialValue);
}

StringBuilder.prototype.append =  function (appendValue)
{
    // ** validate data
    if(appendValue == null)
    {
        return;
    }
    
    // ** Increase the internal bufferlength
    this.bufferLength += appendValue.length;
    
    // ** And add the new value to the array.
    this.buffer.push(appendValue)
}

StringBuilder.prototype.clear =  function ()
{
    this.buffer = new Array();
    this.bufferLength = 0;
}

StringBuilder.prototype.length =  function ()
{
    return this.bufferLength;
}

StringBuilder.prototype.toString =  function ()
{
    return this.buffer.join("");
}

