/* 
 * TLM-Bibliothek
 *
 * author: A. Kuzmanovski
 * date: 24.8.2010
 * version: 1.0
 */
jQuery.noConflict();


var _utf8_encode = function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	}

	// private method for UTF-8 decoding
var	_utf8_decode = function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}


/*
 *
*/
function addUploadDialog(uploadID, tempContainerID, inputID, callback) {
    jQuery("#" + uploadID).click(function() {
     tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
     window.send_to_editor = function(html) {
        jQuery('#' + tempContainerID).html(html);
         src = jQuery("#" + tempContainerID + " img").attr("src");
         jQuery("#" + uploadID).attr("src", src);
         if (typeof(inputID) != 'undefined' && inputID != "" && inputID != null) {
          jQuery("#" + inputID).val(src);
         }
         if (callback) {
           callback();
         }
         tb_remove();
     }
     return false;
    });
}//

/*
 * Dynamische Listen für den Admin-Bereich
 *
*/
var TLMList = function (op) {
  var me = this,
      count = 0;
  if (op == undefined) {
    return;
  }
  if (op.container == undefined || op.container == "") {
    return;
  }
  op.addButtonText = op.addButtonText || "";
  op.addCallback = op.addCallback || null;
  op.removeText = op.removeText || "Remove";
  op.moveUpText = op.moveUpText || "Move Up";
  op.moveDownText = op.moveDownText || "Move Down";
  op.one = op.one || false;
  op.imagewidth = op.imagewidth || 160;

  var swapLI = function(a, b) {
    if (a != undefined && b != undefined) {
        var aID = jQuery(a).attr("id"), bID =jQuery(b).attr("id");
        for (var dataID in op.dataIDs) {
          if (op.dataIDs.hasOwnProperty(dataID)) {
            var idA = aID + "-content-" + dataID,
                idB = bID + "-content-" + dataID;
            switch (op.dataIDs[dataID]) {
              case "input":
                valueA = jQuery("#" + idA).val();
                valueB = jQuery("#" + idB).val();
                break;
              case "select":
                valueA = jQuery("#" + idA + " :selected").val();
                valueB = jQuery("#" + idB + " :selected").val();
                break;
              case "image":
                valueA = jQuery("#" + idA).attr("src");
                valueB = jQuery("#" + idB).attr("src");
                break;
              case "checkbox":
                valueA = jQuery("#" + idA).attr("checked");
                valueB = jQuery("#" + idB).attr("checked");
                break;
            }
            switch (op.dataIDs[dataID]) {
              case "image":
                jQuery("#" + idA).attr("src", valueB);
                jQuery("#" + idB).attr("src", valueA);
                break;
              case "checkbox":
                jQuery("#" + idA).attr("checked", valueB);
                jQuery("#" + idB).attr("checked", valueA);
                break;
              default:
                jQuery("#" + idA).val(valueB);
                jQuery("#" + idB).val(valueA);
            }
            jQuery("#" + idA).trigger("change");
            jQuery("#" + idB).trigger("change");
          }
        }
    }
    saveData();
  }//

  var activatedDeactivedButtons = function(nr) {
    var countLIs =jQuery("#" + op.container + "-list li").size();
    if (countLIs == 1) {
      jQuery("#" + op.container + "-list li:first").find(".moveupButton").hide();
      jQuery("#" + op.container + "-list li:first").find(".movedownButton").hide();
    } else {
      jQuery("#" + op.container + "-list li:first").find(".moveupButton").hide();
      jQuery("#" + op.container + "-list li:first").find(".movedownButton").show();
      jQuery("#" + op.container + "-list li:last").find(".moveupButton").show();
      jQuery("#" + op.container + "-list li:last").find(".movedownButton").hide();
    }
  }//

  var saveData = function () {
    var value = "", data = [];
    for (var i=0; i < count; i++) {
      var elemData = null;
      if (jQuery("#" + op.container + "-element" + i).length > 0) {
        elemData = {};
      }
      for (var dataID in op.dataIDs) {
        if (op.dataIDs.hasOwnProperty(dataID)) {
          var id = op.container + "-element" + i + "-content-" + dataID;
          if (jQuery("#" + id).length > 0) {

            switch (op.dataIDs[dataID]) {
              case "input":value = jQuery("#" +id).val();
                break;
              case "select":value = jQuery("#" + id + " :selected").val();
                break;
              case "image":value = jQuery("#" + id).attr("src");
                break;
              case "checkbox":value = jQuery("#" + id + ":checked").val();
                break;
              default: value = jQuery("#" +id).val();
                break;
            }
            elemData[dataID] = value;
          }
        }
      }
      if (elemData != null) {
        data.push(elemData);
      } 
    }
    jQuery("#" + op.container + "-data").val(JSON.stringify(data));
    //jQuery("#" + op.container + "-label").html(JSON.stringify(data));
    //alert(JSON.stringify(data));
  }//

  var addChangeEvents = function (containerID) {
    for (var dataID in op.dataIDs) {
      if (op.dataIDs.hasOwnProperty(dataID)) {
        var id = containerID + "-" + dataID;
        if (jQuery("#" + id).length > 0) {
          switch (op.dataIDs[dataID]) {
            case "input":
              jQuery("#" + id).keyup(function() {
                saveData();
              });
              break;
            default:
              jQuery("#" + id).change(function() {
                saveData();
              });
              break;   
          }
        }
      }
    }
  }//

  var addCallback = function() {
    var htm = "", liID = op.container + "-element" + count, nr = count;

    jQuery("#" + op.container + "-list li:last").find(".moveupButton").show();
    jQuery("#" + op.container + "-list li:last").find(".movedownButton").show();

    htm += "<div style=\"clear: both;\"></div>";
    htm += "<div id=\"" + liID + "-content\">";
    
    htm += "</div>";

    if (!op.one) {
      htm += "<div style=\"margin-top:10px;\">";
      htm += "<input style=\"float:right;margin-left:20px;\" id=\"" + op.container + "-remove" + count + "\" type=\"button\" value=\"" + op.removeText + "\" />";
      htm += "<input style=\"float:right;\" class=\"moveupButton\" id=\"" + op.container + "-moveup" + count + "\" type=\"button\" value=\"" + op.moveUpText + "\" />";
      htm += "<input style=\"float:right;\" class=\"movedownButton\" id=\"" + op.container + "-movedown" + count + "\" type=\"button\" value=\"" + op.moveDownText + "\" />";
      htm += "<div style=\"clear: both;\"></div>";
      htm += "</div>";
      htm += "<hr>";
    }
    jQuery("#" + op.container + "-list").append("<li id=\"" + liID + "\">" + htm + "</li>");
    if (op.addCallback != null) {
      op.addCallback(liID + "-content", count, saveData,op.imagewidth, op.one);
      addChangeEvents(liID + "-content");
    }
    
    jQuery("#" + op.container + "-remove" + count).click(function(){
      jQuery("#" + liID).remove();
      activatedDeactivedButtons();
      saveData();
    });

    jQuery("#" + op.container + "-movedown" + count).click(function(){
      if (jQuery("#" +  op.container + "-list li:last").attr("id") != liID) {
        swapLI(jQuery("#" + liID), jQuery("#" + liID).next());
      }
      
    });
    jQuery("#" + op.container + "-moveup" + count).click(function(){
      if (jQuery("#" +  op.container + "-list li:first").attr("id") != liID) {
        swapLI(jQuery("#" + liID), jQuery("#" + liID).prev());
      }
    });
    activatedDeactivedButtons(count);
    
    count++;
    saveData();
    return liID;
  };//

  var setData = function(liID, data) {
    for (var dataID in op.dataIDs) {
        if (op.dataIDs.hasOwnProperty(dataID)) {
          var id = liID + "-content-" + dataID;
          if (jQuery("#" + id).length > 0) {
            switch (op.dataIDs[dataID]) {
              case "image":
                jQuery("#" + id).attr("src", data[dataID]);
                break;
              case "checkbox":
                jQuery("#" + id).attr("checked", data[dataID]);
                break;
              default:
                jQuery("#" + id).val(data[dataID]);
                break;
            }
            jQuery("#" + id).trigger("change");
            //alert(liID + ": '" + data[dataID] + "' " + dataID);
          }
        }
      }
    saveData();
  };//

  var init = function() {
    var liID = "", data = [],
        list = "<ol id=\"" + op.container + "-list\"></ol>",
        addButtonValue = "Add " + op.addButtonText,
        addButton = "<input id=\"" + op.container + "-addbutton\" type=\"button\" value=\"" + addButtonValue + "\" />",

        hiddenData = "", //<input id=\"" + op.container + "-data\" type=\"hidden\" value=\"\" />",
        label = "<label id=\"" + op.container + "-label\"></label>";
        if (jQuery("#" + op.container + "-data").length > 0) {
          val = jQuery("#" +  op.container + "-data").val();
          if (val != "") {
            //data =  JSON.parse(unescape(val));
            data = JSON.parse(decodeURIComponent((val + '').replace(/\+/g, '%20')));
          } else {
            data = [];
          }

          //alert(unescape(jQuery("#" +  op.container + "-data").val()));
        }

    jQuery("#" + op.container).append(list + addButton + hiddenData + label);
    jQuery("#" + op.container + "-addbutton").click(addCallback);
  
    if (op.one) {
      jQuery("#" + op.container + "-addbutton").hide();
    }
    
    for (var i in data) {
      if (data.hasOwnProperty(i)) {
        liID = addCallback();
        setData(liID, data[i]);
      }
    }

    /*if (op.one && count == 0) {
      addCallback();
    }*/

  };//
  
  init();
};//Class TLMList





