function register(chk) {
	htmlModal(_php_self + '?module=102&mode=formRegistration&chk=' + chk);
}

function password(chk) {
	htmlModal(_php_self + '?module=102&mode=formPassword&chk=' + chk);
}

function profile(id,chk) {
	htmlModal(_php_self + '?module=106&mode=profileForm&id=' + id + '&chk=' + chk);
}

function access(id,chk) {
	htmlModal(_php_self + '?module=106&mode=accessForm&id=' + id + '&chk=' + chk);
}

function afterAccessSave(url) {
	window.location= url;
}


function logout(id,chk, url) {
	htmlModal(_php_self + '?module=106&mode=logout&id=' + id + '&chk=' + chk);
	//window.location= _php_self + '?module=101';
}


function isset(variableName) {
  if (typeof window[variableName] == 'undefined') {
      return false;
  }
  return true;
}

// --- TREE VIEW --- //
function tw_toggle(tw_name, id, chk, obj) {
	if($(obj).text() == '+') {
		$(obj).text('-');
		//nastavimo cookie da je veja odprta 
		$.cookie('tw_'+tw_name+'_'+id, 'open');
	}
	else {
		$(obj).text('+');
		//pobrišemo cookie
		$.cookie('tw_'+tw_name+'_'+id, null);
	}
	if($('#'+tw_name+'_childs_'+id).size() > 0) { // ze imamo podatke
		$('#'+tw_name+'_childs_'+id).toggle();	
	}
	else { // moramo dobiti podatke
		$('#'+tw_name+'_parent_'+id).append('<div id="'+tw_name+'_childs_'+id+'"><img src="'+_template_dir+'/images/loading.gif"></div>');
		var url = _php_self + '?module=' + _module_id + '&mode=tw_childs&id=' + id + '&chk=' + chk;
		$.get(url, function(data){
				$('#'+tw_name+'_childs_'+id).html(data);
				}); 
	}	
}


// --- RECORD LIST --- //
$(function(){
  // Inicializiramo vse inpute, ki imajo datepicker (class=rl_date)
  //$('.rl_date').datepicker({changeFirstDay: false});
   $('.rl_date').mask("99.99.9999");  
   $('.rl_time').mask("00:00"); 
   //$('#container-1').tabs();
   	$(".rl_date").datepicker({
           showOn: 'both', 
           buttonImage: _template_dir + '/images/calendar.png', 
           buttonImageOnly: true,
           dateFormat: 'dd.mm.yy'
           });
   
  //$(function(){ access()  });
		
	  $('#container-1').tabs();
	  $('#tabs').tabs();
		// koda za tabe
			  
	    var tabContainers = $('div.tabs > div');
	    $('#tabs').click(function () {
	        tabContainers.hide().filter(this.hash).show();
	        $('div.tabs ul.tabNavigation a').removeClass('selected');
	        $(this).addClass('selected');
	        
	        return false;
	    }).filter(':first').click();

});


function rl_sort(name, field, order) {
  var record_list = $('#'+name+'_content');
	var page_start = ($('#rl_start', record_list).val()) > 1 ? $('#rl_start', record_list).val() : 1 ;
	var search = $('#rl_search', record_list).val();
	rl_load(name, field, order, page_start, search);
}

function rl_search(name, mode) {
	var search_link = '&'+$('#'+name+'_search').serialize();
	var record_list = $('#'+name+'_content');
	$('#rl_search', record_list).val(search_link);
	rl_pagination(name, mode, 1);
}

function rl_pagination(name, mode, page_start) {
	var record_list = $('#'+name+'_content');
	var field = $('#rl_sort_field', record_list).val();
	var order = $('#rl_sort_direction', record_list).val();
	var search = $('#rl_search', record_list).val();
	if(mode == 'ajax') {
		rl_load(name, field, order, page_start, search);
	}
	else {
		rl_location(name, field, order, page_start, search);
	}
}

function rl_refresh(name) {
	var record_list = $('#rl_'+name);	
	var field = $('#rl_sort_field', record_list).val();
	var order = $('#rl_sort_direction', record_list).val();
	var page_start = ($('#rl_start', record_list).val()) > 1 ? $('#rl_start').val() : 1 ;
	var search = $('#rl_search', record_list).val();
	rl_load(name, field, order, page_start, search);
}

