$(function() {
	$.ajaxSetup({
		cache: false
	});
	
	$('button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td div.ui-state-default','#calendar_big')
		.live('mouseout', function(){
			if(!$(this).hasClass('ui-state-disabled'))
			{
				$(this).removeClass('ui-state-hover');
				if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).removeClass('ui-datepicker-prev-hover');
				if(this.className.indexOf('ui-datepicker-next') != -1) $(this).removeClass('ui-datepicker-next-hover');
			}
		})
		.live('mouseover', function(){
			if(!$(this).hasClass('ui-state-disabled'))
			{
				$(this).parents('.ui-datepicker-calendar').find('div').removeClass('ui-state-hover');
				$(this).addClass('ui-state-hover');
				if(this.className.indexOf('ui-datepicker-prev') != -1) $(this).addClass('ui-datepicker-prev-hover');
				if(this.className.indexOf('ui-datepicker-next') != -1) $(this).addClass('ui-datepicker-next-hover');
			}
		});
		
		
		/*
		
	$('#ramowka')
		.live('mouseout', function(){
			$('#ramowka_srodek').hide();
		})
		.live('mouseover', function(){
			$('#ramowka_srodek').show();
		});
	*/
	
	$("#f_odkogo").attr("disabled", "").val("");
	$("#f_tresc").attr("disabled", "").val("");
	$("#f_wyslij").attr("disabled", "");

	updateInfo();
	doDMarquee();
	
	$("#f_wyslij").livequery("click",function()
	{
		$.post("/inc/panel.php",
		{ 
			tresc: $("#f_tresc").val(),
			odkogo: $("#f_odkogo").val()
		}, function(data)
		{
			$('#komunikat_pozdro').html(data.message);
			if(data.error == 0)
			{
				initRefresh(30);
			}
		}, "json");
	});
	
	$("#f_zapisz_newsletter").livequery("click",function()
	{
		$.post("/inc/newsletter.php",
		{ 
			newsletter_email: $("#f_newsletter_email").val(),
			newsletter_nick: $("#f_newsletter_nick").val()
		}, function(data)
		{
			$('#komunikat_newsletter').html(data.message);
			if(data.error == 0)
			{
				initRefresh2(15);
			}
		}, "json");
	});
	
	$("#f_zaloz_bloga").livequery("click",function()
	{
		$(".blog_error").html('').css('display','none');
		$.post("signup.php",
		{
			signup_form_id: $("input[name=signup_form_id]").val(),
			_signup_form: $("input[name=_signup_form]").val(),
			user_name: $("input[name=user_name]").val(),
			user_email: $("input[name=user_email]").val(),
			blogname: $("input[name=blogname]").val(),
			blog_title: 'alteRadio.pl',
			blog_public: $("input[@name=blog_public]:checked").val(),
			signup_for: $("input[name=signup_for]").val()
		}, function(data)
		{
			if(data.error == 1)
			{
				var objCount=0;
				for(_obj in data.errors)
				{
					var error = '';
					for(err in data.errors[_obj])
					{
						error += data.errors[_obj][err] + '<br />';
					}
					
					$("#"+_obj+"_error").html(error).css('display','block');		
				}
			}
			else
			{
				$('#wlasny_blog').html(data.message);
			}
			
		}, "json");
	});
	
	$("#submitcomment").livequery("click",function()
	{
	
		$.post("/news/shownews.php?a=169&kat=41",
			{ 
				id: $("#news_id").val(),
				name: $("#commentname").val(),
				email: $("#commentemail").val(),
				captcha: $("#commentcaptcha").val(),
				captcha_answer: $("#captcha_answer").val(),
				comment: $("#commenttext").val()
			}, function(data)
			{
				if(data.error == 1)
				{
					$('#commenterror').html(data.message);
				}
				else
				{
					var t = new Date().getTime();
					$('#content_main').load($("#commentredirect").val()+'&t='+t);
				}
			}, "json");
	});
	
	$.address.change(function(event)
	{

		$('#content_main').html($('#loading').html());
		var t = new Date().getTime();
		
		if(event.value == "/")
		{
			$('#content_main').load('ajax.php?pokaz=main&t='+t);
		}
		else
		{
			$('#content_main').load(event.value+"&t="+t);			
		}
	});
	
	
	$('a').click(function() {
		if($(this).attr("rel"))
		{
			//scroll(0,300);
			$.address.value($(this).attr('href'));
			
		}
	});
});



var varRefresh,varRefresh2;
function initRefresh(refresh) {
	varRefresh = refresh;
	setTimeout("updateRefresh();", 1000);
	$("#f_odkogo").attr("disabled", "disabled");
	$("#f_tresc").attr("disabled", "disabled");
	$("#f_wyslij").attr("disabled", "disabled");
}

function initRefresh2(refresh) {
	varRefresh2 = refresh;
	setTimeout("updateRefresh2();", 1000);
	$("#f_zapisz_newsletter").attr("disabled", "disabled");
	$("#f_newsletter_nick").attr("disabled", "disabled");
	$("#f_newsletter_email").attr("disabled", "disabled");
}

function updateRefresh() {
	varRefresh--;
	if (varRefresh >= 0) {
	    $("#span_refresh").html(String(varRefresh));
	    setTimeout("updateRefresh();", 1000);
	}
	else
	{
		$("#komunikat_pozdro").html("");
		$("#f_odkogo").attr("disabled", "").val("");
		$("#f_tresc").attr("disabled", "").val("");
		$("#f_wyslij").attr("disabled", "");
	}
}

