var formObj = document.liveForm;
var chkboxes;


function initVariables(){
	chkboxes = getElementsById("input","chkboxes");
}





//Sayfa load olduğunda(refresh vs halinde) cookie deki durumuna göre
//seçililer, hepsi butonunun durumunu ayarlar
if(getCookie("CHOICED") && (getCookie("LASTCLICK") && getCookie("LASTCLICK")=='s' )){
	f1("spnsecili").style.display = 'none';
	f1("spnhepsi").style.display = 'inline';
}else{
	f1("spnsecili").style.display = 'inline';
	f1("spnhepsi").style.display = 'none';        
}
if(getCookie("CHOICED_GOAL_SOUND")){
	for(var i=0;i<document.liveForm.ddlGoalSound.options.length;i++){
		if(formObj.ddlGoalSound.options[i].value == getCookie("CHOICED_GOAL_SOUND")){
			formObj.ddlGoalSound.options[i].selected = true;
		}
	}
}





if((getCookie("SOUNDEFFECT") && getCookie("SOUNDEFFECT")=='OFF')){
		f1("spnSoundOff").style.display = 'none';
		f1("spnSoundOn").style.display = 'inline';
		f1("hdnSoundStatus").value="OFF";
}else{
		f1("spnSoundOff").style.display = 'inline';
		f1("spnSoundOn").style.display = 'none';
		f1("hdnSoundStatus").value="ON";
}



    
function filterMatches(){
    seqVal++;
    
    if(f1('imgFootball').src.indexOf('over') != -1 && f1('imgBasketball').src.indexOf('over') != -1)
    {
         sportType = 0;
    }
    else if(f1('imgFootball').src.indexOf('over') == -1 && f1('imgBasketball').src.indexOf('over') != -1)
    {
        sportType = 2;
    }
    else if(f1('imgFootball').src.indexOf('over') != -1 && f1('imgBasketball').src.indexOf('over') == -1)
    {
        sportType = 1;
    }
    else
    {
        f1('imgFootball').src= variables.imagePath+'futbol-over.gif';
        f1('imgFootball').alt = 'Futbol açık / Kapat';
        sportType = 1;
    }
    
    setUserSportsChoice(sportType)
    getMatches();
}


function submitForm()
{
    var combo = document.getElementById('ddlGrup');
    var grup_text = combo.options[combo.selectedIndex].value;
    
    var txtbox = document.getElementById('txtDate');
    var date_text = txtbox.value;

	document.liveForm.action= "/canli_sonuclar/?filter=&ddlGrup=" + grup_text + "&date=" + date_text;
	document.liveForm.target="";
	document.liveForm.submit(); 			
}



function tercihGG(p){
	if(p=='s'){
		f1("spntercihgoster").style.display = 'none';
		f1("spntercihgizle").style.display = 'inline';
		var chkTdObjs = getElementsById("td","chkbtd")
		if(chkTdObjs){
			for(var i=0;i<chkTdObjs.length;i++){
				chkTdObjs[i].style.display = document.all ? 'inline':'table-cell';
			}
		}
	}else{
		f1("spntercihgoster").style.display = 'inline';
		f1("spntercihgizle").style.display = 'none';
		var chkTdObjs = getElementsById("td","chkbtd")
		if(chkTdObjs){
			for(var i=0;i<chkTdObjs.length;i++){
				chkTdObjs[i].style.display = 'none'
			}
		}
	
	}
}








