document.cookie = "res_width="+screen.width;
document.cookie = "res_height="+screen.height;

function winPopUp(name, url, width, height) {
var x = Math.floor((screen.availWidth-width) / 2);
var y = Math.floor((screen.availHeight-height) / 2);
var config = ", left=" + x;
config += ", top=" + y;
config += ", scrollbars=yes,resizable=yes,status=yes";

var popup=window.open(url,name,'width='+width+', height='+height+config);
} /*winPopUp*/

function winPopUpRet(name, url, width, height) {
var x = Math.floor((screen.availWidth-width) / 2);
var y = Math.floor((screen.availHeight-height) / 2);
var config = ", left=" + x;
config += ", top=" + y;
config += ", scrollbars=yes,resizable=yes,status=yes";

var popup=window.open(url,name,'width='+width+', height='+height+config);
return popup;
} /*winPopUp*/

function winPopUp2(name, url, width, height) {
winPopUp(name, url, width, height);
//    return false;
}

function parentRefresh (parent_anchor_id, element) {
var text = "<table align=\"center\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\" width=\"100%\" class=\"blockItem\">\n"
  +"<tr>\n<td valign=\"top\">"+ element.value
  +"</td>\n</tr>\n</table><br />";

opener.document.getElementById(parent_anchor_id).innerHTML = text;
} /*parentRefresh*/

function ResetParent (parent_anchor_id) {
if (reset_parent_cond) {
  opener.document.getElementById(parent_anchor_id).innerHTML = start_value;
} //if
} /*ResetParent*/

function parentSetFormInput(input, value)
{
  var formInput = opener.document.getElementById(input);
  if (formInput != null)
  {
	  formInput.value = value;
  }
  else
	  alert('[ERROR] Field not found: ' + input);
}

function parentOpenerSetFormInput(input, value)
{
  var formInput = parent.opener.document.getElementById(input);
  if (formInput != null)
  {
	  formInput.value = value;
  }
  else
	  alert('[ERROR] Field not found: ' + input);
}

function getValue(input)
{
  var formInput = window.document.getElementById(input);
  if (formInput == null)
	  alert('[ERROR] Field not found: ' + input);

  return formInput.value;
}


function chooseDocument(input, id)
{
 parentSetFormInput(input, '?document=' + id);
 window.close();
 return false;
}

function chooseDocumentID(input, id)
{
 parentSetFormInput(input, id);
 window.close();
 return false;
}

function chooseDocumentAlias(input, name)
{
 parentSetFormInput(input, '?documentName=' + name);
 window.close();
 return false;
}

function chooseDocumentAliasName(input, name)
{
 parentSetFormInput(input, name);
 window.close();
 return false;
}

function chooseFile(input, path)
{
 parentOpenerSetFormInput(input, path);
 parent.window.close();
 return false;
}


function showHideLayer (layer_id, show) {
    var styl = gls (layer_id);

    if (styl) {
        if (show) {
            styl.display = 'none';
        } //if
        else {
            styl.display = 'block';
        } //else
    } //if

    return false;

} /*showHideLayer*/


function gls (id) {
  var isIE = (document.all) ? true : false; 
  var isNS4 = (document.layers) ? true : false; 
  var isNS6 = (document.getElementById&&!document.all) ? true : false;

if (id != "") {
  var styl;
  if (isIE) {
    styl = document.all[id].style;
  } //if
  else if (isNS6) {
    styl = document.getElementById(id).style;
  } //else if
  else if (isNS4) {
    return false;
  } //else if
  return styl;
} //if
else {
  return false;
} //else
} /*gls*/


function correctTextXHTML (text) {
	return String(text).replace(/\&/g, "&amp;").replace(/</g, "&lt;");
} /*correctTextXHTML*/

function validateXHTML (form) {

    var elems = form.elements;
    var i, text='', elem;
    for (i=0;i<elems.length; i++) {
        elem = elems[i];
        if (elem.type == 'text') {
            elem.value = correctTextXHTML (elem.value);
        } //if
        else if (elem.type == 'textarea') {
/*
            var nodes = elem.childNodes;
            var val = '';
            for (var i = 0; i < nodes.length; i++)
                val += getXhtml(nodes[i]);
            elem.value = val;
*/
        } //else if
    } //for
} /*validateXHTML*/

/*
* 
* Ajax i funkcje pomocnicze
*
*/

