if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
}

function poleUstaw(pozycja, nieaktywny)
{
	var e = _e(pozycja);
	e.disabled = nieaktywny;
	if(!nieaktywny)
		e.style.background = '';
	else
		e.style.background = '#808080';
}

function odbiorcaUstaw(typ)
{
	switch(typ)
	{
		case 0:
			poleUstaw('r_faktura_imie',false);
			poleUstaw('r_faktura_nazwisko',false);
			show('starImie');
			show('starNazwisko');

			poleUstaw('r_faktura_firma',true);
			poleUstaw('r_faktura_nip',true);
			hide('starFirma');
			hide('starNip');
		break;
		case 1:
			poleUstaw('r_faktura_imie',true);
			poleUstaw('r_faktura_nazwisko',true);
			hide('starImie');
			hide('starNazwisko');

			poleUstaw('r_faktura_firma',false);
			poleUstaw('r_faktura_nip',false);
			show('starFirma');
			show('starNip');
		break;
	}
}

function searchValid(){
	var input=_e('search_fraza');
	
		if(input.value.length<3) {
			showMessage('Musisz wprowadziÄ? co najmniej 3 znaki');
			return false;
		}
		return true;
}



function loginCheckFunction(){
	var e = _e('noaccount');
	if(e.checked){
		document.location.href="rejestracja.php";
		return false;
	}else
		return true;
}
						
function newsletterUstaw()
{
	var bp = _e('blok-przelacz-news');

	if(bp)
	{
		var label = bp.getElementsByTagName('label');
		var licznik = 0;

		if(label)
		{
			for(var i=0; i<label.length; i++)
			{
				var kat = _e('k_'+i);
				if(kat && kat.checked)
				{
					licznik++;
				}
			}

			_e('newsletter_wszystko').checked = (i>0 && (i-1) == licznik)? 'checked' : '';
		}
	}
}

function newsletterZaznaczWszystko()
{
	var e, bp = _e('blok-przelacz-news');
	var label = bp.getElementsByTagName('label');
	if(label) {
		for(var i=0; i<label.length; i++) {
			if(e = _e('k_'+i))
				e.checked = (_e('newsletter_wszystko').checked)? 'checked' : '';
		}
	}
}


function faqToggleQuestion(id){
	$('#'+id).slideToggle("slow");
}


function blokPrzelacz(id)
{
	var bpp  = _e('bpp'+id);
	var bpu  = _e('bpu'+id);
	var bp  = _e('blok-przelacz-'+id);

	if((bpp == null) || (bpu == null))
		return;

	if(bpu.style.display == "none") //ukryj(bpu) - pokazany, pokaz(bpp) - ukryty
	{
		bpu.style.display = "";
		bpp.style.display = "none";
		bp.style.display = "";
	}
	else
	{
		bpu.style.display = "none";
		bpp.style.display = "";
		bp.style.display = "none";
	}
}

/* Wykorzystane przy przekierowaniu do platnosci.pl */
var aktywuj = false;
var gotowy = false;

function odliczaj()
{
	var button = _e('butt');

	if(aktywuj)
	{
		setTimeout('odliczaj()', 5000);
		aktywuj = true;
	}
	else
	{
		button.style.display = '';
		if(button.style.display != 'none')
		{
			if(gotowy)
			{
				_e('platnosci').submit();
			}
			else
			{
				setTimeout('odliczaj()', 3000);
				gotowy = true;
			}
		}
	}
}

function validateEmail(email) {
	var reg=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
	return reg.test(email);
}

function newsletter_confirm(){
	var params= new Array();
	var email = _e('newsletter_email');
	if (validateEmail(email.value)){
		params['okFunction'] = "newsletter_zapisz('add')";
		LightBox.showFromTemplate('KomunikatNewsletterPotwierdzenie',params);
	}else{
		params['message'] = "WprowadÅº poprawnie adres email!";
		LightBox.showFromTemplate('KomunikatBlad',params);
	}
}

function przypomnienie_wyslij(){
/*
	var form_data= harvestData('przypomnienieForm');
	form_data=form_data+'&action='+action;
	ajax.callMethod('NewsletterZapiszKomponent','zapisz', null,form_data, null, function(http,bb) {
	
		var root = http.getResponseJSON(showErrorMessage);
		if(!root) return;
		var e =_e('newsletter_email');
		e.value='';
		LightBox.show(root['!html']);
	}.bindResponse(this));*/
}


