var ajax_url = "/wordpress/wp-content/themes/niezgrani_v2/includes/ajax.php";

$(document).ready(function() {
	
	//big spots
    $('#spot').cycle({ 
        fx:     'fade', 
        speed:  'slow', 
        timeout: 6000,
        slideExpr: 'div.spot_b_s',
        pager:  '#nav' ,
        pagerAnchorBuilder: paginate		
	});
    //small spots
    $('#spot_m_slide').cycle({ 
        fx:     'scrollHorz',        
        slideExpr: 'div.spot_m_all',
        prev:   '#prev1',
        next:   '#next1',
        cleartypeNoBg: true,
        timeout: 0
        		
	});
    //paginete big spots
    function paginate(ind, el) {
    	return '<span>&nbsp;</span>';
    };
    
    //lightbox
    $('.gallery a').lightBox({
    	fixedNavigation:true,
    	txtImage: 'Obrazek',
    	txtOf: 'z'
    });
    
    //login
    $('a.close, #fade').live('click', function () { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function () {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });
    
    //login form     
	$(".a_login").click(function()
	{
		
		$.ajax({
			cache: false,
			beforeSend : function ()			{
				//$("#a_data").html('walczę....')
			},
			contentType: "text/html; charset=UTF-8",
			type: "GET",
			url: ajax_url ,
			data: "p=ajax&ajax=login_form",
			success: function(data)
			{
		    	$("#a_log_ax").html(data)
		   	}
		 });
		
		ShowLoginForm(1);
		$("#a_tab1").addClass("selected");
		$("#a_tab2").removeClass("selected");
		$("#tab2").hide();
		$("#tab1").show();
		  
	});
	//registration form
	$(".a_regi").click(function()
	{
		$.ajax({
			cache: false,
			beforeSend : function ()			{
				//$("#a_data").html('walczę....')
			},
			contentType: "text/html; charset=UTF-8",
			type: "GET",
			url: ajax_url ,
			data: "p=ajax&ajax=login_form",
			success: function(data)
			{
		    	$("#a_log_ax").html(data);
				
				$("#a_tab2").addClass("selected");
				$("#a_tab1").removeClass("selected");
				$("#tab1").hide();
				$("#tab2").show();
		   	}
		 });				

		ShowLoginForm(0);
	});	
	
	//scrolling lasto comments
	/*$("#clast").scroll(function() {
	   	if($("#clast").scrollTop() == $("#clast").height() - 250)
	    {	
			$('div#loadMoreComments').show();
			$.ajax({
			url: ajax_url ,	
			data: "p=ajax&ajax=lc&id=25&id="+$(".pc:last").attr("id") ,
			success: function(html)
				{
					if(html)
					{		
						$(".ul_pc").append(html);
						//$('div#loadMoreComments').hide();
				}
					else
					{		
						$('div#loadMoreComments').replaceWith("<center><h1 style='color:red'>End of countries !!!!!!!</h1></center>");
					}
				}
			});
		
		}
	});
	*/
	//most comments
	$("#clast #ulcomtab").idTabs();
	$("#ina_comtab2").click(function()
	{
		$("#a_comtabl2").addClass("comtabl_ac");
		$("#a_comtabl1").removeClass("comtabl_ac");
	});
	$("#ina_comtab1").click(function()
	{
		$("#a_comtabl1").addClass("comtabl_ac");
		$("#a_comtabl2").removeClass("comtabl_ac");
	});
	
	//social work
	$("#podc #ulspoltab").idTabs();
	$("#ina_spoltab2").click(function()
	{
		$("#a_spoltabl2").addClass("comtabl_ac");
		$("#a_spoltabl1").removeClass("comtabl_ac");
	});
	$("#ina_spoltab1").click(function()
	{
		$("#a_spoltabl1").addClass("comtabl_ac");
		$("#a_spoltabl2").removeClass("comtabl_ac");
	});
	
	//add class to archive calendar
	$("#calendar_wrap table tbody td a").parent("td").addClass("a_bg_fff");	
	
	
});

function ShowLoginForm(typ)
{
	
    var popID = 'popup'; //Get Popup Name
    var popWidth = 600;

    //Fade in the Popup and add close button
   $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('');
   
    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;

}

function Login(login, passwd)
{
		$("span.error_in").remove();
		$("#tab1 input").removeClass("error_b");
		$("#tab1 a").removeClass("error_in");
		$.ajax({
			cache: false,
			beforeSend : function ()			{
				//$("#a_data").html('walczę....')
			},
			
			type: "POST",
			dataType: "json",
			url: ajax_url,
			data: {	p_:		'ajax',
					ajax_:	'login',
					login_:	login,
					passwd_:	passwd
				},
				
			success: function(data)
			{
		    	$("#footer2").html(data['res']);
		    	
		   	}
		 });
}

function Registration(username, email, email2, passwd,passwd2, chkagree, chkmark)
{
		$("#tab2 input").removeClass("error_b");
		$("#tab2 a").removeClass("error_in");
		$("span.error_in").remove();
		var data = "&username=" + username + "&email=" + email + "&email2=" + email2 + "&passwd=" + passwd + "&passwd2=" + passwd2 + "&chkagree=" + chkagree + "&chkmark=" + chkmark;
		//"&username=" + username + "&email=" + email + "&email2=" + email2 + "&passwd=" + passwd + "&passwd2" = passwd2
		$.ajax({
			cache: false,
			beforeSend : function ()			{
				//$("#a_data").html('walczę....')
			},
			contentType: "text/html; charset=UTF-8",
			type: "GET",
			url: ajax_url,
			data: "p=ajax&ajax=registration" + data,
			success: function(data)
			{
		    	$("#a_data").html(data)
		   	}
		 });
}

function Logout()
{
		$.ajax({
			cache: false,
			beforeSend : function ()			{
				//$("#a_data").html('walczę....')
			},
			contentType: "text/html; charset=UTF-8",
			type: "GET",
			url: ajax_url,
			data: "p=ajax&ajax=logout",
			success: function(data)
			{
		    	$("#footer2").html(data)
		   	}
		 });
}

function Response(id, user)
{	
	var strT = $("#phpbbcomment").val();
	var strC = "[quote=\"" + user +  "\"]" + $("#text-" + id + "").text() + "[/quote]";

	$("#phpbbcomment").val(strT + strC);
	 $('html, body').animate({
	     scrollTop: $("#commentlist li:last").offset().top
	 }, 1000);
}

function SkinChange(id)
{
	$.ajax({
		cache: false,
		beforeSend : function ()			{
			//$("#a_data").html('walczę....')
		},
		contentType: "text/html; charset=UTF-8",
		type: "GET",
		url: ajax_url,
		data: "p=ajax&ajax=skinchange&id=" + id,
		success: function(data)
		{
	    	$("#footer2").html(data)
	   	}
	 });
	
}

/**
 * Dodawanie aktywności gamifikacyjnej do linków 
 */
function ActivRegistration(activityName)
{
	$.ajax({
		cache: false,
		beforeSend : function ()			{
			//$("#a_data").html('walczę....')
		},
		contentType: "text/html; charset=UTF-8",
		type: "GET",
		url: ajax_url,
		data: "p=ajax&ajax=activityregistration&activityName=" + activityName,
		success: function(data)
		{
	    	//$("#a_data").html(data)
			return true;
	   	}
	 });
}