function aSwitchForm( aDivFrom, aDivH, aForm ) {
	var elementFrom = document.getElementById(aDivFrom);
	var elementH = document.getElementById(aDivH);
	var elementForm = document.getElementById(aForm);
	
	elementFrom.style.display = 'none';
	elementH.style.display = 'block';
	
	advAJAX.submit(elementForm, {
    	onSuccess : function(obj) {writeElement(aDivFrom, obj.responseText); elementFrom.style.display = 'block'; elementH.style.display = 'none'; },
    	onError : function(obj) { alert("Error: " + obj.status); }
	});
} /*aSwitchForm*/


function writeElement(id, text) {
	var element = window.document.getElementById(id);
    if(element) {
		element.innerHTML = text;
    }
    else {
        alert(id+' ERROR');
    }
}

function aSwitchDivCommand( aDiv, aCommand) {
	advAJAX.get({
	    url: "index.php",
	    parameters: { "command" : aCommand , "ajax" : "1"},
	    onSuccess: function(obj) { writeElement(aDiv , obj.responseText); },
	    onError: function(obj) {alert("Error: " + obj.status);}
	 });
	
} /*aSwitchDiv*/


function sendAjaxFileFManager(aId, aDir) {
	var pDivFile = $j('#divFile'+aId);
	var pNewId = aId - 1;
	
	// Chowamy stary form
	$j('#divFile'+aId).hide();
	
	var newInList = '<div id="newInList'+aId + '"><img src="gallery/system/ajax-loader.gif" /></div>';
	$j('#fileListFManage').before(newInList);
	
	// Wysylamy
	//$j('#divFile'+aId).ajax
	var pFormFileId = '#formFile'+aId;

	$j.ajaxFileUpload
			(
				{
					url: 'index.php?command=files/saveAjax',
					secureuri: false,
					fileElementId: 'file' + aId,
					oldFileId: aDir,
					dataType: 'json',
					success: function (data, status) {
						if(typeof(data.error) != 'undefined') {
							if(data.error != '') {
								alert(data.error);
								$j('#newInList' + aId).html('');
							}
                            else {
								$j('#newInList' + aId).html(data.fileInfo);
							}
						}
					},
					error: function(data, status, e) {
						alert('Error: '+data.error);
					}
				}
			);

	//wyswietlamy

	//tworzymy nowy 
	var pNewDivHtml = $j('#divFile'+aId).html();
	//zamieniamy id
	var pR0 = pNewDivHtml.replace(aId, pNewId);
	var pR1 = '<div id="divFile'+ pNewId +'">'+pR0.replace(aId, pNewId)+'</div>';
	//dodajemy nowy form
	$j('#fileManageDivContent').before(pR1.replace(aId, pNewId));
	
}

