var $ = jQuery.noConflict();

function oferta()
{
	if(!$('#hosting').attr('id')) return false;
	$('#media_box').html('<div class="loading"></div>');
	$('#hosting').html('<div class="loading"></div>');
	$('#mini_blog').html('<div class="loading"></div>');
		
	$.ajax({
			type: 'post',
			url: '/pl/content/index/list/id/109/',
			dataType: 'json',
			error: function(m)
			{
			},
			success: function(t)
			{
			   mediaBox(t.articles);
			}
	});
	
	$.ajax({
			type: 'post',
			url: '/pl/content/index/list/id/119/',
			dataType: 'json',
			error: function(m)
			{
			},
			success: function(t)
			{
			   $('#hosting').html(t.content.content_text);
			   	
				$('.oferta').hide();
				var _href= $('.active').attr('href');
				_href= _href.split('#');
				$('#'+_href[1]).show();
			}
	});
	
	$.ajax({
			type: 'post',
			url: '/pl/blog/index/shortlastpostsajax/blogId/1',
			dataType: 'html',
			error:function(m)
			{
			},
			success: function(t)
			{
				$('#mini_blog').html(t);
			}
	});
	
	$('#oferta_menu a').livequery(function(){
		$(this).click(function(){
			$('#oferta_menu a').attr('class', '');
			$(this).attr('class', 'active');
			$('.oferta').hide();
			var _href= $('.active').attr('href');
			_href= _href.split('#');
			$('#'+_href[1]).show();
			return false;
		});
	});
}

function active_menu()
{
	var d_url = new String($(document).attr("location"));		
	var c_url = d_url.split("/id/");
	
	var c_url= d_url.split('/');
	var url= '/'+c_url[parseInt(c_url.length)-1]; 
	
	$('#left_menu ul ul').hide();
	var obj= $('#left_menu a[href*='+url+']');
	$('#left_menu a[href='+url+']').attr('class', 'active');
	
	if ($(obj).parent().parent().parent().parent('li').attr('id'))
	{
		$(obj).parent().parent().parent().show();
		var id= $(obj).parent().parent().parent().parent('li').find('a').attr('href');
		$('#main_menu a[href*='+id+']').attr('class', 'active_menu');
	}
	else
	{
		$(obj).parent().parent().find('ul').show();
		var id= $(obj).attr('href');
		$('#main_menu a[href*='+id+']').attr('class', 'active_menu');
	}	
	$('#category109 a').attr('target', '_blank');
}


function mediaBox(t)
{
	var _html='';
	for (var i=0; i<=(t.length-1); i++)
	{
		var pretitle= t[i].articles_pretitle;
		if (parseInt(pretitle.length)>45)
		pretitle= pretitle.substr(0, 42)+'...';
		
		_html+='<tr class="medias"><td class="media_title"><div class="media_date">'+t[i].articles_created+'</div><a href="'+t[i].articles_link+'" target="_blank">'+pretitle+'</a></td></tr>';
		_html+='<tr class="medias"><td class="media_img"><img src="'+t[i].articles_photo_url+'" /></td></tr>';
		if (i==1) break; 
	}
	$('#media_box').html('<h2>Media o nas</h2><div class="media_table"><table >'+_html+'</table></div><div class="media_more"><a href="/mediaonas.html">więcej</a></div>');
}

function chat()
{
	window.open('/pl/chat/index/login/', 'chat', 'scrollbars=yes,width=700,height=600,resizable=false');
}


