// JavaScript Document

function hide_apla() {
	try{
		Effect.Fade('dok_apla');
		}
	catch (exc) {
		$('dok_apla').hide();
	}
}

function show_apla (dok_id) {
	dok_akt=dok_id;
	
	$('dok_apla_white').update("");
	//$('dok_details').update("");
	try{
		Effect.Appear('dok_apla',{afterFinish:init});
	}
	catch (exc) {
		$('dok_apla').show();
	}
	//alert(dok_id);
}
function init() {
	if(typeof data=='undefined') doRequest();
	else {
		showMain();
		ScrollLoad ("scrollholder", "scroll", false);
	}
	
}


/* wysyla asynchroniczne zadanie, aby pobrac nowe wiadomosci, i inne takie */
var ar;
var interacting = false;
var dok_akt=1;
var data;
function doRequest()
{
	
	if(interacting) return;
	interacting = true;

	var params = {};
	params.action = 'get_all';
	
	//$("debug").innerHTML = params.action;
	//$("debug").innerHTML += ' '+params.mode;
	// wywoluje dzialania po stronie serwera
	
	ar = new Ajax.Request('dok_data.php', {
		method: 'post',
		parameters: params,
		onFailure: function(tr) {
			//alert('Error code: ' + tr.status + '\n');
			//alert('yo2');
			interacting = false;
			},
		onSuccess: function(tr) {
			//alert('yo');
			interacting = false;
			var resp = '';
			
			try {
				//$("debug").innerHTML = tr.responseText;
				resp = tr.responseText.evalJSON();
				data = resp;
				showMain();
				ScrollLoad ("scrollholder", "scroll", false);
			}
			catch(err) {
				$("debug").innerHTML += ' err '+err.description;
				interacting = false;
				//$("debug").innerHTML = tr.responseText;
				//alert('blad w polaczeniu');
			}
			//if (tr.responseText) $("debug").innerHTML = (tr.responseText);
		}
	});
}
function change_dok(dok_id) {
	dok_akt=dok_id;
	showMain();
	ScrollLoad ("scrollholder", "scroll", false);
}
function show_details_photo(file_photo,dok_title) {
	$('dok_details').update("");
	$('dok_details_title').update("");
	ret='<img src="images/dok/photos/thumbs/'+file_photo+'" border="0" />';
	$('dok_details').update(ret);
	dok_title = dok_title.split('&quot;').join('"');
	if (dok_title!="null") $('dok_details_title').update(dok_title);
}
function show_default_details() {
	$('dok_details').update("");
	$('dok_details_title').update("");
	ret='<img src="images/fabryka_duze_kolo.jpg" border="0" />';
	$('dok_details').update(ret);
}
function show_details_film(file_film,dok_title) {
	$('dok_details').update("");
	$('dok_details_title').update("");

	var so = new SWFObject('flash/player2.swf?filename='+file_film, 'idx', 380, 316, '8', '#FFFFFF');
	so.addParam('salign','tl');
	so.write('dok_details');
	dok_title = dok_title.split('&quot;').join('"');
	if (dok_title!="null") $('dok_details_title').update(dok_title);
}

function show_details_radio(file_radio,dok_title) {
	$('dok_details').update("");
	$('dok_details_title').update("");
	//alert('flash/mp3_player2.swf?mp3_file='+file_radio);
	var so = new SWFObject('flash/mp3_player2.swf?mp3_file='+file_radio, 'idx', 380, 316, '8', '#FFFFFF');
	so.addParam('salign','tl');
	so.write('dok_details');
	dok_title = dok_title.split('&quot;').join('"');
	if (dok_title!="null") $('dok_details_title').update(dok_title);
	
}


