$(document).ready(function() {

	 $("#facebook").toggle(
	function () {
		$("#facebook").animate({left: "0px"}, 1000 );
	},  function () {
		$("#facebook").animate({left: "-200px"}, 1000 );
	});	
 
 });




function insertTag(przed, po, v)
{
	if (v) 
	pole = document.getElementById(v);
	else
	pole = document.getElementById("tresc");

	if (document.selection) // IE/Opera
	{ 
		pole.focus();
		obszar = document.selection.createRange();
		obszar.text = przed+obszar.text+po;
		obszar.select();
	} 
	else if (pole.selectionStart>-1)  // FireFox
	{
		start = pole.selectionStart;
		tekst = przed+pole.value.substring(start,pole.selectionEnd)+po;
		pole.value = pole.value.substring(0, start) + tekst + pole.value.substring(pole.selectionEnd, pole.value.length);
		pole.selectionStart = start+tekst.length;
		pole.selectionEnd = pole.selectionStart;
	}

}


var surf_timer_id=0;
var surf_contol_timer_id=0;
var surf_time=0;
var surf_bonus=0;
function surfLoad()
{
	surf_time = surf_time_c;
	$('#licznik').html(surf_time);
	$('#btn_bonus').hide();
	
	$.post("/autosurf/load.html", { },
	function(data){
		surf_timer_id = setInterval('surfOut()', 1000);
		
		surf_bonus = data.bonus;
		setTimeout('surfBonus()', 3000);
		
		$('#iframe').attr('src', data.url);
	}, "json");
	
}

function surfControl()
{
	surfLoad();
}

function surfOut()
{
	surf_time--;
	
	if (surf_time == 0)
	{
		clearInterval(surf_timer_id);
		surfLoad();
	}
	else
		$('#licznik').html(surf_time);
}

function surfPauza()
{
	if ($('#btn_pauza').val()=='PAUZA')
	{
		clearInterval(surf_timer_id);
		$('#btn_pauza').val('START');
	}
	else
	{
		surf_timer_id = setInterval('surfOut()', 1000);
		$('#btn_pauza').val('PAUZA');
	}
}

function surfBonus()
{
	if (surf_bonus==1)
		$('#btn_bonus').show();
	else
		$('#btn_bonus').hide();
}

function surfBonusAdd()
{
	$.post("/autosurf/ajax_bonus_add.html", { },
	function(data){
		alert('Dostałeś '+data.pkt+' punktów!');
	}, "json");
	
	$('#btn_bonus').hide();
}

var exit=false;
function stopExit() {
	if (exit) return true;
	else
	{
		window.location.href='/autosurf/exit.html';
		return false;
	}
}
function startExit() {
	$("#surf").attr("onUnload", "");
	$("#surf").attr("onbeforeUnload", "");
	
	exit = true;
}


function zglosNaruszenie()
{
	if ($('#btn_pauza').val()=='PAUZA')
		surfPauza();
	
	$('#naruszenie .zn_z').hide();
	$('#naruszenie .zn_w').show();
	$('#zn_inne').hide();
	$('#zn_powod').val(0);
	$('#naruszenie').css("height",($("body#surf").height()-200)+"px");
	$('#naruszenie').show();
}

function zglosNaruszenieSend()
{
	$.post("/autosurf/ajax_zglos_naruszenie.html",{ 
		powod: $('#zn_powod').val(),
		inne: $('#zn_inne').val()
	},
	function(data){
		if (data.error)
		{
			$('.zn_z').html('Wybierz powód zgłoszenia!');
		}
		else
		{
			$('.zn_z').html('Zgłoszenie zostało wysłane! Dziękujemy!');
			$('.zn_w').hide();
		}
		$('.zn_z').show();
	}, 
	"json");
}



var new_timer_id=0;
var new_time=0;
function newLoad(url)
{
	if (!url)
		top.close();

	new_timer_id = setInterval('newOut()', 1000);
	new_time = new_time_c;
	$('#licznik').html(new_time);
}

function newOut()
{
	new_time--;
	
	if (new_time == 0)
	{
		clearInterval(new_timer_id);
		$('#btn_dodaj').css('visibility','visible');
	}
	
	$('#licznik').html(new_time);
}

function newDodaj()
{
	$.post("/strony/dodaj_wer.html",{ 
		send: 1
	},
	function(data){
		opener.location.href = '/strony.html';
		top.close();
	});
}






var ptc_timer_id=0;
var ptc_time=0;
function ptcLoad()
{
	ptc_timer_id = setInterval('ptcOut()', 1000);
	ptc_time = ptc_time_c;
	$('#licznik').html(ptc_time);
}

function ptcOut()
{
	ptc_time--;
	
	if (ptc_time == 0)
	{
		$('#licznik').html(0);
		clearInterval(ptc_timer_id);
		ptcSave();
		
		$('#btn_pauza').hide();
		$('#btn_zakoncz').show();
	}
	else
		$('#licznik').html(ptc_time);
}

function ptcPauza()
{
	if ($('#btn_pauza').val()=='PAUZA')
	{
		clearInterval(ptc_timer_id);
		$('#btn_pauza').val('START');
	}
	else
	{
		ptc_timer_id = setInterval('ptcOut()', 1000);
		$('#btn_pauza').val('PAUZA');
	}
}
function ptcSave()
{
	$.post("/ptc/save.html",{
	},
	function(data){
		$('#info').html('Reklama została zaliczona. Zdobyłeś '+data+' punktów.');
	});
}