function przypomnienieLayer(){
	var params= new Array();
	params['okFunction'] = "przypomnienie_wyslij()";
	LightBox.showFromTemplate('KomunikatPrzypomnienieHasla',params);
}

function newsletter_zapisz(action)
{

	var form_data= harvestData('newsletter_lista_zapis');
	form_data=form_data+'&action='+action;
	ajax.callMethod('NewsletterZapiszKomponent','zapisz', null,form_data, null, function(http,bb) {
		var root = http.getResponseJSON(showErrorMessage);
		if(!root) return;
		var e =_e('newsletter_email');
		e.value='';
		LightBox.show(root['!html']);
	}.bindResponse(this));
}

function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}

function showErrorMessage(msg,redirect) {
	showMessage(msg,redirect,'error');
}

function breakeString(str, limit){
	var str_arr=str.split(' ');
	var output = new Array();
	var i=0;
	output[i]='';
	for(var c=0; c<str_arr.length; c++){
		if(output[i].length +str_arr[c].length<limit){
			output[i]=$output[i]+str_arr[i];
		}else {
			i++;
			output[i]=str_arr[i];
		}
	}
	return output.join('<br />');
}
/*
public static function breakString($str, $rowLimit){
			$str_arr=explode(' ',$str);
			$output=array();
			$i=0;
			$output[$i]='';
			foreach($str_arr as $key =>  $value){
				
				if((strlen($output[$i])+ strlen($value))<$rowLimit || strlen($value)>$rowLimit){
					$output[$i].=' '.$value;
				}else{
					$i++;
					$output[$i].=' '.$value;
				}
			}
			return implode('<br />', $output);
		}
*/
function showMessage(msg, redirect, type) {

	var template='KomunikatInformacja';
	if(type && type=='error') template='KomunikatBlad';
	
	if(LightBox){
		var paramsPost = 'template=KomunikatInformacja&content='+encodeURIComponent(msg)+'&redirect='+encodeURIComponent(redirect);

	ajax.callMethod('KomunikatKomponent','getMessageBody',paramsPost,paramsPost,null,
		function(http,bb) {		
			var root = http.getResponseJSON(alert);

			if (root) {
				showMessageCallback(root);
			}	
		}.bindResponse(this));
	}else{
		alert(msg);
	}
}

function showMessageCallback(root){

	if(root && root['html']){
		LightBox.show(root['html']);
	}
}

function showConfirmMessage(msg,yeshandler,nohandler) {
	var box = _e('showMessageBox'), content = _e('showMessageContent');

	if(box && content) {
		hide("msg-ok-btn");
		show("msg-confirm-btn");
		show(box);

		centerElement('showMessageBox');
		box.style.zIndex = '50';
		content.innerHTML = msg;
		_e('cm-tak-btn').onclick = function() {
			if(yeshandler) yeshandler();
			hide(_e('showMessageBox'));
		};
		_e('cm-nie-btn').onclick = function() {
			if(nohandler) nohandler();
			hide(_e('showMessageBox'));
		};
	} else {
		alert(msg);
	}
}

function idz(url) {
	window.location = url;
}


//podmiana obrazkÃ³w dla odpowiednich przyciskÃ³w
function ustawPrzyciskiOver() {
	this.src = this.src.replace('.jpg', '_zap.jpg');
	this.src = this.src.replace('.gif', '_zap.gif');
}
function ustawPrzyciskiOut() {
	this.src = this.src.replace('_zap.jpg', '.jpg');
	this.src = this.src.replace('_zap.gif', '.gif');
}
function ustawPrzyciski(root) {
	var a, i, n, regexp = /\?zap$/;
	if (!root) {
		root = document;
	}
	a = root.getElementsByTagName('input');
	for (i = 0, n = a.length; i < n; i += 1) {
		if (a[i].src.match(regexp)) {
			addEvent(a[i], 'mouseover', ustawPrzyciskiOver.bind(a[i]));
			addEvent(a[i], 'mouseout', ustawPrzyciskiOut.bind(a[i]));
			a[i].src = a[i].src.replace(regexp, '');
		}
	}
	a = root.getElementsByTagName('img');
	for (i = 0, n = a.length; i < n; i += 1) {
		if (a[i].src.match(regexp)) {
			addEvent(a[i], 'mouseover', ustawPrzyciskiOver.bind(a[i]));
			addEvent(a[i], 'mouseout', ustawPrzyciskiOut.bind(a[i]));
			a[i].src = a[i].src.replace(regexp, '');
		}
	}
}

