﻿var currentTeaser = 0;
var currentGuide = 0;
var clickInProgress = false; 
var urlBase = 'http://tvpol.vo.llnwd.net/d1/';
var urlBase2 = 'http://www.tvpolonia.com/';


var mycarousel_itemList = [
    { url: urlBase+'filmStrip/siostryS.jpg', title: 'Siostry: Saturdays', description: 'Newest series "Siostry". The usual quiet life of nuns is full of adventures and challenges. See how the sisters will cope with the new situation and neighbors. ' },
	{ url: urlBase+'filmStrip/krolowieSrodmiesciaS.jpg', title: 'Królowie śródmieścia: Sundays', description: 'Comedy story of several inhabitants of Warsaw, whose destinies intertwine in various situations. There are also a very funny Vietnamese bar-owners, who bravely resist the exotic Polish reality. ' },
    { url: urlBase+'filmStrip/domNadRozlewiskiemS.jpg', title: 'Dom nad rozlewiskiem: Saturdays', description: 'This movie series is directed to all those, who value home as the most important place of family life. ' },
	{ url: urlBase+'filmStrip/sforaS.jpg', title: 'Sfora', description: 'Criminal movie series, eponymous gang is a group of friends, of whom three are working in the police, and one of the prosecution.' },
	{ url: urlBase+'filmStrip/pitbullS.jpg', title: 'Pitbull: Thursdays', description: 'Business and private lives of Warsaw police officers. Many motives used in series refers to the actual cases which recently amended public opinion.' },
	{ url: urlBase+'filmStrip/klanS.jpg', title: 'Klan: Mon-Fri', description: 'The film series happens in real time and presents reality of everyday life for many Poles.' },			
	{ url: urlBase+'filmStrip/plebaniaS.jpg', title: 'Plebania: Mon-Fri', description: 'Tulczyn, small town in East Poland is the place where the movie series takes place. The authors of this series do not seek sensations, and show life as it is, true. ' },
	{ url: urlBase+'filmStrip/naDobreInaZleS.jpg', title: 'Na dobre i na złe: Fridays', description: 'Popular movie series presenting lives and affairs of local doctors in "Leśna Góra" hospital.' },
	{ url: urlBase+'filmStrip/mJakMiloscS.jpg', title: 'M jak miłość: Saturdays and Sundays', description: 'One of the most popular family series voted by viewers. It has been broadcasted since 2000 and still is at the forefront in the size of its audience.' },
	{ url: urlBase+'filmStrip/barwySzczesciaS.jpg', title: 'Barwy szczęścia: Mon, Tue, Wed', description: 'Two generations of “Zaciszna” street residents faced with daily problems. Younger are looking for their "road of life", as elders are trying to find themselves in the new reality.' },
	{ url: urlBase+'filmStrip/ekspresS.jpg', title: 'Magazyn Ekspresu Reporterów - Thursdays', description: 'The "Magazine" reporters are where there is something important. Their subjects are ordinary people their concern, tragedies, joy and success. They do not circumvent provoking topics and unmasking fraud and lawlessness.' },
    { url: urlBase+'filmStrip/forumS.jpg', title: 'Forum - Mondays', description: 'Opposite views, conflicting opinions in a hot discussion where all parliamentary clubs representatives dispute current political affairs.' },
    { url: urlBase+'filmStrip/sportS.jpg', title: 'Sport', description: 'Sporting events, news and updates' },
    { url: urlBase+'filmStrip/teleexpressS.jpg', title: 'Teleexpress - daily', description: 'Fastest. Longest period of time in the timetable among the Polish information programs. Fifteen minutes express travels by newest and most important information from the country and the world.' },
    { url: urlBase+'filmStrip/tomaszLisS.jpg', title: 'Tomasz Lis na żywo: Wedenesdays', description: 'Controversy and emotions – live public program hosted by one of the most respected Polish journalist. ' },
    { url: urlBase+'filmStrip/wartoRozmawiacS.jpg', title: 'Warto rozmawiać: Tuesdays', description: 'Topical issues moving public opinion and hot debate. The discussion in the program include opposite truths, arguments and the exploration of possible solutions.' },
    { url: urlBase+'filmStrip/rozrywkaS.jpg', title: 'Entertainment in tvpolonia', description: 'Many fun and exciting entertainment shows, concerts, stand-up comedy, game shows...' },
    { url: urlBase+'filmStrip/szansaNaSukcesS.jpg', title: 'Szansa na sukces: Sundays', description: 'The biggest hits of Polish music scene, performed by amateurs, which are accompanied by the stars themselves as jurors. ' },
    { url: urlBase+'filmStrip/wideotekaS.jpg', title: 'Wideoteka dorosłego człowieka: Saturdays', description: 'Maria Szabłowska and Krzysztof Szewczyk invite viewers to remember last century music. Host stars in the program and tell the story of contemporary atmosphere, the compositors, fans and fashion...' },
    { url: urlBase+'filmStrip/dobranockaS.jpg', title: 'Dobranocka: daily', description: 'Cartoons for the youngest' },
    { url: urlBase+'filmStrip/domisieS.jpg', title: 'Domisie: Tuesdays', description: 'Childrens program, play with "Domisie".' },
    { url: urlBase+'filmStrip/budzikS.jpg', title: 'Budzik: Thursdays', description: 'These are not ordinary cats: they can speak! Budzik and Ruda broadcast their own television. Dwarf pixel helps them out, as no one knows more than him about electronics.' },
    { url: urlBase+'filmStrip/podrozeKulinarneS.jpg', title: 'Podróże kulinarne: Saturdays ', description: 'The culinary detective known in the various corners of the world is seeking national specialties and interesting rites.' },
    { url: urlBase+'filmStrip/bosoPrzezSwiatS.jpg', title: 'Boso przez świat... - Saturdays', description: 'Not only he examines distant corners of our globe, but also wins prestigious awards. Mr Wojciech leads in Empik polls of "Bestsellers 2008".' },
    { url: urlBase+'filmStrip/dzikaPolskaS.jpg', title: 'Dzika Polska: Saturdays ', description: 'World of wild animals, nature and people who protect it. Expeditions take place in the most secluded areas in Poland.' },
    { url: urlBase+'filmStrip/teatrS.jpg', title: 'Teatr TV', description: 'Theatrical spectacles' },
	{ url: urlBase+'filmStrip/trzeciOficerS.jpg', title: 'Trzeci oficer: Fridays', description: 'Criminal movie series, which is the continuation of series "Oficerowie" and "Oficer". The Main subject of this one is fighting the terrorists.' }
];


