
/***************************	Menu Hide/Show		****************************/

function menuCollapseAll()
{
	var expandMenuObj = document.getElementById('menuEx');
	if(expandMenuObj)
	{
		var elementsToHide = expandMenuObj.getElementsByTagName('UL');
		
		for(var i=0; i<elementsToHide.length; i++)
		{
			if(elementsToHide[i].className == 'menuKategoriaAkcji' || elementsToHide[i].className == 'menuKategoriaAkcjiHide')
				elementsToHide[i].className = 'menuKategoriaAkcjiHide';
		}
	}
}

function menuAddEvent()
{
	var expandMenuObj = document.getElementById('menuEx');
	if(expandMenuObj)
	{
		var clickedElement = expandMenuObj.getElementsByTagName('H3');
		for(var i=0; i<clickedElement.length; i++)
		{
			clickedElement[i].onclick = menuShowHideThis;
		}
	}
}

function menuShowHideThis()
{
	var thisParent = this.parentNode;
	var subMenu = thisParent.getElementsByTagName('UL');

	if(subMenu[0].className == 'menuKategoriaAkcjiHide')
	{
		menuCollapseAll();
		subMenu[0].className = 'menuKategoriaAkcji'; //show this
	}
	else if(subMenu[0].className == 'menuKategoriaAkcji')//close opened submenu
	{
		menuCollapseAll();
	}
}

function menuActivThisOne(iActive)
{
	if(iActive != 'undefined')
	{
		var expandMenuObj = document.getElementById('menuEx');
		var colMenuElements = expandMenuObj.getElementsByTagName('UL');// ul element
	
		if(colMenuElements)
		{
			iActive = iActive - 1;// iActive is counting from 1, colMenuElements is counting form 0
	
			// highlight category name
			var parentElement = colMenuElements[iActive].parentNode;
			var elementToHighlight = parentElement.getElementsByTagName('H3');
			elementToHighlight[0].className = 'menuImgKategoriaActive';
	
			// expand active on start this one menu
			colMenuElements[iActive].className = 'menuKategoriaAkcji';
		}
	}
}

/***************************	Imagie Gallery	******************************/

function gallery()
{
	if(document.getElementById('imgGallery'))
	{
		var objGallery = document.getElementById('imgGallery');
		var colA = objGallery.getElementsByTagName('A');

		for(var i=0; i<colA.length; i++)
			colA[i].onclick = changePhoto;
	}
}

function changePhoto()
{
	var smallImgSource = this.getElementsByTagName('IMG')[0].src;
	var tempSrc = smallImgSource.substring(0, smallImgSource.length - 1)
	var newImgSource = tempSrc + 'n' ;
	var normalOldImg = document.getElementById('bigPhoto');
	normalOldImg.src = newImgSource;
	return false;
}

/***************************	onLoad Action	******************************/

function initMenu()
{
	gallery();
}

window.onload = initMenu;


/***************************	Show img in new Window	******************************/


var newWindow = '';
function zoom(img,w,h)
{
	if(newWindow != '' && newWindow.closed == false)
	{
		newWindow.close();
	}


	config="width="+w+","+"height="+h+","+"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";

	newWindow=window.open("","windowName",config);
	newWindow.document.open();
	newWindow.document.clear();
	newWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"');
	newWindow.document.write('"http://www.w3.org/TR/html4/strict.dtd">');
	newWindow.document.write('<html>');
	newWindow.document.write('<title>Image</title>');
	newWindow.document.write('<body style="margin: 0px; padding: 0px; overflow:hidden;">');
	newWindow.document.write('<a href="#" onclick="window.close(); return false;" title="Zamknij okno"><img src='+img+' alt="Wyroznianie ogloszeń" style="border: 0px;" ></a>');
	newWindow.document.write('</body>');
	newWindow.document.write('</html>');
	newWindow.document.close();
}


/*
function usunOpisKosza()
{
	if(document.getElementById('listaOgloszen'))
	{
		objOgloszenia = document.getElementById('listaOgloszen');
		colHref = objOgloszenia.getElementsByTagName('DIV');

		for(var i=0; i<colHref.length; i++)
		{
			if(colHref[i].className == 'ogRegulamin' || colHref[i].className == 'ogRegulamin' || colHref[i].className == 'ogRegulamin')
			{
				colHref[i].getElementsByTagName('A')[0].childNodes.item(1).nodeValue = '';
				colHref[i].onmouseover = opisKoszaTak;
				colHref[i].onmouseout = opisKoszaNie;
			}
		}
	}
}

function opisKoszaTak()
{
	this.getElementsByTagName('A')[0].childNodes.item(1).nodeValue = 'Ogłoszenie łamie regulamin? Zgłoś do weryfikacji!';
}

function opisKoszaNie()
{
	this.getElementsByTagName('A')[0].childNodes.item(1).nodeValue = '';
}

//window.onload=usunOpisKosza;
*/

