/**
 * Main function
 */
jQuery(document).ready(function($) { 
   
  // Browser Test
	$.browserTest = function (a, z) {
		var u = 'unknown', x = 'X', m = function (r, h) {
			for (var i = 0; i < h.length; i = i + 1) {
				r = r.replace(h[i][0], h[i][1]);
			}       
			return r;
		}, c = function (i, a, b, c) {
			var r = {
				name: m((a.exec(i) || [u, u])[1], b)
			};

			r[r.name] = true;

			r.version = (c.exec(i) || [x, x, x, x])[3];

			if (r.name.match(/safari/) && r.version > 400) {
				r.version = '2.0';
			}     
			if (r.name === 'presto') {
				r.version = ($.browser.version > 9.27) ? 'futhark' : 'linear_b';
			}
			r.versionNumber = parseFloat(r.version, 10) || 0;
			r.versionX = (r.version !== x) ? (r.version + '').substr(0, 1) : x;
			r.className = r.name + r.versionX;

			return r;
		};

		a = (a.match(/Opera|Navigator|Minefield|KHTML|Chrome/) ? m(a, [
			[/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
			['Chrome Safari', 'Chrome'],
			['KHTML', 'Konqueror'],
			['Minefield', 'Firefox'],
			['Navigator', 'Netscape']
		]) : a).toLowerCase();

		$.browser = $.extend((!z) ? $.browser : {}, c(a, /(camino|chrome|firefox|netscape|konqueror|lynx|msie|opera|safari)/, [], /(camino|chrome|firefox|netscape|netscape6|opera|version|konqueror|lynx|msie|safari)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/));

		$.layout = c(a, /(gecko|konqueror|msie|opera|webkit)/, [
			['konqueror', 'khtml'],
			['msie', 'trident'],
			['opera', 'presto']
		], /(applewebkit|rv|konqueror|msie)(\:|\/|\s)([a-z0-9\.]*?)(\;|\)|\s)/);

		$.os = {
			name: (/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase()) || [u])[0].replace('sunos', 'solaris')
		};

		if (!z) {
			$('html').addClass([$.os.name, $.browser.name, $.browser.className, $.layout.name, $.layout.className].join(' '));
		}
	};
  
  $.browserTest(navigator.userAgent);       

   
   // Hintergrund-Skalierung
//    $('#zoom-left div img').css('width', 'auto'); //Breite automatisch bestimmen
    
    var imageAspectRatio = 0;
    var viewAspectRatio = 0;
    $('#zoom-left').css('width', '100%').css('height', '100%').css('overflow', 'hidden');

    var f_background = function() {
      if (imageAspectRatio == 0) imageAspectRatio = $('#zoom-left div img').width()/$('#zoom-left div img').height();
      viewAspectRatio = $(window).width()/$(window).height();
      if (imageAspectRatio < viewAspectRatio) {
        // scale width
        if ($.browser.name == 'msie') {
          $('#zoom-left div img').css('width', $(window).width()-3);
        } else {
          $('#zoom-left div img').css('width', $(window).width());
        }
        $('#zoom-left div img').css('height', 'auto');
      } else {
        // scale height
        if ($.browser.name == 'msie') {
          $('#zoom-left div img').css('height', $(window).height()-3);
        } else {
	  $('#zoom-left div img').css('height', $(window).height());
        }
        $('#zoom-left div img').css('width', 'auto');
      }
    }
    $(window).resize(f_background);
    f_background();
    
    
  // jScrollPane activate
  
  if ($("#map_container").length > 0) {
    var gmapPos = $("#map_container");
    var position = gmapPos.position();
    gmapTop = position.top;
  }
                          
  $('#mainOuterDef').each(
		function()
		{
			$(this).jScrollPane(
				{
					showArrows: $(this).is('.arrow')
				}
			);
			var api = $(this).data('jsp'); 
      
      
      if ($("#map_container").length > 0) {
        $('#dealers a.dealerName').bind(
		      'click',
		      function() {
			     api.scrollTo(0, gmapTop, true );
			     return false;
		      }
	       );
      }
              
			var throttleTimeout;
			$(window).bind(
				'resize',
				function()
				{
					if ($.browser.msie) {
						// IE fires multiple resize events while you are dragging the browser window which
						// causes it to crash if you try to update the scrollpane on every one. So we need
						// to throttle it to fire a maximum of once every 50 milliseconds...
						if (!throttleTimeout) {
							throttleTimeout = setTimeout(
								function()
								{
									api.reinitialise();
									throttleTimeout = null;
								},
								50
							);
						}
					} else {
						api.reinitialise();
					}
				}
			);
		}
	);
  
  
  //In case the user resize the browser, we will need to recalculate the height and top for #fake_body and #dock
  $(window).resize(function () {
    resizeEvent();
  });
  resizeEvent();
  
  setTimeout(function () {
  
    resizeEvent();
  }, 500);

  function resizeEvent() {
    $('html.msie9 #flashcontent object').height($(window).height()-30);
  }
     
});



