function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

window.addEvent('load', function(){
	externalLinks();
	
	$ES('a', 'main-menu').addEvents({
		'focus' : function() {
			this.blur();
		}
	});
});


/*-- OPERACJE NA PLYWAJACYCH OKNACH --*/
/*
 *	Plynne przesuwanie okna
 */
function go() {
	if (window.scrollX>=0) { // przesunięcie okna dla Netscape
		x = window.scrollX; y = window.scrollY;
	} else { // dla IE, Opery, Firefox
		x = document.body.scrollLeft;  y = document.body.scrollTop;
	}
	docelowax = pozycjax + x;
	doceloway = pozycjay + y;

	yd = (doceloway - parseInt(obj.style.top)) / 5;
	xd = (docelowax - parseInt(obj.style.left)) / 5;

	obj.style.left = parseInt(obj.style.left) + xd + "px";
	obj.style.top = parseInt(obj.style.top) + yd + "px";

/*	if (widoczne) {*/ setTimeout("go()", 50); //}
//	else { obj.style.visibility = "hidden"; }
}


/**
 *	Otwieranie okna (div) za pomoca funckji AJAX'owych
 *	----
 *	tworzenie diva
 *	ustawianie go na srodku ekranu biorac pod uwage jego rozmiary oraz rozmiary okna przegladarki
 *	nastepna funkcja (np. FillWindow) powinna umiescic w nim tresc HTML (odpowiedz z zadania AJAX)
 *	pokazanie okna (byc moze za pomoca innej funkcji, ktora doda np efekt plynnego pokazania sie)
 *	okno powinno byc zawsze na srodku, nawet jak przesune pasek przewijania w przegladarce
 *	@param int id
 *	@param int szer
 *	@param int wys
 *	@return obj
 */
function CreateWindow(id, szer, wys) {
	var ekranX = ekranY = 0;
	if (typeof(window.innerWidth)=='number') {
		ekranX = window.innerWidth;
		ekranY = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth
	|| document.documentElement.clientHeight)) {
		ekranX = document.documentElement.clientWidth;
		ekranY = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		ekranX = document.body.clientWidth;
		ekranY = document.body.clientHeight;
	}
	
	var x = y = 0;
	if (typeof(window.pageYOffset)=='number') {
		x = window.pageXOffset;
		y = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}

	pozycjax = ekranX/2 - szer/2;	//polowa serokosci diva
	pozycjay  = ekranY/2 - wys/2;	//polowa wysokosci diva
	startpozycjax = pozycjax + x;
	startpozycjay = pozycjay + y;
	

	
	var el = new Element('div', {
		'styles': {
			'width': szer+'px',
			'height': wys+'px',
			'position': 'absolute',
			'top': startpozycjay+'px',		//to musi byc obliczane, aby bylo na srodku ekranu
			'left': startpozycjax+'px',		//to musi byc obliczane, aby bylo na srodku ekranu
			'background-color': '#fff',
			'z-index': '1000',
			'opacity': '0'
		},
		'id': id
	});
	el.injectBefore('main');
	
	//dopisalem do plynnego przesuwania okna
	var widoczne = true; // okienko ma być widoczne
	var yd = 0;
	var xd = 0;
	obj = $(id);
	go();
	
	setCanvas(id);
	
	return el;
}


/*
 *	Wypelnianie okna trescia
 *	@param obj el
 *	@param string dane
 */
function FillWindow(el, dane) {
	el.setHTML(dane);
}


/*
 *	Plynne pokazanie okna (diva)
 *	@param obj id
 */
function ShowWindow(id) {
	var efekt = new Fx.Style(id,'opacity',{
		duration: 600
	});
	
	efekt.start(0,1);
}

/*
 *	Plynne zamkniecie okna (diva)
 *	@param obj id
 */

function CloseWindow(id) {
	var efekt = new Fx.Style(id,'opacity',{
		duration: 600,
		onComplete: function(){
			id.remove();
			var Node = $('Canvas');
			Node.parentNode.removeChild(Node);
		}
	});
	
	efekt.start(1,0);
}

function ChangeSNEnable(wartosc) {
	if (wartosc != 'N') {
		document.getElementById('sn').disabled = false;
	} else {
		document.getElementById('sn').value = '';
		document.getElementById('sn').disabled = true;
	}
}

/**
 *	Pokazywanie grafiki przedstawiającej ładowanie danych podczas wczytywania zawartości
 *	@param string divID
 *	@param int imgSize
 */
function Loader(divID, imgSize) {
	divX = $(divID).getStyle('width');
	divY = $(divID).getStyle('height');
	
	if (imgSize == undefined || imgSize == null || imgSize == '' || imgSize > 32) {
		imgSize = 32;		//domyslna maksymalna wartosc
	} else if (imgSize < 12) {
		imgSize = 12;		//domyslna minimalna wartosc
	}
	
	imgLeft = (parseInt(divX) - imgSize) / 2;
	imgTop = (parseInt(divY) - imgSize) / 2;
	
	var el = new Element('div', {
		'styles': {
			'width': divX,
			'height': divY,
			'padding-left': imgLeft+'px',
			'padding-top': imgTop+'px'
		}
	});
	el.injectInside(divID);
	
	var img = new Element('img', {
		'src': 'img/loader.gif',
		'width': imgSize+'px',
		'height': imgSize+'px'
	});
	img.injectInside(el);
}

