var subdomains = new Subdomains();
var informations = new Informations();
var panormamaFirm = new PanoramaFirm();

window.onload = function()
{
	if(category != undefined)
		informations.remember_category(category);
	informations.getHowMany('informacje');
}

//klasa informacji
function Informations()
{
	var informations = "informacje";
	var last;
	var firsts = new Object();
	var yet = Object( { informations: true });
	var howManyInSide = 6;
	var title = -1;
	
	this.setFirst = function(index, how)
	{
		firsts[index] = Number(how);
	}
	
	this.setTitle = function(id)
	{
		title = id;
	}
	
	this.naviagtionsNews = function(type, namePane)
	{
		var howMany = $("#howMany-" + namePane).val();

		if(firsts[namePane] == undefined)
				firsts[namePane] = 0;
		
		if(howMany < firsts[namePane] + (howManyInSide*2))
			$("#nextNews-" + namePane).css("visibility", "hidden");
		
		if(type == "next")
		{
			$("#prevNews-" + namePane).css("visibility", "inherit");
			firsts[namePane] += howManyInSide;
		}
		else if(type == "prev")
		{
			firsts[namePane] -= howManyInSide;
			if(firsts[namePane] < howManyInSide)
				$("#prevNews-" + namePane).css("visibility", "hidden");
		}
		else
			firsts[namePane] = 0;
			
		
		if(howMany > firsts[namePane] + howManyInSide)
			$("#nextNews-" + namePane).css("visibility", "inherit");

		var path = absolute_server_path + "indexAjax.php";
		$.get(path, {path: 'informations', type: namePane, first: firsts[namePane], title: title},
		function(data)
		{
			$("#div-informations-" + namePane).html(data);
		});
	}

	this.getHowMany = function(namePane)
	{
		var path = absolute_server_path + "indexAjax.php";
		$.get(path, {path: 'informations', type: namePane, option: 'howMany'},
		function(data)
		{
			$('#howMany-' + namePane).val(data);
			if(data > 6)
				$('#nextNews-'+ namePane).css("visibility", "inherit");
		});
	}

	//zmiana zakladki informacji
	this.changeTab = function(which)
	{
		if(yet[which.id] == undefined)
		{
			this.naviagtionsNews('this', which.id);
			this.getHowMany(which.id);
			yet[which.id] = true;
		}
		$("#informations-tr > .menu-item-select").attr("class", "menu-item");
		which.className = "menu-item-select";
		informations = which.id;
		$("#informations-news-td > div").attr("class", "informations-tabs-unvisible");
		$("#informations-" + informations).attr("class", "informations-tabs-visible");
		this.remember_category(which.id);
	};
	
	this.switchTab = function(item)
	{
		var subdomain_frame = $(item).parent().attr("id");

		$("#" + subdomain_frame + " > .menu-item-select").each(function (e) 
		{
			if(this.id != informations)
				$(this).attr("class", "menu-item");
		});
		
		item.className = 'menu-item-select';
	};
	
	this.mouseOut = function(item)
	{
		var subdomain_frame = $(item).parent().attr("id");

		$("#" + subdomain_frame + " > .menu-item-select").each(function (e) 
		{
			if(this.id != informations)
				$(this).attr("class", "menu-item");
		});
	};
	
	//zapamietanie kategorii
	this.remember_category = function(id)
	{
		old_link = $("#back-navigation-button").attr("href");
		if(old_link != null)
		{
			if(old_link.match(/inf_cat/) == null)
				$("#back-navigation-button").attr("href", old_link + "?inf_cat=" + id);
			else
			{
				for(var i=0; i < categories.length; i++)
					old_link = old_link.split("?inf_cat=" + categories[i] ).join("");
				$("#back-navigation-button").attr("href", old_link + "?inf_cat=" + id);
			}
		}
		
		$(".information-title-" + id).each(function (e) 
		{
			old_link = $(this).attr("href");
			if(old_link != null)
			{
				if(old_link.match(/inf_cat/) == null)
					$(this).attr("href", old_link + "?inf_cat=" + id);
			}
		}); 
		
		$(".news-title-link").each(function (e) 
		{
			old_link = $(this).attr("href");
			if(old_link != null)
			{
				if(old_link.match(/inf_cat/) == null)
					$(this).attr("href", old_link + "?inf_cat=" + id);
				else
				{
					for(var i=0; i < categories.length; i++)
						old_link = old_link.split("?inf_cat=" + categories[i] ).join("");
					$(this).attr("href", old_link + "?inf_cat=" + id);
				}
			}
		}); 
		
	};
	
	//zmiana wiadomosci po najechaniu na link
	this.changeNews = function(which)
	{
		var informationsLinks = $(which).parent().parents();
		$(informationsLinks).children("span").children("a").attr("class", "information-title-a");
		$(which).attr("class", "information-title-a-select");
		$(informationsLinks).children("span").attr("class", "informations-title");
		$(which).parent().attr("class", "informations-title-select");
		
		this.changeImage($(which).attr("id"));
		
		$("#img-" + which.id).css("display", "none").fadeIn(500);	
	};
	
	//zmiana obrazka wiadomosci
	this.changeImage = function(which)
	{
		which = "img-" + which;
		$("#"+which).parent().parent().children("div").children("img").css("display", "none").fadeOut(500);
	};
	
	//zwraca aktualna nazwe zakladki z informacjami
	this.getInformations = function()
	{
		return informations;
	};
}

function Subdomains()
{
	this.change = function(item)
	{
		var subdomain_frame = $(item).parent().attr("id");
		$("#" + subdomain_frame + " > .menu-item-select").each(function (e) 
		{
			if(this.id != portal_subdomain)
				$(this).attr("class", "menu-item");
		});
		
		item.className = 'menu-item-select';
	}
	
	this.mouseOut = function(item)
	{
		var subdomain_frame = $(item).parent().attr("id");
		$("#" + subdomain_frame + " > .menu-item-select").each(function (e) 
		{
			if(this.id != portal_subdomain)
				$(this).attr("class", "menu-item");
		});
	};
	
	//zaznaczenie aktualnej zakladki
	this.change_init = function(which)
	{
		if(which == 'mojemiasto')
		{
			which = 'portal';
			portal_subdomain = 'portal';
		}
		$("#" + which).attr("class", "menu-item-select");
			
	};
}

function PanoramaFirm()
{
	this.panoramaFirm = function() 
	{
		var form = document.forms['pfform'];
		var keyword = form.keyword.value.replace( /-/g,'~' );
		var city = form.city.value.replace( /-/g,'~' );
		var url = 'http://www.pf.pl/serwis/' + encodeURIComponent( keyword ) + '-' + encodeURIComponent( city ) + '-V1_YP.html?token=5eLVdrvCv76P3bRpR6e5N6uqxi4jiM4b';
		window.open( url );
		form.keyword.value = '';
		form.city.value = '';
		return false;
	}
	
	this.enterClick = function(e)
	{
		var keynum;
		
		if(window.event) // IE
		  keynum = e.keyCode;
		else if(e.which) // Netscape/Firefox/Opera
		  keynum = e.which;
	
		if(keynum == 13)
			this.panoramaFirm();
	}
}