function mouse_position(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY)   {
    posx = e.pageX;
    posy = e.pageY;
  }
  else if (e.clientX || e.clientY)   {
    posx = e.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
  }
  return { top:posy, left:posx };
}

var menu_animation = false;

var logosAnimationDir = 'right';
function logos_animate() {
	logosAnimationDir = ( logosAnimationDir == 'right' ? 'left' : 'right' );
	var newMarginLeft = ( logosAnimationDir == 'right' ? 50 : -2050 );
	$("#logos_container").animate({marginLeft:newMarginLeft+'px'},50000,function(){ // 30000
		logos_animate();
	});
}

function numOnly(e) {
  return true;
  var charcode = (e.which) ? e.which : event.keyCode;
  return ( charcode >= 48 && charcode <=57 );
}

function szt2jm(obj,jmszt) {
  var szt = parseInt(obj.value);
  if( isNaN(szt) ) {
    obj.value = '';
    $('#ilosc').val('');
  } else {
    $('#ilosc').val(szt*jmszt);
  }
}

$(function(){
	
	if( $('#logos_container').length ) {
		logos_animate();
	}
	
	if( $('.first_row').length ) {
		var height_max = 200;
		$('.first_row').each(function(){
			var wys = $(this).outerHeight();
			if( wys > height_max ) height_max = wys;
		});
		$('.first_row').css({height:height_max+'px'});
	}
	
	if( $('.second_row').length ) {
		var height_max = 200;
		$('.second_row').each(function(){
			var wys = $(this).outerHeight();
			if( wys > height_max ) height_max = wys;
		});
		$('.second_row').css({height:height_max+'px'});
	}
	
	$('a.active').parent().parent().parent().show();
	
	if( menu_animation ) {
		$("a[id^='cat']").click(function(){
			var cat_id = $(this).attr('id').match(/[0-9]+/);
			if( $('#sub_'+cat_id).length == 1 ) {
				if( $('#sub_'+cat_id).is(':hidden') ) {
					$("div[id^='sub_']").slideUp('slow');
					$('#sub_'+cat_id).slideDown('slow');
				}
				return false;
			}
		});
	}
	
    $('.help').each(function(index){
    	var help_text = $(this).attr('title');
    	$(this).attr('title','');
    	$(this).mousemove(function(event){
    		pos = mouse_position(event);
    		$('#help').html(help_text).css({
    			top: pos.top + 10 + 'px',
    			left: pos.left + 10 + 'px'
    		}).show();
    	}).mouseout(function(){
    		$('#help').html('').hide();  
    	});
    });
    
    $(".product_frame").each(function(){
    	var id_arr = $(this).attr('id').split('_');
    	var curr_id = id_arr[1];
    	$(this).mousemove(function(event){
    		pos = mouse_position(event);
    		//var ww = $(window).width();
    		var pt = pos.top;
    		var pl = pos.left;
    		//var nt = pt - 10;
    		//var nl = ( (pl - 422) < 0 ? pl + 30 : pl - 422 );
    		var nt = pt + 30;
    		var nl = pl - 185;
    		$('#opis_'+curr_id).css({
    			top: nt + 'px',
    			left: nl + 'px'
    		}).show();
    	}).mouseout(function(){
    		$('#opis_'+curr_id).hide();  
    	});
    });

    //$("a[href$='jpg'],a[href$='JPG'],a[href$='gif'],a[href$='GIF']").colorbox();
    $("a[rel='colorbox']").colorbox();
    
});

function switchView(viewType) {
	$("#view_type").val(viewType);
	$("#view_form").submit();
}

function switchLang(language) {
	$("#lang").val(language);
	$("#lang_form").submit();
	return false;
}
