function start() {
	//start
	
	if(jskomShow&&$('komunikat')) {
		jskomAppear();
		$('komunikat').observe('click',jskomFadeOut);
	}
	
	
	
	allBannersSmall = $$('[id^=bannersmall_]');
	if(allBannersSmall.length>0) bannerExecuterSmall=new PeriodicalExecuter(bannerRightSmall, 6);

	allPolecane = $$('[id^=produkty_item_]');
	if(allPolecane.length>0) setPolecane();
}
window.onload = start;


var allPolecane = [];
var actPolecane = 1;
function setPolecane() {
	for(var i=1;i<=5;i++) {
		if(i==actPolecane) {
			if(!$('produkty_item_'+i).hasClassName('active')) $('produkty_item_'+i).addClassName('active');
			$('produkty_item_'+i).stopObserving('click',polecaneClick);
			$('produkty_foto_'+i).setStyle({cursor:'auto'});
		}
		else {
			if($('produkty_item_'+i).hasClassName('active')) $('produkty_item_'+i).removeClassName('active');
			$('produkty_item_'+i).observe('click',polecaneClick);
			$('produkty_foto_'+i).setStyle({cursor:'pointer'});
		}
	}
}
function polecaneClick(evt) {
	var nr = evt.target.getAttribute('nr');
	if(nr==actPolecane) return;
	actPolecane = nr;
	setPolecane();
}

function headerOn() {
	bannerExecuterSmall.stop();
	$('bannersmall').hide();
	$('banner').show();
	
	allBanners = $$('[id^=banner_]');
	if(allBanners.length>0) bannerExecuter=new PeriodicalExecuter(bannerRight, 6);
	
	bannerOpt = 2;
}
function headerOff() {
	bannerExecuter.stop();
	$('banner').hide();
	$('bannersmall').show();
	
	allBannersSmall = $$('[id^=bannersmall_]');
	if(allBannersSmall.length>0) bannerExecuterSmall=new PeriodicalExecuter(bannerRightSmall, 6);
	
	bannerOpt = 1;
}

var bannerExecuter;
var actBanner = 1;
var nextBanner = 1;
var bannerChangeGoing = false;
var allBanners = [];
var bannerOpt = 1;//1 - mały, 2 - duży
function bannerLeft() {
	if(bannerChangeGoing) return;
	nextBanner = actBanner-1;
	if(nextBanner==0) nextBanner = allBanners.length;
	changeBanner();
}
function bannerRight() {
	if(bannerChangeGoing) return;
	nextBanner = actBanner+1;
	if(nextBanner==allBanners.length+1) nextBanner = 1;
	changeBanner();
}
function changeBanner() {
	bannerChangeGoing = true;
	bannerExecuter.stop();
	allBanners[actBanner-1].fade({ duration: 1, afterFinish: changeBannerFinish });
	allBanners[nextBanner-1].appear({ duration: 1 });
}
function changeBannerFinish() {
	actBanner = nextBanner;
	bannerChangeGoing = false;
	bannerExecuter=new PeriodicalExecuter(bannerRight, 6);
}



var bannerExecuterSmall;
var actBannerSmall = 1;
var nextBannerSmall = 1;
var bannerChangeGoingSmall = false;
var allBannersSmall = [];
function bannerLeftSmall() {
	if(bannerChangeGoingSmall) return;
	nextBannerSmall = actBannerSmall-1;
	if(nextBannerSmall==0) nextBannerSmall = allBannersSmall.length;
	changeBannerSmall();
}
function bannerRightSmall() {
	if(bannerChangeGoingSmall) return;
	nextBannerSmall = actBannerSmall+1;
	if(nextBannerSmall==allBannersSmall.length+1) nextBannerSmall = 1;
	changeBannerSmall();
}
function changeBannerSmall() {
	bannerChangeGoingSmall = true;
	bannerExecuterSmall.stop();
	allBannersSmall[actBannerSmall-1].fade({ duration: 1, afterFinish: changeBannerFinishSmall });
	allBannersSmall[nextBannerSmall-1].appear({ duration: 1 });
}
function changeBannerFinishSmall() {
	actBannerSmall = nextBannerSmall;
	bannerChangeGoingSmall = false;
	bannerExecuterSmall=new PeriodicalExecuter(bannerRightSmall, 6);
}



function textMaxLen(inputId,counterId,maxlimit) {
	if($(inputId).value.length > maxlimit) $(inputId).value = $(inputId).value.substring(0, maxlimit);
	$(counterId).update(maxlimit - $(inputId).value.length);
}

function setText(oTextInput) {
	if(oTextInput.value=='') oTextInput.value = oTextInput.defaultValue;
}
function clearText(oTextInput) {
	if(oTextInput.value==oTextInput.defaultValue) oTextInput.value = '';
}

function isEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return(reg.test(address));
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function selectedRadioValue(oForm, sRadioName) {
	var retval = '';
	for(var i=0;i<oForm.elements.length;i++) {
		if(oForm.elements[i].type!='radio'||oForm.elements[i].name!=sRadioName) continue;
		if(oForm.elements[i].checked) retval = oForm.elements[i].value;
	}
	return retval;
}

