/*
 * Tekst podczas ładowania się strony
 */
var info = "Proszę czekać, trwa realizacja zadania...";

/*
 * Wyrażenie regularne sprawdzające poprawność maila
 */
var reg = new RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi");

/*
 * Usunięcie "mrówek" po kliknięciu w linka
 */
function removeBlur(){
	$("a").bind("focus",function(){
		if(this.blur)
			this.blur();
	});
}

/*
 * Kliknięcie w przycisk subskrypcji - dodawanie
 */	
function addSubscribe(){
	$('#addSubsribe').click( function(){
		var mail = $('#subscribeMail').val();
	
		if( $.trim( mail )=='' || reg.test( mail )==false ) 
		{
			alert('Proszę podać poprawny adres e-mail');
			return false;
		}
		$('#subscribe').empty().addClass('loading').text(info);
		$.ajax({
			type: "POST",
			url: "subscribe.php?act=add",
			data: "mail="+mail,
			success: function(msg)
			{
				$('#subscribe').empty().removeClass('loading').text(msg);
			}
		});
	})
}

/*
 * Kliknięcie w przycisk subskrypcji - dodawanie
 */	
function delSubscribe(){
	$('#delSubsribe').click( function(){
	
		var mail = $('#subscribeMail').val();
	
		if( $.trim( mail )=='' || reg.test( mail )==false ) 
		{
			alert('Proszę podać poprawny adres e-mail');
			return false;
		}
		$('#subscribe').empty().addClass('loading').text(info);
		$.ajax({
			type: "POST",
			url: "subscribe.php?act=del",
			data: "mail="+mail,
			success: function(msg)
			{
				$('#subscribe').empty().removeClass('loading').text(msg);
			}
			});
			
	})
}

/*
 * 
 */	
function footer(){
	$('.fade').hover( function(){
		$(this).animate({ 
			opacity: 1
		}, 500 )
	}, function(){
		$(this).animate({ 
			opacity: 0.5
		}, 500 )
	})
	$('.fade').animate({ 
		opacity: 0.5
	}, 10 );
}

/*
 * 
 */	
function rss(){
	$('#rss').hover( function(){
		$(this).animate({ 
			opacity: 1
		}, 500 )
	}, function(){
		$(this).animate({ 
			opacity: 0.5
		}, 500 );
	})
	$('#rss').animate({ 
		opacity: 0.5
	}, 10 )
}

/*
 * Kliknięcie w przycisk, który ma atrybut rel="ajax"
 */	
var s = '';
var sp = '';
var p = '';
var pp = '';
var url = '';
var h = '';
var left = '';
var right = '';
var id = '';
var c = '.cLeft .ins';
function ajax(){
	$('a').unbind('click').click(function(){
		url = $(this).attr('href');

        if( url=='rss.php' || url=='mailto:bok@enterso.pl' || ($(this).attr('rel')=='prettyPhoto'))
			return true;

		if( url.substring( 0, 7)=='http://' )
		{
			window.open(url).focus()
			return false;
		}
       
          $('#nbUl a').removeClass('active');
          $('#nbUl a[href='+url+']').addClass('active');
		url = url.substring( 0, (url.length-5) );
          s = url;
		id='';
          c = '#cIns';

		if(url.substring(0,5)=='news-')
		{
			s = 'news';
			id = url.substring(5).split('-').shift();
		}
          
          $('#cIns').empty().append('<div class="LoadingContent">Proszę czekać, trwa ładowanie...</div>');
          $.ajax({
               url: "content.php",
               data: 's='+s+'&id='+id,
               success: function(msg)
               {
                    $('#cIns').empty().removeClass('LoadingContent').html(msg).show();
                    ajax();
                    $("a[rel^='prettyPhoto']").prettyPhoto({
                    animationSpeed: 'normal', /* fast/slow/normal */
                    padding: 40, /* padding for each side of the picture */
                    opacity: 0.35, /* Value betwee 0 and 1 */
                    showTitle: true, /* true/false */
                    allowresize: true, /* true/false */
                    counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
                    theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
                    callback: function(){}
                });
               }
          });
	return false;
	})
}

function subscribeMail()
{
	$("#subscribeMail").focus(function(){
		if( $(this).val()=='Wpisz swój e-mail...' )
			$(this).val('');
	}).blur(function(){
		if( $(this).val()=='' )
			$(this).val('Wpisz swój e-mail...');
	});
}

/*
 * Po załadowaniu drzewa DOM wykonaj wszystkie zadnia
 */
$(document).ready( function(){

	removeBlur();
	addSubscribe();
	delSubscribe();
	ajax();
	footer();
	rss();
	subscribeMail();
     if ( $.trim( window.location.pathname.substring(1) ) !='' )
     {
          $('#nbUl a[href='+window.location.pathname.substring(1)+']').addClass('active');
     }else{
          $('#nbUl a:first').addClass('active');
     }
	
	$("#search_form").focus(function(){
		if( $(this).val()=='Szukaj...' )
			$(this).val('');
	}).blur(function(){
		if( $(this).val()=='' )
		{
			$(this).val('Szukaj...');
		}
	});
	
	if($.browser.opera){
		$("head").append('<link rel="stylesheet" href="opera.css" type="text/css" />');
	}
	$("a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				padding: 40, /* padding for each side of the picture */
				opacity: 0.35, /* Value betwee 0 and 1 */
				showTitle: true, /* true/false */
				allowresize: true, /* true/false */
				counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
				callback: function(){}
	});
})
