/**
 * Browser detection
 */
var browser = {
 
  isIE : false,
  isNS : false,
  isFF : false,
  isOP : false,
  
  // Set the browser variables
  init : function(){
    agt = navigator.userAgent.toLowerCase();
    
    this.isIE  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.isIE6 = (this.isIE && (agt.indexOf("msie 6") != -1));
    this.isNS  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('firefox')==-1) && (agt.indexOf('hotjava')==-1));
    this.isFF  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('firefox')!=-1) && (agt.indexOf('hotjava')==-1));
    this.isOP  = window.opera;
    
    // Stop background image flicker in IE
    if(this.isIE){
      try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}  
    }
  } 
};
browser.init(); 


/**
 * Page functions
 */
var page = {

  onload : function(){
    
    // Position cursor in search field
    // var sf = document.getElementById("fieldSearch");
    // if(sf) sf.focus();
    
    // Initialize map's hovering
    hover.init();
    
    // Fix netscape bug if center column isn't the longest page element
    if(browser.isNS) {
      var center = document.getElementById("center");  
      var left = document.getElementById("left");  
      var right = document.getElementById("right");  
      
      // Need at least a center element to proceed
      if(center && center.style) {
      
        var cHeight = center.offsetHeight;
        var lHeight = left ? left.offsetHeight : 0;
        var rHeight = right ? right.offsetHeight : 0;
        
        // Adjust center's bottom padding to make it same height as longest column
        if(cHeight < lHeight || cHeight < rHeight) {
          var pad = ((lHeight < rHeight) ? rHeight - cHeight : lHeight - cHeight) + 30;
          center.style.marginBottom = pad + "px";        
        }     
      }
    }
  },
  
  
  // Hovers an element - must have a CSS class with suffix "Hover"
  hover : function(elem){
  
    // Only IE6 and NS need this, everyone else does just fine the :hover pseudoclass
    if(!browser.isIE6 && !browser.isNS) return;  

    if(elem && elem.className){
      var idx = elem.className.indexOf("Hover");
      elem.className = (idx > -1) ? elem.className.substring(0, idx) : elem.className + "Hover";
    }
  }
};

/**
 * Image Maps
 */

var hover = {

  // Create the mouseover/mouseout functions
  init : function(){
    
    // Initialize the link hovers of the image map
    var list = document.getElementById('links');
    if(list){
      var links = list.getElementsByTagName('a');
      for(var i = 0; i < links.length; i++){
        links[i].onmouseover = links[i].onmouseout = function(){
          hover.toggle(this.className);        
        }  
        links[i].id = links[i].className + "Link";
      }
    }    
    
    // Initialize the image map hovers
    var map = document.getElementById('Map');
    if(map){
      var areas = map.getElementsByTagName('area');
      for(var i = 0; i < areas.length; i++){
        areas[i].onmouseover = areas[i].onmouseout = function(){
          hover.toggle(this.className);        
        }  
      }
    }
  },

  // Toggle the hovered region
  toggle : function(id) {
    var map = document.getElementById(id);
    if(map && map.style) map.style.zIndex = map.style.zIndex == "3" ? "1" : "3";
    
    var link = document.getElementById(id + "Link");
    if(link && link.style) link.style.textDecoration = link.style.textDecoration == "none" ? "underline" : "none";
  }

}


/*
 * Handles Share popup links
 */
var share = {

  digg : function(){  
    document.location = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(document.title); 
    return false;
    
  },
  
  delicious : function(){
    document.location = 'http://del.icio.us/post?v=2&url='+encodeURIComponent(document.location.href)+'&title='+encodeURIComponent(document.title);
    return false;
  }  
  

}

/*
 * Handles Subscription links
 */
var subscribe = {

  rssUrl : "http://www.mygermancity.com/feed",

  rss : function(){
    document.location = this.rssUrl;
    return false;
  },

  google : function(){
    document.location = 'http://www.google.com/reader/preview/*/feed/'+this.rssUrl;
    return false;
  },

  yahoo : function(){
    document.location = 'http://add.my.yahoo.com/rss?url='+this.rssUrl;
    return false;
  },
  
  msn : function(){
    document.location = 'http://my.msn.com/addtomymsn.armx?id=rss&ut='+this.rssUrl;
    return false;
  }



}

/*
 * Handles webcam functions
 */
var webcam = {

  refreshTime : 62000,  // Length between refresh in milliseconds

  // refreshes the webcam image
  getNewImg : function(imgId){
  
    var img = document.getElementById(imgId);    
    
    // Set the image source and use the new Date() to stop image caching.  The 'return false' then
    // prevents the links href from firing.
    if(img != null) {    
      var src = webcam.removeCacheBuster(img.src) + '?d=' + (new Date()).getTime();
      img.style.visibility = 'hidden';
      img.src = '';      
      setTimeout(function(){img.src = src;}, 250);
      setTimeout(function(){img.style.visibility = 'visible';}, 2000);       
      return false;
    }  
    
    return true;    
  },
  
  // removes the date cache buster
  removeCacheBuster : function(src){
    
    var idx = src.indexOf("?d=");
    if(idx > -1) 
      src = src.substring(0, idx);
  
    return src; 
    
  },
  
  // Recursive update of webcam images
  periodical : function(){

     var imgs = jQuery("ul#webcams a img");
     for(var i = 0; i < imgs.length; i++){
       webcam.getNewImg(imgs[i].id);
     }  
     
     interval = setTimeout(webcam.periodical, webcam.refreshTime); 
  }
}

var interval = setTimeout(webcam.periodical, webcam.refreshTime);