//0=CST, 1=NST, 2=AST, 3=EST, 4=CST, 5=MST, 6=PST, 7=YDT
var times = new Array();
var guide = new Array();



$(document).ready(function(){ 
	getGuide('0',0,1);	//initial value fillup
});

function getGuide(forDate,dayID)
{
	
	//for (var i=0;i<7;i++)
	$(".link_"+day_number).removeClass("currentDate");

	day_number = dayID;
	
	var _url = urlBase2+'helper/get_guide_data.php?dayID='+dayID;
	
	if (forDate!='0')
		_url = _url + '&date='+forDate;
	
	$(".link_"+dayID).addClass("currentDate");
	$("#guide_content").html('<img class="loaderGif" src="'+urlBase2+'graphics/ajax-loader.gif" />');

	
	$.getJSON(_url, function(json) {	
		guide = new Array();
		times = new Array();
		
		$.each(json.guide,function(i,item) {
		
			if (item.title != "undefined") {
				var itemid = parseInt(item.id);
				guide[itemid] = item.title;
				times[itemid] = new Array();
				//0=CST, 1=NST, 2=AST, 3=EST, 4=CST, 5=MST, 6=PST, 7=YDT
				times[itemid][0] = new Array();
				times[itemid][0][0] = item.startH_CEST;
				times[itemid][0][1] = item.startM_CEST;
				
				times[itemid][1] = new Array();
				times[itemid][1][0] = item.startH_NST;
				times[itemid][1][1] = item.startM_NST;

				times[itemid][2] = new Array();
				times[itemid][2][0] = item.startH_AST;
				times[itemid][2][1] = item.startM_AST;
				
				times[itemid][3] = new Array();
				times[itemid][3][0] = item.startH_EST;
				times[itemid][3][1] = item.startM_EST;
				
				times[itemid][4] = new Array();
				times[itemid][4][0] = item.startH_CST;
				times[itemid][4][1] = item.startM_CST;
				
				times[itemid][5] = new Array();
				times[itemid][5][0] = item.startH_MST;
				times[itemid][5][1] = item.startM_MST;
				
				times[itemid][6] = new Array();
				times[itemid][6][0] = item.startH_PST;
				times[itemid][6][1] = item.startM_PST;
				
				times[itemid][7] = new Array();
				times[itemid][7][0] = item.startH_YDT;
				times[itemid][7][1] = item.startM_YDT;
				
				
			}
			
		});
		
		if (arguments.length == 2)
			renderDay(dayID);
	});
	
}
	
