function openPDFWindow ( el ) {
 var pdfWindow = window.open ( el.getAttribute ( 'href' ), 'pdfWindow', 'toolbar=yes,resizable=yes' );
}

function openBrandWindow ( el ) {
 var brandWindow = window.open ( el.getAttribute ( 'href' ), 'brandWindow', 'toolbar=yes,resizable=yes' );
}

function openPicWindow ( el ) {
 var picWindow = window.open ( '', 'picWindow', 'width=500,height=320,resizable=yes' );
 picWindow.document.write ( '<html><head><title>Picture Viewer</title></head><body><center><img src="' + el.getAttribute ( 'href' ) + '"/></center></body></html>' );
}

function openVidWindow ( el ) {
 var vidWindow = window.open ( el.getAttribute ( 'href' ), 'vidWindow', 'width=640,height=420,resizable=yes' );
}

function makeMap ( id, lat, lng, url ) {
 var map = new GMap2 ( document.getElementById ( id ) );
 map.addControl ( new GSmallMapControl () );
 map.setCenter ( new GLatLng ( lat, lng ), 15 );
 map.setMapType ( G_NORMAL_MAP );
 var marker = new GMarker ( new GPoint ( lng, lat ) );
 var infoTabs = [
   new GInfoWindowTab ( 'info', '<a href="' + url + '">Get driving directions.</a>' )
  ];
 GEvent.addListener ( marker, 'click',
   function () {
    marker.openInfoWindowTabsHtml ( infoTabs );
   }
  );
 map.addOverlay ( marker );
 new GKeyboardHandler ( map );
}

function rTrim ( str ) {
 var whitespace = new String ( " \t\n\r" );
 var s = new String ( str );
 if ( whitespace.indexOf ( s.charAt ( s.length - 1 ) ) != -1 ) {
  var i = s.length - 1;
  while ( i >= 0 && whitespace.indexOf ( s.charAt ( i ) ) != -1 ) {
   i--;
  }
  s = s.substring ( 0, i + 1 );
 }
 return s;
}

function marqueeTitle ( title, title1, count ) {
 if ( title && count ) {
  if ( count <= 3 ) {
   title = title.substring ( 1 ) + rTrim ( title.substring ( 0, 1 ) );
  } else {
   title = title.substring ( 1 ) + title.substring ( 0, 1 );
  }
  count = count - 1;
  document.title = title;
  setTimeout ( 'marqueeTitle ("' + title + '", "' + title1 + '", ' + count + ')', 500 );
 } else if ( title ) {
  document.title = title1 + ' - ' + title;
  rotateTitles ();
 } else {
  var i = document.title.lastIndexOf ( ' - ' );
  title1 = document.title.substring ( 0, i );
  title = document.title.substring ( i + 3 ) + '   ';
  count = title.length;
  setTimeout ( 'document.title = "' + title + '"', 5000 );
  setTimeout ( 'marqueeTitle ("' + title + '", "' + title1 + '", ' + count + ')', 5500 );
 }
}

function rotateTitles ( title, title2 ) {
 if ( title && title2 ) {
  document.title = title;
  setTimeout ( 'rotateTitles ("' + title2 + '", "' + title  + '")', 10000 );
 } else {
  title = document.title;
  if ( i = document.title.lastIndexOf ( ' - ' ) ) {
   title2 = document.title.substring ( i + 3 );
  } else {
   title2 = 'Welcome!';
  }
  if ( Math.round ( 10 * Math.random () ) >= 7 ) {
   marqueeTitle ();
  } else {
   setTimeout ( 'rotateTitles ("' + title2 + '", "' + title  + '")', 10000 );
  }
 }
}

function disableStyles () {
 for ( i = 0; ( link = document.getElementsByTagName ( 'link' )[i] ); i++ ) {
  if ( link.getAttribute ( 'rel' ).indexOf ( 'style' ) > -1 ) {
    link.disabled = true;
  }
 }
}

function include_js ( src ) {
 if ( ! document.getElementById ( src ) ) {
  var scriptElem = document.createElement ( 'script' );
  scriptElem.setAttribute ( 'id', src );
  scriptElem.setAttribute ( 'src', src );
  scriptElem.setAttribute ( 'type', 'text/javascript' );
  document.getElementsByTagName ( 'head' )[0].appendChild ( scriptElem );
 }
}

