String.prototype.startsWith = function(s) {return (s==null)? false : (this.substring(0, s.length)==s);}
String.prototype.endsWith = function(s) {return (s==null)? false : (this.substr(this.length-s.length,s.length)==s);}
/*
Copyright (c) 2007, Parsek d.o.o. All rights reserved.
version: 0.1.0
*/
var PARSEK;
var YAHOO;

if (!PARSEK)
		PARSEK = {VERSION: 1};

/*
var s = document.getElementsByTagName('script');
for(i=0;i<s.length;i++) {
	var l = s[i].src.length;
	alert('startsWith: '+s[i].src);
	if(s[i].src.startsWith('/external/')) {
		var c = 17;
	} else {
		var c = 7;
	}
	var p = s[i].src.substr(c,l);
	var b = p.split('/');
	var uh = 'http://'+b[0];
	alert('UH: '+uh);
}
*/

var uh='http://simobil2.parsek.tv';
/*
	The var uh currently isn't used, because it breakes the behaviour in IE.
*/

/**
  * @class
  * The util utility provides functions to clear memory leaks.
  * It also tries to automatically load necessary javascript libraries.
  * @constructor
  */
  PARSEK.util = {
         /**
          * Name of this package
          * @type string
          */
         NAME: "util",
         /**
          * URL of server where PJT is installed
          * @type string
          * @private
          */
         ServerHome: "http://javascript.parsek.net",
         /**
          * YUI libraries that need to be loaded
          * @type array
          * @private
          */
         RequiredLibs: [["Anim", "animation"], ["Connect", "Connection"],
                        ["DragDrop", "Dragdrop"]],

         /**
          * Extends object with properties and functions of another one
          *
          * @method extendObject
          * @param {Object} Element to be extended
          * @param {Object} Object with new properties
          */
         extendObject: function(el,obj) {
                  if (!el) el = {};
                  for (var prop in obj) el[prop] = obj[prop];
                  return el;
         },
         /**
          * Breaks memory leak causing cycles by removing functions from
          * DOM nodes.
          *
          * @method purgeLeaks
          * @param {HTMLElement} Element to be cleansed
          */
         purgeLeaks: function (el) {
                 // Break reference cycles using Douglas Crockford algorithm
                 // (http://javascript.crockford.com/memory/leak.html)
                 var d = YAHOO.util.Dom.get(el);
                 var a = d.attributes, i, l, n;
                 if (a) {
                         l = a.length;
                         for (i = 0; i < l; i += 1) {
                                 n = a[i].name;
                                 if (typeof d[n] === 'function') {
                                         d[n] = null;
                                 }
                         }
                 }
                 a = d.childNodes;
                 if (a) {
                         l = a.length;
                         for (i = 0; i < l; i += 1) {
                                 PARSEK.util.purgeLeaks(d.childNodes[i]);
                         }
                 }
         },

         /**
          * Breaks memory leak causing cycles by removing functions from all
          * body DOM nodes (used when document unloads).
          *
          * @method purgeDocumentLeaks
          */
         purgeDocumentLeaks: function () {
                 var body = document.getElementsByTagName("body")[0];
                 if (body) PARSEK.util.purgeLeaks(body);
         },

         /**
          * Return elements with specified attribute or when value is set, their
          * subset with specified value.
          **/
         getElementsByAttribute: function (attribute, value, tag, root) {
              if (!attribute) return [];

			  var t = {
				  colspan:   "colSpan",
				  rowspan:   "rowSpan",
				  valign:    "vAlign",
				  datetime:  "dateTime",
				  accesskey: "accessKey",
				  tabindex:  "tabIndex",
				  enctype:   "encType",
				  maxlength: "maxLength",
				  readonly:  "readOnly",
				  longdesc:  "longDesc"
				};

              var r = (root) ? YAHOO.util.Dom.get(root) : document;
              var t = (tag) ? tag : "*";
              var elms, nodes = [];

              if (!r) r = document;
              elms = r.getElementsByTagName(t);

			  var attr = t[attribute] || attribute;
              for(var i=0;i<elms.length;i++) {
					if (elms[i].getAttributeNode(attr)) {
					
//                     if (elms[i].hasAttribute && elms[i].hasAttribute(attribute)) {
							var node = elms[i].getAttributeNode(attr);
							var elm_value = node.specified ? node.nodeValue : elms[i].getAttribute(attr);
                            if (value && elm_value != value) {
                                   continue;
							}
					} else continue;
                     nodes[nodes.length] = elms[i];
              }
              return nodes;
         }
};

