var newsMG = {
	init:function() {
	
		$('.news-more').bind('click', function(e){
			var newsId = $(this).attr('id').replace('news-', '');
			newsMG.raiseShowWholeNewsDescription(newsId);
			return false;
		});
	},
	
	raiseShowWholeNewsDescription: function(newsId){
		if ($('#news-description-'+newsId).is(':visible')){
			$('#news-description-'+newsId).slideUp('fast');
			$('#news-'+newsId).text('więcej');
		} else {
			$('#news-description-'+newsId).slideDown('fast');
			$('#news-'+newsId).text('zwiń');
		}
		return false;
	}
};

var productMG = {
	init:function() {
	
		$('a.offer-link').bind('click', function(e){
			var offerId = $(this).attr('id').replace('offer-', '');
			productMG.raiseShowOfferProducts(offerId);
			return false;
		});

		$('#print-active-offers').bind('click', function(e){
			productMG.raiseGetActiveOffers();
			return false;
		});
	},
	
	raiseShowOfferProducts: function(offerId){
		if ($('#offer-products-'+offerId).is(':visible')){
			$('#offer-products-'+offerId).slideUp('medium');
			$('#offer-name-space-'+offerId).removeClass('aktywna');
			$('#toogle-offer-products-'+offerId).attr('src', '/img/plus.png').attr('alt', 'Rozwiń');
		} else {
			$('#offer-products-'+offerId).slideDown('medium');
			$('#offer-name-space-'+offerId).addClass('aktywna');
			$('#toogle-offer-products-'+offerId).attr('src', '/img/minus.png').attr('alt', 'Zwiń');
		}
		return false;
	},

	raiseGetActiveOffers: function(){
		var activeOffers = $('div.obszar-glowny').find('div.aktywna');

		redirectLocation = document.location.href;
		redirectLocation.replace('#', '');
		var activeOffersString = ''
		$.each(activeOffers, function(j){
			idActiveOffer = $(this).attr('id').replace('offer-name-space-', '');
			if (activeOffersString == '') {
				activeOffersString = activeOffersString + idActiveOffer;
			} else {
				activeOffersString = activeOffersString + ','+idActiveOffer;
			}
		});
		if (activeOffersString != ''){
			redirectLocation = redirectLocation + '?drukuj=tak' + '&&oferty=' + activeOffersString;
		} else {
			redirectLocation = redirectLocation + '?drukuj=tak';
		}
		window.open(redirectLocation, 'impuls');
		return false;
	}
};

var treeMG = {
	init:function() {
//		catalog tree
//		slideUp, slideDown tree
		$('a.rolldown-catalog-category').bind('click', function(e){
			var rollId = $(this).attr('id').replace('rolldown-catalog-category-', '');
			treeMG.raiseRollCatalogTree(rollId);
			return false;
		});
		
		$('a.rollup-catalog-category').bind('click', function(e){
			var rollId = $(this).attr('id').replace('rollup-catalog-category-', '');
			treeMG.raiseRollCatalogTree(rollId);
			return false;
		});

//		check catalog parent if child was checked
		$('.catalog-category-2level').bind('change', function(e){
			if ($(this).is(':checked')) {
				var idParent = $(this).parent('li').parent('ul').attr('id').replace('catalog-category-children-', '');
				$('#catalog-category-'+idParent).attr('checked', 'checked');
			}
			return false;
		});
//		uncheck catalog category if parent was unchecked
		$('.catalog-category-1level').bind('change', function(e){
			if (!($(this).is(':checked'))) {
				var idParent = $(this).attr('id').replace('catalog-category-', '');
				$('#catalog-category-children-'+idParent).find('input').attr('checked', '');
			}
			return false;
		});
//		end catalog tree

//		offer tree
//		slideUp, slideDown tree
		$('a.rolldown-offer-category').bind('click', function(e){
			var rollId = $(this).attr('id').replace('rolldown-offer-category-', '');
			treeMG.raiseRollOfferTree(rollId);
			return false;
		});

		$('a.rollup-offer-category').bind('click', function(e){
			var rollId = $(this).attr('id').replace('rollup-offer-category-', '');
			treeMG.raiseRollOfferTree(rollId);
			return false;
		});
		
//		check/uncheck offer parent if child was checked
		$('.offer-category-1level').bind('change', function(e){
			if (!($(this).is(':checked'))) {
				var idParent = $(this).attr('id').replace('offer-category-', '');
				$('#offer-category-children-'+idParent).find('input').attr('checked', '');
			}
			return false;
		});
		
		$('.offer-category-2level').bind('change', function(e){
			if ($(this).is(':checked')) {
				var id1levelParent = $(this).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id1levelParent).attr('checked', 'checked');
			} else {
				var idParent = $(this).attr('id').replace('offer-category-', '');
				$('#offer-category-children-'+idParent).find('input').attr('checked', '');
			}
			return false;
		});
		$('.offer-category-3level').bind('change', function(e){
			if ($(this).is(':checked')) {
				var id1levelParent = $(this).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id1levelParent).attr('checked', 'checked');
				var id2levelParent = $('#offer-category-'+id1levelParent).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id2levelParent).attr('checked', 'checked');
			} else {
				var idParent = $(this).attr('id').replace('offer-category-', '');
				$('#offer-category-children-'+idParent).find('input').attr('checked', '');
			}
			return false;
		});
		$('.offer-category-4level').bind('change', function(e){
			if ($(this).is(':checked')) {
				var id1levelParent = $(this).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id1levelParent).attr('checked', 'checked');
				var id2levelParent = $('#offer-category-'+id1levelParent).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id2levelParent).attr('checked', 'checked');
				var id3levelParent = $('#offer-category-'+id2levelParent).parent('li').parent('ul').attr('id').replace('offer-category-children-', '');
				$('#offer-category-'+id3levelParent).attr('checked', 'checked');
			}
			return false;
		});
//		end offer tree
	},
	
//	catalog tree
	raiseRollCatalogTree: function(rollId){
		if (($('#catalog-category-children-'+rollId)).is(':visible')){
			$('#catalog-category-children-'+rollId).slideUp('fast');
			$('#rollup-catalog-category-'+rollId).hide();
			$('#rolldown-catalog-category-'+rollId).show();
		} else {
			$('#catalog-category-children-'+rollId).slideDown('fast');
			$('#rolldown-catalog-category-'+rollId).hide();
			$('#rollup-catalog-category-'+rollId).show();
		}
		return false;
	},
//	end catalog tree

//	offer tree
	raiseRollOfferTree: function(rollId){
		if (($('#offer-category-children-'+rollId)).is(':visible')){
			$('#offer-category-children-'+rollId).slideUp('fast');
			$('#rollup-offer-category-'+rollId).hide();
			$('#rolldown-offer-category-'+rollId).show();
		} else {
			$('#offer-category-children-'+rollId).slideDown('fast');
			$('#rolldown-offer-category-'+rollId).hide();
			$('#rollup-offer-category-'+rollId).show();
		}
		return false;
	}
//	end offer tree
}