function SetCookie(cookieName,cookieValue,nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(cookieName);
    if (ind==-1 || cookieName=="") return ""; 
    var ind1=theCookie.indexOf(';',ind);
    if (ind1==-1) ind1=theCookie.length; 
    return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

/* przejscie do dokumentu jezeli ok */
function confirmLocation( tinfo, url ) {
    if(confirm(tinfo)) {
        document.location=url;
    }
}

/* pomocnicze funkcje do zabawy elementami przy uzyciu prepare w finpucie do dodawania zdjec */
function pSwitchObj(idObjH, idObjS) {
	$j(idObjH).hide();
	$j(idObjS).show();
}

function finputGalleryUploadFoto(id) {
 if($j('#fileFinput_'+id).val() != '') {
	$j('#finputGalleryImgInser_'+id).hide();
	$j('#finputGallerySnake_'+id).show();

	$j.ajaxFileUpload
			(
				{
					url:'index.php?command=service/addFotoFinputGallery',
					secureuri:false,
					fileElementId: 'fileFinput_'+id,
					oldFileId: id,
					dataType: 'json',
					success: function (data, status)
					{
						if(typeof(data.error) != 'undefined')
						{
							if(data.error != '')
							{
								alert(data.error);
								$j('#finputGallerySnake_'+id).hide();
								$j('#finputGalleryImgInser_'+id).show();
								
							}
							else
							{
								$j('#field_'+id).val(data.fotoName);
								$j('#finputGallerySnake_'+id).hide();
								$j('#finputGalleryImgInser_'+id).hide();
								$j('#finputGalleryImg_'+id).show();
							}
						}
						else 
						{	
							alert('Error: %555');
							$j('#finputGallerySnake_'+id).hide();
							$j('#finputGalleryImgInser_'+id).show();
						}
					},
					error: function (data, status, e)
					{
						alert('Problem z przeslaniem pliku!!!');
						$j('#finputGallerySnake_'+id).hide();
						$j('#finputGalleryImgInser_'+id).show();
					}
				}
			);
	}
	else {
		alert('Brak pliku');
	}
}

// Podmiana sekcji
function ajaxSectionReload(aSectionId, aSectionIdent) {
  $('#sectionId_'+aSectionIdent).html('<img src="gallery/serwis/load.gif"  class="animSectionLoader"  />')
  $.get("index.php", { main_template: "main/blank", portlet: "app/showSection", sectionId: aSectionId, ajax: '2'},
	  function(data){
	  	$('#sectionId_' + aSectionIdent).html(data);
	  });
}

// Shift element
function ajaxElementMove(aSectionIdent, aElement, aDocument, aShift) {
	var pEmpty = '';
	var pSectionIdent = aSectionIdent.replace(/\//g, pEmpty);
	
	// Dajemy loader
	$('#sectionId_'+pSectionIdent).html('<img src="gallery/serwis/load.gif"  class="animSectionLoader" />');	
	
	// Wysylamy o przesuniecie
	$.get("index.php", { main_template: "main/blank", command: "service/move_element_ajax", element: aElement, document: aDocument, sectionIdent: aSectionIdent, shift: aShift ,ajax: '2' },
		 function(aData){

			var data = eval('(' + aData + ')');
			if(typeof(data.error) != 'undefined') {
				if(data.error != '') {
					alert(data.error);
				}
				else {
					//jest id dokumentu to przeladowujemy calosc
					if(data.newDocument != '') {
						document.location.href = '?document=' + data.newDocument;
					}
					else {
						//przeladowujemy tylko sekcje
						ajaxSectionReload(data.sectionId,pSectionIdent);
					}
				}
			}
			else {
				alert('error: %j564  ');
			}
	});
}

// Delete element
function ajaxElementRemove( aSectionIdent, aElement, aDocument) {
    if(confirm('Element zostanie usuniety!')) {
    var pEmpty = '';
    var pSectionIdent = aSectionIdent.replace(/\//g, pEmpty);

    // Dajemy loader
    $('#sectionId_' + pSectionIdent).html('<img src="gallery/serwis/load.gif" class="animSectionLoader" />');

    // Wysylamy o usuniecie
    $.get("index.php", {
	    main_template: "main/blank",
	    command: "service/remove_element_ajax",
	    element: aElement,
	    document: aDocument,
	    sectionIdent: aSectionIdent,
	    ajax: '2'
    }, function(aData) {
	    var data = eval('(' + aData + ')');
	    if(typeof(data.error) != 'undefined') {
		    if (data.error != '') {
			    alert(data.error);
		    }
		    else {
			    // Jest identyfikator dokumentu to przeladowujemy calosc
			    if (data.newDocument != '') {
				    document.location.href = '?document' + data.newDocument;
			    }
			    else {
				    // Przeladowujemy tylko sekcje
				    ajaxSectionReload(data.sectionId, pSectionIdent);
			    }
		    }
	    }
	    else {
		    alert('error: %j568');
	    }
    });
    }
}

function countBlockText(field_id, show_in, max_count){

	
	var textTmp = $('#'+field_id).val()
	var counterTmp = max_count - textTmp.length
	
	if(counterTmp < 0) {
		counterTmp = 0;
	}
	
	$('#'+show_in).html(' <i>pozostało:&nbsp;'+counterTmp+'&nbsp;znaków</i>');
}


function publicationEditSwitch(aId) {
	$('#siteList'+aId).toggle('400', function () {
		
		if($(this).is(':visible')) { 
			$('#aSwitch'+aId).text('ukryj startsze wersje');
		}
		else {
			$('#aSwitch'+aId).text('pokaż wszystkie wersje');
		}
	 })
}

function flvSwitchData(fName) {
    $('#selectFilmImageEntry').html('<img src="gallery/serwis/load.gif" alt="Loading..." />');
	$.ajax({
		type: "GET",
		url: "index.php?portlet=WEBVideo/xml&main_template=main/blank&ajax=4&flvName=" + fName,
		dataType: "xml",
		success: function(xml) {
			var fotoHtml = '';
		
			$(xml).find('content').each(function(){
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg0"><img src="cache/imgs/_w105/'+$(this).find('_pfile0').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg0" value="'+$(this).find('_pfile0').text()+'" /></span>';
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg1"><img src="cache/imgs/_w105/'+$(this).find('_pfile1').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg1" value="'+$(this).find('_pfile1').text()+'" /></span>';
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg2"><img src="cache/imgs/_w105/'+$(this).find('_pfile2').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg2" value="'+$(this).find('_pfile2').text()+'" /></span>';		    	
		    	
		    	flvSelectTitle = $(this).find('title').text();
		    	flvSelectDesc = $(this).find('desc').text();
		    	flvSelectTag = $(this).find('tags').text();
		    	
				if(flvSelectTitle != '') {
				  if($('#field_title').val() == '' || $('#field_title').val() == flvSelectTitle || confirm('Tytuł zostanie podmieniony na: "' + flvSelectTitle + '"')) {	
		    		$('#field_title').val(flvSelectTitle);
				  }
		    	}
				
				if(flvSelectDesc != '') {
					if($('#field_description').val() == '' || $('#field_description').val() == flvSelectDesc || confirm('Opis zostanie podmieniony na: "' + flvSelectDesc + '"')) {	
			    		$('#field_description').val(flvSelectDesc);
				 }
		    	}
	
				if(flvSelectTag != '') {
					if($('#field_tags').val() == '' || $('#field_tags').val() == flvSelectTag || confirm('Tagi zostaną podmienione na: "' + flvSelectTag + '"')) {		
						$('#field_tags').val(flvSelectTag);
					}
		    	}
			});
			
		    $('#selectFilmImageEntry').html('<div>'+fotoHtml+'</div><div class="zwijacz"> </div>');
			
		    if(flvImage != '') {
		    	if($("#fileImg0").val() == flvImage) {
		    		$("#fileImg0").attr('checked', true);
		    	}
		    	
		    	if($("#fileImg1").val() == flvImage) {
		    		$("#fileImg1").attr('checked', true);
		    	}
		    	
		    	if($("#fileImg2").val() == flvImage) {
		    		$("#fileImg2").attr('checked', true);
		    	}
	    	}

			$('#label_img').show();
		}
	});
}
	
function flvSwitchDataHtml(fName) {
    $('#selectFilmImageEntry').html('<img src="gallery/serwis/load.gif" alt="Loading..." />');
	$.ajax({
		type: "GET",
		url: "index.php?portlet=WEBVideo/xml&main_template=main/blank&ajax=4&flvName=" + fName,
		dataType: "xml",
		success: function(xml) {
			var fotoHtml = '';
		
			$(xml).find('content').each(function(){
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg0"><img src="cache/imgs/_w105/'+$(this).find('_pfile0').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg0" value="'+$(this).find('_pfile0').text()+'" onchange="video_generate_html()" onclick="video_generate_html()" /></span>';
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg1"><img src="cache/imgs/_w105/'+$(this).find('_pfile1').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg1" value="'+$(this).find('_pfile1').text()+'" onchange="video_generate_html()" onclick="video_generate_html()" /></span>';
		    	fotoHtml += '<span display="block" style="float: left; widith: 115px; margin:8px;"><label for="fileImg2"><img src="cache/imgs/_w105/'+$(this).find('_pfile2').text()+'" alt=" " /></label><br /><input type="radio" name="field_img" id="fileImg2" value="'+$(this).find('_pfile2').text()+'" onchange="video_generate_html()" onclick="video_generate_html()" /></span>';
		    	
		    	flvSelectTitle = $(this).find('title').text();
		    	flvSelectDesc = $(this).find('desc').text();
		    	flvSelectTag = $(this).find('tags').text();
			});
			
		    $('#selectFilmImageEntry').html('<div>'+fotoHtml+'</div><div class="zwijacz"> </div>');
			
		    if(flvImage != '') {
		    	if($("#fileImg0").val() == flvImage) {
		    		$("#fileImg0").attr('checked', true);
		    	}
		    	
		    	if($("#fileImg1").val() == flvImage) {
		    		$("#fileImg1").attr('checked', true);
		    	}
		    	
		    	if($("#fileImg2").val() == flvImage) {
		    		$("#fileImg2").attr('checked', true);
		    	}
	    	}

			$('#label_img').show();
		}
	});	
}

function video_generate_html() {
	if($('#field_flv').val() == null) {
		$('#htmlGenerated').val('');
		return;
	}

	if($("input[@name='field_img']:checked").val() == null) {
		$('#htmlGenerated').val('');
		return;
	}
	
	var pHtml = '<!-- Poland - Video - start -->\n';
    //pHtml += '<script type="text/javascript" src="http://'+pHttpHost+'/index.php?portlet=WEBVideo/out&main_template=main/blank&i='+$("input[@name='field_img']:checked").val()+'&f=gallery/_flv/'+$('#field_flv').val()+'"></script>\n';
	pHtml += '<script type="text/javascript" charset="utf-8" src="' + pHttpHostFoto + 'jsVideo/'+$('#field_lang').val() + '/' + $("input[@name='field_img']:checked").val() + '_flv/' + $('#field_flv').val() + '/G.js"></script>\n';
	pHtml += '<!-- Poland - Video - end -->';
	$('#htmlGenerated').val(pHtml);
}
