
function openInOpener(linkElem) {
	if (self.opener) {
		self.opener.top.location.href = linkElem.href;
		self.close();
		return false;
	}
	linkElem.target = "_blank";
	return true;
}

function openInMainFrame(linkElem) {
	if (self.opener && self.opener.parent.multiphotoMainFrame) {
		self.opener.parent.multiphotoMainFrame.location.href = linkElem.href;
		self.opener.parent.multiphotoMainFrame.focus();
		return false;
	} else {
		alert("Nie mogłem przejść do wybranej strony, ponieważ okno otwierające zostało zamknięte.");
		return true;
	}
}

function openCD(elem, id) {
	winname = (id==null) ? 'CD' : 'CD' + id;
	openWin(elem.href, 20, 0, 810, 750, winname, 0);
	return false;
}

function openLicenceRF(elem) {
	openWin(elem.href, 40, 0, 670, 650, "licencjaRF", 1);
	return false;
}

function openLicenceRM(elem) {
	openWin(elem.href, 55, 0, 670, 650, "licencjaRM", 1);
	return false;
}

function openONas(elem) {
	var y=screen.availHeight/2 - 410/2;
	openWin(elem.href, y, 0, 630, 410, "onas", 1);
}

function openKontakt(elem) {
	var y=screen.availHeight/2 - 410/2;
	openWin(elem.href, y, 0, 550, 340, "bankkontakt", 1);
}

function openFaq(elem) {
	var y=screen.availHeight/2 - 350/2;
	openWin(elem.href, y, 0, 670, 350, "multiphotofaq", 1);
}

function openPromotionTerms(elem) {
	var y=screen.availHeight/2 - 500/2;
	openWin(elem.href, y, 0, 670, 500, "promotionTerms", 1);
}

function openFullSize(elem) {
	var y=screen.availHeight/2 - 310/2;
	openWin(elem.href, y, 0, 350, 210, "fullsize", 0);
}

function openForPhotographers(elem) {
	openWin(elem.href, 70, 0, 670, 500, "ForPhotographers", 0);
}

function openFotomodelki(elem) {
	openWin (elem.href, 0, 0, 860, 750, 'portfolioprzyklady', 0);
	return false;
}

function openPortfolio(elem) {
	openWin (elem.href, 0, 0, 810, 750, 'portfolioprzyklady', 0);
	return false;
}


function showCloseButton() {
	if (document.getElementById && document.getElementsByTagName) {
		if (self.opener) {
			x_close=document.getElementById("x");
			x_close.style.visibility="visible";
		}
	}
}

function close_me () {
	self.close();
	return false;
}

function loginToNewWindow(formElem, targetName) {
	formElem.target = targetName;
	formElem.p_ass2.value = formElem.login_p_ass.value;
	formElem.login_p_ass.value = "";
	setTimeout ("document.login_form.p_ass2.value=''", 1000*60);
	openWin ('', 20, 20, 810, 750, targetName, 0);
}




licencja_dropDown=false;

function dropDown(elemId) {
	stopBubbling=true;
	elem=document.getElementById(elemId);
	if (licencja_dropDown) {
	 	elem.style.display="none";
		licencja_dropDown=false;	
	} else {
		elem.style.display="block";
		licencja_dropDown=true;
	}
	setTimeout ("stopBubbling=false", "300");
}

stopBubbling=false;

function hideDropDowns() {
	if (!stopBubbling) {
		document.getElementById('nav-licencja').style.display="none";
		licencja_dropDown=false;	
	}
}

function goTo(inpElem) {
	document.location.href=inpElem.value;
}


// TOOLTIPS
function initTooltips() {
	allowShowTooltip = true;
	offsetY=false;
	offsetX=false;
	
	var photo_id;
	var tab = document.getElementById('zdjecia-tab');
	if (!tab) return;
	
	var as = tab.getElementsByTagName('a');
	for (var i=0; i<as.length; i++) {
		
		var imgs = as[i].getElementsByTagName('img');
		if (as[i].href.indexOf('zdjecie.php?')>-1 && (imgs.length && !imgs[0].title)) {
			segm = as[i].href.split('photo_id=');
			photo_id = parseFloat(segm[1]);
			
			as[i].photo_id = photo_id;
			as[i].onmousemove = showTooltip;
			as[i].onmouseout = function() { hideTooltip(this.photo_id); };
			as[i].onmousedown = function() { delayNextTooltip(this.photo_id); };
		}
	}
}