function frmSubmit(pFilterType)
{
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + (24 * 60 * 60 * 1000)); 
	setCookie("LASTCLICK",pFilterType,expiration);
    
	if(pFilterType == 's'){ 
		var matches = getChoicedMatches();
		if(matches == "" )
			return false;
		var expiration = new Date();
		expiration.setTime(expiration.getTime() + (120 * 60 * 1000)); 
		setCookie("CHOICED",matches,expiration);					
		f1("spnsecili").style.display = 'none';
		f1("spnhepsi").style.display = 'inline';
		f1("hepsi").style.display = 'none';
		f1("hepsib").style.display = 'inline';
		f1("canli").style.display = 'inline';
		f1("canlib").style.display = 'none';         		
		showHideMatches();
	}
    
    
    else if(pFilterType == 'h'){//hepsi tıklanınca
		f1("spnsecili").style.display = 'inline';
		f1("spnhepsi").style.display = 'none';
		f1("hepsi").style.display = 'inline';
		f1("hepsib").style.display = 'none';		
		f1("canli").style.display = 'inline';
		f1("canlib").style.display = 'none';  		
	}
 
 
    else if(pFilterType == 'cm'){//canlı tıklanınca
		f1("spnsecili").style.display = 'inline';
		f1("spnhepsi").style.display = 'none';    
		f1("canli").style.display = 'none';
		f1("canlib").style.display = 'inline';  
		f1("hepsi").style.display = 'none';
		f1("hepsib").style.display = 'inline'; 
        formObj.action = "Canli-Skor/Canli.html";   
		formObj.ddlGoalSound.disabled=true;	
		formObj.filter.value = pFilterType;
		formObj.submit();               
	} 
 
 
 
 
 
 
    
    
    else{
		if(chkboxes){
				for(var i=0;i<chkboxes.length;i++){
					chkboxes[i].disabled = true;
				}
		}


		if(f1("chkall"))		
			f1("chkall").disabled = true;
		formObj.hdnSoundStatus.disabled=true;

		if(!isExchange) 
		{
		    formObj.action = "Canli-Skor.html?bolum=" +pFilterType + "&grup=" + sDdlGrup;
		}

		formObj.ddlGoalSound.disabled=true;	
		
		formObj.filter.value = pFilterType;
		formObj.submit();				
	}
}

//Hepsini sec, gizle checkbox ina tiklandiginda calisir
function checkUnCheckAll(){
	var boolChecked = f1("chkall").checked;
	if(chkboxes){
		for(var i=0;i<chkboxes.length;i++){
			chkboxes[i].checked = boolChecked;
		}
	}	
}

//Secim gozetmeksizin tum maclari listeler
function showAllMatches(){
	var buff = 0;
	if(chkboxes){
		for(var i=0;i<chkboxes.length;i++){
			var obj = chkboxes[i].parentNode.parentNode;
			obj.style.display = document.all ? 'inline' : 'table-row';
			if((document.all && obj.style.backgroundColor !="#bdf899")||(!document.all && rgb2Hex(obj.style.backgroundColor) !="#bdf899" )){ //Macta yeni gol olmussa acik yesil olan satir bg sini bozma!
				if(buff % 2 == 0)
					obj.style.backgroundColor ='#ffffff';	
				else
					obj.style.backgroundColor ='#f0f0f4';	
			}
			buff++;
		}
	}			

}



//secilileri gizleyip gosteren function
function showHideMatches(){
	var matches = getCookie("CHOICED");
	var sonTik = getCookie("LASTCLICK");


	if(!matches)
		matches = "";
	var buff = 0;
	if(chkboxes){
		for(var i=0;i<chkboxes.length;i++){
			var obj = chkboxes[i].parentNode.parentNode;
			if(sonTik && sonTik =='s'){//en son secili butonu tiklandiysa
				if( matches.indexOf( chkboxes[i].value ) != -1){
					chkboxes[i].checked = true;
					obj.style.display = document.all ? 'inline' : 'table-row';//checksizleri gizliyoruz)
					
					if((document.all && obj.style.backgroundColor !="#bdf899")||(!document.all && rgb2Hex(obj.style.backgroundColor) !="#bdf899" )){
						if(buff % 2 == 0)
							obj.style.backgroundColor ='#ffffff';	
						else
							obj.style.backgroundColor ='#f0f0f4';
					}	
					buff++;
				}else{
					obj.style.display = 'none';
				}
			}else{ 
				if((document.all && obj.style.backgroundColor !="#bdf899")||(!document.all && rgb2Hex(obj.style.backgroundColor) !="#bdf899" )){
					if(i % 2 == 0)
						obj.style.backgroundColor ='#ffffff';	
					else
						obj.style.backgroundColor ='#f0f0f4';
				}				
				//Hepsini gosteriyor olsak da kullanicinin tercihlerini yine de check li getiriyoruz(ama checksizleri gizlemiyoruz)
				if( matches.indexOf( chkboxes[i].value ) != -1){
					chkboxes[i].checked = true;
				}
			}
		}

		if(sonTik && sonTik =='s' && buff == 0){
			showAllMatches();
		}
	}			

}