function showMain() {
	//alert(data[0].dokonania.dok_nazwa);
	//alert(dok_akt+'l'+data.length);
	var num_next=-1;
	var num_prev=-1;	
	var data_akt;

	for (var i=0;i<data.length;i++) {
		if (data[i].dokonania.dok_id==dok_akt) {
			data_akt=data[i];
			
			if (typeof data[i+1]!='undefined') num_next=data[i+1].dokonania.dok_id;
			if (i>0) num_prev=data[i-1].dokonania.dok_id;
		}
	}
	
	
	ret='<table cellspacing="0" cellpadding="0" border="0" width="990" style="margin:0px;" >';
    ret+='<tr><td colspan="5"><a onclick="hide_apla();" style="cursor:pointer;"><img src="images/dok_close.png" style="margin:27px 0px 5px 916px;" border="0" /></a></td></tr>';
    
	
	ret+='<tr>'+
            '<td width="73" align="left" valign="top"><img src="images/none1x1.gif" width="73" height="451" border="1" /></td>'+
            '<td width="401" align="left" valign="top"><img src="images/none1x1.gif" width="401" height="1" border="0" /><br />'+
                '<table cellspacing="0" cellpadding="0" border="0">'+
				'<tr>';
				
				content_height='380';
				if(typeof data_akt.kreacje=='undefined') {
					var puste=1;
					ret+='<td style="padding-top:33px;">';
					ret+='<img src="images/none1x1.gif" border="0" width="89" height="1" style="margin-bottom:5px;" />';
					//ret+='<img src="images/none1x1.gif" border="0" width="89" height="61" style="margin-bottom:5px;" /><br /><img src="images/none1x1.gif" border="0" width="89" height="61" style="margin-bottom:5px;" /><br /><img src="images/none1x1.gif" border="0" width="89" height="61" style="margin-bottom:5px;" /><br />';
					ret+='</td>';
					
				}
				else {
					var puste=0;
					var kreacja=0;
					for (var j=1;j<=4;j++) {

						if(typeof data_akt.kreacje[j]!='undefined') {
							ret+='<td align="left" valign="top">';
							for (var m=0;m<=2;m++) {
								
								//alert(data_akt.kreacje[j][m].file_thumb);
								if (typeof data_akt.kreacje[j][m]!="undefined") {
									if (typeof data_akt.kreacje[j][m].file_nazwa!="undefined" && data_akt.kreacje[j][m].file_nazwa!='null') napis=data_akt.kreacje[j][m].file_nazwa;
									else napis="";
									
									if (m==0) {
										if (j==1) ret+='<img src="images/dok_label_print.png" border="0" /><br /><img src="images/none1x1.gif" border="0" width="1" height="11" /><br />';
										if (j==2) ret+='<img src="images/dok_label_film.png" border="0" /><br /><img src="images/none1x1.gif" border="0" width="1" height="11" /><br />';
										if (j==3) ret+='<img src="images/dok_label_radio.png" border="0" /><br /><img src="images/none1x1.gif" border="0" width="1" height="11" /><br />';
										if (j==4) ret+='<img src="images/dok_label_inne.png" border="0" /><br /><img src="images/none1x1.gif" border="0" width="1" height="11" /><br />';
									}
									
									if (kreacja==0) {
										kreacja=1;
										var first_photo=data_akt.kreacje[j][m].file_photo;
										var first_plik=data_akt.kreacje[j][m].file_plik;
										var first_napis=napis;
										var first_index=j;
									}
									if(napis==null) napis='';
									if ((j==1 || j==4) && (typeof data_akt.kreacje[j][m].file_photo!="undefined")) ret+='<img onclick="show_details_photo(\''+data_akt.kreacje[j][m].file_photo+'\',\''+htmlentities(napis)+'\');" src="images/dok/minis/rounded/'+data_akt.kreacje[j][m].file_thumb+'" border="0" style="margin-bottom:5px; cursor: pointer;" /><br />';
									if (j==3 && (typeof data_akt.kreacje[j][m].file_plik!="undefined")) ret+='<img onclick="show_details_radio(\''+data_akt.kreacje[j][m].file_plik+'\',\''+htmlentities(napis)+'\');" src="images/dok_ico_radio.png" border="0" style="margin-bottom:5px; cursor: pointer;" /><br />';
									if (j==2 && (typeof data_akt.kreacje[j][m].file_photo!="undefined")) {
										ret+='<img onclick="show_details_film(\''+data_akt.kreacje[j][m].file_plik+'\',\''+htmlentities(napis)+'\');" src="images/dok/minis/rounded/'+data_akt.kreacje[j][m].file_thumb+'" border="0" style="margin-bottom:5px; cursor: pointer;" /><br />';
									}
									
									if (content_height>(380-((m+1)*66))) content_height=380-((m+1)*66);
									
									}
								else {
									//ret+='<img src="images/none1x1.gif" border="0" width="89" height="61" style="margin-bottom:5px;" /><br />';
									}
								
								}
								
							ret+='</td>';
					        ret+='<td align="left" width="5" valign="top"><img src="images/none1x1.gif" width="5" height="1" border="0" /></td>';
							}
						
						//if (j<4) ret+= '<td align="left" width="5" valign="top"><img src="images/none1x1.gif" width="5" height="1" border="0" /></td>';
						}
					}
				
                ret+='</tr>';
				
                ret+='</table>';
                ret+='<div id="operaBug">';
				ret+='<div id="scrollholder" class="scrollholder" style="height:'+content_height+'px;">';
				ret+='<div id="scroll" class="scroll">';
				ret+='<div id="dok_content_title">';
				ret+=data_akt.dokonania.dok_nazwa;
			   	ret+='</div>';
				ret+='<div id="dok_content">';
                ret+=data_akt.dokonania.dok_opis_pl;
				ret+='</div>';
				ret+='</div>';
				ret+='</div>';
				ret+='</div>';

			ret+= '</td>';
            ret+= '<td width="52" align="left" valign="top"><img src="images/none1x1.gif" width="52" height="1" border="0" /></td>';
			ret+= '<td width="417" align="left" valign="top" style="padding-top:50px;" ><img src="images/none1x1.gif" width="417" height="1" border="0" /><br />';
			ret+='<div id="dok_details"></div>';
			ret+='<div id="dok_details_title"></div>';

			ret+= '</td>';
            ret+= '<td width="47" align="left" valign="top"><img src="images/none1x1.gif" width="47" height="1" border="0" /></td>';
			
        ret+= '</tr>';
		
		ret+='<tr>';
		ret+='<td colspan="2" align="left" valign="top">';
		if (num_prev>=0) ret+='<img src="images/dok_link_prev.png" onclick="change_dok('+num_prev+');" style="cursor:pointer; margin:20px 0px 0px 55px;" border="0" />';
		else ret+='<img src="images/none1x1.gif" border="0" width="10" height="10" />';
		ret+='</td>';
		ret+='<td colspan="3" align="right" valign="top">';
		if (num_next>=0) ret+='<img src="images/dok_link_next.png" onclick="change_dok('+num_next+');" style="cursor:pointer; margin:20px 55px 0px 0px;" border="0" />';
		else ret+='<img src="images/none1x1.gif" border="0" width="10" height="10" />';
		ret+='</td>';
		ret+='</tr>';
		
        ret+= '</table>';
	//alert(ret);
	
	$('dok_apla_white').update(ret);
	
	if ((first_index==1 || first_index==4) && (typeof first_photo!="undefined")) show_details_photo(first_photo,first_napis);
	if (first_index==3 && (typeof first_plik!="undefined")) show_details_radio(first_plik,first_napis);
	if (first_index==2 && (typeof first_plik!="undefined")) show_details_film(first_plik,first_napis);										

	if (puste==1) show_default_details();
	
}


