/** **************************************************************
*	Web part of Raspored.rs application
*	Copyright 2011 Ras TimeTable Software. All Rights Reserved. 
******************************************************************/

$(document).ready(function()
{
	if( typeof (g_DB.noResize) === 'undefined' ){
		g_DB.noResize = false;
	}
	if( typeof (g_DB.partial) === 'undefined' ){
		g_DB.partial = true;
	}
	if( typeof (g_root) === 'undefined' ){
		g_root = "";
	}
	
	var $radios = $('input:radio[name=izbor]');
	$radios.filter('[value=nast]').attr('checked', true);
	
	g_blok = g_DB.oblast.indReset-g_DB.oblast.indStart+1;

	g_fontSize = 75;
	setFontSize();
	
	fillRasMatrix();
	
	if(g_DB.partial){
		fillRasNast();
		fillRasOdel();
	}

	// dinamicka promena velicine fontova pri resize
	if(!g_DB.noResize){
	    $('.mainTable th.headCelPer').css('font-size', g_fontSize + '%');
	    $('.mainTable th.headCelPerEven').css('font-size', g_fontSize + '%');
		
		$('.mainTable td').not('.rasCelOdel').css('font-size', g_fontSize + '%');
		$('.mainTable td.rasCelNast').css('font-size', g_fontSize*1.15 + '%');
		
		$(window).resize(function () {
			waitForFinalEvent(function(){
				doResize();
			}, 500, "some unique string");
		});
	}

	$( "#printBre" ).click(	function(){
		printRas("#for_print");
		return( false );
	});
	
    // hover
	//1.
	$('#for_print').live('mouseover',
	function(){
		$('#right_menu,#left_menu').css('background-color','#f0fada');
	}).live('mouseout',
	function(){
		$('#right_menu,#left_menu').css('background-color','#b9d5ba');
	});
	//2.
    $('.mainTable tr').live('mouseover',
    function(){
		$(this).children('.rasCelNast,.rasCelOdel').addClass('datahighlight');
    }).live('mouseout',
    function(){
        $(this).children('.rasCelNast,.rasCelOdel').removeClass('datahighlight');
    });
	//3.
    $('.toolbar').live('mouseover',
    function(){
		$(this).addClass('datahighlight').css('cursor','pointer');
    }).live('mouseout',
    function(){
        $(this).removeClass('datahighlight');
    });
	//4.
    $('.mainTable td.rasCelNast').live('mouseover',  
	function(){
        var rowIndex = parseInt(  ($(this).parent().attr('id')).substr(2)  );
        $('#loadme').html(getTooltip(rowIndex, "nast"));
    });
	//5.
    $('.mainTable td.rasCelOdel').live('mouseover',  
	function(){
        var rowIndex = parseInt(  ($(this).parent().attr('id')).substr(2)  );
        $('#loadme').html(getTooltip(rowIndex, "odel"));
    });
	
	if ($.browser.msie) 
		$('input[name="izbor"]').click(changeView);
	else
		$('input[name="izbor"]').change(changeView);
		
	function changeView(){
		$(document).trigger('hideCluetip');
		if ($('input[name="izbor"]:checked').val() == 'nast'){
			$('#raspored_odel').hide();//fadeOut();
			$('#raspored_nast').show();//fadeIn();
		}
		else if ($('input[name="izbor"]:checked').val() == 'odel'){
			$('#raspored_nast').hide();//fadeOut();
			$('#raspored_odel').show();//fadeIn();
		}		
	}
	
	var ctWidth = 60 * (g_DB.oblast.brCas+1);
	if(ctWidth > 700)
		ctWidth = 700;
	if ($.browser.msie && parseInt($.browser.version, 10) < 9){
		var ctClass = "default";
		var ctAct = "click";
	}
	else{
		var ctClass = "rounded";
		var ctAct = "hover";
	}
		
	$('.rasCelNast').cluetip({
		local:true, 
		arrows:false, 
		cluetipClass:ctClass, 
		width:ctWidth, 
		positionBy:"auto",
		leftOffset: 15,
		topOffset: 15,
		showTitle:false,
		mouseOutClose:false,
		sticky:true,
		closeText:'<img src="' + g_root + 'scripts/img/close.gif" alt="close" />'
		,activation:ctAct
	});
	
	var wO = 80 * (g_DB.oblast.brCas+1);
	if(wO > 800)
		wO = 800;
	$('.rasCelOdel').cluetip({
		local:true, 
		arrows:false, 
		cluetipClass:ctClass, 
		width:wO, 
		positionBy:"auto",
		leftOffset: 50,
		topOffset: 15,
		showTitle:false,
		mouseOutClose:false,
		sticky:true,
		closeText:'<img src="' + g_root + 'scripts/img/close.gif" alt="close" />'
		,activation:ctAct
	});
	
});