/**
  * @class
  * The ui class provides functions for effects and progressive enhancements for
  * user interface.
  * @constructor
  */

PARSEK.util.Loader = {
         /**
          * URL of server where PJT is installed
          * @type string
          * @private
          */
        ServerHome: "http://javascript.parsek.net",
         /**
          * YUI libraries that need to be loaded
          * @type array
          * @private
          */
        TestObjects: {"animation" : "Anim", "connection": "Connect",
				"dragdrop": "DragDrop", "ui": "ui.DDApp"},

        // PJT ui is dependant on YUI dragdrop, so every UI function needs it :(
		LibDependencies: {"parsek-stripe": {"yui": [], "pjt": ["ui"],
						"el" : ["ul", "ol", "table"], "type": "class"},
					"parsek-expandable": {"yui": ["animation"], "pjt": ["ui"],
						"el": ["*"], "type": "class"},
					"parsek-detailed": {"yui": [], "pjt": ["ui"],
						"el": ["*"], "type": "class"},
					"parsek-sort": {"yui": ["dragdrop"], "pjt": ["ui"],
						"el": ["*"], "type": "class"},
                    "lightbox": {"yui":[], "pjt": ["ui"],
                        "el": ["a"], "type": "rel"}},

		Handlers: { "parsek-stripe": "PARSEK.ui.stripeLines",
					"parsek-expandable" : "PARSEK.ui.toggleDialog",
					"parsek-detailed": "PARSEK.ui.toggleDetails",
					"parsek-sort": "PARSEK.ui.DDApp.init",
                    "lightbox": "PARSEK.ui.Lightbox.init"},

		MatchingElements: {},

		MatchingLibs: { "yui": {}, "pjt": {} },

         /**
          * Load necessary YUI libraries.
          *
          * @method loadCore
          */
		loadCore: function (e) {
			var load = PARSEK.util.Loader;
			 var ld = load.LibDependencies;
			 var ml = load.MatchingLibs;
			 var me = load.MatchingElements;
			 var to = load.TestObjects;
			 var head = document.getElementsByTagName("head")[0];
			 var scr, elms;
			 var pel, i, j;

			 if(!YAHOO || !YAHOO.util || !YAHOO.util.Dom) {
					// Dom should be loaded. If not, restart a bit later
					window.setTimeout("PARSEK.util.Loader.loadCore()", 100);
					return;
			 }

			 // Find elements and needed libraries
			 for(pel in ld) {
				for(i=0;i<ld[pel]["el"].length;i++) {
                    if (ld[pel]["type"] == "class")
    					elms = YAHOO.util.Dom.getElementsByClassName(pel, ld[pel]["el"][i]);
                    else {
                        elms = PARSEK.util.getElementsByAttribute(ld[pel]["type"], pel, ld[pel]["el"][i]);
					}

					if (!elms.length) continue;

					// Save found elements
					if (!me[pel])
						me[pel] = elms;
					else {
						me[pel].concat(elms);
					}
					// Save dependencies
					for(j=0;j<ld[pel]["yui"].length;j++)
						ml["yui"][ld[pel]["yui"][j]] = true;
					for(j=0;j<ld[pel]["pjt"].length;j++)
						ml["pjt"][ld[pel]["pjt"][j]] = true;
				}
			}

			// Add YUI libraries
			for(pel in ml["yui"]) {
				if (!eval("YAHOO.util."+to[pel])) {
					scr = document.createElement("script");
					scr.type = "text/javascript";
					/*	
						scr.src = PARSEK.util.ServerHome+"/"+PARSEK.VERSION
						+"/yui/build/"+pel+"/"+pel+".js";
					*/
					scr.src = "/_common/js/yui/"+pel+"/"+pel+".js";
					head.appendChild(scr);
				}
			}
			PARSEK.util.Loader.attachHandlers();
		},

         /**
          * Attach handlers for PJT elements found on page
          *
          * @method attachHandlers
          */
		attachHandlers: function () {
            var elms, i, pel, scr;
            var head = document.getElementsByTagName("head")[0];
            var ml = this.MatchingLibs;
            var to = this.TestObjects;
            var libs = ["YAHOO", "YAHOO.util"];

            // quit if this function has already been called
           if (arguments.callee.done) return;

           for(pel in ml["yui"])
               libs[libs.length] = "YAHOO.util."+to[pel];

           // Check if YAHOO libs are loaded
           for(i=0;i<libs.length;i++)
               if (!eval(libs[i])) {
                   window.setTimeout("PARSEK.util.Loader.attachHandlers()", 100);
                   return;
               }

           // Load PARSEK libs, if not loaded yet (check, if libs have already been
           //     added to the page)
           if (!arguments.callee.parsek) {
               // Add libs to page
               for(pel in ml["pjt"]) {
                   scr = document.createElement("script");
                   scr.type = "text/javascript";
					// scr.src = PARSEK.util.ServerHome+"/"+PARSEK.VERSION
					// +"/"+pel+".js";
                   scr.src = "../lib/"+pel+".js";
				   
                   head.appendChild(scr);
               }
               arguments.callee.parsek = true;
           }

           // Now check, if Parsek libs are actually loaded
           libs = ["PARSEK"];
           for(pel in ml["pjt"])
               libs = libs.concat(["PARSEK."+pel, "PARSEK."+to[pel]]);

           for(i=0;i<libs.length;i++)
               if (!eval(libs[i])) {

                   window.setTimeout("PARSEK.util.Loader.attachHandlers()", 100);
                   return;
               }

           // flag this function so we don't do the same thing twice
           arguments.callee.done = true;

             // Add handlers
             for(pel in this.MatchingElements) {
                    elms = this.MatchingElements[pel];

                    for(i=0;i<elms.length;i++)
                           eval(this.Handlers[pel]+"(elms[i])")
             }

            // Add leak remover on page unload
            YAHOO.util.Event.addListener(window, "unload", PARSEK.util.purgeDocumentLeaks);
		}
};

