var ow = [];
ow.offerId = 0;
ow.shown = true;
ow.timer = null;
ow.positionTimer = null;

function offerWindow(id, e){
	// Pokaż preloader
	var father = e.relatedTarget || e.fromElement;

	var fatherHeight = father.offsetHeight;

	$("#popup_preloader").css("top", findPos(father)[1] + fatherHeight/2-18);
	$("#popup_preloader").css("left",findPos(father)[0]-90);
	$("#popup_preloader").css("display", "block");

	// Pozycjonowanie początkowe
	var wH = $("#popupOffer").height();
	$("#popupOffer").css("left", e.clientX + 50);

	if($.browser.msie) topPos = e.clientY + document.body.scrollTop;
	else topPos = e.pageY;

	if(e.clientY > wH) {
		$("#popupOffer").css("top", topPos-wH);
	} else if (document.documentElement.clientHeight - e.clientY > wH){
		$("#popupOffer").css("top", topPos);
	} else $("#popupOffer").css("top", (topPos - e.clientY) + ((document.documentElement.clientHeight-wH)/2))
	

	ow.shown = true;
	ow.offerId = id;

	// Użytkownik musi się zatrzymać na ofercie przynajmniej na 0.5 sekundy
       clearTimeout(ow.timer);
       ow.timer = null;
       ow.timer = setTimeout("$.getJSON('offerPreview.php?id="+id+"', offerWindowDatahandler);", 300);


	// Fix na błędny z-index w IE
	if($.browser.msie) $("#popupOffer").bgiframe();
}

function offerWindowDatahandler(data, textStatus){
	// Jeżeli ID jest inne niż obecnie podświetlana oferta
	// natychmiast schowaj okienko i pozwól się wyświetlić innej ofercie.
	if(!data['cnt_id'] == ow.offerId)  {
		ow.shown = false;
		hideOfferWindow();
		return;
	}
	$("#pwin_stanowisko").html(data["cnt_title"]);

	$("#pwin_nazwafirmy").html(data['cnt_company']);
	if(data['logo']) $("#pwin_image").html('<img align="right" src="_panel/pics_employers/'+data['logo']+'"/>');
	else $("#pwin_image").html('<img align="right" src="gfx/blank.gif" />');

	$("#pwin_stanowisko").html(data['cnt_title']);
	$("#pwin_lokalizacja").html(data['cnt_country']+', '+ data['cnt_city']);
	$("#pwin_wynagodzenie").html(data['salary']);

	var wymagania = filterRequirements(data["cnt_text"]);
	$("#pwin_wymagania").html(wymagania);
	$("#pwin_OpisPracy").html(data['cnt_offertype']);
	$("#pwin_wyksztalcenie").html(data['cnt_minEducation']);
	$("#pwin_NrRef").html(data['cnt_nr_ref']);
	$("#pwin_wizyta").html(data['count']);
	$("#pwin_data").html(data['cnt_date']);

	launchOffer();
}
function filterRequirements(text, openPopup){
 
	openPopup = typeof(openPopup) != 'undefined' ? openPopup : false;
	if(openPopup) text = text.replace(/window\.location\s*='(.*)'/, "window.open('$1','', 'top=0,left=0,screenX=0, screenY=0,scrollbars=yes, resizable=yes,menubar=yes,height='+(document.documentElement.clientHeight)+',width='+(document.documentElement.clientWidth))");

	if(!openPopup) text = text.replace(/window\.location\s*=\s*'.*'/, '');
	return text;
}
function launchOffer(){
	// Najpierw niech się załaduje zdjęcie - dopiero wtedy pokażemy okienko.
	if($("#pwin_image img").length > 0) {
		$("#pwin_image img").load(showOfferWindow);
	}

}
function hideOfferWindow(){
	$("#popup_preloader").css("display", "none");

	ow.shown = false;
	clearTimeout(ow.timer);
	$("#popupOffer").css("display", "none");
	$().unbind("mousemove");
	ow.timer = null;
	$("#pwin_image img").unbind("load");

}

function showOfferWindow(){
	if(ow.shown){
		$("#popup_preloader").css("display", "none");
		$().mousemove(positionHandler);
		$("#popupOffer").css("display", "block");
	}
}

function positionHandler(e){
	var wH = $("#popupOffer").height();

	$("#popupOffer").css("left", e.pageX+50);
	
	if(e.clientY > wH) {
		// Okienko na górze
		$("#popupOffer").css("top", e.pageY-wH);
	} else if (document.documentElement.clientHeight - e.clientY > wH){
		$("#popupOffer").css("top", e.pageY);
	} else $("#popupOffer").css("top", (e.pageY - e.clientY) + ((document.documentElement.clientHeight-wH)/2))
	
}
function findPos(obj) {
	var curleft = curtop = 0;
	if(obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
		} while (obj = obj.offsetParent);
	};

	return [curleft, curtop];
}

/*
function positionHandler(e){
	var wH = $("#popupOffer").height();

	$("#popupOffer").css("left", e.pageX+50);
	
	if(e.clientY > wH) {
		// Okienko na górze
		$("#popupOffer").css("top", e.pageY-wH);
	} else if (document.documentElement.clientHeight - e.clientY > wH){
		$("#popupOffer").css("top", e.pageY);
	} else $("#popupOffer").css("top", (e.pageY - e.clientY) + ((document.documentElement.clientHeight-wH)/2))
	
}*/

