/*
 * 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 - 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') || ($(this).attr('rel')=='prettyPhoto') )
            return true;
		url = $(this).attr('href');
		if( url.substring( 0, 7)=='http://' )
		{
			window.open(url).focus()
			return false;
		}
          if( url=='rss.php' || url=='./files/SemiAdderDemo.msi' || url=='mailto:bok@codecleaner.pl')
		{
			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 = $("#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();
     $('#nbUl a:first').addClass('active');
     /*left = $('.cLeft').height();
     right = $('.cRight').height();
     $('.cLeft').height(left);
     $('.cRight').height(right);*/
	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(){}
	});
})