function fillRasMatrix()
{
	g_Ras = new Object;
	
	g_brNast = 0;
	g_Ras.nast = new Array();
	for (var r = 0; r < g_DB.nast.length; r++)
	{
		if(g_DB.nast[r].no)
			continue;

		g_DB.nast[r].no = g_brNast;
		g_Ras.nast.length = g_brNast+1;
		g_Ras.nast[g_brNast] = new Array( g_DB.oblast.brDan );
		for (var d = 0; d < g_DB.oblast.brDan; d++){
			g_Ras.nast[g_brNast][d] = new Array( g_DB.oblast.brCas );
			for (var c = 0; c < g_DB.oblast.brCas; c++){
				g_Ras.nast[g_brNast][d][c] = new Object;
			}
		}
		g_brNast++;
	}
	
	g_brOdel = 0;
	g_Ras.odel = new Array();
	for (var r = 0; r < g_DB.odel.length; r++)
	{
		if(g_DB.odel[r].no)
			continue;
			
		g_DB.odel[r].no = g_brOdel;
		g_Ras.odel.length = g_brOdel+1;
		g_Ras.odel[g_brOdel] = new Array( g_DB.oblast.brDan );
		for (var d = 0; d < g_DB.oblast.brDan; d++){
			g_Ras.odel[g_brOdel][d] = new Array( g_DB.oblast.brCas );
			for (var c = 0; c < g_DB.oblast.brCas; c++){
				g_Ras.odel[g_brOdel][d][c] = new Object;
			}
		}
		g_brOdel++;
	}
	
    // ubacivanje kartica
    for (var n = 0; n < g_DB.cvor.length; n++){
        var oCvor = g_DB.cvor[n];
		prepareCard(oCvor);
        for(var i=0; i < oCvor.n.length; i++){
        	var redN = g_DB.nast[oCvor.n[i]].no;
			if(redN < 0)
				continue;
			g_Ras.nast[redN][oCvor.d][oCvor.c].cvorInd = n;
			g_Ras.nast[redN][oCvor.d][oCvor.c].entiInd = i;
		}
        for(var i=0; i < oCvor.o.length; i++){
        	var redO = g_DB.odel[oCvor.o[i]].no;
			if(redO < 0)
				continue;
			g_Ras.odel[redO][oCvor.d][oCvor.c].cvorInd = n;
			g_Ras.odel[redO][oCvor.d][oCvor.c].entiInd = i;
		}
	}
}