function showTooltip(e) {
	if (!allowShowTooltip) return;
	
	var tooltip = document.getElementById('tooltip' + this.photo_id);
	
	if (tooltip) {
		var cursorx = 0;   // get cursor position relative to document
		var cursory = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			cursorx = e.pageX;
			cursory = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			cursorx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			cursory = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		var scrollPos = getScrollingPosition();
		var winSize = getWindowSize();
		var absCursorY = cursory - scrollPos[1];
		var absCursorX = cursorx - scrollPos[0];
		
		if (offsetY===false) {
			offsetY = (absCursorY > winSize[1] - 50-tooltip.offsetHeight) ? 118+tooltip.offsetHeight : 78;
		}
		
		if (offsetX===false) {
			if (absCursorX > winSize[0] - (tooltip.offsetWidth/2 + 40)) {
				offsetX = tooltip.offsetWidth+(tooltip.offsetWidth/2 + 40) - (winSize[0]-absCursorX);
			} else if (absCursorX < tooltip.offsetWidth/2 + 17) {
				offsetX = (tooltip.offsetWidth/2 - 17) + absCursorX;
			} else {
				offsetX = tooltip.offsetWidth;
			}
		}

		tooltip.style.top = cursory - offsetY + tooltipOffsetY + "px";
		tooltip.style.left = cursorx - offsetX + tooltipOffsetX + "px";
	}
}


function hideTooltip(photo_id) {
	var tooltip = document.getElementById('tooltip' + photo_id);
	if (tooltip) {
		tooltip.style.left = '-999px';
		offsetY = false;
		offsetX = false;
	}
}

function delayNextTooltip(photo_id) {
	hideTooltip(photo_id);
	allowShowTooltip = false;
	setTimeout('allowShowTooltip=true', 1000);
}



function getScrollingPosition()
{
 var position = [0, 0];
 if (typeof window.pageYOffset != 'undefined')
 {
   position = [
       window.pageXOffset,
       window.pageYOffset
   ];
 }
 else if (typeof document.documentElement.scrollTop
     != 'undefined' && document.documentElement.scrollTop > 0)
 {
   position = [
       document.documentElement.scrollLeft,
       document.documentElement.scrollTop
   ];
 }
 else if (typeof document.body.scrollTop != 'undefined')
 {
   position = [
       document.body.scrollLeft,
       document.body.scrollTop
   ];
 }
 return position;
}


function getWindowSize() {
	var y=0;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return Array(x,y);
}


// add or remove photo from lightbox
function addRemoveLightbox(photo_id) {
	parent.lightbox.location.replace("user-lightbox.php?action=addRemovePhoto&photo_id=" + photo_id);
}

// add or remove photo from lightbox - executed from new window
function addRemoveLightboxWindow(photo_id) {
	if (window.opener && window.opener.parent.lightbox) {
		lightboxWin = window.opener.parent.lightbox;
		lightboxWin.location.replace("user-lightbox.php?action=addRemovePhoto&photo_id=" + photo_id + "&refreshWin=" + window.name);
		lightboxWin.wasRefreshed = false;
		myInterval = setInterval('refreshOnLightboxChange()', 300);
	} else {
		alert("Nie mogłem dodać/usunąć wybranego zdjęcia, ponieważ podstawowe okno zostało zamknięte");
	}
}

function refreshOnLightboxChange() {
	if (lightboxWin.wasRefreshed) {
		clearInterval(myInterval);
		location.reload();
	}
}



function markLightboxButtons(photo_ids) {
	var photo_id, segm;
	if (!document.getElementById('zdjecia-tab')) return;
	
	var divs = document.getElementById('zdjecia-tab').getElementsByTagName('div');
	
	for (var i=0; i<divs.length; i++) {
		if (divs[i].id.indexOf('LBButton_') == 0) {
			segm = divs[i].id.split("_");
			photo_id = segm[1];
			
			if (photo_ids.indexOf("," + photo_id + ",") > -1) {
				// this photo is in lightbox
				divs[i].className = "lightbox-button in-lightbox";		
			} else {
				// not in lightbox
				divs[i].className = "lightbox-button not-in-lightbox";	
			}
		}
	}	
}