function polecZnajomemu(pid){
		var params=new Array();
		params['okFunction'] = "polecZnajomenu_wyslij("+pid+")";
		LightBox.showFromTemplate('KomunikatPolecForm',params);
}

function polecZnajomenu_wyslij(pid){
	var name=_e('polecUsername');
	var notice=_e('polecNotice');
	var email=_e('polecEmail');

	var form_data= harvestData('polecForm');
	form_data=form_data+'&pid='+pid;
	if(!validateEmail(email.value)) { showErrorMessage('Musisz podaÄ? adres e-mail adresata wiadomoÅ?ci'); return;}
	ajax.callMethod('PowiadomKomponent','wyslij', null,form_data, null, function(http,bb) {
		var root = http.getResponseJSON(showErrorMessage);
		if(!root) return;
		if ('error' == root['html']) {
			showMessage(root['html']);
		} else {
			showMessage(root['html']);
		}
	}.bindResponse(this));
}

function IsNumeric(inputVal) {
     if (isNaN(parseFloat(inputVal))) {
          return false;
     }
     return true
}


function ustawCzcionke(percent, id)
{
	var contener=document.getElementById(id);
	if(contener){

		var types=new Array('p', 'h1', 'h2', 'h3', 'span','li','div');
		var ntypes=new Array('i','b','strong','u','a','big','br','center','em','small');
		var all=new Array();
		var tmp=contener.getElementsByTagName('*');
		
		if(contener.style){
			if(! contener.defstyle){
				contener.defstyle=new Array()
				var size=_style(contener).fontSize.replace("px","");
				if(IsNumeric(size))
					contener.defstyle['fontSize']= parseInt(size);
				else contener.defstyle['fontSize']= size;
				//contener.defstyle['fontSize']= parseInt(_style( contener).fontSize.replace("px",""));
				contener.defstyle['lineHeight']= parseInt(_style( contener).lineHeight.replace("px",""));
			
			}
		}
		
			for(i=0;i<tmp.length;i++) {
				if(tmp[i].style && !tmp[i].defstyle){
						tmp[i].defstyle=new Array()
						var size=_style(tmp[i]).fontSize.replace("px","");
						if(IsNumeric(size))
							tmp[i].defstyle['fontSize']= parseInt(size);
						else tmp[i].defstyle['fontSize']= size;
						tmp[i].defstyle['lineHeight']= parseInt(_style(tmp[i]).lineHeight.replace("px",""));
				}
			}
			
			for(i=0;i<tmp.length;i++) {
				if (tmp[i].defstyle){
					if(tmp[i].className!='noresize' && types.indexOf(tmp[i].tagName.toLowerCase())!=-1){
						tmp[i].style.lineHeight = Math.floor(tmp[i].defstyle['lineHeight']*percent/100)+'px';
						if(IsNumeric(tmp[i].defstyle['fontSize'])) 
							tmp[i].style.fontSize = Math.floor(tmp[i].defstyle['fontSize']*percent/100)+"px"; 
						else{
							var size=getRelativSize(tmp[i].defstyle['fontSize'],percent);
							tmp[i].style.fontSize=size;
						}
					}else{
						if(ntypes.indexOf(tmp[i].tagName.toLowerCase())==-1){
							tmp[i].style.lineHeight = tmp[i].defstyle['lineHeight']+'px';
							if(IsNumeric(tmp[i].defstyle['fontSize'])) tmp[i].style.fontSize = tmp[i].defstyle['fontSize']+"px";
							else{
								//'var size=getRelativSize(tmp[i].defstyle['fontSize'],100);
								tmp[i].style.fontSize=tmp[i].defstyle['fontSize'];
							}
						}
					}
				}
			}
			
		if(contener.style ){
		
			if(contener.className!='noresize'){
				contener.style.lineHeight = Math.floor(contener.defstyle['lineHeight']*percent/100)+'px';
				if(IsNumeric(contener.defstyle['fontSize']))contener.style.fontSize = Math.floor(contener.defstyle['fontSize']*percent/100)+"px"
			}else{
				contener.style.lineHeight = contener.defstyle['lineHeight']+'px';
				if(IsNumeric(contener.defstyle['fontSize']))contener.style.fontSize = contener.defstyle['fontSize']+"px";
				else contener.style.fontSize=getRelativSize(contener.defstyle['fontSize']);
			}
		}
	}
}