function run_when_ready ( func, limit ) {
 setTimeout (
  function () {
   try {
    func ();
   } catch ( err ) {
    if ( limit >= 0 ) {
     run_when_ready ( func, limit - 1 );
    }
   }
  },
  1000 );
}

function prefetch ( href ) {
 if ( href.indexOf ( location.hostname ) >= 0 && href.indexOf ( '?' ) < 0 ) {
  if ( href != location.href && ( ! document.getElementById ( href ) ) ) {
   var linkElem = document.createElement ( 'link' );
   linkElem.setAttribute ( 'id', href );
   linkElem.setAttribute ( 'href', href );
   linkElem.setAttribute ( 'rel', 'prefetch' );
   document.getElementsByTagName ( 'head' )[0].appendChild ( linkElem );
  }
 }
}

if ( ( navigator.userAgent.indexOf ( 'Opera' ) > 0 ) && ( navigator.userAgent.indexOf ( 'Opera 9' ) < 0 ) ) {
 disableStyles ();
} else if ( ( navigator.appName.indexOf ( 'Internet Explorer' ) > 0 ) && ( navigator.platform.indexOf ( 'PPC' ) > 0 ) ) {
 disableStyles ();
}

var jsRules = {
 '#load' : function ( el ) {
  load ();
 },
 'A' : function ( el ) {
  setTimeout ( function () { prefetch ( el.href ); }, 1000 );
 },
 'IMG' : function ( el ) {
  if ( document.body.filters ) {
   var src = el.src;
   var width = el.width;
   var height = el.height;
   el.src = '/images/site/blank.gif';
   el.width = width;
   el.height = height;
   el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader ( src = '" + src + "', sizingMethod = 'scale' )";
   el.style.visibility = 'visible';
  }
 },
 '.brands A' : function ( el ) {
  el.onclick = function () {
   openBrandWindow ( el );
   return false;
  }
 },
 '.picLink' : function ( el ) {
  el.onclick = function () {
   openPicWindow ( el );
   return false;
  }
 },
 '.vidLink' : function ( el ) {
  el.onclick = function () {
   openVidWindow ( el );
   return false;
  }
 },
 '.pdfLink' : function ( el ) {
  include_js ( '/js/plugin.js' );
  run_when_ready ( function () {
   if ( Plugin.isInstalled ( 'Acrobat' ) ) {
    el.onclick = function () {
     openPDFWindow ( el );
     return false;
    }
   }
  }, 30 );
 },
 '#embeded' : function ( el ) {
  setTimeout ( 'self.close ()', 180000 );
 },
 '#searchBlock' : function ( el ) {
  el.onmouseover = function () {
   var searchToolbar = document.getElementById ( 'search_toolbar' );
   searchToolbar.style.display = 'block';
  }
  el.onmouseout = function () {
   var searchToolbar = document.getElementById ( 'search_toolbar' );
   searchToolbar.style.display = 'none';
  }
 },
 '.tabbedPages' : function ( el ) {
  include_js ( '/js/widgets.js' );
  run_when_ready ( function () { new TabbedPages ( el ) }, 30 );
 },
 '.slideshow' : function ( el ) {
  include_js ( '/js/widgets.js' );
  run_when_ready ( function () {
   if ( navigator.appVersion.indexOf ( 'MSIE 6' ) > 0 ) {
    var transition = null;
    var button = new Button ( 'slideshowButton', el, 37, 37, '/images/widgets/slideshow_button.gif', '/images/widgets/slideshow_button_hover.gif', '/images/widgets/slideshow_button_active.gif' );
   } else {
    var transition = new SlideshowTransition ( el, '/images/widgets/fade.png', 6, 166 );
    var button = new Button ( 'slideshowButton', el, 37, 37, '/images/widgets/slideshow_button.png', '/images/widgets/slideshow_button_hover.png', '/images/widgets/slideshow_button_active.png' );
   }
   var slideshow = new Slideshow ( el, '/images/widgets/slideshow.jpg', 5, 5000, transition, button );
   button.setAction ( function () { slideshow.start () } );
  }, 30 );
 }
};
Behaviour.register ( jsRules );
