/**
	Utility functions
**/
Function.prototype.bind = function(scope) {
  var _function = this;  
  return function() {
    return _function.apply(scope, arguments);
  }
}

/**
	Class: Localization
**/
function Localization() {
	this.locale = new Array();
	this.setDefault();
}
Localization.prototype.setDefault = function() {
	this.set("until_days", "(še @days dni)")
	this.set("of","od");
}
Localization.prototype.set = function(description, value) {
	this.locale[description] = value;
}
Localization.prototype.get = function(description) {
	return this.locale[description];
}
var localization = new Localization();

/**
	Class: SystemCheck
**/

function SystemCheck() {
	
}
SystemCheck.prototype.CookiesEnabled = function() {
	return navigator.cookieEnabled;
}
SystemCheck.prototype.FlashVersion = function() {
	playerVersion = swfobject.getFlashPlayerVersion();
	return playerVersion.major;
}
SystemCheck.prototype.JavaScriptEnabled = function() {
	return true;
}
SystemCheck.prototype.Browser = function() {
	return navigator.appVersion;
}
SystemCheck.prototype.Platform = function() {
	return navigator.platform;
}
SystemCheck.prototype.AdBlocker = function() {
	return true;
}
SystemCheck.prototype.GeoIPCheck = function() {
	return true;
}

var systemCheck = new SystemCheck();

/**
	Class: Reminder
	Params:
	@UL_ID : element name of the list that will get updated
	@TEXT_ID : element name of the text placeholder "Opomniki". If the list of reminders is empty this field will automatically be hidden
**/

function Reminder() {
	this.list_id = "";
	this.text_id = "";
/*	this.add_url = "/sportovi/script/communication.php?add=@id";
	this.remove_url = "/sportovi/script/communication.php?remove=@id";
	this.refresh_url = "/sportovi/script/communication.php?refresh";*/
	this.add_url = "/script/addreminder?ev=@id";
	this.remove_url = "/script/delreminder?ev=@id";
	this.refresh_url = "/script/reminders";
	this.enabled = true;
}
Reminder.prototype.setEnabled = function( bool ) {
	this.enabled = bool;
}
Reminder.prototype.init = function( UL_ID , TEXT_ID ) {
	this.list_id = UL_ID;
	this.text_id = TEXT_ID;	
}
Reminder.prototype.add = function( id ) {
	if (!this.enabled) return;
	$.ajax({ url:this.add_url.replace('@id',id), /*dataType:"json",*/ success:this.onAdd.bind(this) });
}
Reminder.prototype.remove = function( id ) {
	if (!this.enabled) return;	
	$.ajax({ url:this.remove_url.replace('@id',id), /*dataType:"json",*/ success:this.onRemove.bind(this) });
}
Reminder.prototype.refresh = function() {
	if (!this.enabled) return;	
	$.getJSON(this.refresh_url,this.onRefresh.bind(this));
}
Reminder.prototype.onRefresh = function( response ) {
	if (this.list_id == "" && this.text_id== "") return;
	output = "";
	hide_reminder_text = true;
	$("#"+this.list_id).empty();
	for (i=0; i<response.length; i++) {
		date_obj = new Date(parseInt(response[i].efrom));
		date_text = date_obj.getDate()+"."+date_obj.getMonth()+"."+date_obj.getFullYear()+" ob "+date_obj.getHours()+":"+date_obj.getMinutes();
		output += '<li><p><em><a href="javascript:reminder.remove(\''+response[i].id+'\')"><img src="close.png" width="15" height="15" alt="remove" /></a></em>('+response[i].days+')<span>'+date_text+'</span><a href="#">'+response[i].name+'</a></li>';
		hide_reminder_text = false;				
	}
	$(".message").css("min-height",response.length*100+"px");
	
	if (hide_reminder_text) $("#"+this.text_id).hide();
	else $("#"+this.text_id).show();
	$("#"+this.list_id).append(output);
}
Reminder.prototype.onAdd = function( response ) {
	//no need to do anything with the response
	this.refresh();
}
Reminder.prototype.onRemove = function( response ) {
	//no need to do anything with the response	
	this.refresh();
}

function Updater(instance_name) {
	this.data_url = "/script/liveplresult";
	this.running = false;
	this.timeout = 0;
	this.instance_name = instance_name;
}
Updater.prototype.start = function() {
	this.running = true;
	this.timeout = setTimeout( this.instance_name+'.refresh()', 1000 );
}
Updater.prototype.stop = function() {
	clearTimeout(this.timeout);
	this.running = false;
}
Updater.prototype.refresh = function() {
	if (this.running) {
		$.ajax({ url:this.data_url, success:function(response) {
			update = $('<ul></ul>');
			display_all = true;
			current_match = "";
			if ( $('meta[name="event_name"]') != null && $('meta[name="event_name"]').attr("value") != null) {
				current_match = $('meta[name="event_name"]').attr("value").toUpperCase();	
			}
			
			current_match_elt = null;
			
			if (  $('meta[name="type"]').attr("value") == "live" ) {
				for (i=0; i<response.matches.length;i++) {
					team1 = response.matches[i].home.toUpperCase();
					team2 = response.matches[i].guest.toUpperCase();
					if ( current_match.indexOf(team1) >= 0 && current_match.indexOf(team2) >= 0 ) {
						display_all = false;
						current_match_elt = null;
						if (response.matches[i].homegoals > response.matches[i].guestgoals) {
							current_match_elt = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em><strong>'+response.matches[i].homegoals+'</strong> : '+response.matches[i].guestgoals+'</em></a></li>');	
						} else if (response.matches[i].homegoals > response.matches[i].guestgoals) {
							current_match_elt = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em>'+response.matches[i].homegoals+' : <strong>'+response.matches[i].guestgoals+'</strong></em></a></li>');	
						} else {
							current_match_elt = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em>'+response.matches[i].homegoals+' : '+response.matches[i].guestgoals+'</em></a></li>');	
						}					
					}				
				}
			}
			$(update).append('<li class="first"><h6>'+response.round+'. krog</h6></li>');
			if (display_all ==  true) {
				for (i=0; i<response.matches.length;i++) {				
						match = null;
						if (response.matches[i].homegoals > response.matches[i].guestgoals) {
							match = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em><strong>'+response.matches[i].homegoals+'</strong> : '+response.matches[i].guestgoals+'</em></a></li>');	
						} else if (response.matches[i].homegoals > response.matches[i].guestgoals) {
							match = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em>'+response.matches[i].homegoals+' : <strong>'+response.matches[i].guestgoals+'</strong></em></a></li>');	
						} else {
							match = $('<li><a href="javascript:updater.refresh();">'+response.matches[i].home+' - '+response.matches[i].guest+'<em>'+response.matches[i].homegoals+' : '+response.matches[i].guestgoals+'</em></a></li>');	
						}					
						$(update).append(match);
				}			
			} else {
				$(update).append(current_match_elt);
			}

			$(update).append('<li class="last"></li>')			
			$(".liga").html(update);
		} });
		this.timeout = setTimeout( this.instance_name+'.refresh()', 60000 );
	}
}



// Reminder instance
var reminder = new Reminder();
var updater = new Updater("updater");
updater.start();
