/* 
 * all js initilisations
 *
 * $Id: init.js 521 2009-09-22 12:24:24Z wwwabda $
 */


/* 
attention $() is provided by Mootols !!!
jQuery resides in jQuery()
*/

// Library to encode/decode to/from UTF
// needed for E-Tracker 
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/utf-8 [rev. #1]

UTF8 = {
    encode: function(s){
        for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
            s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
        );
        return s.join("");
    },
    decode: function(s){
        for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
            ((a = s[i][c](0)) & 0x80) &&
            (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
            o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
        );
        return s.join("");
    }
};


/*
 * domready stuff
 */
window.addEvent('domready', function() {

   // resize the menu elements
	var navItems = jQuery('#mainnavi ul.nav1 li a');
	var numberElements = navItems.size();
	var navTotalWidth = (960-(numberElements*(20+1))); // subtract padding and border
	
	var currentWidth = 0;
	var diffWidth = 0;
	
	navItems.each(function(index) {
		// console.log(index);
		// console.log(this);
		// console.log(jQuery(this).width());
		currentWidth += jQuery(this).width();
	});
	numberElements = navItems.size();
	// console.log("current total width: " + currentWidth);
 	diffWidth = navTotalWidth-currentWidth;
	// console.log("current total additonal width " + diffWidth);
	if (diffWidth > 0 || 1==1 ) {
		addItemWidth=Math.floor(diffWidth/numberElements);
		// console.log('addItemWidth: ' + addItemWidth);
		navItems.each(function(index) {
			var navObject = jQuery(this);
			var oldWidth = navObject.width();
			var newWidth = oldWidth + addItemWidth
			// add remaining pixels to last item on list
			if (index == (numberElements-1)) {
				newWidth += (diffWidth-(numberElements*addItemWidth));
			}
			navObject.css('width',newWidth+'px');
			// console.log('Item: ' + index + ' addItemWidth:' + addItemWidth + ' oldWidth:'  + oldWidth+ ' newWidth:'+newWidth);
		})
		// adding width to elemnts
	}	
 
  // adopt height of all elements in #themen(only used on HOME)
 	var themeItems = jQuery('#themen div.column2, #themen div.column2_last');
	var maxHeight = 0;
	themeItems.each(function(index) {
		currentHeight = jQuery(this).height();
		if (currentHeight > maxHeight) {
			maxHeight = currentHeight;
		}
	});
	
	themeItems.each(function(index) {
		var themeObject = jQuery(this);
		themeObject.css('height',maxHeight+'px');
	})
	
 

  // hide div elements in dropdown
	$$('div.ce-fce-dropdown div.container>div').each(function(item, index) {
		// console.log(item);
		item.setStyles({
			display: 'none'
		})
	});
  
  // add event to dropdown
	$$('div.dropdown select.options').addEvent('change',showHideElements);

  // adding pagetools
        jQuery('#pagetools').append('<a href="#" class="sendpage" title="Artikel per E-Mail weiterleiten"></a><a href="#" class="printpage" title="Seite drucken" onClick="window.print();return false"></a><a href="#" id="bookmarkpage" class="bookmpage" title="Seite zu Lesezeichen hinzufügen"></a><br class="clear" />');
	
	jQuery("#bookmarkpage").click(function(e){
		e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = document.URL;
		var bookmarkTitle = document.title;
 
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		} else if( window.external || document.all) { // For IE Favorite
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);	
		} else if(window.opera) { // For Opera Browsers
			jQuery('#bookmarkpage').attr("href",bookmarkUrl);
			jQuery('#bookmarkpage').attr("title",bookmarkTitle);
			jQuery('#bookmarkpage').attr("rel","sidebar");
		} else { // for other browsers which does not support
			 alert('Ihr Browser wird leider nicht unterstützt');
			 return false;
		}
	});

	//
	// attach etracker ET_EVENT to all elements
	//
        trackElements = jQuery("a[href^='fileadmin/assets']").add("a[href^='uploads']"); // find href starting with fileadmin/assets/ or uploads/
	// pdf/PDF
        trackElements.filter("a[href$='pdf'], a[href$='PDF']").each(function (index){
          var filenamePlain = decodeURI(jQuery(this).attr('href'));    // decodeURI, as href is urlEncoded 
          jQuery(this).unbind("mousedown.etracker");
          jQuery(this).bind("mousedown.etracker", {fnP: filenamePlain }, function (event) {
            ET_Event.download(event.data.fnP, 'PDF'); 	       // finally the event funxtion, that needs to be bind()
         })
        })

        // doc/DOC
	trackElements.filter("a[href$='doc'], a[href$='DOC']").each(function (index){
          var filenamePlain = decodeURI(jQuery(this).attr('href'));    // decodeURI, as href is urlEncoded 
          jQuery(this).unbind("mousedown.etracker");
          jQuery(this).bind("mousedown.etracker", {fnP: filenamePlain }, function (event) {
            ET_Event.download(event.data.fnP, 'DOC'); 	       // finally the event funxtion, that needs to be bind()
         })
        })

	// mov,flv,mp4,mp3,MP3
	trackElements.filter("a[href$='mov'], a[href$='flv'], a[href$='mp4'], a[href$='mp3'], a[href$='MP3']").each(function (index){
          var filenamePlain = decodeURI(jQuery(this).attr('href'));    // decodeURI, as href is urlEncoded 
          jQuery(this).unbind("mousedown.etracker");
          jQuery(this).bind("mousedown.etracker", {fnP: filenamePlain }, function (event) {
            ET_Event.download(event.data.fnP, 'MEDIA'); 	       // finally the event funxtion, that needs to be bind()
         })
        })
	
	// ppt
	trackElements.filter("a[href$='ppt']").each(function (index){
          var filenamePlain = decodeURI(jQuery(this).attr('href'));    // decodeURI, as href is urlEncoded 
          jQuery(this).unbind("mousedown.etracker");
          jQuery(this).bind("mousedown.etracker", {fnP: filenamePlain }, function (event) {
            ET_Event.download(event.data.fnP, 'PRESENTATION'); 	       // finally the event funxtion, that needs to be bind()
         })
        })
	
	// zip/tgz
	trackElements.filter("a[href$='zip'], a[href$='tgz']").each(function (index){
          var filenamePlain = decodeURI(jQuery(this).attr('href'));    // decodeURI, as href is urlEncoded 
          jQuery(this).unbind("mousedown.etracker");
          jQuery(this).bind("mousedown.etracker", {fnP: filenamePlain }, function (event) {
            ET_Event.download(event.data.fnP, 'ARCHIV'); 	       // finally the event funxtion, that needs to be bind()
         })
        })
	delete(trackElements);

});