function prepareCard(oCvor)
{
	var brNast = oCvor.n.length;
	var brOdel = oCvor.o.length;
	oCvor.brOdel = new Array(brNast);
	oCvor.firstOdel = new Array(brNast);
	oCvor.lastOdel = new Array(brNast);
	
	oCvor.brPred = new Array(brOdel);
	oCvor.lastPred = new Array(brOdel);
	
	if(brNast === 1  && brOdel == 1){
		oCvor.brOdel[0] = 1;
		oCvor.firstOdel[0] = 0;
		oCvor.lastOdel[0] = 0;
		oCvor.brPred[0] = 1;
		oCvor.lastPred[0] = 0;
		return;
	}

	// koliko ima odeljenja i koje je prvo (tj. grupa je != -1)
	for(var i=0; i < brNast; i++){
		if(brOdel > 1){// ako nije, mozda ne postoje grupe
			var efBrOdel = 0;
			oCvor.firstOdel[i] = -1;
			for(var j=0; j < brOdel; j++){
				if(oCvor.g[i][j] >= 0){
					efBrOdel++;
					if(oCvor.firstOdel[i] < 0)
						oCvor.firstOdel[i] = j;
					oCvor.lastOdel[i] = j;
				}
			}
			oCvor.brOdel[i] = efBrOdel;
		}
		else{
			oCvor.brOdel[i] = 1;
			oCvor.firstOdel[i] = 0;
			oCvor.lastOdel[i] = 0;
		}		
	}
	// sad nastavnici (tj. predmeti)
	for(var i=0; i < brOdel; i++){
		if(brNast > 1){
			oCvor.brPred[i] = 0;
			for(var j=0; j < brNast; j++){
				if(oCvor.g[j][i] >= 0){
					oCvor.brPred[i]++;
					oCvor.lastPred[i] = j;
				}
			}
		}
		else{
			oCvor.brPred[i] = 1;
			oCvor.lastPred[i] = 0;
		}		
	}
}

function fillRasNast()
{
    for (var n = 0; n < g_DB.nast.length; n++)
	{
		var red = g_DB.nast[n].no;
		if(red < 0)
			continue;
	
		var trID = "#n_" + n.toString();
		var html = "";
        for (var d = 0; d < g_DB.oblast.brDan; d++){
            html += '<th class="razmak"></th>';
            for (var c = 0; c < g_DB.oblast.brCas; c++){
				var oRasCell = g_Ras.nast[red][d][c];
				if(typeof (oRasCell.cvorInd) != 'undefined'){//ako nije kreirano, onda je ova pozicija prazna
					html += getNastCardHTML(oRasCell.cvorInd, oRasCell.entiInd);
					if(typeof (g_DB.cvor[oRasCell.cvorInd].t) != 'undefined')// ako ne postoji, onda je duzina 0, pa nema colspan
						c += g_DB.cvor[oRasCell.cvorInd].t - 1;
				}
				else{
					var celClass = '';
					if(g_blok >= g_DB.oblast.brCas){
						if(d%2 === 0)
							celClass = ' class="ev"';
					}
					else if(g_blok <= 0){
						if(c%2 === 0)
							celClass = ' class="ev"';
					}
					else{
						if( Math.floor(c/g_blok) % 2 === 0)
							celClass = ' class="ev"';
					}						
					html += '<td' + celClass + '>';
					html += '</td>';
				}
            }
        }
        $(trID).append(html);
    }
}