function rl_location(name, field, order, page_start, search) {
	var url = _php_self+'?module='+_module_id+'&sort_field='+field+'&sort_direction='+order+'&start='+page_start+'&mode=rl_'+name+search + '&rdm=' + Math.random().toFixed(5);
	window.location = url;
}

function rl_load(name, field, order, page_start, search) {
	var url = _php_self+'?module='+_module_id+'&sort_field='+field+'&sort_direction='+order+'&start='+page_start+'&mode=rl_'+name+search + '&rdm=' + Math.random().toFixed(5);
	var rl = $('#rl_'+name);
	rl.load(url, {}, function(){
		// refresh datepicker fields
		//$('.rl_date').datepicker({changeFirstDay: false});
		$(".rl_date").datepicker({
	           showOn: 'both', 
	           buttonImage: _template_dir + '/images/calendar.png', 
	           buttonImageOnly: true,
	           dateFormat: 'dd.mm.yy'
	           });
	});
}

// --- AUTO SUGGEST --- //
var as_currentSelected = -1;
var as_cache = [];
function as_buttonClick(name) {
  $('#'+name+'_div').show();
  new_keyword = $('#'+name+'_input').val();
  if(new_keyword != "") {
      $('#'+name+'_keyword').val(new_keyword);
      as_CheckForChanges(name);
  }
  	$('#'+name+'_keyword').focus();
}

function as_selected(name, index, text) {
	$('#'+name+'_input').val(text);
	$('#'+name+'_return').val(index);
	as_hide(name);
}

function as_hide(name) {
	$('#'+name+'_suggest').html('');
	$('#'+name+'_div').hide();
}

// function handles pressed keys
function as_KeyUp(e, name) {  
  // get the character code of the pressed button and event
  if(window.event) {
      code = window.event.keyCode;     //IE
      e = window.event;
  }
  else {
      code = e.which;     //firefox
      e = e;
  }
  as_isKeyUpDownPressed = false; 
  /* Enter do submit*/
  if(code == 13) { // enter - sprožimo click na izbranem
    $('#'+name+'_suggest a').eq(as_currentSelected).click();
    as_hide(name);
  }
	else if(code == 27) { //esc - zapremo brez sprememb
    as_hide(name);
	}        
  else if(code == 40) { // dol
    var suggestions = $('#'+name+'_suggest a');
    if(as_currentSelected < suggestions.size()-1) {
      as_currentSelected++;
      suggestions.eq(as_currentSelected).addClass("as_selected")
				.prev().removeClass("as_selected");
		}
		as_cursor_to_end(name);
  }
  else if(code == 38) { // gor
    var suggestions = $('#'+name+'_suggest a');
    if(as_currentSelected > 0) {
      as_currentSelected--;
      suggestions.eq(as_currentSelected).addClass("as_selected")
				.next().removeClass("as_selected");
		}
		as_cursor_to_end(name);
	}
  else if((code == 37 ) || (code == 39)) {
		// levo, desno - da ne sproži iskanja
	}
  else {
    var lastKeyword = $('#'+name+'_keyword').val();
    setTimeout(function () {as_CheckForChanges(name, lastKeyword)}, 200);
  }     
}

function as_cursor_to_end(name) {
	  var input = $('#'+name+'_keyword').get(0);
	  var text = $('#'+name+'_keyword').val();		 
    if(input.createTextRange) { // IE
        var range = input.createTextRange(); 
        range.move("character", text.length); 
        range.select(); 
    } else if(input.selectionStart) { // firefox 
        input.setSelectionRange(text.length, text.length); 
    } 
} 

// funkcija poišče rezultate
function as_CheckForChanges(name, lastKeyword) {
  var keyword = $('#'+name+'_keyword').val();
	if(keyword != '' && keyword == lastKeyword) { // ni prazen in smo nehali tipkat
		// pogledamo, če je iskana beseda v cacheu
		if(data = as_inCache(keyword)) {
		  as_Results(data, "success");
		}
		else { // če ne gremo na strežnik po podatke
			var data = '<ajax mode="as_suggest"><select keyword="'+keyword+'" name="'+name+'"></select></ajax>';
			var url = _php_self+'?module='+_module_id+'&mode=as_suggest&q='+keyword+'&data='+data;
			$.get(url, as_Results);
		}
	}
}  