//secili olan maclari cookiye atacagimiz hale donusturur
function getChoicedMatches(){
	var retStr = "";
	if(chkboxes){
		for(var i=0;i<chkboxes.length;i++){
			if(chkboxes[i].checked){
				if(retStr != "")
					retStr += "|";
				retStr += chkboxes[i].value;
			}
		}
	}

	return retStr;

}


function soundOnOff(p){
    if(p=='f'){
	    f1("spnSoundOff").style.display = 'none';
	    f1("spnSoundOn").style.display = 'inline';
	    f1("hdnSoundStatus").value="OFF";

	    var expiration = new Date();
	    expiration.setTime(expiration.getTime() + (365 * 24 * 60 * 60 * 1000)); //1 gün
	    setCookie("SOUNDEFFECT","OFF",expiration);			
    }else{
	    f1("spnSoundOff").style.display = 'inline';
	    f1("spnSoundOn").style.display = 'none';
	    f1("hdnSoundStatus").value="ON";
	    setCookie("SOUNDEFFECT","ON",expiration);			
    }
}


function playSound(soundName) {
	var sStr = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' WIDTH='1' HEIGHT='1'>";
	sStr +=         "<param name='movie' value='"+soundName+"'>";
	sStr +=         "<param name='quality' value='high'><param name='wmode' value='transparent'>";
    sStr +=         "<embed src='"+soundName+"' style=\"width:1px; height:1px\" ";
	sStr +=         "quality=best wmode=transparent";
	sStr +=         "</object>";
    f1("SOUND_HOLDER").innerHTML = sStr;
}

function playGoalSound(newGoals){

	if(newGoals == ''){
		return;
	}
	var isPlaySound = false;
	var playedGoals = getCookie("PLAYEDGOALS");
	var colArray = newGoals.split('|');

	try{	
		for(var i=0;i<colArray.length-1;i++){
			var newGoalToken = colArray[i];
			var arrGoalToken = newGoalToken.split('_');
			var macID = arrGoalToken[0] + "_" + arrGoalToken[1];
			parentTR = f1("S" + macID).parentNode.parentNode.parentNode.parentNode;
			if( ( !isPlaySound && (!playedGoals || playedGoals.indexOf(newGoalToken)==-1) && parentTR.style.display != 'none' && document.liveForm.hdnSoundStatus.value == 'ON')){					
				isPlaySound = true;						
			}
		}	
	}catch(e){
	}
	
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + (120 * 60 * 1000)); //90 dakika
	setCookie("PLAYEDGOALS",newGoals,expiration);						

	if(isPlaySound){
			playSound(variables.imagePath +"sounds/"+document.liveForm.ddlGoalSound.value);
	}
}
function setUserGoalSound(soundName){
    playSound(variables.imagePath+'sounds/'+ soundName)
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + (24 * 60 * 60 * 1000)); 
	setCookie("CHOICED_GOAL_SOUND",soundName,expiration);
}

function setUserSportsChoice(userOption){
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + (365 * 24 * 60 * 60 * 1000)); 
	setCookie("SPORT_CHOICE",userOption,expiration);
}

function refreshScoresInterval(intervalInSeconds){
	
	window.clearInterval(intInterval);
	
	intInterval = window.setInterval('getMatches()',intervalInSeconds * 1000);
}