function fillRasOdel()
{
	if(g_brOdel == 0)
		return;
	var html = "";
    /////////////////////////////////////////
    // mainHeader
    ////////////////////////////////////////
	html += "<table class='mainTable'>";
	html += "<thead>";
	html += "<tr>";
    html += '<th class="headCelDay" style="width:20px;font-size:90%;" rowspan="2">' + g_DB.opsti.rbrLoc + '</th>';
    html += '<th class="headCelDay" style="width:5em;font-size:90%;" rowspan="2">' + g_DB.opsti.odelLoc.substr(0,4)+'.' + '</th>';

    for (var i = 0; i < g_DB.oblast.brDan; i++){
        html += '<th class="razmakOdel"></th>';
        var h = 'headCelDay';
        if (i % 2 == 0)
			h = 'headCelDayEven';
        html += '<th class="' + h + '" colspan="' + g_DB.oblast.brCas + '">' + g_DB.opsti.daniLoc[i] + '</th>';
    }
    html += "</tr>";
	
	// redni brojevi casova
	html += "<tr>";
	var h = "headCelPer";
	for (var i=0; i < g_DB.oblast.brDan; i++){
		html += "<th class='razmakOdel'></th>";
		var count = 0;
		for (var j=0; j < g_DB.oblast.brCas; j++){
			if(count === 0){
				if(h === "headCelPer")
					h = "headCelPerEven";
				else
					h = "headCelPer";
			}
			html += '<th class="' + h + '">' + (g_DB.oblast.indStart + count).toString() + '</th>';
			count++;
			if(g_DB.oblast.indStart + count > g_DB.oblast.indReset)
				count = 0;
		}
	}
	html += "</tr>";
	html += "</thead>";

	////////////////////////////////////////////////
	// mainTable
	//////////////////////////////////////////////
	html += "<tbody>";
	var rbr = 0;
	for (var n = 0; n < g_DB.odel.length; n++)
	{
		// ovde ce ici provera da li je odel izabrano za export
		var red = g_DB.odel[n].no;
		if(red < 0)
			continue;
		
		rbr ++;
		html += "<tr id='o_" + n + "'>";
		html += "<td>" + rbr + ".</td>";
        html += '<td class="rasCelOdel" rel="#loadme">' + g_DB.odel[n].naziv + '</td>';
		for (var d = 0; d < g_DB.oblast.brDan; d++){
            html += '<th class="razmakOdel"></th>';
            for (var c = 0; c < g_DB.oblast.brCas; c++){
				var oRasCell = g_Ras.odel[red][d][c];
				if(typeof(oRasCell.cvorInd) != 'undefined'){//ako nije kreirano, onda je ova pozicija prazna
					html += getOdelCardHTML(oRasCell.cvorInd, oRasCell.entiInd);
					if(typeof(g_DB.cvor[oRasCell.cvorInd].t) != 'undefined')// ako ne postoji, onda je duzina 0, pa nema colspan
						c += g_DB.cvor[oRasCell.cvorInd].t - 1;
				}
				else{
					var celClass = '';
					if(g_blok >= g_DB.oblast.brCas){
						if(d%2 === 0)
							celClass = ' class="ev"';
					}
					else if(g_blok <= 0){
						if(c%2 === 0)
							celClass = ' class="ev"';
					}
					else{
						if( Math.floor(c/g_blok) % 2 === 0)
							celClass = ' class="ev"';
					}						
					html += '<td' + celClass + '>';
					html += '</td>';
				}
            }
		}
		
		html += "</tr>";
	}
	html += "</tbody>";
	html += "</table>";
	//html += "</div>";

	$('#raspored_odel').append(html);
}

function getOdelCardHTML(iCard, iOdel)
{
	var oCvor = g_DB.cvor[iCard]; 
	var retVal = '<td';
	if(typeof(oCvor.t) != 'undefined')
		retVal += ' colspan="' + oCvor.t + '"'; 
	retVal += '>';

	var iLastPred = oCvor.p[ oCvor.lastPred[iOdel] ];
	retVal += '<div class="b' + cutHex(g_DB.pred[iLastPred].boja) + '">';
	if(oCvor.brPred[iOdel] === 1){
		retVal += g_DB.pred[iLastPred].skr;
	}
	else{
		var lineHeight = 3 / oCvor.brPred[iOdel];
		for (var g = 0; g < oCvor.p.length; g++){
			if(typeof(oCvor.g) !== 'undefined'){
				if(oCvor.g[g][iOdel] < 0)
					continue;
			}
			iPred = oCvor.p[g];
			retVal += '<div style="';
			retVal += 'line-height:' + lineHeight + 'em;';
			retVal += 'height:' + lineHeight + 'em;';
			if(iPred !== iLastPred)
				retVal += 'background-color:' + g_DB.pred[iPred].boja + ';';
			retVal += '" class="cb';
			if (getBrightness(g_DB.pred[iPred].boja) <= 128)
				retVal += 'W';
			retVal += '">';
			retVal += g_DB.pred[iPred].skr;
			retVal += '</div>';
		}
	}
	retVal += '</div>';
    retVal += '</td>';
    return retVal;
}


