/*
 * Wyrazenie regularne sprawdzajace poprawnosc maila
 */
var reg = new RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi");

/*
 * Usuniecie "mrówek" po kliknieciu w linka
 */
function removeBlur(){
    $("a").bind("focus",function(){
        if(this.blur)
            this.blur();
    });
}

/*
 * Klikniecie w przycisk subskrypcji - dodawanie
 */	
function addSubscribe(){
    $('#addSubsribe').click( function(){
        var mail = $('#subscribeMail').val();
	
        if( $.trim( mail )=='' || reg.test( mail )==false )
        {
            alert(validMail);
            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);
            }
        });
    })
}

/*
 * Klikniecie w przycisk subskrypcji - usuwanie
 */	
function delSubscribe(){
    $('#delSubsribe').click( function(){
	
        var mail = $('#subscribeMail').val();
	
        if( $.trim( mail )=='' || reg.test( mail )==false )
        {
            alert(validMail);
            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 )
}

/*
 * Klikniecie w przycisk, który ma atrybut rel="ajax" ?? chyba w a i inne warunki 
 */	

var s = '';
var sp = '';
var p = ''; //page ?
var pp = '';
var url = '';
var h = '';
var left = '';
var right = '';
var id = '';
var c = '.cLeft .ins';

function ajax(){
	$('a').unbind('click').click(function(){
                if( $(this).attr('rel')=='noajax' ) return true; //nie dziala na noajax
                url = $(this).attr('href');
                if(url.substring(0, 7) == 'mailto:') return true; // nie dziala na mailto
		if( url.substring( 0, 7)=='http://' ) //sprawdzenie czy jest to http://... czyli czy ma być przeniesione na inną zakładkę
		{
			window.open(url).focus()
			return false;
		}
                if( url=='rss.php' || url=='mailto:bok@codecleaner.pl' || $(this).attr('rel')=='prettyPhoto') // nie działa na tych warunkach
		{
                    return true;
		}
                $('#nbUl a').removeClass('active'); //usunięcie z wyszystkich anchor class active
                $('#nbUl a[href='+url+']').addClass('active'); // dodanie do tej którą klikamy klasy active
                //$(this).addClass('active');
		url = url.substring( 0, (url.length-5)); //usuniecie .html (5 znaków)
		id='';
		if( url != 'home' )
		{
                    s = 'more';
                    p = url;
                    if(sp == 'more') c = '.cLeft .ins';
                    else c = '.cLeft .ins';
		}else{
                    s = 'home';
                    p = '';
                    c = '.cLeft .ins';
		}
		if(url.substring(0,5) == 'news-') //jezeli wybrano news'y'
		{
			p = 'news';
			id = url.substring(5).split('-').shift();
		}
                //var div = $('div').addClass('LoadingContent');  dodaje do wszystkich div'ów
		if( p!=pp || s!=sp || p=='news' )
		{
                    pp = p;
                    //$('#cIns').empty().append(div);
                    $('#cIns').empty().append('<div class="LoadingContent">'+waitLoading+'</div>');
                    //$('#cIns').empty().addClass('LoadingContent').text(info);
                    $.ajax({
			url: "content.php",
			data: 's='+s+'&p='+p+'&id='+id+'&sp='+sp,
			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()
{
    var subscribeMailText = $("#subscribeMail").val();
    var searchTextText = $("#searchText").val();

    $("#subscribeMail").focus(function(){
        if( $(this).val()==subscribeMailText )
            $(this).val('');
    }).blur(function(){
        if( $(this).val()=='' )
            $(this).val(subscribeMailText);
    });
    $("#searchText").focus(function(){
        if( $(this).val()==searchTextText )
            $(this).val('');
    }).blur(function(){
        if( $(this).val()=='' )
            $(this).val(searchTextText);
    });
}
/*
 * Po zaladowaniu 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');
    }
    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(){}
    });
})
