/*

init - range of functions

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!TYPE YOUR CLIENTSIDE JAVASCRIPT HERE!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

*/


$(document).ready(function(){
	initDropDownMenu();
	initSwitchBox();
	initJSwitchBox();
	initNavigationMenuVisibility();
	initImageGalleries();
})

function initJSwitchBox(){
	$('table.jswitchTable').each(function(tabidx, table){
		var switchTableHTML = "<div class='switchBoxTabs_" + tabidx + "'>";
		switchTableHTML += "<ul>"
		$('td:first-child', table).each(function(idx, e){
			var tabHeaderHTML =  jQuery.trim($(e).text());
			
			switchTableHTML += "<li>";
			switchTableHTML += "<a href='#switchBoxTabs_" + tabidx + "-" + (idx+1) + "'>";
			switchTableHTML += tabHeaderHTML;
			switchTableHTML += "</a>";
			switchTableHTML += "</li>";
		});
		switchTableHTML += '</ul>';
	
		//Content
		$('td:nth-child(2)', table).each(function(idx, e){
			var tabContentHTML = e.innerHTML;
			switchTableHTML += "<div id='switchBoxTabs_" + tabidx + "-" + (idx+1) + "'>";
			switchTableHTML += tabContentHTML;
			switchTableHTML += "</div>";
		});
		switchTableHTML += '</div>';
		$(table).replaceWith(switchTableHTML);
		$('.switchBoxTabs_' + tabidx).tabs()
		//$('#tabs').tabs()
	});
}

function initDropDownMenu(){
	$('#menu_main ul li').mouseenter(function(){
		$(this).find('ul').slideDown({ duration: 200 /*, easing: "easeOutElastic" */});
	}).mouseleave(function(){
		$(this).find('ul').slideUp(0);
	})
}

function initSwitchBox(){
	$('table.switchTable').each(function(idx, element){
		createSwitchBox(idx, element);
	});
}


function chekForThumbImages(gallery){
	if($('tr td', gallery).length >= 2){
		return true
	}
	else{
		return false
	}
}

function createThumbGallery(idx, gallery){
	var thumbs = [];
	$('td:first-child img', gallery).each(function(imgIdx, image){
		thumbs.push(image.src) 
	})
	var images = []
	$('td:nth-child(2) img', gallery).each(function(imgIdx, image){
		images.push(image.src) 
	})
	var additionalStyles = ''
	if($(gallery).hasClass('scroll')){
		additionalStyles = 'jGalleryScroll'
	}
	var html = '<div class="jGallery ' + additionalStyles + ' jThumbGallery_' + idx + '">';
	for(var i = 0; i < thumbs.length; i++){
		var thumbName = thumbs[i];
		if(images.length > i){
			var imageName = images[i]
			var style = 'width="72" height="72"'
			if($(gallery).hasClass('fitToContent')){
				style = ''
			}
			html += '<a href="' + imageName + '"><img src="' + thumbName + '" ' + style + ' alt="" /></a>'
		}
	}
	html += '</div>'
	$(gallery).replaceWith(html)
	$('.jThumbGallery_' + idx + ' a').lightBox({fixedNavigation:true});
}

function createGallery(idx, gallery){
	var additionalStyles = ''
	if($(gallery).hasClass('scroll')){
		additionalStyles = 'jGalleryScroll'
	}
	var html = '<div class="jGallery ' + additionalStyles + ' jGallery_' + idx + '">';
	$('img', gallery).each(function(imgIdx, image){
		html += '<a href="' + image.src + '"><img src="' + image.src + '" width="72" height="72" alt="" /></a>'
	})
	html += '</div>'
	$(gallery).replaceWith(html)
	$('.jGallery_' + idx + ' a').lightBox({fixedNavigation:true});
}

function initImageGalleries(){
	// pozostalosc po niemcach
	$('a.lightBox').lightBox();
	// Dysant modification - pozwala zamienic wszystkie zdjecia w tabeli w galerie
	$('.gallery').each(function(idx, gallery){
		if(chekForThumbImages(gallery)){
			createThumbGallery(idx, gallery)
		}else{
			createGallery(idx, gallery)
	    }
	})
}

function initNavigationMenuVisibility(){
	// ukrywamy menu ktore nie zostalo zaznaczone
	$('.navigationMenu .subMenuLevel3').hide();
	var parentMenu = $('.navigationMenu  .on').parent();
	if(parentMenu.hasClass('subMenuLevel3')){
		parentMenu.show();
	}
	else{
		$('.subMenuLevel3', $('.navigationMenu  .on').parent()).show()
	}	
}

function getURLParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function openRecruitmentApplication(){
	var id = getURLParam('offerId')
	var urlPartId = '';
	if(id){
		urlPartId = '&offerId=' + id
	}
	location.href = DBName + '/index.xsp?site=aplikacja' + urlPartId
}


function createSwitchBox(id, table){
	var switchTableHTML = "<div id='" + id + "_switchBox' class='switchBox'>";
	
	// Header with tabs
	switchTableHTML += "<div class='switchBoxTabs'><ul>";
	$('td:first-child', table).each(function(idx, e){
		var tabHeaderHTML =  jQuery.trim($(e).text());
		
		var tabClass = '';
		if(idx==0){
			tabClass = ' activeSwitchBoxTab'
		}
		
		switchTableHTML += "<li id='" + id + '_' + idx + "_SwitchBoxTab' class='switchBoxTab" + tabClass + "'>";
		switchTableHTML += "<a onclick='" + 'switchTabInSwitchBox("' + id + '","' + idx + '")' + "'>";
		switchTableHTML += tabHeaderHTML;
		switchTableHTML += "</a>";
		switchTableHTML += "</li>";
	});
	switchTableHTML += '</ul></div>';
	
	//Content
	switchTableHTML += "<div class='switchBoxContainer'>";
	$('td:nth-child(2)', table).each(function(idx, e){
		var tabContentHTML = e.innerHTML;
		var contentClass = '';
		if(idx==0){
			contentClass = ' activeSwitchBoxContent'
		}
		switchTableHTML += "<div id='" + id + '_' + idx + "_SwitchBoxContent' class='switchBoxContent" + contentClass + "'>";
		switchTableHTML += tabContentHTML;
		switchTableHTML += "</div>";
	});
	switchTableHTML += '</div>';
	
	switchTableHTML += "<div class='clear'></div>";
	switchTableHTML += "<div class='switchBoxFooterFrame'><div class='switchBoxFooter'></div><div class='switchBoxFooterEnd'></div></div>";
	switchTableHTML += '</div>';
	$(table).replaceWith(switchTableHTML);
}

function switchTabInSwitchBox(id, idx){
	$('.activeSwitchBoxContent', $('#' + id + '_switchBox')).toggleClass('activeSwitchBoxContent');
	$('#' + id + '_' + idx + '_SwitchBoxContent').toggleClass('activeSwitchBoxContent');
	$('.activeSwitchBoxTab', $('#' + id + '_switchBox')).toggleClass('activeSwitchBoxTab');
	$('#' + id + '_' + idx + '_SwitchBoxTab').toggleClass('activeSwitchBoxTab');
}

function openAttachment(docUnid, fileName){
	var res = window.open(DBName + '/0/' + docUnid + '/$File/' + fileName, fileName);
}

