/**
 * the following are jslint (http://www.jslint.com) options
 * the syntax is very specific, especially there is no space
 * after the first asterisk
 *
 * the first allows common browser globals like window and document
 * the second says that Drupal and $ are valid global variables
 */
/*jslint browser: true */
/*extern Drupal, $ */


// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie("single").sendEvent(typ,prm); }
function __getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout(function(){getItemData(currentItem);},100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 === undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
}

// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie("single").loadFile(obj); }
function addItem(obj,idx) { thisMovie("single").addItem(obj,idx); }
function removeItem(idx) { thisMovie("single").removeItem(idx); }
function getItemData(idx) {
	var obj = thisMovie("single").itemData(idx);
	var nodes = "";
	for(var i in obj) { 
		nodes += "<li>"+i+": "+obj[i]+"</li>"; 
	}
	document.getElementById("debug-data").innerHTML = nodes;
}

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

$(document).ready(
	function(){
		$('.playlist ul').width($('.playlist li img').size() * 96);

		if($.fn.jVertScrollPane){
			$('.playlist').jVertScrollPane(
				{
					dragMinWidth: 25,
					dragMaxWidth: 325,
					scrollbarMargin: 0,
					scrollbarWidth: 15,
					showArrows: false
				}
			);
		}

		$('.playlist li').click(
			function(){
				var i = $('.playlist li').index(this);
				$('#single')[0].sendEvent('ITEM', i);
			}
		);
    
    $('.works-board li a').not('.add-link').click(
      function(){
        if (this.hash) {
          $(this.hash).show();
          $('.works-board tbody tr').not(this.hash).hide();
          
          $('.works-board a.active').removeClass('active');
          $(this).addClass('active')[0].blur();
          return false;
        }
      }
    ).filter(':first').trigger('click');
    
    // set the height of the table since there might be more or less than 1 row
    // and we dont want the page jumping around.
    $('.works-board table').each(
      function(){
        $(this).height($(this).height()).css('overflow', 'hidden');
      }
    );
    
//		$('body').append('<ul id="debug-data"></ul>');

	}
);