$(document).ready(function(){
		$('div#logowanie form').submit(function(){
			
			logowanie(this);
			
			return false;
	});
	
	function logowanie(obj_this)
	{
		var login= $('input[name=login]', obj_this).val();
		var password= $('input[name=password]', obj_this).val();
		var obj= obj_this;
		$(obj_this).unbind('submit');
			
			$(obj_this).hide();
			$('div#logowanie').append('<div class="loading" style="margin: 0;"></div>');
			
			$.ajax({
					type: 'post',
					url: '/pl/login2be/index/login/',
					data: 'login='+login+'&password='+password,
					dataType: 'html',
					error: function(m)
					{
					},
					success: function(t)
					{
					   if (parseInt(t)==1)
					   {
						   $(obj).submit();
					   }
					   else
					   {
						   	$('#logowanie fieldset p').html('<div class="login-message" style="color: red;">Niepoprawny login lub hasło</div>');
						   	$('#logowanie .loading').remove();
						   	$('div#logowanie form').show();
						   	$('div#logowanie form').bind('submit', function(){logowanie(this); return false;});
					   }
					   
					   
					   
					}
			});
	}
	
	$('.content_sub_menu #menu_122 a').livequery(function(){		
		$(this).click(function(){
			var a = $(this).attr('href');
			var tab = a.split('/');
			if (!tab[1]) return false;						
			window.location.href = '/'+tab[1]+'/blog/index/showblog/blogId/1';
			return false;
		});
	});
	
	//sending search form
	$('#form_search').livequery(function(){
		$(this).submit(function(){			
			var act= '/'+$(this).attr('action')+'/default/index/searchajax/';
			var word= 'search_word='+$('#search_word_id').attr('value');
			
			var target = getTarget('.content_text');			
			$.ajax({				
				type: 'post',
				url: act,
				data: word,
				dataType: 'html',
				error: function(m)
				{
				},
				success: function(t)
				{
					//if($('#blog_content').attr('id')) $('#blog_content').html(t);		
					//else $('.content_text').html('<div id="search_content">'+t+'</div>');		
					$(target).html('<div id="search_content">'+t+'</div>');		
					
				}
			});
			return false;
		});	
	});
	
	//---------------------- search pagination list actions
	$('.Mhit_pn, .Mhit_pn_checked').livequery(function(){
		$(this).click(function(){			
			$('.Mhit_pn_checked').attr('class','Mhit_pn');
			$(this).attr('class','Mhit_pn_checked');	
			showLoader('.Mhit_page');
			
			var act= '/'+$('#form_search').attr('action')+'/default/index/searchpage/';
			var p = parseInt($(this).html())-1;			
			$.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_next').livequery(function(){
		$(this).click(function(){
			var n = $(this).attr('name');
			$('[name=Mhit_pn_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_prev').attr('name',b);	
				$('#Mhit_pn_prev').show();
			}
			else
			{
				$('#Mhit_pn_prev').hide();
			}
			
			
		});
	});
	
	$('#Mhit_pn_prev').livequery(function(){
		$(this).click(function(){
			var n = $(this).attr('name');
			$('[name=Mhit_pn_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_next').attr('name',b);	
				$('#Mhit_pn_next').show();
			}
			else
			{
				$('#Mhit_pn_next').hide();
			}			
		});
	});
	//end search pagination list actions ----------------------

	
	$('#content_76').livequery(function(){
		$('#content_76').append('<div class="loading"></div>');
		$.ajax({
					type: 'post',
					url: '/pl/sitemap/index/index',
					dataType: 'html',
					error: function(m)
					{
					},
					success: function(t)
					{
						$('.loading').remove();
					   $('#content_76').append(t);
					}
			});
		
	});
});

$(document).ready(oferta);
$(document).ready(active_menu);

function showLoader(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 = 25;
				w = 25;
			}		
		}
		else
		{
			top = 25;
			w = 25;
		}		
		par.html('<div style="padding-top:'+top+'px; height:'+w+'px" class="loading2">&nbsp;</div>');
		
	}
}

function getTarget(p)
{		
	var rid;
	var t = $(p).each(function(){
		
		var id = String($(this).attr('id'));				
		if(id != 'undefined')
		{
			$(this).html('<div style="padding-top:20px; height:50px" class="loading2">&nbsp;</div>');
			rid = '#'+id;
		}		
	});	
	//if(!rid) rid = p;
	$('.articles').hide();
	$('.articles_pages').hide();
	return rid;
}