﻿	function idgTvLoadJSON(baseURL, tvPage, tvTreeId){
		altParm = 'baseURL:'+baseURL;
		altParm += '||tvPage:'+tvPage;
		altParm += '||tvTreeId:'+tvTreeId;
		$("#tvMainMenu").attr("alt", altParm);
		$("#tvMainMenu").html('<div align="center"><img src="http://g1.pcworld.pl/idg/gfx/loader.gif" alt="" /></div>');
		for(i=0; i<6; i++) {
			tmpElement = document.getElementById('navMenuOption'+i);
			if(i == tvTreeId) {
				tmpElement.className = 'actual';
			}
			else {
				tmpElement.className = '';
			}
		}
		$.ajax({
			type: "GET",
			url: baseURL+"/jsontv/json/p/"+tvPage+"/t/"+tvTreeId+"/",
			dataType: "jsonp",
			callback: tvMenuMakerJSON
		});
	}

	function tvMenuMakerJSON(response) {
		var html = "", counter = 1, pageAll, boxHeader = '';
		baseURL = getAltValue('tvMainMenu', 'baseURL');
		tvMovieId = $("#nowPlayingID").val();
		tvPage = getAltValue('tvMainMenu', 'tvPage');
		tvTreeId = getAltValue('tvMainMenu', 'tvTreeId');

		if(response) {
			if(tvTreeId == 0) {
				boxHeader = 'Najnowsze ('+response.tvIdgCfg.count+')';
			}
			$(response.tvIdgData).each(
				function() {
					if(tvTreeId == this.drzewo_id && boxHeader == '') {
						boxHeader = this.nazwa+' ('+response.tvIdgCfg.count+')';
					}
					if (this.id == tvMovieId) html += '<div class="klatkawyb" id="tvBox'+this.id+'">';
					else html += '<div class="klatka" id="tvBox'+this.id+'">';
					html += '<a href="'+baseURL+this.link+'"><img src="'+this.g1link+'" alt="iko" /></a>';
					html += '<a href="'+baseURL+this.link+'" class="opklat">'+this.tytul+'</a>';
					html += '</div>';
					if (counter % 3 == 0) html += '<div class="linia"></div>';
					counter++;
				}
			)
			counter--;
			if(counter < 18 && counter % 3 != 0) {
				html += '<div class="linia"></div>';
			}
			html = '<h2 class="tyt2">'+boxHeader+'</h2>'+html;

			pageAll = parseInt(response.tvIdgCfg.count / 18);
			if ((response.tvIdgCfg.count % 18) > 0) pageAll++;

			if(pageAll > 1) {
				html += '<div class="stronicowanie">'
				if (tvPage > 1) html += '<a onclick="idgTvLoadJSON(\''+baseURL+'\', '+(parseInt(tvPage)-1)+', '+tvTreeId+');" class="prevPage"><span>wstecz</span></a>';

				scope = parseInt(tvPage / 10);

				if ((scope * 10) >= tvPage) scope--;
				pageStart = (scope * 10);
				if (pageStart == 0) pageStart = 1;
				else pageStart++;
				pageStop = pageStart + 10;
				if (pageStop > pageAll) pageStop = pageAll+1;
				for(var a = pageStart; a < pageStop; a++) {
					if (a == tvPage) html +='<span class="thisPage">'+a+'</span>';
					else html += '<a onclick="idgTvLoadJSON(\''+baseURL+'\', '+a+', '+tvTreeId+');" class="otherPage">'+a+'</a>';
				}

				if (pageAll > tvPage) html += '<a onclick="idgTvLoadJSON(\''+baseURL+'\', '+(parseInt(tvPage)+1)+', '+tvTreeId+');" class="nextPage"><span>dalej</span></a>';
				html +='</div>'
			}
		}
		else {
			html = 'Brak materiałów w wybranej kategorii.';
		}

		$("#tvMainMenu").html(html);
	}

	function tvPlayMovie(id) {
		$("#tvBox"+$("#nowPlayingID").val()).removeClass("klatkawyb");
		$("#tvBox"+$("#nowPlayingID").val()).addClass("klatka");
		$("#tvBox"+id).removeClass("klatka");
		$("#tvBox"+id).addClass("klatkawyb");
		$("#nowPlayingID").val(id);
		baseURL = getAltValue('tvMainMenu', 'baseURL');
		document.location.hash = 'tvScreen';
		przekaz(id);
		idgTvRelatedLoadJSON(baseURL, id);
	}

	function przekaz(str) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		var IE = 0;
		if(isIE){
			IE = 1;
		}
		thisMovie("js_f").asFunc(str,IE);
		return false;
	}


	function thisMovie(movieName)
	{
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
    	return (isIE) ? window[movieName] : document[movieName];
	}

	function idgTvRelatedLoadJSON(baseURL, id){
		$("#div_z_1_html").html('<div align="center"><img src="http://g1.pcworld.pl/idg/gfx/loader.gif" alt="" /></div>');
		$.ajax({
			type: "GET",
			url: baseURL+"/jsontv/jsonrel/id/"+id+"/",
			dataType: "jsonp",
			callback: tvRelatedMakerJSON
		});
	}

	function tvRelatedMakerJSON(response){
		var html = '';
		baseURL = getAltValue('tvMainMenu', 'baseURL');
		if(response) {
			$(response.tvIdgData).each(
				function() {
					html += '<div class="box_sc">';
					html += '<a href="'+baseURL+this.link+'"><img src="'+this.g1link+'" alt="'+this.tytul+'" class="iko_lewo" alt="" /></a>';
					html += '<h3 class="tyt_m"><a href="'+baseURL+this.link+'">'+this.tytul+'</a></h3>';
					html += '<p>'+this.zajawka+'</p>';
					html += '</div><div class="clearb"></div>';
				}
			)
			$("#div_z_1_html").html(html);
			$("#relatedBox").css("display", "");
		}
		else {
			$("#relatedBox").css("display", "none");
		}
	}

	function getAltValue(divBox, valueName) {
		var value, tmp, tmp2;
		value = $("#"+divBox).attr("alt");
		tmp = value.indexOf(valueName+':');
		if(tmp != -1) {
			tmp=tmp+valueName.length+1;
			tmp2 = value.indexOf('||', tmp);
			if(tmp2 == -1) {
				tmp2 = value.length;
			}
			value = value.substr(tmp, tmp2-tmp);
		}
		else {
			value = '';
		}
		return value;
	}
