/***************************************************************
*  Copyright notice
*
*  (c) 2009 Ralf Hettinger <ng@ralfhettinger.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

	var tx_broadcastplaylist_lastResultList = null;
	var tx_broadcastplaylist_loadingImage = null;
	var tx_broadcastplaylist_splitDate = '.';

	var tx_broadcastplaylist_pi2_initDate;
	var tx_broadcastplaylist_pi2_initHour;
	var tx_broadcastplaylist_pi2_initMinute;

	var tx_broadcastplaylist_pi2_listType = '';

	var tx_broadcastplaylist_pi2_timezoneOffset = 120 //given in minutes: offset of server time to UTC
	var tx_broadcastplaylist_pi2_timezoneCorrection = 0;



/* main */

window.addEvent('domready', function() {

	// reinit searchform and list (necessary if cached)
	tx_broadcastplaylist_initView();
});



	function tx_broadcastplaylist_initView() {
		tx_broadcastplaylist_initTimeData();
		tx_broadcastplaylist_initPlaylistSearchform();
		tx_broadcastplaylist_initPlaylistResultlist();
	}



	function tx_broadcastplaylist_initTimeData() {
		var localTime = new Date()
		tx_broadcastplaylist_pi2_timezoneCorrection = (localTime.getTimezoneOffset() + tx_broadcastplaylist_pi2_timezoneOffset) * 60 * 1000;
		tx_broadcastplaylist_pi2_timezoneCorrection = 0;
		var now = new Date();
		now.setTime(now.getTime() + tx_broadcastplaylist_pi2_timezoneCorrection);

		var day = now.getDate() + "";
		if (day.length == 1) day = "0" + day;
		var month = now.getMonth() + 1 + "";
		if (month.length == 1) month = "0" + month;
		var year = now.getYear();
		if (year < 999) year += 1900;
		tx_broadcastplaylist_pi2_initDate = day + tx_broadcastplaylist_splitDate + month + tx_broadcastplaylist_splitDate + year;

		tx_broadcastplaylist_pi2_initHour = now.getHours();

		tx_broadcastplaylist_pi2_initMinute = now.getMinutes();
	}



	function tx_broadcastplaylist_initPlaylistSearchform() {
		
		// datesel
		if (document.getElementById('datesel')) {
			$('datesel').value = tx_broadcastplaylist_pi2_initDate;
		}

		// hoursel - substract one hour if > 0 ?
		var searchFromHour = tx_broadcastplaylist_pi2_initHour;
		/*
		if (tx_broadcastplaylist_pi2_initHour > 0) {
			searchFromHour-= 1;
		}
		*/
		if (document.getElementById('hoursel')) {
			$$('#hoursel option').each(function(optEl){
				if (optEl.value == searchFromHour) {
					optEl.selected = true;
				}
			});
		}

		// minsel
		if (document.getElementById('minsel')) {
			var optNotSet = true;
			$$('#minsel option').each(function(optEl){
				if (optNotSet && optEl.value >= Math.floor(tx_broadcastplaylist_pi2_initMinute/5) * 5) {
					optEl.selected = true;
					optNotSet = false;
				}
			});
		}
	}



	function tx_broadcastplaylist_initPlaylistResultlist() {
		var data = 'tx_broadcastplaylist_pi2[date]=' + tx_broadcastplaylist_pi2_initDate + '&tx_broadcastplaylist_pi2[hour]=' + tx_broadcastplaylist_pi2_initHour + '&tx_broadcastplaylist_pi2[minute]=' + tx_broadcastplaylist_pi2_initMinute;
		var url = location.href + '?type=5589';

		// run 1: click searchform, if available
		var updatedBySearchform = false;
		$$('.tx_broadcastplaylist_submitbutton').each(function(inputButton){
			inputButton.click();
			updatedBySearchform = true;
		});

		// run 2: update, if no searchform found
		if (!updatedBySearchform) {
			$$('.tx_broadcastplaylist_resultlist_container').each(function(resultList){
				tx_broadcastplaylist_search(data, resultList, url);
			});
		}
	}



	function tx_broadcastplaylist_browse(page) {
		var data = 'tx_broadcastplaylist_pi2[date]=' + tx_broadcastplaylist_pi2_initDate + '&tx_broadcastplaylist_pi2[hour]=' + tx_broadcastplaylist_pi2_initHour + '&tx_broadcastplaylist_pi2[minute]=' + tx_broadcastplaylist_pi2_initMinute;
		var url = location.href + '?type=5589' + '&tx_broadcastplaylist_pi2[listbrowse]=' + page;

		$$('.tx_broadcastplaylist_resultlist_container').each(function(resultList){
			tx_broadcastplaylist_search(data, resultList, url);
		});
	}



	function tx_broadcastplaylist_submit(buttonid) {
		document.getElementById(buttonid).click();
	}



	function tx_broadcastplaylist_search(form, resultid, url) {
		var tx_broadcast_resultlist = $(resultid);

		if (tx_broadcastplaylist_pi2_listType) {
			var listType = tx_broadcastplaylist_pi2_listType;
		} else {
			var listType = 'ul';
		}

		// send request, Mootools 1.2
		var tx_broadcastplaylist_updater = new Request({
			url		: url,
			method		: 'post',
			data		: form,
			onRequest	: function() {

				// style the effect during request

				// some opacity
				if (tx_broadcast_resultlist) {
					tx_broadcast_resultlist.setStyle('opacity', 0.75);
				}

				// loading image
				if($$('.tx_broadcastplaylist_resultlist_loading')) {
					$$('.tx_broadcastplaylist_resultlist_loading').each(function(el){
						el.setStyle('margin-left', '120px');
						el.setStyle('margin-top', '50px');
						el.setStyle('display', 'block');
					});
				}

			},
			onSuccess	: function(responseObj){
				if (tx_broadcast_resultlist) {

					// the old list
					var resultList = tx_broadcast_resultlist.getFirst(listType);

					// make the list invisibile
					tx_broadcast_resultlist.setStyle('opacity', 0.0);

					// append the response and store it - and slide it in.
					tx_broadcast_resultlist.set('html', responseObj);
					tx_broadcastplaylist_lastResultList = tx_broadcast_resultlist.getFirst(listType);
					if (tx_broadcastplaylist_lastResultList) {
						var myFx = new Fx.Slide(tx_broadcastplaylist_lastResultList, {
							mode: 'horizontal',
							duration: 500,
							onComplete: function(){
								// turn off loading image
								if($$('.tx_broadcastplaylist_resultlist_loading')) {
									$$('.tx_broadcastplaylist_resultlist_loading').each(function(el){
										el.setStyle('margin-top:', '-9999px');
									});
								}
							}
						}).slideOut().chain(function() {

							// make list visible again.
							tx_broadcast_resultlist.setStyle('opacity', 1.0);
							this.slideIn('horizontal');

						});
					}
				}

				// re-initialize the tooltips, if available
				if($$('.tracktip')) {
					var trackTip = new Tips($$('.tracktip'), {
						className	: 'track-tips',
						fixed		: 1,
						offsets		: {x: 0, y: 15},
						onShow		: function(tip) {tip.set('tween', {duration: 250}).fade('in');},
						onHide		: function(tip) {tip.fade('out');}
					});
				};
			}
		}).send();
	}