/* VIMEO FUNCTIONS */




/*!
 * Froogaloop Microframework.
 * Copyright 2010 Vimeo, LLC.
 * @author: Joseph Schmitt
 *
 * Froogaloop is a small Javascript utility framework to help with the complexities
 * involved in dealing with using the Javascript API through window.postMessage
 * for controlling Vimeo's Moogaloop video player. This framework provides a simple
 * API to call functions and add event listeners to Moogaloop when it is embedded
 * using an <iframe>.
 *
 * USAGE:
 * The Froogaloop framework automatically searches the document for all iframes that
 * are embeds of Vimeo videos. It then adds two new functions to the iframe HTMLElement:
 * api and addEvent. To call a function that acts upon the player (such as play, pause,
 * or seek), use the api method. To add an event listener that listens for player
 * events, use the addEvent method.
 *
 * NOTES:
 * 1. To gain JavaScript api access to the Vimeo player, you must pass in a value of 1
 *    for the js_api url parameter to the iframe.
 * 2. If you have more than one iframe per page, each iframe must have a unique id attribute
 *    set and that id must be passed to the iframe via the js_swf_id query parameter.
 *
 *
 * EXAMPLE:
 * var iframe = document.getElementById("myIframe");
 * iframe.api("api_play");
 * iframe.api("api_seekTo", 30);
 * iframe.api("api_setVolume", 80);
 *
 * iframe.addEvent("onPlay", function(player_id){
 *    // Handle onPlay event
 * });
 *
 * iframe.addEvent("onSeek", function(time, player_id){
 *    // Handle onSeek event
 * });
 */
