var $ = jQuery.noConflict();


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


$(document).ready(function(){

    $('label[for="user_msg_accept"]').livequery(function(){$(this).html('Akceptuję <a style="color:#7CA601;" href="/247/Regulamin,29.html">Regulamin</a>')});
	$("#whoiswhoForm #regulamin").parent().append('<a style="padding-left: 5px; color: #7CA601; " href="/247/Regulamin,28.html">Akceptuję Regulamin</a>');
	$("a[href = '/content/index/deletewhoiswho/']").click(function(){
		if(!confirm("Czy napewno chcesz usunąć swoje dane?")){
			return false;
		}
	});

    $("#polecane__szkolenia").livequery(function(){
        $(this).click(function(){
           $(this).stop();
           createCookie('szkolenia_search_category','polecane');
           createCookie('szkolenia_search_powiat',0);
           return true;
        });
    });
	
//	$("#login_content form").submit(function(){
//		var username= $('input[name=username]').val();
//		var password= $('input[name=password]').val();
//		var new_url;
//		
//		$.ajax({
//			url: '/forum/ucp.php',
//			type: 'post',
//			dataType: "html",
//			async: false,
//			success: function(t){
//				    new_url = $(t).find("form").attr("action").replace(/^.{2}(.*)/,'/forum/$1');
//			}
//		});
//		
//		var sid = new_url.replace(/.*?sid=(.*)$/,'$1');
//
//		$.ajax({
//			url: new_url,
//			type: "POST",
//			data: "username="+username+"&password="+password+"&sid="+sid+"&redirect='index.php'",
//			async: false,
//			success: function(e){
//				$(".info").html(e);
//			}
//		});
//		
//		return false;
//	});
	

    $('option[label^="___"]').each( function(){ var txt = $(this).text().replace(/_/g, "&nbsp;"); var label= $(this).text().replace(/_/g, " ");; $(this).html(txt);$(this).attr('label',label); });


    $(".subsite_top").livequery(function(){
        var offset = (180 - $(".subsite_top").height()) * (-1);
        $("#main").attr('style', "top:"+offset+"px; margin-bottom:"+offset+"px; ");
    });

    

    

		$('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_page0').show(); //displaying first article page - if article was paggined in tiny MCE
	$('a[href=#content_page0]').attr('class','article_content_choosed');
	$('.article_content_pages a').click(function(){ slert('s');
		var id = $(this).attr('href');
		
		$('.article_content_page').hide();
		$('.article_content_pages a').removeAttr('class');
		$(this).attr('class','article_content_choosed');
				
		$(id).show();
		return false;
	});
	
	/** - LGHTBOX -- **/
	$(function() {
    	$('a[rel*=lightbox]').lightBox();    	
	});
	$('a[rel*=lightbox]').livequery(function() {
    	$(this).lightBox();
	});

    /** GALLERY **/
    $('.gallery').each(function(){
        if ( gallery == undefined || !$(this).attr('id').length || !$(this).attr('rel').length ) { return false; }
        else { gallery( $(this).attr('id') ); }
    });
});


function menu()
{    
	var str = $('#menu ul').attr('id');
	var ids = str.substr(2);  	
	if(ids)
	{  	
		ids = ids.split('_');		
		if($('.sub_menu_'+ids[0]).attr('class'))
		{	
			/*$('.sub_menu_'+ids[0]).fadeIn();			*/
			var hover = $('#menu ul').attr('class');  			
			if(hover)
			{
				var style = 'active'+hover;					
				$('div#sub_menu').attr('class',style);
				$('#sub_menu ul li').attr('class','active');
				if(ids[1]) $('#submenu_'+ids[1]+' a').attr('class','active');
				//$('#sub_menu ul li a').attr('class',style);
				$('#menu_'+hover+' a').attr('class','active');
			}
			window.at=setTimeout(function(){$('.sub_menu_'+ids[0]).fadeIn();},100);
			window.aat=setTimeout(function(){$('.sub_menu_'+ids[0]).show();},500);
			window.aaat=setTimeout(function(){$('.sub_menu_'+ids[0]).show();},2500);
		}
		else
		{
			$('div#sub_menu').hide();
			$('div#sub_menu').css('height','28px');
		}
	}
	else 
	{		
		$('div#sub_menu').hide();
		$('div#sub_menu').css('height','28px');
	}
	
	$('#menu ul li').livequery(function(){
		$(this).mouseover(function(){
			var str = $(this).attr('id');
			var id = str.substr(5);			
			
	clearTimeout(window.at);
	clearTimeout(window.aat);
	clearTimeout(window.aaat);
	
            window.buftime = setTimeout(function(){

                $('#sub_menu ul').hide()
                $('.sub_menu_'+id).fadeIn();

                if($('#menu ul').attr('class')){
                    var style = 'active'+id;                    
                    $('#sub_menu').attr('class',style);
                    $('#sub_menu ul li a').attr('class',style);
                }
                $('div#sub_menu').slideDown("slow");	               
            },200);
				if(window.pdmtime)
				{
					clearTimeout(window.pdmtime);					
				}				
		});
		$(this).mouseout(function(){			
			if(window.pdmtime)
			{
				clearTimeout(window.pdmtime);
			}
            if(window.buftime)
            {
                clearTimeout(window.buftime);
            }
			window.pdmtime = setTimeout("HideSubMenu()",2000);
		});
	});
		
	$('#menus').livequery(function(){	
		$(this).mouseout(function(){
			if(window.pdmtime)
			{
				clearTimeout(window.pdmtime);
			}
			window.pdmtime = setTimeout("HideSubMenu()",2000);
		});
	});
	
	$('#sub_menu').livequery(function(){
		$(this).mouseover(function(){			
			if(window.pdmtime)
			{
				clearTimeout(window.pdmtime);
			}	
		});
		$(this).mouseout(function(){
			if(window.pdmtime)
			{
				clearTimeout(window.pdmtime);
			}
			window.pdmtime = setTimeout("HideSubMenu()",2000);
		});
	});
}

function HideSubMenu()
{		
	var str = $('#menu ul').attr('id');
	var ids = str.substr(2);  
		ids = ids.split('_');	
	var vis = $('div#sub_menu').attr('class');
	if(!vis) var id_vis = 0;
	else var id_vis = parseInt(vis.substr(6));    
	if(id_vis != ids[0])
	{	
		if($('.sub_menu_'+ids[0]).attr('class')) //prevent hiding when default page displaying withput setted main submenu
		{
			var style = 'active'+ids[0];
			$('#sub_menu ul').hide()
			$('.sub_menu_'+ids[0]).fadeIn(1000);
			$('#sub_menu').attr('class',style);
			//$('#sub_menu ul li a').attr('class',style);
			if(ids[1]) $('#submenu_'+ids[1]+' a').attr('class','active');
		}
	}
	else if(id_vis == ids[0] && ids[1])
	{
		//$('#submenu_'+ids[1]+' a').attr('class','active');
	}
	
}


$(document).ready(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;
}



/******************************** FRONTEND BOXES ***********************************************/
function boxesJS()
{
	$('li.box_tab_header a').live('click',function(){		
		var s = $(this).attr('href');
		var ch = 'box_tab_'+s;		
		$('li.box_tab_header a').removeAttr('class');
		$(this).attr('class','active');
		$('div.box_tab_content').each(function(){
			var n = $(this).attr('id');			
			if(n != ch)
			{
				$('#'+n).hide();			
			}
		});
		$('#'+ch).show();
		return false;
	});
}
$(document).ready(boxesJS);
/***********************************************************************************************/