function getRelativSize(size,percent){
	//return percent+'%';

	if(percent==100) return size;
	else if (percent>=125 && percent<150){
		switch(size){
			case 'xx-small' : return 'x-small'; break; 
			case 'x-small' : return 'small'; break;
			case 'small' : return 'medium'; break;
			case 'medium': return 'large'; break;
			case 'large':  return 'x-large'; break;
			case 'x-large': return 'xx-large'; break;
			case 'xx-large': return 'xx-large'; break;
		}
	}else if (percent>=150){
		switch(size){
			case 'xx-small' : return 'small'; break; 
			case 'x-small' : return 'medium'; break;
			case 'small' : return 'large'; break;
			case 'medium': return 'x-large'; break;
			case 'large':  return 'xx-large'; break;
			case 'x-large': return 'xx-large'; break;
			case 'xx-large': return 'xx-large'; break;
		}
	}
	
	return size;
}
/*
function ustawCzcionke2(percent, id){
	var container =$('#'+id);

	if(container){
		container.contents().each(
		function(index){
			$(this).css('font-size',$(this).css('font-size'));
			
		});
		container.contents().not('.noresize').each(
		function(index){
			$(this).css('font-size',$(this).css('font-size'));
			
		});
		//elements=container.children(':not(.noresize)');
		alert('dddd');
	}
}
*/

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		var relvalue = anchor.getAttribute("rel");

		if (anchor.getAttribute("href")) {
			var external = /external/;
			var relvalue = anchor.getAttribute("rel");
			if (external.test(relvalue)) { 
				anchor.target = "_blank"; }
		}
	}
}
window.onload = externalLinks; 

$(document).ready(function() {  

	$(".toggle").click(function(){
			var nextp = $(this).next("div");
			nextp.slideToggle("slow");
			})
			.toggle(
				function () {
					var nextp = $(this).next("div");},
				function () {
					var nextp = $(this).next("div");
				});
});


function getPromoProducts(statusId, page){

	if(page=='prev'){
		var  prev=$('.PromoBox'+statusId+' .kantChange.active').prev('.kantChange');
		if(prev.length>0){
			$('.PromoBox'+statusId+' .kantChange').removeClass('active');
			prev.addClass('active');
			
			if(prev.prev('.kantChange').length>0){
				$('#naviPrev'+statusId).removeClass('disable');
			}else{
				$('#naviPrev'+statusId).addClass('disable');
			}
			if(prev.next('.kantChange').length>0){
				$('#naviNext'+statusId).removeClass('disable');
			}else{
				$('#naviNext'+statusId).addClass('disable');
			}
		}else return;
	}else if(page=='next'){
		var  next=$('.PromoBox'+statusId+' .kantChange.active').next('.kantChange');
		
		if(next.length>0){
			$('.PromoBox'+statusId+' .kantChange').removeClass('active');
			next.addClass('active');
			
			if(next.next('.kantChange').length>0){
				$('#naviNext'+statusId).removeClass('disable');
			}else{
				$('#naviNext'+statusId).addClass('disable');
			}
			
			if(next.prev('.kantChange').length>0){
				$('#naviPrev'+statusId).removeClass('disable');
			}else{
				$('#naviPrev'+statusId).addClass('disable');
			}
		}else return;
			
		
	}else{
		if($('.PromoBox'+statusId+' #pager'+statusId+'_'+page).hasClass('active')) return;
		
		$('.PromoBox'+statusId+' .kantChange').removeClass('active');
		$('.PromoBox'+statusId+' #pager'+statusId+'_'+page).addClass('active');
		if(page>1){
			$('#naviPrev'+statusId).removeClass('disable');
		}else{
			$('#naviPrev'+statusId).addClass('disable');
		}
		if($('.PromoBox'+statusId+' #pager'+statusId+'_'+page).next('.kantChange').length>0){
			$('#naviNext'+statusId).removeClass('disable');
		}else{
			$('#naviNext'+statusId).addClass('disable');
		}
		
	}
	
	var paramsPost = 'statusId='+statusId+'&page='+page;

	ajax.callMethod('Index','getPromoProducts',paramsPost,paramsPost,null,
		function(http,bb) {		
			var root = http.getResponseJSON(alert);

			if (root) {
				getPromoProductsCallback(root);
			}	
	}.bindResponse(this));
}

function getPromoProductsCallback(data){
	if(data['reload']){
		var content=$('#inBoxContent'+data['statusId']);
		var inbox=$('#inBox'+data['statusId']);
		$('#inBoxLoader'+data['statusId']).css('margin-left', (inbox.width()/2-16)+'px').css('margin-top', (inbox.height()/2-16)+'px').show();
		content.fadeOut("slow",
			function(){
				content.html(data['html']);
			}
		);
	}
}