/*
 *	Flash okna
 *	@param string id
 */
function FlashWindow(id) {
	var efekty = $(id).effects({
		duration: 50
	});
	
	efekty.start({
		"opacity": [1,0.7]
	}).chain(function(){
		efekty.start({
			"opacity": [0.7,1]
		});
	}).chain(function(){
		efekty.start({
			"opacity": [1,0.7]
		});
	}).chain(function(){
		efekty.start({
			"opacity": [0.7,1]
		});
	});
}
/*-- END OPERACJE NA PLYWAJACYCH OKNACH --*/

/*-- GALERIA --*/
var ShowPhoto = new XHR({
	onRequest: function() {
		FillWindow($('gallery'), '');
		Loader($('gallery'));
	},
	onSuccess : function() {
		$('gallery').innerHTML = this.response.text;
	//	ShowFrame('gallery','480','450');
	}
});

var NextPhoto = new XHR({
	onRequest: function() {
		FillWindow($('gallery'), '');
		Loader($('gallery'));
	},
	onSuccess : function() {
		document.getElementById('gallery').innerHTML = this.response.text;
	}
});

function ShowNextPhoto (theURL) {
//	Loader($('gallery'));
	NextPhoto.send(theURL,'get');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	var divID = 'gallery';
	if (!$(divID)) {
		
		div = CreateWindow(divID,'480','480');
		
		ShowPhoto.send(theURL,'get');
		ShowWindow(div);
	} else {
		FlashWindow($(divID));
	}
}

function setCanvas(obj) {
	var divMaster = null;
	var divCanvas = null;
	
	var PageWidth = document.body.scrollWidth+'px';
	var PageHeight = document.body.scrollHeight+'px';
	
	divMaster = document.body;

	var divCanvas = new Element('div', {
		'id': 'Canvas',
		'name': 'Canvas',
		'styles': {
			'position': 'absolute',
			'display': 'none',
			'zindex': '1',
			'top': '0',
			'left': '0',
			'width': PageWidth,
			'height': PageHeight,
			'opacity': '.5',
			'backgroundColor': '#000000'
		},
		'events': {
			'click': function(){
				CloseWindow($(obj));
			}
		}
	});
	divCanvas.injectInside(divMaster);
	divCanvas.setStyle('display', 'block');
}

function HideErrorFrame(divID) {	//alias funkcji
	CloseWindow($(divID));
}
/*-- END GALERIA --*/

function down(xw, yw, idshp)
{
mywin=window.open('wtb_popup.php?id='+escape(idshp),'Note','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width='+escape(xw)+',height='+escape(yw)+',left=100,top=50');
mywin.focus();
}

function popUp(URL) {
	resx = (screen.width - 700) / 2;
	resy = (screen.height - 480) / 2;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,		menubar=0,resizable=1,width=700,height=480,left = "+resx+",top = "+resy+"');");
}

/*	DOWNLOAD	*/
function CheckForm() {
 var topic = document.comment_insert.comment_topic.value;
 if ( topic == "" || topic.length < 3 ) {
	alert ("Wpisz temat");
	document.comment_insert.comment_topic.focus();
	return;
 }
 var content = document.comment_insert.comment_content.value;
 if ( content == "" || content.length < 3 ) {
	alert ("Wpisz tre¶ć");
	document.comment_insert.comment_content.focus();
	return;
 }
 else {
	document.comment_insert.action = "download_comment_insert.php";
	document.comment_insert.submit();
 }
}

function SearchDL(keyword) {
	var product = document.getElementById('product_id').value;
	var category = document.getElementById('category_id').value;
//	var keyword = document.getElementById('keyword').value;
	if (document.getElementById('list')) {
		var per_site = document.getElementById('list').value;
	}
//	keyword = str2b64(keyword);
	location.href = category+","+product+","+keyword+",,"+per_site+",download.php";
}


function MM_jumpMenu(keyword){ //v3.0
	var per_site = document.getElementById('quickmenu').list.value;
	var product = document.getElementById('product_id').value;
	var category = document.getElementById('category_id').value;
//	var keyword = document.getElementById('keyword').value;
//	keyword = str2b64(keyword);
	location.href = category+","+product+","+keyword+",,"+per_site+",download.php";
}

function HideValueTxt (f,txt_default) {
	if (f.value == txt_default) {
		f.value = '';
	}
}

function ShowValueTxt (f,txt_default) {
	if (f.value == '') {
		f.value = txt_default;
	}
}

function ChangeFaqProduct (p) {
	var product = p.value;
	location.href = ","+product+",,,faq.php";
}

function ChangeReviewProduct (p) {
	var product = p.value;
	location.href = ","+product+",,,recenzje.php";
}