function updateRefresh2() {
	varRefresh2--;
	if (varRefresh2 >= 0) {
	    setTimeout("updateRefresh2();", 1000);
	}
	else
	{
		$("#komunikat_newsletter").html("");
		$("#f_newsletter_nick").attr("disabled", "").val("");
		$("#f_newsletter_email").attr("disabled", "").val("");
		$("#f_zapisz_newsletter").attr("disabled", "");
	}
}

function writeCookie(name, value, exp)
{
	var exdate=new Date();
	var the_cookie = name+"="+value;
	exdate.setDate(exdate.getDate()+exp);
	document.cookie=the_cookie+";expires="+exdate.toGMTString();
}

function PokazUkryj(id)
{
	var res = stan(id);
	if (id	!= '')
	{
		writeCookie(id,res,90);
	}
}


function displayCalendar(m,y) 
{
	var t = new Date().getTime();
	$.ajax({
		type: "GET",
		url: "ajax.php?action=get_calendar&t="+t,
		data: "m="+m+"&y="+y,
		success: function(msg){
			$("#calendar_big").html(msg);

		}
	});
}


function stan(id)
{
	var itm = null;
	itm = document.getElementById(id);
	if (!itm)
	{
		// do nothing
	}
	else if (itm.style)
	{
		if (itm.style.display == "none")
		{
			itm.style.display = "";
			document.getElementById(id+'_arr').src = '/gfx/arrow.gif';
			document.getElementById(id+'_arr').title = 'Zwiń';
			return 1;
		}
		else
		{
			itm.style.display = "none";
			document.getElementById(id+'_arr').src = '/gfx/arrow_h.gif';
			document.getElementById(id+'_arr').title = 'Rozwiń';
			return 2;
		}
	}
	else
	{
		itm.visibility = "show";
		return 1;
	}
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(";",offset);
	if (endstr == -1)
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
		{
			break;
		}
	}
	return null;
}


function zmniejsz_obrazki()
{
	for (i=0;i<=document.getElementsByTagName("img").length;i++)
	{
		if(document.getElementsByTagName("img")[i].id == "obrazek")
		{
			document.getElementsByTagName("img")[i].src = document.getElementsByTagName("img")[i].src.replace(/title_b/gi, "title_s");
		}
	}
}

var intervalID;

var oMarquees = [], oMrunning,
	oMInterv =        20,     //interval between increments
	oMStep =          1,      //number of pixels to move between increments
	oStopMAfter =     0,     //how many seconds should marquees run (0 for no limit)
	oResetMWhenStop = false,  //set to true to allow linewrapping when stopping
	oMDirection =     'left'; //'left' for LTR text, 'right' for RTL text

/***     Do not edit anything after here     ***/

function doMStop() {
	clearInterval(oMrunning);
	for( var i = 0; i < oMarquees.length; i++ ) {
		oDiv = oMarquees[i];
		oDiv.mchild.style[oMDirection] = '0px';
		if( oResetMWhenStop ) {
			oDiv.mchild.style.cssText = oDiv.mchild.style.cssText.replace(/;white-space:nowrap;/g,'');
			oDiv.mchild.style.whiteSpace = '';
			oDiv.style.height = '';
			oDiv.style.overflow = '';
			oDiv.style.position = '';
			oDiv.mchild.style.position = '';
			oDiv.mchild.style.top = '';
		}
	}
	oMarquees = [];
}
function doDMarquee() {
	if( oMarquees.length || !document.getElementsByTagName ) { return; }
	var oDivs = document.getElementsByTagName('div');
	for( var i = 0, oDiv; i < oDivs.length; i++ ) {
		oDiv = oDivs[i];
		if( oDiv.className && oDiv.className.match(/\bdmarquee\b/) ) {
			if( !( oDiv = oDiv.getElementsByTagName('div')[0] ) ) { continue; }
			if( !( oDiv.mchild = oDiv.getElementsByTagName('div')[0] ) ) { continue; }
			oDiv.mchild.style.cssText += ';white-space:nowrap;';
			oDiv.mchild.style.whiteSpace = 'nowrap';
			oDiv.style.height = oDiv.offsetHeight + 'px';
			oDiv.style.overflow = 'hidden';
			oDiv.style.position = 'relative';
			oDiv.mchild.style.position = 'absolute';
			oDiv.mchild.style[oMDirection] = oDiv.offsetWidth + 'px';
			oMarquees[oMarquees.length] = oDiv;
			i += 2;
		}
	}
	oMrunning = setInterval('aniMarquee()',oMInterv);
	if( oStopMAfter ) { setTimeout('doMStop()',oStopMAfter*1000); }
}
function aniMarquee() {
	var oDiv, oPos;
	for( var i = 0; i < oMarquees.length; i++ ) {
		oDiv = oMarquees[i].mchild;
		oPos = parseInt(oDiv.style[oMDirection]);
		if( oPos <= -1 * oDiv.offsetWidth ) {
			oDiv.style[oMDirection] = oMarquees[i].offsetWidth + 'px';
		} else {
			oDiv.style[oMDirection] = ( oPos - oMStep ) + 'px';
		}
	}
}

function updateInfo()
{
    var now = new Date();
	
	$.getJSON('/inc/shout_info.php?ts=' + now.getTime(), function(data){
		if(data.error === 1)
		{
			$("#marqtext").html("Awaria");
			$('#listeners').html("?");
			$('#maxlisteners').html("?");
		}
		else
		{
			$('#listeners').html(data.listeners);
			$('#maxlisteners').html(data.maxlisteners);
			$("#marqtext").html(data.title);
		}
	});

	setTimeout('updateInfo()', 15000);
}