function getNastCardHTML(iCard, iNast)
{
	var oCvor = g_DB.cvor[iCard]; 
	var retVal = '<td';
	if(typeof(oCvor.t) != 'undefined')
		retVal += ' colspan="' + oCvor.t + '"'; 
	retVal += '>';

	var iLastOdel = oCvor.o[ oCvor.lastOdel[iNast] ];
	retVal += '<div class="b' + cutHex(g_DB.odel[iLastOdel].boja) + '">';
	if(oCvor.brOdel[iNast] === 1){
		retVal += g_DB.odel[iLastOdel].naziv;
	}
	else{
		var lineHeight = 3 / oCvor.brOdel[iNast];
		for (var g = 0; g < oCvor.o.length; g++){
			if(typeof(oCvor.g) != 'undefined'){
				if(oCvor.g[iNast][g] < 0)
				continue;
			}
			iOdel = oCvor.o[g];
			retVal += '<div style="';
			retVal += 'line-height:' + lineHeight + 'em;';
			retVal += 'height:' + lineHeight + 'em;';
			if(iOdel !== iLastOdel)
				retVal += 'background-color:' + g_DB.odel[iOdel].boja + ';';
			retVal += '" class="cb';
			if (getBrightness(g_DB.odel[iOdel].boja) <= 128)
				retVal += 'W';
			retVal += '">';
			retVal += g_DB.odel[iOdel].naziv;
			retVal += '</div>';
		}
	}
	retVal += '</div>';
    retVal += '</td>';
    return retVal;
}

function getTooltip(rowIndex, what)
{
	// zaglavlje
	if(what === "nast"){
		var red = g_DB.nast[rowIndex].no;
		if(red < 0) // ovo bi trebalo da se nikad ne dogodi?!
			return;
		var matrix = g_Ras.nast;
		var getFunc = getNastToolCardHTML;
		html = '<h2>' + g_DB.nast[rowIndex].ime + '</h2>';
		var iPred = g_DB.nast[rowIndex].pred;
		if(iPred >= 0)
			html += '<h3>' + g_DB.pred[iPred].naziv + '</h3>'; 
		var iKabi = g_DB.nast[rowIndex].kabi;
		if(iKabi >= 0)
			html += '<h4>' + g_DB.opsti.kabiLoc + ": " + g_DB.kabi[iKabi] + '</h4>'; //.skr
		html += '<br>'; 
	}
	else{
		var red = g_DB.odel[rowIndex].no;
		if(red < 0) return;
		var matrix = g_Ras.odel;
		var getFunc = getOdelToolCardHTML;
		html = '<h2>' + g_DB.odel[rowIndex].naziv + '</h2>';
		var iNast = g_DB.odel[rowIndex].nast;
		if(iNast >= 0){
			html += '<h4>' + g_DB.opsti.razStarLoc + ':</h4>'; 
			html += '<h3>' + g_DB.nast[iNast].ime + '</h3>'; 
		}
		html += '<br>'; 
	}
		
	html += '<table class="toolTable">';
    html += '<thead>';
    html += '<tr>';
	html += '<th></th>';
	var count = 0;
    for (var i = 0; i < g_DB.oblast.brCas; i++){
        html += '<th>' + (g_DB.oblast.indStart + count).toString() + '</th>';
		count++;
		if(g_DB.oblast.indStart + count > g_DB.oblast.indReset)
			count = 0;
    }
    html += "</tr>";
	html += '</thead>';
	
	html += '<tbody>';
	for (d = 0; d < g_DB.oblast.brDan; d++){
		html += '<tr>';
		html += '<th>' + g_DB.opsti.daniLoc[d].substring(0,3) + '</th>';
		for (var c = 0; c < g_DB.oblast.brCas; c++){
			var oRasCell = matrix[red][d][c];
			if(typeof(oRasCell.cvorInd) != 'undefined'){
				html += getFunc(oRasCell.cvorInd, oRasCell.entiInd);
				if(typeof(g_DB.cvor[oRasCell.cvorInd].t) != 'undefined')
					c += g_DB.cvor[oRasCell.cvorInd].t - 1;
			}
			else{
				html += '<td';
				if(g_blok < g_DB.oblast.brCas && g_blok > 0){
					if(Math.floor(c/g_blok) % 2 !== 0)
						html += ' class="od"';
				}
				html += '></td>';
			}
        }
		html += "</tr>";
    }
	html += '</tbody>';
	html += '</table>';
	return html;
}