/*
  * Initalize toolkit on load.
  */
(function ( ) {
         var head = document.getElementsByTagName("head")[0];
         var scr;

         // Always load YUI Dom and Event utilities, if not loaded yet.
		 // Load the packed version
         if(!YAHOO || !YAHOO.util || !YAHOO.util.Dom) {
                 scr = document.createElement("script");
                 scr.type = "text/javascript";
				// scr.src = PARSEK.util.ServerHome+"/"+PARSEK.VERSION
				// +"/yui/build/yahoo-dom-event/yahoo-dom-event.js";
                 scr.src = "/_common/js/yui/yahoo-dom-event/yahoo-dom-event.js";
                 head.insertBefore(scr, head.firstChild);
         }

         // Execute PJT, when loaded
         // PARSEK.util.loadLibraries();

	    /* for Mozilla */
	    if (document.addEventListener) {
	        // document.addEventListener("DOMContentLoaded", PARSEK.util.attachHandlers, false);
	        document.addEventListener("DOMContentLoaded", PARSEK.util.Loader.loadCore, false);
	    }

	    /* for Internet Explorer */
	    if (document.getElementById) {
	        var deferScript = document.getElementById('__init_script');
	        if (deferScript) {
	            deferScript.onreadystatechange = function() {
	                if (this.readyState == 'complete') {
	                    // PARSEK.util.attachHandlers();
	                    PARSEK.util.Loader.loadCore();
	                }
	            };
	            /* check whether script has already completed */
	            deferScript.onreadystatechange();
	            /* clear reference to prevent leaks in IE */
	            deferScript = null;
	        }
	    }

	    /* for other browsers */
	    // window.onload = PARSEK.util.attachHandlers;
	    window.onload = PARSEK.util.Loader.loadCore;
})( );