function renderDay(dayID)
{
	for (var i=0;i<7;i++)
		$(".link_"+i).removeClass("currentDate");
	$(".link_"+dayID).addClass("currentDate");	
	//fill up stuff
	var content = '<div id="day_'+dayID+'" class="guideContent" style="display:block !important">';
	
	var tz = document.getElementById('timezone').value;
	//alert('tz='+tz+' day='+dayID);
	
	
	for (var i=1;i<guide.length;i++){
		//alert(i + 'tz = '+tz + ' day = '+dayID+' aH = '+times[i][tz][0]);
		var actH = times[i][tz][0];
		var actM = times[i][tz][1];
		var title = guide[i];
		content = content+'<div class="startTime">'+actH+':'+actM+'</div><div class="entryTitle">'+title+'</div><br class="clear" />';
	}
	
	content = content + '<br/></div>';	
	$("#guide_content").html(content);

	document.getElementById('overlay').style.display='none';
}
	
function changetimezones(val) {
	$(".overlay").fadeIn("fast");
	$(".overlay").css("margin-top","3px");
	$(".guideList").css("margin-top","-298px");
}
	
function moveToLocal() { 
	var y = new Date().getFullYear();
	var m = new Date().getMonth()+1;
	var d = new Date().getDate();
	var str = 'index.php?yset='+y;
	var str2 = '&mset='+m;
	var str3 = '&dset='+d;
	window.location.href=str+str2+str3;
}

var userAgent = navigator.userAgent.toLowerCase();
var isIe6 = userAgent.search("msie 6.0");
var isIe7 = userAgent.search("msie 7.0");
var isSafari = userAgent.search("safari");


function fadeNow() { 
	$(".imgBg img").fadeIn("slow", function() { 
		clickInProgress = false;
		currentTeaser=parseInt(currentTeaser)+1;
	});	
};

function updateTeaser(currElement) { 
	if(!clickInProgress) {	
		clickInProgress = true;
		var newImg = mycarousel_itemList[currentTeaser].url;
		
		$(".filmTitle").text(mycarousel_itemList[currentTeaser].title);
		$(".filmSD").text(mycarousel_itemList[currentTeaser].description);			
		var newSrc = newImg.substr(0, newImg.length-5) + "B.jpg";
		$(".imgBg img").fadeOut("fast", function() { 
			if(isIe6 != -1 || isIe7 !=-1) { 
				$(".imgBg img").attr("src",newSrc);
				fadeNow();				
			} else { 
				$(".imgBg img").attr("src",newSrc).load(fadeNow);	
			} 
		});
		
		if(currentTeaser==mycarousel_itemList.length-1) { 
			currentTeaser=0;		
		}	
	}
};

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1], parseInt(idx)-1));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

function mycarousel_initCallback(carousel)
{
    carousel.clip.hover(function() {
		Versions.use('jquery', 'default')(".mask").stopTime("autoMove");
        carousel.stopAuto();
    }, function() {
		Versions.use('jquery', 'default')(".mask").everyTime(6000, "autoMove", function() { 
			updateTeaser(currentTeaser);		
		});			
        carousel.startAuto(); 
    });	

};

function mycarousel_getItemHTML(item, index)
{	
    return '<div class="itemMask"></div><a href="#"><img src="' + item.url + '" width="111" height="93" name="'+ index +'" alt="' + item.title + '" /></a>';
};

function tvPolonia(url,windowname,w,h,x,y){
	window.open(url,windowname,"resizable=no,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+w+",height="+h+",left="+x+",top="+y+"");
}

jQuery(document).ready(function() {
	Versions.use('jquery', 'latest')(function($) {
	
		Versions.use('jquery', 'default')('#filmStrip').jcarousel({
			start: 1,
			scroll: 1.5,
			animation: "slow",
			auto: 1,
	        wrap: 'circular',
	        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
	        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
			initCallback: mycarousel_initCallback
		});
		
		var newImg = mycarousel_itemList[currentTeaser].url;
		var newSrc = newImg.substr(0, newImg.length-5) + "B.jpg";
		$(".imgBg img").fadeOut("fast", function() { 
			$(".imgBg img").attr("src",newSrc);
			$(".filmTitle").text(mycarousel_itemList[currentTeaser].title);
			$(".filmSD").text(mycarousel_itemList[currentTeaser].description);		
			$(".imgBg img").fadeIn("fast", function() { 
				clickInProgress = false;
				currentTeaser=parseInt(currentTeaser)+1;
			});
		});	

		$("li").live("click", function(){ 		
			var currId = $(this).find("img").attr("name");		
			currentTeaser = currId;
			updateTeaser(currentTeaser);
			
			return false;
		});	
		
		$(".btn_player").click(function() { 
			tvPolonia("http://www.tvpolonia.com/player/","TvPolonia.com Player", 800, 800, 0, 0);
			return false;
		});

		Versions.use('jquery', 'default')(".mask").everyTime(8000, "autoMove", function() { 
				updateTeaser(currentTeaser);		
			});	
		});			

});