function as_Results(data, textStatus) {
	var string = (new XMLSerializer()).serializeToString(data);
	//alert(string);
	select = $('select', data);
	name = select.attr('name');
	keyword = select.attr('word');
	// še vedno išečmo to besedo
	if(keyword == $('#'+name+'_keyword').val()) {
		$('#'+name+'_suggest').html('');
		rows = $('row', select).each(function () {
			var index = $(this).attr('index');
			var text = $(this).text();
			$('#'+name+'_suggest').append('<a href="#" onclick="as_selected(\''+name+'\','+index+',\''+text+'\')">'+text+'</a>');
			//resetiramo trenutno izbranega
			as_currentSelected = -1;
		});
	}
	// dodamo v cache
	if(!as_inCache(keyword))
	  as_cache = $.merge(as_cache,[{word:keyword,data:data}]);
}

function as_inCache(keyword) {
	var return_value = false;
	$.each(as_cache, function() {
		if(this.word == keyword) {
      return_value = this.data;
		}
	});
	return return_value;
}

// --- OCENJEVANJE --- //
function rateComplete(data, textStatus) {
	if(textStatus == "success") {
	  //console.dirxml(data);
	}
	else {
		alert('Napaka');
	}
}

function std_select_move_up(prefix, select_count) {
  for(i=1;i<select_count;i++) {
      document.getElementById(prefix+i).selectedIndex = document.getElementById(prefix+(i+1)).selectedIndex;
  }
  document.getElementById(prefix+select_count).selectedIndex = 0;
  std_select_disable_next(prefix, select_count)
}

function std_select_move_down(prefix, select_count) {
  for(i=select_count;i>1;i--) {
      document.getElementById(prefix+i).selectedIndex = document.getElementById(prefix+(i-1)).selectedIndex;
  }
  document.getElementById(prefix+'1').selectedIndex = 0;
  std_select_disable_next(prefix, select_count)
}

function std_disable_same_news(prefix, select_count) {
  std_select_disable_next(prefix, select_count);
  for(i=1;i<=select_count;i++) {
      std_select_enable_all(document.getElementById(prefix+i));
      for(j=1;j<=select_count;j++) {
          if(j!=i) {
              if(document.getElementById(prefix+j).selectedIndex != 0)
                  document.getElementById(prefix+i).options[document.getElementById(prefix+j).selectedIndex].disabled = true;
          }
      }
  }
}

function std_select_enable_all(obj_select) {
  for(var i=0;i<obj_select.options.length;i++) {
      obj_select.options[i].disabled = false;
  }
}

function std_select_disable_next(prefix, select_count) {
  for(i=1;i<select_count;i++) {
      if(document.getElementById(prefix+i).selectedIndex == 0) {
          if(document.getElementById(prefix+(i+1)))
              if(document.getElementById(prefix+(i+1)).selectedIndex == 0)
                  document.getElementById(prefix+(i+1)).disabled = true;
      }
      else {
          if(document.getElementById(prefix+(i+1)))
              document.getElementById(prefix+(i+1)).disabled = false;
      }
  }
}

function showHideDiv(div_name)
{
	var obj = document.getElementById(div_name);
	if(obj.style.display=='block'){
		obj.style.display='none';
	}else{
		obj.style.display='block';
	}		
}

function showMessage(html_obj, action, msg) {
if(action == "OK") {
	$(html_obj).html('<div class="msg_ok">'+((msg != undefined) ? msg : "Operacija uspela")+'</div>').fadeIn(500);
}
else {
	$(html_obj).html('<div class="msg_error">'+((msg != undefined) ? msg : "Operacija NI uspela")+'</div>').fadeIn(500);
}
setTimeout('$("'+html_obj+'").fadeOut(1500);',2500);

}

function edit_points(id, value, chk) {
	$('#points_'+id).append('<div id="edit_points" style="padding:3px; border:1px solid #0753A3; background-color:#ffffff; position:absolute; z-index:10;">' +
					'<input type="text" id="rank_points" name="rank_points" size="5" value="'+value+'" />' +
					'<button onClick="edit_points_save('+id+','+chk+');">Shrani</button></div>');
}

