// JavaScript Document
///////////////////////////////////////////////
function initDocument() {
	// Layout 'Featured Bike'
	$('#featured_bike .csc-textpic-imagewrap').after( $('.feature_header') );

	// Bikeübersicht Mousovers
	if (document.location.href.indexOf('id=7')>-1) $('.csc-textpic-image a img').mouseover(Bikes.pictureoverlay).mouseout(Bikes.removePictureoverlay);

	// Bikegallery Init
	Bikegallery.init();
}

// Mouseover in der Bikeübersicht
/////////////////////////////////////////////// 
Bikes = {
	offsetOptions: {margin:0,border:0,padding:0,scroll:0},
	overlayid: 1,
	overlayimage: function() {
		return $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent) ? 'img/clickfordetails.gif' : 'img/clickfordetails.png';
	}(),

	pictureoverlay: function() {
		var $$ = $(this);
		$$.after('<img src="'+Bikes.overlayimage+'" id="overlaypicture_' + Bikes.overlayid + '" style="position:absolute;display:none;" />');
		
		var overlay = $("#overlaypicture_" + Bikes.overlayid);
		overlay.css({bottom: '2px',left: '2px',zIndex:9999,border:'0 transparent none'}).show();
	},

	removePictureoverlay: function() { $("#overlaypicture_" + Bikes.overlayid++).fadeOut(2000,function() { $(this).remove();}); }
}

// Gallerie 'Bikes'
/////////////////////////////////////////////// 
Bikegallery = {
	images: {},
	imagepath: [],
	linkpath: [], 
	counter: 0,
	init: function() {
		if ($('.dynamicgallery').length) {
			$('.csc-textpic-imagewrap').append('<img src="img/prev.gif" border="0" class="jcarousel-prev" /><ul></ul><img src="img/next.gif" border="0" class="jcarousel-next" />');
			this.images = $('.dynamicgallery img');
			this.images.each(function() {
				$$ = $(this);
				Bikegallery.imagepath[Bikegallery.counter]=$$.attr('id','bikegallery_' + Bikegallery.counter).attr('src'); 
				if($$.parent().attr('onclick')) Bikegallery.linkpath[Bikegallery.counter]=$$.parent().attr('onclick');
				Bikegallery.counter++;
			} );
			$('.csc-textpic-imagewrap dl').remove();
			$('.csc-textpic-imagerow').remove();
			$(".csc-textpic-imagewrap").jcarousel({
				itemVisible: Math.min(Bikegallery.imagepath.length,3.5), 
				itemScroll: 1,
				wrap: false,
				orientation: 'vertical',
				loadItemHandler: Bikegallery.loadItemHandler,
				nextButtonStateHandler: Bikegallery.nextButtonStateHandler,
				prevButtonStateHandler: Bikegallery.prevButtonStateHandler
			});
			$('.csc-textpic').css({textAlign:'left'}).append('<img class="fullpic" src="' + Bikegallery.imagepath[0] + '" />');
			if (!!Bikegallery.linkpath[0]) {
				if ($.browser.msie) {
					eval ("$('.fullpic').css({cursor:'pointer'}).click( " + Bikegallery.linkpath[0] + " );");
				} else {
					eval ("$('.fullpic').css({cursor:'pointer'}).click(function() { " + Bikegallery.linkpath[0] + " } );");
				}
			}
			Bikegallery.initThumbs();
			$('.jcarousel-scope').css({width:'75px',float:'right'});
			$('.jcarousel-list li:first-child').css({border:'2px #cb5911 solid'});
		}
	},

	initThumbs: function() {
		$('.jcarousel-thumb').click(
			function() {
				$$ = $(this);
				id = $$.attr('id').substr($$.attr('id').indexOf('_')+1);
				path = $$.attr('src').substr($$.attr('src').indexOf('?')+1);
				$('.jcarousel-list li').css({border:'2px #9b762f solid'});
				$$.parent().css({border: '2px #CB5911 solid'});
				$('.fullpic').attr('src',path);
				if (!!Bikegallery.linkpath[id-1]) {
					if ($.browser.msie) {
						eval ("$('.fullpic').css({cursor:'pointer'}).click( " + Bikegallery.linkpath[id-1] + " );");
					} else {
						eval ("$('.fullpic').css({cursor:'pointer'}).click(function() { " + Bikegallery.linkpath[id-1] + " } );");
					}
				}
			} 
		);
	},

	loadItemHandler: function(carousel, start, last, available) {
		if (!available) {
			for (var i = start; i <= last; i++) {
				if (i > Bikegallery.imagepath.length) {
					break;
				}
				carousel.add(i, Bikegallery.getItemHTML(Bikegallery.imagepath[i - 1],i));
			}
		}

		// Trigger loaded
		carousel.loaded();
		Bikegallery.initThumbs();
	},

	nextButtonStateHandler: function (carousel, button, enabling) {
		if (enabling) {
			$(button).attr("src", "img/next.gif")
				.bind("mouseover", Bikegallery.nextOver)
				.bind("mouseout", Bikegallery.nextOut)
				.bind("mousedown", Bikegallery.nextDown);
		} else {
			$(button).attr("src", "img/next-disabled.gif")
				.unbind("unmouseover", Bikegallery.nextOver)
				.unbind("unmouseout", Bikegallery.nextOut)
				.unbind("unmousedown", Bikegallery.nextDown);
		}
	},

	nextOver: function() {
		$(this).attr("src", "img/next-over.gif");
	},

	nextOut: function() {
		$(this).attr("src", "img/next.gif");
	},

	nextDown: function() {
		$(this).attr("src", "img/next-down.gif");
	},

	prevButtonStateHandler: function (carousel, button, enabling) {
		if (enabling) {
			$(button).attr("src", "img/prev.gif")
				.bind("mouseover", Bikegallery.prevOver)
				.bind("mouseout", Bikegallery.prevOut)
				.bind("mousedown", Bikegallery.prevDown);
		} else {
			$(button).attr("src", "img/prev-disabled.gif")
				.unbind("unmouseover", Bikegallery.prevOver)
				.unbind("unmouseout", Bikegallery.prevOut)
				.unbind("unmousedown", Bikegallery.prevDown);
		}
	},

	prevOver: function() {
		$(this).attr("src", "img/prev-over.gif");
	},

	prevOut: function() {
		$(this).attr("src", "img/prev.gif");
	},

	prevDown: function() {
		$(this).attr("src", "img/prev-down.gif");
	},

	getItemHTML: function (url,i) {
		return '<img id="carouselthumb_' + i + '" class="jcarousel-thumb" src="thumb.php?' + url + '" width="' + 75 + '" height="' + 75 + '" />';
	}
}


// Macromedia Stuff
/////////////////////////////////////////////// 
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


MM_preloadImages('/img/nav_01_over.gif','/img/nav_02_over.gif','/img/nav_03_over.gif','/img/nav_04_over.gif','/img/nav_05_over.gif','/img/nav_06_over.gif');
