// wy[wietla w bramce poparcia liste wpisow

function loadEntries(siteUrl) {

  $(document).ready(function(){
   
   $('#bramka_cont').load(siteUrl+'/bramka/entries.php'+Uniq('?uniq='));

  });

}



// wy[wietla w bramce poparcia formularz dodawania

function loadForm(siteUrl) {

  $(document).ready(function(){

    $('#bramka_cont').load(siteUrl+'bramka/form.php'+Uniq('?uniq='));
	
  });

}



function Uniq(prefix)
{
	if(typeof prefix == 'undefined') prefix = '';
	
	return prefix + Math.round(Math.random(new Date().getMilliseconds())*1000000);
}


// walidacja formularza

function checkEmail(email)

{

  var re = /^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)*(\.[a-zA-Z]{2,4})$/i;

  if(email.match(re) == null)

      return true;

  else

      return false;

}



// zapisuje wpis z bramki

function addEntry() {



  var name = document.getElementById('name').value;

  var email = document.getElementById('email').value;

  var text = document.getElementById('text').value;

  if((name == '') || (email == '') || (text == '') || (checkEmail(email))) {

    $('.error_0').fadeIn("slow");

  }

  else {

    $('.error_0').fadeOut("slow");

  }

  if((name != '') && (email != '') && (text != '') && (!checkEmail(email))) {

    $('#bramka_cont').load('bramka/add.php'+Uniq('?uniq='), { name: document.getElementById('name').value, email: document.getElementById('email').value, text: document.getElementById('text').value });

  }

}



function preloadImages()
{
	var aSrc = new Array();
	aSrc.push('menu/btn_aktualnosci_active.png');
	aSrc.push('menu/btn_aktualnosci_hover.png');
	aSrc.push('menu/btn_idea_active.png');
	aSrc.push('menu/btn_idea_orange.png');
	aSrc.push('menu/btn_kontakt_active.png');
	aSrc.push('menu/btn_kontakt_hover.png');
	aSrc.push('menu/btn_wioski_active.png');
	aSrc.push('menu/btn_wioski_orange.png');
	aSrc.push('menu/btn_zalozenia_active.png');
	aSrc.push('menu/btn_zalozenia_orange.png');

	aSrc.push('bg_bramka_top.gif');
	aSrc.push('bg_bramka_bottom.gif');

	aSrc.push('menu/idea/main.png');
	aSrc.push('menu/idea/bottom.png');
	aSrc.push('menu/idea/top.png');
	aSrc.push('menu/wioski/top.png');
	aSrc.push('menu/zalozenia/top.png');

	aSrc.push('menu/submenu_highlight.png');

	//var footer = document.getElementById('widebar');
	var img = new Array();
	for(var i=0; i<aSrc.length; i++) {
		img[i] = document.createElement('img');
		img[i].src = '/public/images/'+aSrc[i];
		img[i].style.display = 'none';
		//footer.appendChild(img);
	}
}


// ukrywa wszystkie submenu boxy

function hideAll() {

  var oIdea = document.getElementById('idea');
  var oZalozenia = document.getElementById('zalozenia');
  var oWioski = document.getElementById('wioski');
  
  if(oIdea.className.indexOf('current') !== -1)			oIdea.style.background = "url('/public/images/menu/btn_idea_active.png')";
  else													oIdea.style.background = "url('/public/images/menu/btn_idea.png')";
  
  if(oZalozenia.className.indexOf('current') !== -1)	oZalozenia.style.background = "url('/public/images/menu/btn_zalozenia_active.png')";
  else													oZalozenia.style.background = "url('/public/images/menu/btn_zalozenia.png')";

  if(oWioski.className.indexOf('current') !== -1)		oWioski.style.background = "url('/public/images/menu/btn_wioski_active.png')";
  else													oWioski.style.background = "url('/public/images/menu/btn_wioski.png')";

  document.getElementById('idea_box').style.display='none';
  document.getElementById('zalozenia_box').style.display='none';
  document.getElementById('wioski_box').style.display='none';

}



// ukrywa submenu box

function displayNone(element) {

  var submenu = element+'_box';
  document.getElementById(submenu).style.display='none';
  
  var oElement = document.getElementById(element);

  current = oElement.className.indexOf('current') !== -1;

  if(current)	oElement.style.background = "url('/public/images/menu/btn_"+element+"_active.png')";
  else			oElement.style.background = "url('/public/images/menu/btn_"+element+".png')";

}



// wyswietla submenu box