function edit_points_save(id, chk) {				
	var points = $('#rank_points').val();
	$('#points_'+id).html('Shranjujem...');
	$('#points_'+id).load('public.php?module=405&mode=add_point&rank_id='+id+'&rank_points='+points+'&rank_chk='+chk);
	$('#edit_points').remove();
}

function file_select(obj, id) {
	if($('#file_select').size() > 0) {
		$('#file_select').remove();	
	}
	else {
	$(obj).after('<div id="file_select" style="padding:3px; border:1px solid #0753A3; background-color:#ffffff; position:absolute; z-index:10;">' +
					'<form action="public.php?module=405&mode=rank&event=import&event_id=' + id + '" method="post" id="file_select_form" enctype="multipart/form-data">' +
					'<input type="file" id="cvs_file" name="cvs_file" />' +
					'</form>' +
					'<button onClick="$(\'#file_select_form\').submit();">Shrani</button></div>');
	}
}




 // getPageSize()
  // Returns array with page width, height and window width, height
  // Core code from - quirksmode.com
  // Edit for Firefox by pHaez
  function getPageSize() {
  	
  	var xScroll, yScroll;
  	
  	if (window.innerHeight && window.scrollMaxY) {	
  		xScroll = window.innerWidth + window.scrollMaxX;
  		yScroll = window.innerHeight + window.scrollMaxY;
  	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  		xScroll = document.body.scrollWidth;
  		yScroll = document.body.scrollHeight;
  	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  		xScroll = document.body.offsetWidth;
  		yScroll = document.body.offsetHeight;
  	}
  	
  	var windowWidth, windowHeight;
  
  	if (self.innerHeight) {	// all except Explorer
  		if(document.documentElement.clientWidth){
  			windowWidth = document.documentElement.clientWidth; 
  		} else {
  			windowWidth = self.innerWidth;
  		}
  		windowHeight = self.innerHeight;
  	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  		windowWidth = document.documentElement.clientWidth;
  		windowHeight = document.documentElement.clientHeight;
  	} else if (document.body) { // other Explorers
  		windowWidth = document.body.clientWidth;
  		windowHeight = document.body.clientHeight;
  	}	
  	
  	// for small pages with total height less then height of the viewport
  	if(yScroll < windowHeight){
  		pageHeight = windowHeight;
  	} else { 
  		pageHeight = yScroll;
  	}
  
  	// for small pages with total width less then width of the viewport
  	if(xScroll < windowWidth){	
  		pageWidth = xScroll;		
  	} else {
  		pageWidth = windowWidth;
  	}
  
  	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  	return arrayPageSize;
  }

  //
  // getPageScroll()
  // Returns array with x,y page scroll values.
  // Core code from - quirksmode.com
  //
  function getPageScroll(){
  
  	var xScroll, yScroll;
  
  	if (self.pageYOffset) {
  		yScroll = self.pageYOffset;
  		xScroll = self.pageXOffset;
  	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
  		yScroll = document.documentElement.scrollTop;
  		xScroll = document.documentElement.scrollLeft;
  	} else if (document.body) {// all other Explorers
  		yScroll = document.body.scrollTop;
  		xScroll = document.body.scrollLeft;	
  	}
  
  	arrayPageScroll = new Array(xScroll,yScroll) 
  	return arrayPageScroll;
  }
  
  function show_groups(id) {
		$('#groups_'+id).toggle();
	}

	function send_newsletter(id, url) {
	  var group = $('select',$('#groups_'+id)).val();
	  if(group != null) {
			url = url + "&group_id=" + group;
			window.location = url;
		}
	}

// --- FILE UPLOAD --- //
function upload_add_file_input(name, max_inputs) {
	var file_inputs = $('input[name='+name+']');
	if(file_inputs.size() < max_inputs) {
		var html = '<div><input type="file" name="'+name+'[]" id="'+name+file_inputs.size()+'" />';
		html = html + '<button onclick="upload_add_file_input(\''+name+'\','+max_inputs+');" type="button" style="width: 20px;">+</button></div>';
		$('#fields_'+name).append(html);
	}
}
$(window).scroll(function()
{
  $('#message_box').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});
});

$('#close_message').click(function()
{
  //the messagebox gets scrool down with top property and gets hidden with zero opacity
  $('#message_box').animate({ top:"+=15px",opacity:0 }, "slow");
});

