var currSize = 1;

function swichStyleSheetSrc(size) {
    var styleName = 'size'+size;
    $('link[@rel*=STYLE][@title]').each(function() {
        this.disabled = true;
        if($(this).attr('title')==styleName) {
            this.disabled = false;
        }
    });
    $.cookie('styles',size,{expires:7,path:'/'});
}

function setBookmark() {
    var bookmarkurl = location.href;
    var text = document.title;
    text = text.replace(/:/ ,"");
    text = text.replace(/\*/ ,"");
    text = text.replace(/\?/ ,"");
    text = text.replace(/\\/ ,"");
    text = text.replace(/"/ ,"");
    text = text.replace(/</ ,"");
    text = text.replace(/>/ ,"");
    text = text.replace(/|/ ,"");
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(text, bookmarkurl,"");
    } else if( window.external ) { // IE Favorite
        window.external.AddFavorite( bookmarkurl, text);
    } else if(window.opera && window.print) { // Opera Hotlist
        return true;
    }
}

function getPageToolUrl(param) {
	 // grep current location
	  newLoc = self.location.href.toString();
	  var baseURL;
	  // check if there are any additional information in the location string
	  // and extract the baseURL from location string
	 if(newLoc.match(/(.+)[\?#]/)) {
	  	baseURL = newLoc.match(/(.+)[\?#]/)[1];
	  } else {
	  	baseURL = newLoc;
	  }
	  // add existing parameter information
	  if (self.location.search && self.location.search != "") {
	  	baseURL+=self.location.search+'&'+param+'=1';
	  } else {
	  	baseURL+='?'+param+'=1';
	  }
	  // add exisiting hash information
	  if(self.location.hash && self.location.hash!="") {
	  	baseURL+=self.location.hash;
	  }	
	  return baseURL;
}

$().ready(function() {
    // ie only functions
    if($.browser.msie) {
    	 // avoid background flickering
    	 try {document.execCommand("BackgroundImageCache",false,true);} catch(e){}
    	 $('link[@title*=bw]').each(function() {
    	 	this.disabled = true;
    	});
    }
    
	/*
    //contentnavigation effect...
    $.ui.accordion.animations.quirky = function(options) {
    	this.slide(options, {
    		easing: "swing",
    		duration: 500,
    		complete: function() {
					if ( !options.autoheight ) {
						options.toShow.css("height", "auto");
					}
					$('div',options.toHide).fadeTo(1, 0.5);
					$('div',options.toShow).fadeTo(10, 1);
					options.complete();
				}
    	});
     };

    $('div div','#ContentNavigation').fadeTo(1, 0.5);
    $('.active div div','#ContentNavigation').fadeTo(1,1);
    
    
    $('#ContentNavigation').accordion({header: "h3",event: "mouseover",autoHeight: true, animated: "quirky"});*/
	
	// Metapur AG Version menu.js
    
    
    // page tools
    // bookmarking
    $('#bookmark').click(function(){setBookmark(); return false;});
    // black and white version
    $('#bwversion').click(function(){
      $('link[@title*=bw]').each(function() {this.disabled = false;});
      $(this).hide();
      $('#colversion').show();
    });    
    $('#colversion').click(function(){
      $('link[@title*=bw]').each(function() {this.disabled = true;});
      $(this).hide();
      $('#bwversion').show();
    });
    // text version
    $('#textversion').click(function(){
      var url = getPageToolUrl('tty');
      location.href = url;
      return false;});
    // print version
    $('#print').each(function(){
      var url = getPageToolUrl('print');
      // relpace the original page Tool content with the new url
      $(this).replaceWith('<li id="print"><a href="'+url+'" target="_blank">'+
      $(this).text()+'</a></li>');
    });
    // end of page tools
	
	// fontsize (cookie reading to grep current size)
    if($.cookie('styles')) {
        currSize = parseInt($.cookie('styles'));
        var isActive = false;
        $('link[@rel*=STYLE][@title]').each(function() {
              if($(this).attr('rel').indexOf('ALTERNATE')==-1
                && $(this).attr('title')=='size'+currSize) {
                    isActive = true;
              }
        });
        $('#fs_size'+currSize).addClass('active');
        if(!isActive) {
            swichStyleSheetSrc(currSize);
        }
    }
    // fontsizer...
    var maxSizes = $('#FontSizer .size').size();
    if(maxSizes > 0) {
        if(currSize==1) {
            $('#fs_minus').addClass('disabled');
        } else if(currSize==maxSizes) {
            $('#fs_plus').addClass('disabled');
        }
        $('#FontSizer .size').click(function() {
            currSize = parseInt(this.id.replace(/fs_size/g,''));
            $('#FontSizer .size').each(function(){$(this).removeClass('active');});
            $(this).addClass('active');
            if(currSize==1) { $('#fs_minus').addClass('disabled');}
            else {$('#fs_minus').removeClass('disabled');}
            if(currSize==maxSizes) { $('#fs_plus').addClass('disabled');}
            else {$('#fs_plus').removeClass('disabled');}
            swichStyleSheetSrc(currSize);
            return false;
        });
         $('#fs_minus').click(function(){
            if(currSize > 1) {
                currSize--;
                $('#FontSizer .size').each(function(){$(this).removeClass('active');});
                $('#fs_size'+currSize).addClass('active');
                if(currSize==1) { $('#fs_minus').addClass('disabled');}
                else {$('#fs_minus').removeClass('disabled');}
                $('#fs_plus').removeClass('disabled');
                swichStyleSheetSrc(currSize);
            }
            return false;
         });
         $('#fs_plus').click(function(){
                 if(currSize < maxSizes) {
                     currSize++;
                     $('#FontSizer .size').each(function(){$(this).removeClass('active');});
                     $('#fs_size'+currSize).addClass('active');
                     if(currSize==maxSizes) { $('#fs_plus').addClass('disabled');}
                     else {$('#fs_plus').removeClass('disabled');}
                     $('#fs_minus').removeClass('disabled');
                     swichStyleSheetSrc(currSize);
                 }
                 return false;
         });
     }
		
		if(typeof(jQuery.jScrollPane)=='object') {
      $('#pane1').jScrollPane({showArrows:true,scrollbarWidth:15});
      if($('#pane2').size()>0) {
        $('#pane2').jScrollPane({showArrows:true,scrollbarWidth:15});
      }      
      $('#tab2').hide();
      $('div.tabset a:eq(0)').addClass('active');
      $('div.tabset a').click(function() {
         var t = $(this).attr('href');
         $('div.tabset a').each(function() {
            var tt =  $(this).attr('href');            
            if(tt!=t) {
              $(this).removeClass('active');
              $(tt).hide();
            }            
         });
         if(!$(this).is('.active')) {
           $(this).addClass('active');
           $(t).show();
         }
         return false;
      }); 
    }
     
     
    $('#CountrySites').css({'cursor':'pointer'}).click(function(){
        if($('#countrypop').size()==0) {
          var h = $('#CountrySites button span').text();
          var _pw = $('<div id="countrypop"></div>').appendTo('body');
          _pw.append('<h3>'+h+'</h3>');
          var _pc = $('<div class="close"><span>X</span></div>').appendTo(_pw);
          $(_pc).click(function() {$('#countrypop').hide();});
          var cols = [];
          for(c=0; c<3; c++) {  
            var cp = $('<div id="col'+c+'" class="col"></div>').appendTo(_pw);
            cols[c] = $('<div></div>').appendTo(cp);
          }
          $select = $('#CountrySites select');
          var data = {};
          var total = 0;
          $('option',$select).each(function() {
            var text = $(this).text();
            var val = $(this).val();          
            if($(this).parents('optgroup').size()>0) {
              var p = $(this).parents('optgroup')[0];
              var id = $(p).attr('label');
              if(!data[id]) {
                data[id] = [];
                total++;
              }
              data[id][data[id].length] = {'value':val,'text':text};
              
            }
          });
          perColumn = Math.floor(total/3);
          var cCol = 0;
          var cnt=0;
          var colBreak = perColumn;
          for(a in data) {
            if(cnt==colBreak) {
              colBreak+=perColumn;
              cCol+=1;
            }
            cols[cCol].append('<h4>'+a+'</h4>');
            for(x=0; x<data[a].length; x++) {
                cols[cCol].append('<p><a href="'+data[a][x]['value']+'" target="_blank">'+data[a][x]['text']+'</a></p>');
                                
            }
            cnt++;
          }
           $('.col a',_pw).click(function() {$('#countrypop').hide();});
        }
        var de = document.documentElement;
        var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
        var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
        var $pw = $('#countrypop');
        try {
          var l = parseInt((w - 800)/2);
  			  var t= parseInt((h - 500)/2);
  			  $pw.css({'left':l,'top':t}).show();
			  } catch(e) {
          $pw.show();
        }
    });
    
    
     
     $('#PostsFormContainer').hide();
     $('#PostsFormToggle').click(
      function() {
        if($('#PostsFormContainer').is(':visible')) {
          $('#PostsFormContainer').hide();
        } else {
          $('#PostsFormContainer').show();
        }
      }
     );
     
});