function displayBlock(element) {

  hideAll();

  var submenu = element+'_box';
  //document.getElementById(element).style.cursor = "none";
  document.getElementById(submenu).style.display = 'block';

  var oElement = document.getElementById(element);

  current = oElement.className.indexOf('current') !== -1;
  
  if(current)	oElement.style.background = "url('/public/images/menu/btn_"+element+"_active.png')";
  else			oElement.style.background = "url('/public/images/menu/btn_"+element+"_orange.png')";
}

var aImagesOBJS = new Array();
var imgIncr = 0;
var iMatched = 0;

function replacePhotoByLightbox(tagName, inObject)
{
	if(typeof inObject == 'undefined') inObject = document;

	objectsByTags = inObject.getElementsByTagName(tagName);

	r = new RegExp("userfiles/image/(idea|zalozenia|wioski)", "ig");

	for(imgIncr=0; imgIncr < objectsByTags.length; imgIncr++) {
		var oFound = objectsByTags[imgIncr];
		bSRCMatch = r.test(oFound.src.toString());

		if( bSRCMatch ) {
			iMatched++;
			oFound.id = imgIncr+'_IMGToReplace';

			var oNewImg = document.createElement('img');
			oNewImg.src = oFound.src;
			oNewImg.id = imgIncr+'_IMGReplaced';

			aImagesOBJS[imgIncr] = oNewImg;
			ShowPhoto();
		}
	}
}

var loopCount = 0;
var replacedCount = 0;
function ShowPhoto(id)
{
	if(typeof id!='undefined') imgIncr = id;

	var oNewImg = aImagesOBJS[imgIncr];
	var oFound = document.getElementById(imgIncr+'_IMGToReplace');

	if(oFound.title!='loading') {
		oFound.title = 'loading';
		
		// changing image to loading circle
		oFound.style.width = '32px';
		oFound.style.height = '32px';
		oFound.src = '/public/images/lightbox/lightbox-ico-loading.gif';
	}

	checkPhoto = null;

	if(oNewImg.width>1 && oNewImg.height != 30 && oNewImg.width != 28) {

		oFound.title = '';

		oNewDiv = document.createElement('div');
		oNewDiv.className = 'imgFrame';

		if(oFound.align == 'left') oNewDiv.className += ' alignLeft';
		if(oFound.align == 'right') oNewDiv.className += ' alignRight';

		oNewAnhor = document.createElement('a');
		oNewAnhor.href = oNewImg.src;
		oNewAnhor.className = 'lightbox';

		oNewImg.className = 'contentImg';
		oNewImg.alt = 'Kliknij aby powiększyć';

		MAXHeight = 160;
		MAXWidth = 163;

		if(oNewImg.height >= oNewImg.width && oNewImg.height > MAXHeight) {
			scale = MAXHeight / oNewImg.height;
			oNewImg.height = MAXHeight;
			oNewImg.width = Math.ceil(scale * oNewImg.width);
			if(oNewImg.width > MAXWidth) {
				scale = MAXWidth / oNewImg.width;
				oNewImg.width = MAXWidth;
				oNewImg.height = Math.ceil(scale * oNewImg.height);
			}
		} else if(oNewImg.height <= oNewImg.width && oNewImg.width > MAXWidth) {
			scale = MAXWidth / oNewImg.width;
			oNewImg.width = MAXWidth;
			oNewImg.height = Math.ceil(scale * oNewImg.height);
			if(oNewImg.height > MAXHeight) {
				scale = MAXHeight / oNewImg.height;
				oNewImg.height = MAXHeight;
				oNewImg.width = Math.ceil(scale * oNewImg.width);
			}
		}

		mt = Math.floor((176/2)-(oNewImg.height/2));
		oNewImg.style.marginTop = mt+'px';

	} else {

		if(loopCount++ > 120) {
			clearInterval(checkPhoto);
			oFound.src = '/public/images/logo.png';
			oFound.title = oNewImg.src;
			oFound.alt = oNewImg.src;
			oFound.style.width = 198;
			oFound.style.height = 108;
			return false;
		}
		checkPhoto = setTimeout('ShowPhoto("'+imgIncr+'")', 120);
		return true;

	}
	clearInterval(checkPhoto);

	oNewAnhor.appendChild(oNewImg);
	oNewDiv.appendChild(oNewAnhor);

	oFound.parentNode.replaceChild(oNewDiv, oFound);
	replacedCount++;
	
	if(replacedCount >= iMatched) InitLightbox();
}

function InitLightbox()
{
	//alert('InitLightbox');
	$(function() {
		$('#content a.lightbox').lightBox();
	});
}

function mainPageFlash(baseUrl) {
  var so = new SWFObject(baseUrl+"/public/swf/flash.swf", "mymovie", '440', '340', "8", "#336699");
  so.addParam("wmode", "transparent");
  so.write("mainPageFlash");
}
