function menuRedirect( selObj, restore ) {
	eval( "document.location='" + selObj.options[ selObj.selectedIndex ].value + "'" );
	if( restore )
		selObj.selectedIndex = 0;
}

function printPage() {
	var tPrintLink = 'index.php?' + printLink + '&_view=print';
	window.open( tPrintLink, 'print', 'scrollbars=yes,status=no,width=640,height=600' );
}

function getCurrentDate() {
	var date = ''; var today = new Date();
	date += today.getFullYear();
	if( today.getMonth()+1 < 10 ) {
		date += '0';
	}
	date += today.getMonth()+1;
	if( today.getDate() < 10 ) {
		date += '0';
	}
	date += today.getDate();
	return parseInt(date);
}

function formatAsMoney(amount) {
	if( amount == 0 ) {
		return amount;
	} else if( amount != null ) {
		amount = amount.formatMoney(2, ',', ' ');
		amount+= '';
	    amount = amount.replace(/^0+/, ''); 
		x = amount.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ' ' + '$2');
		}
		if(!x1>0) {
			x1='0';
		}
		
		
		return x1 + x2;
	}
}

function roundNumber(rnum, rlength) {
  return Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
}

Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};


function updateProvinces( regionId, provinceId ) {
	$.post( 'index.php', {
		page: 'ajax',
		get: 'poviatList',
		regionId: $(regionId).val()
	},
	function( data ) {
		$('#'+provinceId).html( $(data).find('response:first').text() );
	});
}

var subProdMenuActiveId = null;
var subProdMenuTimeoutFunc = null;

function showSubProductsMenu(mid) {
	clearTimeout(subProdMenuTimeoutFunc);
	$("ul.subMenuv20").hide();
	$(mid).show();
	
	$("#subMenuv10 a").removeClass("on");
	
	$("#"+mid.substring(2)).addClass("on");
}
function hideSubProductsMenu(mid) {
	subProdMenuTimeoutFunc = setTimeout(function(){
		
		$(mid).hide();
		
		$("#subMenuv10 a").removeClass("on");
		
		if( subProdMenuActiveId != null ) {
			
			$("#"+subProdMenuActiveId.substring(2)).addClass("on");
			$(subProdMenuActiveId).show();
		}
	}, 300);
}

//cookie

function createCookie( name, value, days ) {
	if( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = "; expires=" + date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for( var i=0; i < ca.length; i++ ) {
		var c = ca[i];
		while( c.charAt(0) == ' ' ) {
			c = c.substring( 1, c.length );
		}
		if( c.indexOf( nameEQ ) == 0 ) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}
function eraseCookie( name ) {
	createCookie( name, "", -1 );
}

function appendObservers(){
	/** logowanie - redirecty */
	$('#logowanieShortcutMenu').submit(function(){
		$('#logowanieShortcutMenuSelect').trigger("change");
		return false;
	});
	$('#logowanieShortcutMenuSelect').change(function(){
		window.location.href = $(this).val();
	}).keyup(function(){
		$(this).trigger("change");
	});
	/** skroty */
	$('#HomeSkroty ul li a#ico2,#HomeSkroty ul li a#ico6').parent().addClass('x');
	
	/** kotwice i linki */
	$('a[href^=#]').each(function(i, obj){
		var baseLocation = document.location.href;
		if (document.location.hash.length) {
			baseLocation = baseLocation.substr(0, baseLocation.indexOf('#'));
		}
		$(this).attr('href', baseLocation + $(this).attr('href'));
	});

	if( $('#skala') ) {
		$('#skala').find('a').each( function( i, obj ) {
			$(obj).click( function() {
				var fontSize = $(this).attr('class').substr(1,2)+'px';
				createCookie( 'fontSize', $(this).attr('class'), 30 );
				$('#NewsContent').css( 'font-size', fontSize );
				$(this).parent().children().each( function( j, obj2 ) {
					$(obj2).removeClass('on');
				});
				$(this).addClass('on');
			});
		});
	}
}
function prepareLightbox() {
	$('a[rel=lightbox]').lightBox({
		overlayBgColor: '#FFF',
		overlayOpacity: 0.6,
		imageBlank: imgDir + 'lightbox/lightbox-blank.gif',
		imageLoading: imgDir + 'lightbox/lightbox-ico-loading.gif',
		imageBtnClose: imgDir + 'lightbox/lightbox-btn-close.gif',
		imageBtnPrev: imgDir + 'lightbox/lightbox-btn-prev.gif',
		imageBtnNext: imgDir + 'lightbox/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: '',
		txtOf: '/'
	});
}

$(document).ready( function() {
	appendObservers();
	prepareLightbox();
	var fontSize = readCookie('fontSize');
	if( fontSize ) {
		$('#skala').find('a.'+fontSize).click();
	}

	$('#HomeGroups .productBox .tabMenu li a').click(function(){
		$('#HomeGroups .productBox .tabMenu li a.on').removeClass('on');
		$(this).addClass('on');
		var no = $(this).attr('id').split('-')[1];
		$('#HomeGroups .productBox .pBoxContent > div.pBox:visible').hide();
		$('#HomeGroups .productBox .pBoxContent > div.pBox:eq('+no+')').show();
		return false;
	});
	
	$('#HomeGroups .productBox .pBoxContent div.pBox a').live('click', function(){
		var href = $(this).attr('href').split('#')[1];
		if(href.match("^art")) {
			$('#HomeGroups .productBox .pBox-loading').show();
			var artId = href.substring(3);
			$.ajax({
				url: 'ajax?get=article&id='+artId,
				success: function(data){
					$('#HomeGroups .productBox .pBoxContent > div.pBox:visible').hide();
					$('#HomeGroups .productBox .pBoxContent > div.pBox:last').html($(data).find('response').text()).show();
					$('#HomeGroups .productBox .pBox-loading').hide();
				}
			});

			return false;
		}
	});	
});