function showKom(kom,kom_status) {
	if(kom_status=='error') {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_error.png';
	}
	else if(kom_status=='info') {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_info.png';
	}
	else {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_ok.png';
	}
	$('kom_text').update(kom);
	jskomAppear();
}
var jskomShow=false;
var jskomZostaw=true;
var peJskom;
function jskomAppear() {
	try{
		//Effect.Appear('jskom');
		$('komunikat').show();
	}
	catch (exc) {
		$('komunikat').show();
	}
	if(!jskomZostaw) peJskom = new PeriodicalExecuter(function(peJskom) {
			jskomFadeOut();
		}, 4);
}
function jskomFadeOut() {
	try{
		Effect.Fade('komunikat');
		}
	catch (exc) {
		$('komunikat').hide();
	}
	//$('komunikat').update('');
	if(peJskom) peJskom.stop();
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function basename (path, suffix) {
    var b = path.replace(/^.*[\/\\]/g, '');
    
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    
    return b;
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack + '').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}

function capitaliseFirstLetter(str)
{
	var ret = '';
	var temp = '';
	var strExp = str.split('.');
	for(var i=0;i<strExp.length;i++) {
		temp = ltrim(strExp[i]);
		ret += (i>0?'. ':'')+temp.charAt(0).toUpperCase() + temp.slice(1);
	}
    return ret;
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function viewportDims() {
	var viewportwidth;
	var viewportheight;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
 	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'&& typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return {width:viewportwidth,height:viewportheight};
}

function postToURL(url, values)
{
    values = values || {};

    var form = new Element("form", {action: url,
                                      method: "POST",
                                      style: "display: none"});
    for (var property in values)
    {
        if (values.hasOwnProperty(property))
        {
            var value = values[property];
            if (value instanceof Array)
            {
                for (var i = 0, l = value.length; i < l; i++)
                {
                    form.insert(new Element("input", {type: "hidden",
                                                             name: property,
                                                             value: value[i]}));
                }
            }
            else
            {
                form.insert(new Element("input", {type: "hidden",
                                                         name: property,
                                                         value: value}));
            }
        }
    }
    document.body.insert(form);
    form.submit();
    form.remove();
	return false;
}

function test(text) {
	window.console.log(text);
}

var img;
var galFotos = [];
var aktFoto = 0;
function setGal(fot,fotos) {
	if(typeof fotos=='object') galFotos = fotos;
	else galFotos = [];
	$('bigFotoContainer').show();
	aktFoto = 0;
	if(galFotos.length>0) {
		for(var i=0;i<galFotos.length;i++) {
			if(galFotos[i]==fot) aktFoto = i;
		}
	}
	pokazFotoGal(fot);
}
function pokazFotoGal(fot) {
	$('bigFotoImg').update('<img src="images/imgloading.gif" border="0" />')
	img = new Element('img');
	img.id = 'theimage';
	img.observe('load', showimg);
	//document.body.appendChild(img);
	img.src = fot;
	//$('bigFotoImg').update('<img src="'+fot+'" border="0" />';
	$('bigFotoImg').show();
	fotoNav();
	//$('main').hide();	
}
function pokazFoto(fot) {
	galFotos = [];
	$('bigFotoImg').update('<img src="images/imgloading.gif" border="0" />')
	img = new Element('img');
	img.id = 'theimage';
	img.observe('load', showimg);
	//document.body.appendChild(img);
	img.src = fot;
	//$('bigFotoImg').update('<img src="'+fot+'" border="0" />';
	$('bigFotoImg').show();
	$('bigFotoContainer').show();
	//$('main').hide();
	aktFoto = 0;
	fotoNav();
}
function showimg(evt) {
	$('bigFotoImg').hide();
	$('bigFotoImg').update(img);
	$('bigFotoImg').show();
	var dim = document.viewport.getDimensions();
	if(img.width>dim.width||img.height>dim.height-160) {
		var new_size = image_small_maker(img.width, img.height, dim.width, dim.height-160)
		img.setStyle({'width':new_size[0]+'px','height':new_size[1]+'px'});
	}
}
function wywalFoto() {
	$('bigFotoContainer').hide();
	$('bigFotoImg').hide();
	//$('main').show();
}
function fotoNav() {
	if($('bigFotoPrev')) {
		if(aktFoto>0) $('bigFotoPrev').show();
		else $('bigFotoPrev').hide();
	}
	if($('bigFotoNext')) {
		if(aktFoto<galFotos.length-1) $('bigFotoNext').show();
		else $('bigFotoNext').hide();
	}
}
function prevFoto() {
	if(aktFoto<=0) return;
	aktFoto--;
	pokazFotoGal(galFotos[aktFoto]);
}
function nextFoto() {
	if(aktFoto>galFotos.length-1) return;
	aktFoto++;
	pokazFotoGal(galFotos[aktFoto]);
}
function image_small_maker(orig_width, orig_height, max_width, max_height) {
	var xRatio = max_width / orig_width; 
	var yRatio = max_height / orig_height; 
	var new_width = 0;
	var new_height = 0;
	if ( (orig_width <= max_width) && (orig_height <= max_height) ) { 
	 new_width = orig_width; 
	 new_height = orig_height; 
	} 
	else if ((xRatio * orig_height) < max_height) { 
	 new_height = Math.ceil(xRatio * orig_height); 
	 new_width = max_width; 
	} 
	else { 
		new_width = Math.ceil(yRatio * orig_width); 
		new_height = max_height; 
		}
	var output = [];
	output.push(Math.round(new_width));
	output.push(Math.round(new_height));
	return output;
}
