var pelMarginOff = '-450px';
var pelMarginOn  = '265px';
var defaultDelay = 400;

$.fn.animateByOrder = function(params, duration, easing, callback){
	var i = -1;
	var arr = this;
	if($.isFunction(easing)) {
		callback = easing;
		easing = null;
	}
	(function(){
		if(arr[++i])
			$(arr[i]).animate(params, duration, easing, arguments.callee)
		else
			if(callback)
				callback();
	})();
}


// sottomenų tipologie
var tipologie = new Array();
	
$(document).ready( function() {
	$.scrollTo(0);
	
	// menu
	$('#menu li').hover( function() {
		if ($('div', this).length > 0) {
			openMenu(this);
		}
	}, function() {
		cancelOpenMenu();
	});
	
	// partono tutti spenti
	$('#tipologie li').each( function() {
		tipologie[$(this).attr('id').split('_')[1]] = false;
	});
	
	$('#tipologie li').click( function() {
		
		var clicked = $(this).attr('id').split('_')[1];
		var to_post = new Array();
		var i = 0;
		
		tipologie[clicked] = !tipologie[clicked];
		//for (t in tipologie) if (tipologie[t] == true) i++;
		//if (i > 0) {
		$(this).css( { backgroundImage: 'url(../img/tipologie_bg_'+Number(tipologie[clicked])+'.gif)' } );;
		for (t in tipologie) if (tipologie[t]) to_post.push(t);
		
		$('#content').fadeOut('fast', function() {
			$.scrollTo(0);
			$('#content').empty().show();
			$('.p_elm').css({marginLeft: pelMarginOff});
			$('#content').load('ajax.php', {'q': to_post.join(','), 'pag': 1}, function() {
				applyOpenPel();
				$('.p_elm').animateByOrder( { marginLeft: pelMarginOn }, 'fast');
			});
		});
		
		
		//} else {
		//	tipologie[clicked] = !tipologie[clicked];
		//}
	});
	
	applyOpenPel();
	
	
  $('.p_elm').animateByOrder( { marginLeft: pelMarginOn }, 'fast' );
  
  
});

function paginate(pag) {
	var to_post = new Array();
	for (t in tipologie) if (tipologie[t]) to_post.push(t);
	
	$('#content').fadeOut('fast', function() {
		$.scrollTo(0);
		$('#content').empty().show();
		$('.p_elm').css({marginLeft: pelMarginOff});
		$('#content').load('ajax.php', {'q': to_post.join(','), 'pag': pag}, function() {
			applyOpenPel();
			$('.p_elm').animateByOrder( { marginLeft: pelMarginOn }, 'fast');
		});
	});
}

function openMenu(jObj) {
	openMenuTimeout = setTimeout( function() { openMenuDelayed(jObj); }, defaultDelay);
}

function openMenuDelayed(jObj) {
	var objId = $('div', jObj).attr('id');
	$('#menu div').not('#'+ objId).slideUp();
	$('div', jObj).slideDown();
}

function cancelOpenMenu() {
	clearTimeout(openMenuTimeout);
}

function applyOpenPel() {
	
	$('.p_elm_img a').fancybox({'overlayShow': true, 'zoomSpeedIn':0, 'zoomSpeedOut':0});
	
	$('.p_elm_data').hover( function() {
  	openPel(this);
 	}, function() {
 		cancelOpenPel();
 	} );
}

function openPel(jObj) {
	pelTimeout = setTimeout( function() { openPelDelayed(jObj); }, defaultDelay );
}

function openPelDelayed(jObj) {
	$('.p_elm_details').slideUp();
	$(jObj).next().slideDown();
}

function cancelOpenPel() {
	clearTimeout(pelTimeout);
}
