/*
 * 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;
        }hm_faqBox
        $('#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 - dodawanie
 */
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"
 */

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(){
        if( $(this).attr('rel')=='noajax' ) return true;
        //Obsługa menu po wybraniu aktualności (przeładowanie od nowa strony)
        $('#hm1, #hm2, #hm3, #hm4, #hm_faq').attr('href', 'pomoc.html');
        //$('#hm1').attr('rel', 'noajax');
        
        url = $(this).attr('href');
        
        if( url.substring( 0, 7)=='http://' )
        {
            window.open(url).focus()
            return false;
        }
        if( url=='rss.php' || url=='mailto:bok@enterso.pl' || $(this).attr('rel')=='prettyPhoto' )
	{
			return true;
	}
        $('#nbUl a').removeClass('active');
        $('#nbUl a[href='+url+']').addClass('active');
        //$(this).addClass('active');
		url = url.substring( 0, (url.length-5) );
		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-')
		{
			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 = $("#searchText2").val();

    $("#subscribeMail").focus(function(){
        if( $(this).val()==subscribeMailText )
            $(this).val('');
    }).blur(function(){
        if( $(this).val()=='' )
            $(this).val(subscribeMailText);
    });
    $("#searchText2").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) ) !='' )
    {
        $('#hm_ul a[href='+window.location.pathname.substring(1)+']').addClass('active');
    }else{
        $('#hm_ul a:first').addClass('active');
    }
    if($.browser.opera){
        $("head").append('<link rel="stylesheet" href="opera.css" type="text/css" />');
    }

    $(".help_menu h2 a").click(function(){
         $('.help_menu h2 a').parent().removeClass("active"); //usuń aktywne menu dla wszystkich (rodziców) > <a> (<h2>)
         $(this).parent().addClass("active"); //ustaw aktywne menu dla klikniętego <a> - <h2>

         var classname = $(this).attr("class"); //pobierze klase z aktywnego <a>
         
         $("#cIns div:visible").hide(); //ukryj wszystkie aktywne
         $("#cIns div."+classname).show().addClass("visibility"); //pokaz div.classname

         return false;
    });
    
    $(".step a").click(function(){

         var link_a = $(this).attr('class');
         $('.help_menu h2').removeClass("active");
         $('.help_menu h2 a.'+link_a).parent().addClass("active");

       //  var this_id = $(this).atrr('id');
       //  $('.'+this_id+'Box').addClass("active");

         var classname = $(this).attr("id");

         $("#cIns div:visible").hide();
         $("#cIns div#"+classname+'Box').show().addClass("visibility");

         return false;
    });

    $("#hm_faq").click(function(){

         $('.help_menu h2').removeClass("active"); //usuwanie aktywnej klasy menu
         var id = $(this).attr("id"); //pobranie id faq

         $("#cIns div:visible").hide(); //schowaj wszystkie widoczne div
         $("#cIns div#"+id+'Box').show().addClass("visibility"); //pokaz div#hm_faqBox

         return false;
    });

    $("#hm_conf").click(function(){

         $('.help_menu h2').removeClass("active"); //usuwanie aktywnej klasy menu
         var id = $(this).attr("id"); //pobranie id faq

         $("#cIns div:visible").hide(); //schowaj wszystkie widoczne div
         $("#cIns div#"+id+'Box').show().addClass("visibility"); //pokaz div#hm_faqBox

         return false;
    });
    
    $("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(){}
    });
	footer();
    subscribeText();
})