function htmlentities (string, quote_style) {
    // Convert all applicable characters to HTML entities  
    // 
    // version: 812.3017
    // discuss at: http://phpjs.org/functions/htmlentities
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: nobbler
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: get_html_translation_table
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (histogram = get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

function get_html_translation_table(table, quote_style) {
    // Returns the internal translation table used by htmlspecialchars and htmlentities  
    // 
    // version: 902.2516
    // discuss at: http://phpjs.org/functions/get_html_translation_table
    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js. Meaning the constants are not
    // %          note: real constants, but strings instead. integers are also supported if someone
    // %          note: chooses to create the constants themselves.
    // %          note: Table from http://www.the-art-of-web.com/html/character-codes/
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    useTable      = (table ? table.toUpperCase() : 'HTML_SPECIALCHARS');
    useQuoteStyle = (quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT');
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
    
    // Map numbers to strings for compatibilty with PHP constants
    if (!isNaN(useTable)) {
        useTable = constMappingTable[useTable];
    }
    if (!isNaN(useQuoteStyle)) {
        useQuoteStyle = constMappingQuoteStyle[useQuoteStyle];
    }
    
    if (useQuoteStyle != 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }

    if (useQuoteStyle == 'ENT_QUOTES') {
        entities['39'] = '&#039;';
    }

    if (useTable == 'HTML_SPECIALCHARS') {
        // ascii decimals for better compatibility
        entities['38'] = '&amp;';
        entities['60'] = '&lt;';
        entities['62'] = '&gt;';
    } else if (useTable == 'HTML_ENTITIES') {
        // ascii decimals for better compatibility
	    entities['38']  = '&amp;';
	    entities['60']  = '&lt;';
	    entities['62']  = '&gt;';
	    entities['160'] = '&nbsp;';
	    entities['161'] = '&iexcl;';
	    entities['162'] = '&cent;';
	    entities['163'] = '&pound;';
	    entities['164'] = '&curren;';
	    entities['165'] = '&yen;';
	    entities['166'] = '&brvbar;';
	    entities['167'] = '&sect;';
	    entities['168'] = '&uml;';
	    entities['169'] = '&copy;';
	    entities['170'] = '&ordf;';
	    entities['171'] = '&laquo;';
	    entities['172'] = '&not;';
	    entities['173'] = '&shy;';
	    entities['174'] = '&reg;';
	    entities['175'] = '&macr;';
	    entities['176'] = '&deg;';
	    entities['177'] = '&plusmn;';
	    entities['178'] = '&sup2;';
	    entities['179'] = '&sup3;';
	    entities['180'] = '&acute;';
	    entities['181'] = '&micro;';
	    entities['182'] = '&para;';
	    entities['183'] = '&middot;';
	    entities['184'] = '&cedil;';
	    entities['185'] = '&sup1;';
	    entities['186'] = '&ordm;';
	    entities['187'] = '&raquo;';
	    entities['188'] = '&frac14;';
	    entities['189'] = '&frac12;';
	    entities['190'] = '&frac34;';
	    entities['191'] = '&iquest;';
	    entities['192'] = '&Agrave;';
	    entities['193'] = '&Aacute;';
	    entities['194'] = '&Acirc;';
	    entities['195'] = '&Atilde;';
	    entities['196'] = '&Auml;';
	    entities['197'] = '&Aring;';
	    entities['198'] = '&AElig;';
	    entities['199'] = '&Ccedil;';
	    entities['200'] = '&Egrave;';
	    entities['201'] = '&Eacute;';
	    entities['202'] = '&Ecirc;';
	    entities['203'] = '&Euml;';
	    entities['204'] = '&Igrave;';
	    entities['205'] = '&Iacute;';
	    entities['206'] = '&Icirc;';
	    entities['207'] = '&Iuml;';
	    entities['208'] = '&ETH;';
	    entities['209'] = '&Ntilde;';
	    entities['210'] = '&Ograve;';
	    entities['211'] = '&Oacute;';
	    entities['212'] = '&Ocirc;';
	    entities['213'] = '&Otilde;';
	    entities['214'] = '&Ouml;';
	    entities['215'] = '&times;';
	    entities['216'] = '&Oslash;';
	    entities['217'] = '&Ugrave;';
	    entities['218'] = '&Uacute;';
	    entities['219'] = '&Ucirc;';
	    entities['220'] = '&Uuml;';
	    entities['221'] = '&Yacute;';
	    entities['222'] = '&THORN;';
	    entities['223'] = '&szlig;';
	    entities['224'] = '&agrave;';
	    entities['225'] = '&aacute;';
	    entities['226'] = '&acirc;';
	    entities['227'] = '&atilde;';
	    entities['228'] = '&auml;';
	    entities['229'] = '&aring;';
	    entities['230'] = '&aelig;';
	    entities['231'] = '&ccedil;';
	    entities['232'] = '&egrave;';
	    entities['233'] = '&eacute;';
	    entities['234'] = '&ecirc;';
	    entities['235'] = '&euml;';
	    entities['236'] = '&igrave;';
	    entities['237'] = '&iacute;';
	    entities['238'] = '&icirc;';
	    entities['239'] = '&iuml;';
	    entities['240'] = '&eth;';
	    entities['241'] = '&ntilde;';
	    entities['242'] = '&ograve;';
	    entities['243'] = '&oacute;';
	    entities['244'] = '&ocirc;';
	    entities['245'] = '&otilde;';
	    entities['246'] = '&ouml;';
	    entities['247'] = '&divide;';
	    entities['248'] = '&oslash;';
	    entities['249'] = '&ugrave;';
	    entities['250'] = '&uacute;';
	    entities['251'] = '&ucirc;';
	    entities['252'] = '&uuml;';
	    entities['253'] = '&yacute;';
	    entities['254'] = '&thorn;';
	    entities['255'] = '&yuml;';
    } else {
        throw Error("Table: "+useTable+' not supported');
        return false;
    }
    
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        histogram[symbol] = entities[decimal];
    }
    
    return histogram;
}