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;
        });
    }
    // 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');
      // replace 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

    //popup tools
    $('#pop_print').each(function(){
      var url = getPageToolUrl('print');
      $(this).replaceWith('<div id="pop_print"><a href="'+url+'">'+
      $(this).text()+'</a></div>');
    });
    $('#pop_close').click(function(){
        self.close();
    });


    // 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;
         });
     }
     // bayer link selection
     $('#healthcare_links select').change(function() {
        var v = $(this).val();
        if(v && v!='') {location.href =  v;}
        return false;
     });
     // enhanced form error highlight
     $('form .error').each(function(){
        if(this.nodeName.toLowerCase!='p') {
            var parent = $(this).parents('p');
            if(parent.length) {
                parent = parent[0];
                $(parent).addClass('error');
                $(this).removeClass('error');
                parent = $(this).parents('fieldset');
                if(parent.length) {
                   $('legend',parent[0]).addClass('error');
                }
            }

        }
     });

});