function PlacementPlus(e) {
	PlacementShift(this, 1);
	
	return false;
}

function PlacementMinus(e) {
	PlacementShift(this, -1);
	
	return false;
}

function PlacementShift(obj, step, clearCache, requestStr) {
	var $papa = $(obj).parents('DIV.placement');
	if (requestStr == undefined) requestStr = '-';
	
	if ($papa.attr('loaded') != 'yes' || clearCache) {
		var dataSrc = $papa.attr('datasrc');
		$papa.attr('loaded_now', $papa.find('DIV.item').length);
		
		$papa.find('DIV.block').html('');
		
		$.get(dataSrc, {filter: requestStr}, function(data) {
			$papa.attr('loaded', 'yes');				
			$papa.find('DIV.block').html(data);
			$papa.attr('loaded_total', $papa.find('DIV.item').length);
			PlacementShift(obj, step);
		});
		
	} else {
		var loadedNow = parseInt($papa.attr('loaded_now'));
		
		//alert(loadedNow);
		
		var loadedTotal = parseInt($papa.attr('loaded_total'));
		
		//alert(loadedTotal);
		
		
		if(step == 0) {
			// если вгружаетяся аяксом, или сбрасывается кэш
			loadedNow = 3;
		} else {
			loadedNow += step;
		}
		
		if (loadedNow <= 1) {
			loadedNow = 1;
		} else {
			
		}
		
		if (loadedNow >= loadedTotal) {
			loadedNow = loadedTotal;
		} else {
		
		}
		
		$papa.attr('loaded_now', loadedNow);
		var cnt = 0; 
		$papa.find('DIV.item').each(function() {
			if (++cnt > loadedNow) {
				this.style.display = 'none';
			} else {
				this.style.display = 'block';
			}
		});
		
		$.cookie($papa.attr('datakey'), loadedNow, {expires: 365, path: '/'});
	}
}

$(document).ready(function() {
	$('DIV.placement')
		.find('A.plus').click(PlacementPlus).end()
		.find('A.minus').click(PlacementMinus).end()

});


function iFrameFileDownload(fileName) {
	$('#FileDownloadIframe').remove();
	$('body').append('<iframe id="FileDownloadIframe" src="/system/php/service.php?_module=filedownloaddl&_action=download&file_name='+ fileName +'" style="display:none; position: absolute; width: 1px; height: 1px;"></iframe>');
}

function openWindow(url, x, y) {
	window.open(url,'PrintPageWindow','width=' + x + ',height=' + y + ',toolbar=0,scrollbars=1,menubar=1');
}


function ChangeLocationWithStats(window_location, destination_url, stats_type, stats_document_id, stats_document_name, stats_document_lang) {
	$.ajax({
		type: "POST",
		url: "/system/php/service.php",
		async: false, 
		data: "lang=" + stats_document_lang + "&type=" + stats_type + "&document_id=" + stats_document_id + "&document_name=" + stats_document_name + "&_module=visitstats&_action=proccess",
		success: function(msg) {
			//alert(stats_document_lang);
			// do nothing
		}
	});
	
	if(destination_url != '') {
		if(window_location != '') {
			openWindow(destination_url, 800, 700);
			//window.open(destination_url, window_location);
		} else {
			window.location.href = destination_url;
		}
	}
}

var searchStat = {
	
	'frame_id' : 'csreuatrcchh',
	
	Go : function(keywords) {
		if(keywords) {
			$('#' + searchStat.frame_id).remove();
			$('body').append('<iframe id="' + searchStat.frame_id + '" src="/searchStat.phtml?search=' + keywords + '" style="display:none; position: absolute; width: 1px; height: 1px;"></iframe>');
		}
	},
	
	GoCustom : function(keywords, request_value) {
		if(keywords) {
			$('#' + searchStat.frame_id).remove();
			$('body').append('<iframe id="' + searchStat.frame_id + '" src="/searchStat.phtml?' + request_value + '=' + keywords + '" style="display:none; position: absolute; width: 1px; height: 1px;"></iframe>');
		}
	}
}


$(document).ready(function() {
	$('a[href*=".pdf"]').each(function() {
		var href = $(this).attr('href');
		var separator = '&';
		if(!href.match(/\system\php\scripts\script4455.php/)){
			href = href.replace(/^(.*?)([^'"=]+\.pdf)[^'"]*(.*?)$/, '$1' + '/system/php/scripts/script4455.php?pf=' + '$2' + separator + 'rand=' + Math.random() + '$3');
		}
		$(this).attr('href', href);
	})
})