var Froogaloop = function() {
    var
    _this = {
        hasWindowEvent: false,
        PLAYER_DOMAIN: '',
        eventCallbacks: {},
        iframe_pattern: /player\.(([a-zA-Z0-9_\.]+)\.)?vimeo(ws)?\.com\/video\/([0-9]+)/i
    },

    /**
     * Attaches itself to all vimeo iframes
     */
    init = function() {
        var iframes = document.getElementsByTagName('iframe'),
        cur_frame,
        i = 0,
        length = iframes.length,
        is_embed_iframe;

        for(i; i<length; i++) {
            cur_frame = iframes[i];
            is_embed_iframe = _this.iframe_pattern.test(cur_frame.getAttribute('src'));

            if(is_embed_iframe) {
                cur_frame.api = _that.api;
                cur_frame.addEvent = _that.addEvent;
            }
        }
    },

    //////////////////////////////////////
    //              API                 //
    //////////////////////////////////////
    _that = {
        /*
         * Calls a function to act upon the player.
         *
         * @param functionName (String): Name of the Javascript API function to call. Eg: "api_play".
         * @param params (Array): List of parameters to pass when calling above function.
         * @param target (HTML Element): Reference to the <iframe> containing the player. Optional if a
         * target was passed when instantiating Froogaloop
         */
        api: function( functionName, params )
        {
            postMessage( functionName, params, this );
        },

        /*
         * Registers an event listener and a callback function that gets called when the event fires.
         *
         * @param eventName (String): Name of the event to listen for.
         * @param callback (Function): Function that should be called when the event fires.
         * @param target (HTML Element): Reference to the <iframe> containing the player. Optional if a
         * target has been set by the Froogaloop.setTarget method.
         */
        addEvent: function( eventName, callback )
        {
            var target_id = this.id != '' ? this.id : null;
            storeCallback(eventName, callback, target_id);

            //The onLoad event is not passed to the SWF
            if(eventName != 'onLoad') {
                postMessage( 'api_addEventListener', [eventName, callback.name], this );
            }

            //Register message event listeners
            if( _this.hasWindowEvent ) {return false;}
            _this.PLAYER_DOMAIN = _utils.getDomainFromUrl(this.getAttribute('src'));

            // Listens for the message event.
            //W3C
            if( window.addEventListener ) {
                window.addEventListener('message', onMessageReceived, false);
            }
            //IE
            else {
                window.attachEvent('onmessage', onMessageReceived, false);
            }

            _this.hasWindowEvent = true;
        }
    },

    //////////////////////////////////////
    //    INTERNAL MESSAGE HANDLERS     //
    //    Do not wander down here.      //
    //    Thar be monsters.             //
    //////////////////////////////////////

    /**
     * Handles posting a message to the parent window.
     *
     * @param method (String): name of the method to call inside the player. For api calls
     * this is the name of the api method (api_play or api_pause) while for events this method
     * is api_addEventListener.
     * @param params (Object or Array): List of parameters to submit to the method. Can be either
     * a single param or an array list of parameters.
     * @param target (HTMLElement): Target iframe to post the message to.
     */
    postMessage = function( method, params, target )
    {
        if(!target.contentWindow.postMessage) {return false;}

        if( params === undefined || params === null ){params = '';}
        var
        url = target.getAttribute('src').split('?')[0],
        p = _utils.serialize({
            'method': method,
            'params': params,
            'id'    : target.getAttribute('id')
        });

        target.contentWindow.postMessage(p, url);
    },

    /**
     * Stores submitted callbacks for each iframe being tracked and each
     * event for that iframe.
     *
     * @param eventName (String): Name of the event. Eg. api_onPlay
     * @param callback (Function): Function that should get executed when the
     * event is fired.
     * @param target_id (String) [Optional]: If handling more than one iframe then
     * it stores the different callbacks for different iframes based on the iframe's
     * id.
     */
    storeCallback = function(eventName, callback, target_id)
    {
        if(target_id) {
            if(!_this.eventCallbacks[target_id]){ _this.eventCallbacks[target_id] = {}; }
            _this.eventCallbacks[target_id][eventName] = callback;
        }
        else {
            _this.eventCallbacks[eventName] = callback;
        }
    },

    /**
     * Retrieves stored callbacks.
     */
    getCallback = function(eventName, target_id)
    {
        if(target_id) {
            return _this.eventCallbacks[target_id][eventName];
        }
        else {
            return _this.eventCallbacks[eventName];
        }
    },

    /**
     * Event that fires whenever the window receives a message from its parent
     * via window.postMessage.
     */
    onMessageReceived = function(event)
    {
        // Handles messages from moogaloop only
        if(event.origin != _this.PLAYER_DOMAIN) {return false;}

        // Un-serialize the data and turn it into an object,
        // then send it to the message handler
        onMoogEvent( _utils.unserialize(event.data) );
    },

    onMoogEvent = function(data)
    {
        var
        params = data.params ? data.params.split('"').join('').split(",") : "",
        eventName = data.method,
        target_id = params[params.length-1];

        if(target_id == ''){target_id = null;}

        var callback = getCallback(eventName, target_id);
        if(!callback) return false;

        if(params.length > 0 ) callback.apply(null, params);
        else callback.call();
    },



    //////////////////////////////////////
    //        UTILITY FUNCTIONS         //
    //////////////////////////////////////
    _utils = {
        r20: /%20/g,

        isArray: function(obj)
        {
            return Object.prototype.toString.call(obj) === "[object Array]";
        },

        isFunction: function(obj)
        {
            return Object.prototype.toString.call(obj) === "[object Function]";
        },

        unserialize: function(str)
        {
            if(!str) {return false;}

            var vars = {},
            arr = str.split("&"),
            key, value,
            i = 0;

            for(i; i<arr.length; i++) {
                key = unescape(arr[i].split("=")[0]);
                value = unescape(arr[i].split("=")[1]);

                //recursively unserialize
                if( value.indexOf("=") > -1 ) {value = _utils.unserialize(value);}

                vars[key] = value;
            }

            return vars;
        },

        // Serialize an array of form elements or a set of
        // key/values into a query string
        // Shamelessly stolen and modified from jQuery 1.4 http://jquery.com
        s: false,
        serialize: function( a )
        {
            _utils.s = [];

            for ( var prefix in a )
            {
                _utils.buildParams( prefix, a[prefix] );
            }

            // Return the resulting serialization
            return _utils.s.join("&").replace(_utils.r20, "+");
        },

        buildParams: function( prefix, obj )
        {
            var v, i=0, k=0;
            if ( _utils.isArray(obj) )
            {
                // Serialize array item.
                for( i; i<obj.length; i++ )
                {
                    obj[i] = encodeURIComponent( obj[i] );
                }
                _utils.addToParam( encodeURIComponent(prefix), obj.join(',') );
            }
            else {
                // Serialize scalar item.
                _utils.addToParam( encodeURIComponent(prefix), encodeURIComponent(obj) );
            }
        },

        addToParam: function( key, value ) {
            // If value is a function, invoke it and return its value
            value = _utils.isFunction(value) ? value() : value;
            _utils.s[ _utils.s.length ] = key + "=" + value;
        },

        /**
         * Returns a domain's root domain.
         * Eg. returns http://vimeo.com when http://vimeo.com/channels is sbumitted
         *
         * @param url (String): Url to test against.
         * @return url (String): Root domain of submitted url
         */
        getDomainFromUrl: function(url)
        {
            var url_pieces = url.split('/'),
            domain_str = '',
            i = 0;

            for(i; i<url_pieces.length; i++)
            {
                if(i<3) {domain_str += url_pieces[i];}
                else {break;}
                if( i<2 ) {domain_str += '/';}
            }

            return domain_str;
        }
    }
    ;//end of Methods list
    init();
};


/* INITIALISATION */
/* ------------------------------ */
jQuery(document).ready(function() {
    //alert("TLM-Bib");
    Froogaloop();

});