////////////////////////////////////// Image upload file /////////////////////////////////////////////


function fileUploadViewer() 
{
	var colFU = document.getElementById('fileUpload');
	var colFileInput = colFU.getElementsByTagName('INPUT');

	for (var i=0; i<colFileInput.length; i++)
	{
		colFileInput[i].onchange = fUV_control;
	}
}

function fUV_control()
{
	var allowedFiles = '';
	var imgPath = this.value;
	var imgClass = 'newImageClass'
	var objParent = this.parentNode;
	var miniatureSize = {};
		miniatureSize.w = 100;
		miniatureSize.h = 80;
	var hrefText = 'Usuń ten obrazek';
	var textClass = 'newTextClass'

	var allowedFiles = fUV_checkFileType(imgPath,objParent);

	if(allowedFiles)
	{
		fUV_clearImage(objParent);
		fUV_clearLink(objParent);
		fUV_createImage(objParent,imgPath,miniatureSize,imgClass);
		fUV_createLink(objParent,hrefText,textClass);

	}
	return false; // onclick
}

function fUV_checkFileType(imgPath,objParent)
{
	if(imgPath)// on href click - no img path
	{
		imgPath = imgPath.substr(imgPath.length-4, imgPath.length);
		imgPath = imgPath.toLowerCase();
	}

	if (imgPath == '.jpg' || imgPath == 'jpeg' || imgPath == '.gif' || imgPath == '.png')
	{
		return true;
	}
	else	
	{
		fUV_clearInputFile(objParent);
		fUV_clearImage(objParent);
		fUV_clearLink(objParent);
		return false;
	}
}

function fUV_clearInputFile(objParent)
{
	var objInputFile = {};
	var obj;

	obj = objParent.getElementsByTagName('INPUT')[0];

	objInputFile.id = obj.id;
	objInputFile.name = obj.name;
	objInputFile.className = obj.className;

	objParent.removeChild(obj);

	fUV_createInputFile(objParent,objInputFile);	
}

function fUV_clearImage(objParent)
{
	var obj = objParent.getElementsByTagName('IMG')[0];
	if(obj)
		objParent.removeChild(obj);
}

function fUV_clearLink(objParent)
{
	var obj = objParent.getElementsByTagName('A')[0];
	if(obj)
		objParent.removeChild(obj);
}

function fUV_createInputFile(objParent,objInputFile)
{
	var newInputFile = document.createElement('INPUT');
	newInputFile.setAttribute('type', 'file');
	newInputFile.setAttribute('id', objInputFile.id);
	newInputFile.setAttribute('name', objInputFile.name);
	newInputFile.className = objInputFile.className;
	newInputFile.onchange = fUV_control;
	objParent.appendChild(newInputFile);
}

function fUV_createImage(objParent,imgPath,miniatureSize,imgClass)
{
	var newImage = document.createElement('IMG');
	newImage.setAttribute('src', imgPath);

	newImage.src=imgPath;
		
	newImage.setAttribute('alt', '');
	newImage.className = imgClass;
	newImage.style.visibility = 'hidden';
	newImage.style.position = 'absolute';

	objParent.appendChild(newImage);
	var bigImgSize = {}
	bigImgSize.w = newImage.width;
	bigImgSize.h = newImage.height;

	var smallImgSize = {}
	smallImgSize = fUV_resizeImage(bigImgSize,miniatureSize)

	newImage.width = smallImgSize.w;
	newImage.height = smallImgSize.h;

	newImage.style.visibility = 'visible';
	newImage.style.position = 'static';
	newImage.style.verticalAlign = 'middle';
}

function fUV_createLink(objParent,hrefText,textClass)
{
	var newLink = document.createElement('A');
	var textObj = document.createTextNode(hrefText);
	
	newLink.href = "#";
	newLink.className = textClass;
	newLink.appendChild(textObj);
	objParent.appendChild(newLink);
	newLink.onclick = fUV_control;
}

function fUV_resizeImage(bigImgSize,miniatureSize)
{
	var proportion
	var newSmallSize = {};

	if(bigImgSize.w >= bigImgSize.h)
	{
		proportion = miniatureSize.w / bigImgSize.w;
		newSmallSize.w = bigImgSize.w * proportion;
		newSmallSize.h = bigImgSize.h * proportion;
		return newSmallSize;
	}
	else //(bigImgSize.w < bigImgSize.h)
	{
		proportion = miniatureSize.h / bigImgSize.h;
		newSmallSize.w = bigImgSize.w * proportion;
		newSmallSize.h = bigImgSize.h * proportion;
		return newSmallSize;
	}
}