function getNastToolCardHTML(iCard, iNast)
{
	var oCvor = g_DB.cvor[iCard]; 
	var retVal = '<td';
	if(typeof(oCvor.t) != 'undefined')
		retVal += ' colspan="' + oCvor.t + '"';
	retVal += '>';
	
	var lineHeight = 36 / oCvor.brOdel[iNast];
	var iOdel = oCvor.o[ oCvor.firstOdel[iNast] ];
	var iPred = oCvor.p[iNast];
	var boja = g_DB.odel[iOdel].boja;
	var pred = "";
	if(iPred !== g_DB.nast[oCvor.n[iNast]].pred){
		pred = '<span class="pki">' + g_DB.pred[iPred].skr + '</span>';
	}

	// pred
    var f = '<div class="toolCard" style="background-color:' + boja + ';">';
    f += '<div class="pk"';
    if (getBrightness(boja) <= 128)
    	f += ' style="color:white;"';   
    f += '>';
    f += pred;
    f += '</div>';
    
	// odeljenja
    for (var g = 0; g < oCvor.o.length; g++){
		if(typeof(oCvor.g) != 'undefined'){
			if(oCvor.g[iNast][g] < 0)
			continue;
		}
		iOdel = oCvor.o[g];
		f += '<div class="toolCard_block" style="';
		f += 'background-color:' + g_DB.odel[iOdel].boja + ';';
		f += 'line-height:' + lineHeight + 'px;';
		f += 'height:' + lineHeight + 'px;';
		if(oCvor.brOdel[iNast] > 1)
			f += 'margin:0 1px;';
		if (getBrightness(g_DB.odel[iOdel].boja) <= 128)
			f += 'color:white;';

		f += '">';
		f += g_DB.odel[iOdel].naziv;
		if(typeof(oCvor.g) != 'undefined' && oCvor.g.length > 0){
			var iG = oCvor.g[iNast][g];
			f += '<span class="pk">-' +g_DB.odel[iOdel].grup[iG] + '</span>';
		}
		f += '</div>';
    }
    
    // kabinet
    var iKabi = oCvor.k[iNast];
    var kabi = "";
	if(iKabi >= 0 && iKabi !== g_DB.nast[oCvor.n[iNast]].kabi){
		kabi = '<span class="pki">' + g_DB.kabi[iKabi] + '</span>';
	}
    f += '<div class="pk"';
    if (getBrightness(boja) <= 128)
    	f += ' style="color:white;"';   
    f += '>';
    f += kabi;
    f += '</div>';
    
    f += '</div>';
    retVal += f; 
    retVal += '</td>';
    return retVal;
}
function getOdelToolCardHTML(iCard, iOdel)
{
	var oCvor = g_DB.cvor[iCard]; 
	var retVal = '<td  class="toolCellOdel"';
	if(typeof(oCvor.t) != 'undefined')
		retVal += 'colspan="' + oCvor.t + '"';
	retVal += '>';
	
	var cellHeight = 6 / oCvor.brPred[iOdel];
	for (var red = 0; red < oCvor.n.length; red++){
		if(typeof(oCvor.g) != 'undefined'){
			if(oCvor.g[red][iOdel] < 0)
			continue;
		}
		var iPred = oCvor.p[red];
		retVal += '<div style="overflow:hidden;border:1px solid #586;height:' + cellHeight + 'em;background-color:' + g_DB.pred[iPred].boja + ';';
		if (getBrightness(g_DB.pred[iPred].boja) <= 128)
			retVal += 'color:white;';
		retVal += '">';

		var iKabi = oCvor.k[red];
		if(iKabi >= 0){
			retVal += '<div class="kab"><span class="kab">' +
				g_DB.kabi[iKabi] + 
				'</span></div>';
		}
		retVal += '<table class="cellTable">';
		retVal += '<tr><td style="height:' + (cellHeight-(iKabi>=0?1:0)).toString() + 'em;">';
		if(typeof(oCvor.g) != 'undefined'){
			var iG = oCvor.g[red][iOdel];
			retVal += '<span class="pk">' +g_DB.odel[oCvor.o[iOdel]].grup[iG] + ': ';
		}
		retVal += g_DB.pred[iPred].naziv +'</span>';
		retVal += '</td></tr></table></div>';
	}

    retVal += '</td>';
    return retVal;
}

