/**
 * Dijaski.net scripting
 * @author Robert Ravnik
 * @version 0.3
 */

var STATIC_URL = 'http://static.dijaski.net/';

/**
 * Toggle element visibility
 * @param elementId
 * @return
 */
function toggle( elementId )
{
	el = document.getElementById( elementId );
	if( el )
	{
		if( el.style.display == 'block' )
			el.style.display = 'none';
		else
			el.style.display = 'block';
	}
}

function tcoll( elementId )
{
	toggle( elementId );
	
	var c_img = getById( elementId.replace( 'Coll', 'CI' ) );
	if( substr_count( c_img.src, 'plus' ) )		
		c_img.src = c_img.src.replace( 'plus', 'minus' );	
	else
		c_img.src = c_img.src.replace( 'minus', 'plus' );
}


function substr_count( orig_string, search )
{
	var c = 0; 
	for (var i=0; i<orig_string.length;i++ ) 
	{ 
		if ( search == orig_string.substr( i,search.length ) ) 
			c++; 
	}
	return c;
}
	
function getById(id) 
{
	if (document.getElementById)
		return document.getElementById(id);
	return document.all[id];
}

function hide(id) { getById(id).style.display="none"; }
function show(id, type) { getById(id).style.display=type; }

function resetFilter( input_field, table_id )
{
	var elm = getById( input_field );
	elm.value = '';
	setFilter( input_field, table_id, -1 );
	return false;
}

// if select_col == -1, full table search
function setFilter(term, table_id, column)
{
	//var select = getById(select_col);
	//var column = select.options[select.selectedIndex].value;

	var search_string = getById(term).value.toLowerCase();
	var table = document.getElementById(table_id);
	var ele;
	
	if(column == -1) {
		var words = search_string.split(" ");
	
		for (var r = 0; r < table.rows.length; r++){
			ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
	        var displayStyle = 'none';
	        for (var i = 0; i < words.length; i++) {
		    	if (ele.toLowerCase().indexOf(words[i])>=0)
					displayStyle = '';
			    else {
					displayStyle = 'none';
					break;
			    }
	        }
			table.rows[r].style.display = displayStyle;
		}
	}
	else {
		for (var r = 0; r < table.rows.length; r++){
			ele = table.rows[r].cells[column].innerHTML.replace(/<[^>]+>/g,"");
			if (ele.toLowerCase().indexOf(search_string)>=0 )
				table.rows[r].style.display = '';
			else table.rows[r].style.display = 'none';
		}
	}
	return false;	// do not postback
}

function vote_cb( id, vote_num )
{
	// res = 1, OK
	// res = 2, already voted	
	// res = ostalo, -> error
	
	var voteMsg = getById( 'VoteMsg' + id );
	var voteWrap = getById( 'VoteWrap' + id );
	
	if( ajax_status == 1 )
	{
		voteMsg.innerHTML = parseInt( voteMsg.innerHTML ) + vote_num;
	}
	else if( ajax_status == 2 ) { voteWrap.innerHTML = 'Za to datoteko ste &#382;e glasovali.'; }
	else { voteMsg.innerHTML = 'Napaka! :(' + ajax_status; }
}

function vote( id, vote_num )
{
	var voteUrl = '/agw?a=vote&id=' + id + '&vote=' + vote_num + '&rnd=' + Math.random();
	
	ajax_req( voteUrl, function() { vote_cb( id, vote_num ); } );
}


var ajax_status;

function ajax_req( url, callback ) 
{ 	
	var req = null; 

	// var contentDiv = document.getElementById("AjaxDebug");	
	// contentDiv.innerHTML = "Pričenjam povezavo ..."; 	 
	if ( window.XMLHttpRequest ) 
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) 
		{
			req.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject)	// IE 
	{	
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	req.onreadystatechange = function() 
	{ 
		// contentDiv.innerHTML = "Waiting for data ...";
		if(req.readyState == 4) 
		{
			if(req.status == 200) 
			{
				ajax_status = req.responseText;				
				if( callback )
				{
					callback();
				}
			}	
			else 
			{
				alert('Ajax error ::' + req.status + ' ' + req.statusText );
				//contentDiv.innerHTML="Error :: " + req.status + " " + req.statusText;
			}	
		} 
	}; 
	
	req.open("GET", url, true); 
	req.send(null); 
	
	return false;
} 

function open_popup( relative_url, width, height )
{
	var title = null;
	var options = "width=" + width + ",height=" + height + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1,copyhistory=no,top=250,left=300";
	var new_window = window.open( relative_url, title, options );
}

function get_window_width()
{
	if( typeof( window.innerWidth ) == 'number' ) { return window.innerWidth; }
	else if( document.documentElement && document.documentElement.clientWidth ) 
	{
	    return document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) 
	{
	    return document.body.clientWidth;
	}
	return 0;	
}

function get_window_height()
{
	  if( typeof( window.innerWidth ) == 'number' ) { return window.innerHeight; }
	  else if( document.documentElement && document.documentElement.clientHeight )  
	  {
	    return document.documentElement.clientHeight;
	  } 
	  else if( document.body && document.body.clientHeight ) 
	  {
	    return document.body.clientHeight;
	  }
	  return 0;
}

function resize_popup( content_id, x_width )
{
	var id = document.getElementById( content_id );
	
	var x = id.scrollWidth;
	if( x_width != "undefined" ) { x = x_width; }
	
	var y = id.scrollHeight;
		
	//alert( 'W' +  get_window_width() + ' ' + x + ' ' + document.body.clientWidth );
	//alert( 'H' + get_window_height() + ' ' + y + ' ' + document.body.clientHeight );
		
	//window.resizeBy(dX, dY);
	window.resizeTo( x + 40, y + 90 );
}
	