var $ = jQuery.noConflict();
var path = location.href;
var home = path;
var global_parent;
var reff = '';
function indexblog() 
{ 
	$(".mod_cat_link").livequery(function(){
		$(this).click(function(){
			var act = $(this).attr('href');
			var tab = act.split('showblogajax');
			showLoaderBlog('#blog_content');
			//showLoader('#mod_category');			
			showLoaderBlog('#mod_cat_list_div');			
			
	     	$.ajax({
	     		type: 'post',
	     		url:  act,
	     		dataType: 'html',
	     		success: function(t)
	     		{
	     			$('#blog_content').html(t);
	     		}
	     	});
	     	
	     	$.ajax({
	     		type: 'post',
	     		url: tab[0]+'modcategoryajax'+tab[1],
	     		dataType: 'html',
	     		success: function(t)
	     		{
	     			$('#mod_category').html(t);
	     		}
	     	});
	     	return false;
     	});
	});
	
	$("#blog_content .post_header a, .read_post, .div_prev a, .div_next a, .pagin_right a, .mod_lastpost_link").livequery(function(){
		$(this).click(function(){
			var act = $(this).attr('href');
			showLoaderBlog('#blog_content');
			$.ajax({
				type: 'post',
				url: act,				
				data: 'parent='+act,				
				dataType: 'html',
				success: function(t)
				{
					$('#blog_content').html(t);
				}
			});			
			reff = 'none';
			return false;
		});
	});
	
	$(".mod_header").livequery(function(){
		$(this).click(function(){
			var par = $(this).parent();
			var ch = par.children(':not(.mod_header)');
			if(ch.css('display') == 'none')
			{
				ch.slideDown();	
			}
			else
			{
				ch.slideUp();
			}			
		});
	});
	
	$(".mod_archive_year a").livequery(function(){
		$(this).click(function(){
			var act = $(this).attr('href');
			showLoaderBlog('#blog_content');
			$.ajax({
				type: 'post',
				url: act,
				dataType: 'html',
				success: function(t)
				{
					$('#blog_content').html(t);
				}
			});
			return false;
		});
	});
	
	$(".img_archive_arrow").livequery(function(){
		$(this).click(function(){
			var src = $(this).attr('src');
			var img1 = 'strzalka1.gif';
			var img2 = 'strzalka2.gif';
			//var ch = $(this).parent().children(':not(:first)').children();
			var ch = $(this).parent().children().children();
			if(ch.css('display') == 'none')
			{
				ch.show();	
				var src2 = src.replace(img1,img2);
			}
			else
			{
				ch.hide();
				var src2 = src.replace(img2,img1);
			}		
			
			$(this).attr('src',src2);
		});
	});
	
	/* ---- calendar functions --- */
	$(".cal_top_link img").livequery(function(){
		$(this).click(function(){
			var act = $(this).parent().attr('name');
			showLoaderBlog('#cal_container');
			$.ajax({
				type: 'post',
				url: act,
				dataType: 'html',
				success: function(t)
				{
					$('#mod_calendar').html(t);
				}
			});
			return false;
		});
	});

	$(".cal_top_link").livequery(function(){
		$(this).click(function(){
			var act = $(this).attr('name');
			showLoaderBlog('#cal_container');
			$.ajax({
				type: 'post',
				url: act,
				dataType: 'html',
				success: function(t)
				{
					$('#mod_calendar').html(t);
				}
			});
			return false;
		});
	});
	
	$(".cal_day_withpost").livequery(function(){
		$(this).click(function(){
			var act = $(this).children('a').attr('href');
			showLoaderBlog('#blog_content');
			$.ajax({
				type: 'post',
				url: act,
				dataType: 'html',
				success: function(t)
				{
					$('#blog_content').html(t);
				}
			});
			return false;
		});
	});
	
	$(".div_back a").livequery(function(){
		$(this).click(function(){
			//act = $(this).attr('name');
			//var tab = act.split('categoryId/');
			//alert('tab 0 '+tab[0]+' tab 1 '+tab[1]);
			if(window.global_parent !== undefined) p = global_parent;
			else p = path; 
			showLoaderBlog('#blog_content');
			$.ajax({
				type: 'post',
				url: p,
				dataType: 'html',
				success: function(t)
				{
					$('#blog_content').html(t);
				}
			});
			return false;
		});
	});
	
	//sending search form
	$('#searchform_ajax').livequery(function()
	{
		//if($('#blog_content')) showLoader('#blog_content');				
		$(this).submit(function(){
			showLoader('#Blog_main');
			showLoader('#blog_content');
			var options = {
				url: $(this).attr('action'),
				dataType: 'html',
				error: function(res)
				{
					$('#message').html("Nie można wysłać zmian bloga");
				},
				success:function(res)
				{
					$('#blog_content').html(res);
					$('#Blog_main').html(res);
				}
			}
			$(this).ajaxSubmit(options);
			return false;
		});
	});
	
	
		//---------------------- search pagination list actions
	$('.Mhit_pn_B, .Mhit_pn_B_checked').livequery(function(){
		$(this).click(function(){			
			$('.Mhit_pn_B_checked').attr('class','Mhit_pn_B');
			$(this).attr('class','Mhit_pn_B_checked');	
			
			showLoader('#hp');
			
			var act= '/'+$('#form_search').attr('action')+'/blog/index/searchpage/';
			var p = $(this).attr('name');			
			$.ajax({				
				type: 'post',
				url: act,
				data: 'search_word='+$('.Mhit_pn_cont').attr('name')+'&page='+p,
				dataType: 'html',
				error: function(m)
				{
				},
				success: function(t)
				{					
					$('#hp').html(t);		
				}
			});
			return false;
		});
	});
	$('#Mhit_pn_B_next').livequery(function(){
		$(this).click(function(){
			var n = $(this).attr('name');
			$('[name=Mhit_pn_B_div]').hide();
			$('#'+n+'_Mhit_hp_cont :first-child').click();
			$('#'+n+'_Mhit_hp_cont').show();
			
			
			var a = parseInt(n)+1;
			if($('#'+a+'_Mhit_hp_cont').attr('id'))
			{
				$(this).attr('name',a);	
			}
			else
			{
				$(this).hide();
			}
			
			var b = a - 2;
			if($('#'+b+'_Mhit_hp_cont').attr('id'))
			{
				$('#Mhit_pn_B_prev').attr('name',b);	
				$('#Mhit_pn_B_prev').show();
			}
			else
			{
				$('#Mhit_pn_B_prev').hide();
			}
			
			
		});
	});
	
	$('#Mhit_pn_B_prev').livequery(function(){
		$(this).click(function(){
			var n = $(this).attr('name');
			$('[name=Mhit_pn_B_div]').hide();
			$('#'+n+'_Mhit_hp_cont :first-child').click();
			$('#'+n+'_Mhit_hp_cont').show();
			
			var a = parseInt(n)-1;
			if($('#'+a+'_Mhit_hp_cont').attr('id'))
			{
				$(this).attr('name',a);	
			}
			else
			{
				$(this).hide();
			}
			
			var b = a + 2;
			if($('#'+b+'_Mhit_hp_cont').attr('id'))
			{
				$('#Mhit_pn_B_next').attr('name',b);	
				$('#Mhit_pn_B_next').show();
			}
			else
			{
				$('#Mhit_pn_B_next').hide();
			}			
		});
	});
	//end search pagination list actions ----------------------
	
	
//	---------------------- search pagination list actions
//	$('.hit_pn, .hit_pn_checked').livequery(function(){
//		$(this).click(function(){
//			$('.hit_page').attr('class','hit_page_h');
//			var v = $(this).attr('name');
//			var n = v.substr(0,1);
//			var div = 'hp_'+n;
//			$('#'+div).attr('class','hit_page');
//			$('.hit_pn_checked').attr('class','hit_pn');
//			$(this).attr('class','hit_pn_checked');
//		});
//	});
//	$('#hit_pn_next').livequery(function(){
//		$(this).click(function(){
//			var n = $(this).attr('name');
//			$('[name=hit_pn_div]').hide();
//			$('#'+n+'_hit_hp_cont :first-child').click();
//			$('#'+n+'_hit_hp_cont').show();
//			
//			var a = parseInt(n)+1;
//			if($('#'+a+'_hit_hp_cont').attr('id'))
//			{
//				$(this).attr('name',a);	
//			}
//			else
//			{
//				$(this).hide();
//			}
//			
//			var b = a - 2;
//			if($('#'+b+'_hit_hp_cont').attr('id'))
//			{
//				$('#hit_pn_prev').attr('name',b);	
//				$('#hit_pn_prev').show();
//			}
//			else
//			{
//				$('#hit_pn_prev').hide();
//			}
//			
//			
//		});
//	});
//	
//	$('#hit_pn_prev').livequery(function(){
//		$(this).click(function(){
//			var n = $(this).attr('name');
//			$('[name=hit_pn_div]').hide();
//			$('#'+n+'_hit_hp_cont :first-child').click();
//			$('#'+n+'_hit_hp_cont').show();
//			
//			var a = parseInt(n)-1;
//			if($('#'+a+'_hit_hp_cont').attr('id'))
//			{
//				$(this).attr('name',a);	
//			}
//			else
//			{
//				$(this).hide();
//			}
//			
//			var b = a + 2;
//			if($('#'+b+'_hit_hp_cont').attr('id'))
//			{
//				$('#hit_pn_next').attr('name',b);	
//				$('#hit_pn_next').show();
//			}
//			else
//			{
//				$('#hit_pn_next').hide();
//			}
//			
//			
//		});
//	});
	//end search pagination list actions ----------------------
	
	//ADDITIONAL FUNCTION - ONLY IN 2BE.pl
	
	$('#onComments, .com_reply').livequery(function(){
		$(this).click(function(){
			var comm = $('#addComment');			
			if(comm.css('display') == 'none')
			{
				comm.fadeIn();
			}
			else
			{
				comm.fadeOut();
			}
		});		
	});
}
$(document).ready(indexblog);

function changeNavi(content)
{
	$('#navigation_bar').html(content);
}

function showLoaderBlog(id)
{
	
	var par = $(id);
	if(par)
	{	
		var w = par.css('height');
		if(w)
		{
			w = w.substr(0,w.length-2);
			if(w>400) w = 400;
			if(w>50 && w < 400) w = w - 25;				
			var top = Math.round(w/2);				
			if(w<25)
			{
				top = 0;
				w = 25;
			}
		}
		else
		{
			top = 25;
			w = 25;
		}		
		par.html('<div style="margin-top:'+top+'px; height:'+w+'px" class="loading">&nbsp;</div>');
	}
}

function checkBackLink()
{
	var a = '';
	if(reff == '')
		a = document.referrer;
		
	if(a.length > 10)
	{
		var b = a.split('blog/index');
		if(!b[1])
		{
			$('#div_back_link').show();
			$('#a_back_link').attr('href',a);
		}
	}
	
}
