var $ = jQuery.noConflict();


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"><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(lang)
{
	window.open('/'+lang+'/chat/index/login/?room_id=7', 'chat', 'scrollbars=yes,width=700,height=600,resizable=false');
}


$(document).ready(function(){
	
	
//			$('.article_content_page').hide();
// 	        $('#content_page0').show();
// 	        $('.article_content_pages :first-child').attr('id', 'current_page');
// 	       
//	       
// 	        $('.article_content_pages a').click(function(){
// 	                var page= $(this).attr('href');
// 	                $('.article_content_page').hide();
// 	                $('.article_content_pages a').attr('id', '');
// 	                $(page).show();
// 	                $(this).attr('id', 'current_page');
// 	        });
 	       
 	        $('#layer1').hide();
 	        $('#btn_newsletter').click(function(){
 	               
 	                var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 	                if (filter.test($('#input_newsletter').attr('value')) == false)
 	                {
 	                        alert('Niepoprawny adres e-mail !');
 	                        return;
 	                }
 	               
 	                var url_s= '/'+$(this).parent().attr('lang')+'/default/index/newsletterajax/';
 	                var data_s= 'adr='+$('#input_newsletter').attr('value');
 	                        $.ajax({
 	                               
 	                                type: 'post',
 	                                url: url_s,
 	                                data: data_s,
 	                                dataType: 'html',
 	                                error: function(m)
 	                                {
 	                                },
 	                                success: function(t)
 	                                {
 	                                   $('#newsletter-popup-content').html(t);
 	                                   $('#layer1').show();
 	                                   //$('#layer1').center();
 	                                }
 	                        });
 	        });
 	       
 	        $('#newsletter-popup-close').click(function(){
 	                $('#layer1').hide();
 	                return false;
 	        });
 	        
	
	
	$('.logowanie form').livequery(function(){ 
		$(this).submit(function(){ 
            var password= $(this).children('[name=password]').attr('value'); 
            var login= $(this).children('[name=login]').attr('value'); 
            var obj= this;             
            if($(obj).attr('id')=='login')
            {
            	password = $(obj).find('input#password').attr('value');
            	login = $(obj).find('input#username').attr('value');            	
            }
            
            //seting height
            var old_h = $('.logowanie').height();
            //-------------
             
            $.ajax({                    
                type: 'post', 
                url: $(obj).attr('action'), 
                data: 'username='+login+'&password='+password, 
                dataType: 'html', 
                error: function(m) 
                { 
                }, 
                success: function(t) 
                {   
                   var tt = $('.logowanie form').parent();                   
                   tt.html(t);	                   
                   setLoginHeight(old_h);	                   
                } 
            });
            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>');	
					$('#Blog_main').html('<div id="search_content">'+t+'</div>');	
					$('.content').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: '/sitemap',
					dataType: 'html',
					error: function(m)
					{
					},
					success: function(t)
					{
						$('.loading').remove();
					   $('#content_76').append(t);
					}
			});
		
	});
	
	$('#window_bar').livequery(function(){
		$(this).click(function(){
			$('#window_maps').remove();
		});		
	});
	
	
	// -------------------- CONTACT SECTION FUNCTION	
	$('.contact_section_nag').livequery(function(){
		$(this).click(function(){
			var n = $(this).next();
			var d = n.css('display');
			if(d=='none') n.fadeIn();
			else n.fadeOut();
		})
	}); 
	// ---------------------------------------------
});

$(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;
}

function addtofavorites(){
    var tytul = 'Laguna - systemy przesuwne';
    var adres = 'http://www.laguna.pl';
       
    if (window.sidebar)
    {
		window.sidebar.addPanel(tytul, adres, "");
    }
    else if(window.opera && window.print)
    {
       var elem = document.createElement('a');
       elem.setAttribute('href',adres);
       elem.setAttribute('title',tytul);
       elem.setAttribute('rel','sidebar');
       elem.click();
 	}
 	else if(document.all)
	{
 	   window.external.AddFavorite(adres, tytul);
    }
}

function showRegion(id)
{
	var links= [];
	
	links[1]='/pl/content/index/read/id/178/id2/81/title/Zachodniopomorskie.html';
	links[2]='/pl/content/index/read/id/174/id2/81/title/Pomorskie.html';
	links[3]='/pl/content/index/read/id/179/id2/81/title/Warminsko-Mazurskie.html';
	links[4]='/pl/content/index/read/id/187/id2/81/title/Podlaskie.html';
	links[5]='/pl/content/index/read/id/177/id2/81/title/Lubuskie.html';
	links[6]='/pl/content/index/read/id/176/id2/81/title/Wielkopolskie.html';
	links[7]='/pl/content/index/read/id/175/id2/81/title/Kujawsko-Pomorskie.html';
	links[8]='/pl/content/index/read/id/185/id2/81/title/Mazowieckie.html';
	links[9]='/pl/content/index/read/id/172/id2/81/title/Dolnoslaskie.html';
	links[10]='/pl/content/index/read/id/186/id2/81/title/Lodzkie.html';
	links[11]='/pl/content/index/read/id/184/id2/81/title/Opolskie.html';
	links[12]='/pl/content/index/read/id/181/id2/81/title/Slaskie.html';
	links[13]='/pl/content/index/read/id/183/id2/81/title/Malopolskie.html';
	links[14]='/pl/content/index/read/id/182/id2/81/title/Podkarpackie.html';
	links[15]='/pl/content/index/read/id/173/id2/81/title/Swietokrzyskie.html';
	links[16]='/pl/content/index/read/id/180/id2/81/title/Lubelskie.html';
	
	var a_url= links[id];
	
	$.ajax({	
			type: 'post',
			url: a_url,
			dataType: 'html',
			error: function(m)
			{
			},
			success: function(t)
			{
			   $('#content_34').append('<div id="window_maps"><div id="window_bar"></div><div id="window_content">'+t+'</div></div>');
			   $('#window_maps').center();
			}
	});
}

function setLoginHeight(old_h)
{
	var l = $('.logowanie div');	
	var h = l.height();	
	var p = l.css('padding-top');
	var sum = parseInt(h)+parseInt(p)+5;	
	if(sum < old_h) sum = old_h;	
	$('.logowanie').css('height',sum+'px');
}