function doResize()
{
	// da li se promenila velicina fonta
	if(setFontSize())
	{
		$('.headCelPer').css('font-size', g_fontSize + '%');
		$('.headCelPerEven').css('font-size', g_fontSize + '%');
		$('.mainTable td').not('.rasCelOdel').css('font-size', g_fontSize + '%');
		$('.mainTable td.rasCelNast').css('font-size', g_fontSize*1.15 + '%');
	}
}
function setFontSize()
{
	if(g_DB.noResize)
		return false;
		
	var lim = [75,90];
	if ($.browser.msie && parseInt($.browser.version, 10) < 8) 
		var lim = [50,80];
    var arrPageSizes = ___getPageSize();
    var pageWidth = arrPageSizes[0];
	var periodWidth = (pageWidth - 200) / (g_DB.oblast.brDan * g_DB.oblast.brCas);
    
	// globalna prom
	var oldFontSize = g_fontSize;
    g_fontSize = Math.round((periodWidth * 100 / 30) * 10) / 10;
    if (g_fontSize > lim[1])
		g_fontSize = lim[1];
    if (g_fontSize < lim[0])
		g_fontSize = lim[0];
	return oldFontSize !== g_fontSize ? true : false;

}

function getBrightness(hex){
    var rgb = getRGB(hex);
    var b = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
    return b;
}

function getRGB(hex){
    var h = cutHex(hex);
    arrayRGB = new Array(parseInt(h.substring(0, 2), 16), parseInt(h.substring(2, 4), 16), parseInt(h.substring(4, 6), 16));
    return arrayRGB;
}

function cutHex(h){
    if (h.charAt(0) == "#")
		h = h.substring(1, 7);
    if (h.length != 6)
		h = h.charAt(0) + h.charAt(0) + h.charAt(1) + h.charAt(1) + h.charAt(2) + h.charAt(2);
    return h;
}

/**
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
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;
};

/** 
* resava visestruki poziv resizeWindow...
*/
var waitForFinalEvent = (function () {
	var timers = {};
	return function (callback, ms, uniqueId) {
		if (!uniqueId) {
			uniqueId = "Don't call this twice without a uniqueId";
		}
		if (timers[uniqueId]) {
			clearTimeout (timers[uniqueId]);
		}
		timers[uniqueId] = setTimeout(callback, ms);
	};
})();

/** **************************************
* Printing
******************************************/

function printRas(el)
{
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	var $this = $( el );
	if ($this.size() > 1){
		$this.eq( 0 ).print();
		return;
	} else if (!$this.size()){
		return;
	}

	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
	
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( '<link rel="stylesheet" href="' + g_root + 'scripts/print.css" media="print" type="text/css" title="" />');
	objDoc.write( "</head>" );
	objDoc.write( "<body>" );
	objDoc.write($this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
	
	g_DB.oblast.brDan * g_DB.oblast.brCas;
	var fontSize = 85 - (g_DB.oblast.brCas/6 - 1)*25;
	//var $body = jFrame.contents().find(el);
    jFrame.contents().find('.mainTable th.headCelPer').css('font-size', fontSize + '%');
    jFrame.contents().find('.mainTable th.headCelPerEven').css('font-size', fontSize + '%');
	jFrame.contents().find('.mainTable td').css('font-size', fontSize + '%');
	//jFrame.contents().find('.mainTable td.rasCelNast').css('font-size', fontSize + '%');

	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}

