var fbcm = {

	init: function() {

		$('.roundbox-container').each(function() {
			fbcm.buildRoundboxContainer($(this));
		});

		$('#top-menu-container').mouseover(function() {
			$('#top-menu-items').removeClass('top-menu-display-arrows');
		});

		$('#top-menu-container').mouseout(function() {
			$('#top-menu-items').addClass('top-menu-display-arrows');
		});

		$('#left-menu-container').mouseover(function() {
			$('#left-menu-items').addClass('left-menu-hover');
			$('#left-menu-container').children('.roundbox-table-container').addClass('roundbox-dark');
		});

		$('#left-menu-container').mouseout(function() {
			$('#left-menu-items').removeClass('left-menu-hover');
			$('#left-menu-container').children('.roundbox-table-container').removeClass('roundbox-dark');
		});

 		$('#flash-header-container').flash({
 			   swf: 'fileadmin/templates/template_fbcm/flash/header.swf?pid='+pid,
 			   width: 996,
 			   height: 170
		});

		if($.browser.safari) {
			if($('#vehicle-detail-table caption').length>0) {
				$('#vehicle-detail-table caption').css( 'padding-bottom','16px' );
			} else {
				$('table caption').css( 'padding-bottom','14px' );
			}
		}

	},

	buildRoundboxContainer: function(roundboxContainer) {

		if ($.browser.msie && parseInt($.browser.version) < 8) {

			var roundBoxBefore = '<table class="roundbox-table-container">'
						+ '<tr class="roundbox-table-row">'
						+ '<td class="roundbox-top-left"></td>'
						+ '<td class="roundbox-top-center"></td>'
						+ '<td class="roundbox-top-right"></td>'
						+ '</tr>'
						+ '<tr class="roundbox-table-row">'
						+ '<td class="roundbox-left"></td>'
						+ '<td class="roundbox-content">';

			var roundBoxAfter  = '</td>'
						+ '<td class="roundbox-right"></td>'
						+ '</tr>'
						+ '<tr class="roundbox-table-row">'
						+ '<td class="roundbox-bottom-left"></td>'
						+ '<td class="roundbox-bottom-center"></td>'
						+ '<td class="roundbox-bottom-right"></td>'
						+ '</tr>'
						+ '</table>';

		}
		else {

			var roundBoxBefore = '<div class="roundbox-table-container ' + roundboxContainer.attr('class') + '">'
						   + '<div class="roundbox-table-row">'
						   + '<div class="roundbox-top-left"></div>'
						   + '<div class="roundbox-top-center"></div>'
						   + '<div class="roundbox-top-right"></div>'
						   + '</div>'
						   + '<div class="roundbox-table-row">'
						   + '<div class="roundbox-left"></div>'
						   + '<div class="roundbox-content">';

			var roundBoxAfter  = '</div>'
						   + '<div class="roundbox-right"></div>'
						   + '</div>'
						   + '<div class="roundbox-table-row">'
						   + '<div class="roundbox-bottom-left"></div>'
						   + '<div class="roundbox-bottom-center"></div>'
						   + '<div class="roundbox-bottom-right"></div>'
						   + '</div>'
						   + '</div>';

		}

		roundboxContainer.replaceWith(roundBoxBefore+roundboxContainer.html()+roundBoxAfter);

	},

	vehiclesList: function() {

		if( $('.vehicle-list-item').length > 0 )
		{
			// Mouseover Effect
			$('.vehicle-list-item').mouseover(function() {
				$(this).find('.roundbox-table-container').addClass('roundbox-dark');
			});

			$('.vehicle-list-item').mouseout(function() {
				$(this).find('.roundbox-table-container').removeClass('roundbox-dark');
			});

			// Move the detail link to the complete div
			$('.vehicle-list-item').each(function(){
				var currentTitle = $(this).find('.vehicle-list-model').text();
				var currentLink  = $(this).find('.vehicles-list-details-link').children('a').attr('href');
				$(this).wrap('<a title="'+currentTitle+'" href="'+currentLink+'"></a>');
				$(this).click(function() {
					document.location.href = currentLink;
				});
				if ($.browser.msie && parseInt($.browser.version) < 8) {
					$(this).css('cursor','hand');
				}
			});
		}

	},

	vehiclesDetail: function() {

		if ($('#details-row-pictures div.details-icon').length > 3) {
			var iconList = $('#details-row-pictures .iconlist');
			iconList.wrapInner('<div id="details-pictures-iconlist-inner"></div>');
			fbcm.slidePictureIcons('start');
			$('#details-arrowLeft').click(function(){
				fbcm.slidePictureIcons('left');
				return false;
			});
			$('#details-arrowRight').click(function(){
				fbcm.slidePictureIcons('right');
				return false;
			});
		}

		$('#details-row-pictures .details-icon').click(function(){
			var iconId = $(this).attr('id');
			if(iconId)
			{
				var imageId = iconId.substr(iconId.length - 2);
				$('.details-icon-link').find('a.active').removeClass('active');
				$('#details-icon-view-'+imageId+' .details-icon-link a').addClass('active');
				$('#vehicle-detail-pictures-main a img').css('display','none');
				$('#photo_'+imageId).css('display','block');
				return false;
			}
		});

		$('#vehicle-detail-pictures-main a img').click(function(){
			var iconId = $(this).attr('id');
			//return false;
		});

		$("#vehicle-detail-pictures-main a").lightbox({
			fileLoadingImage : 'fileadmin/templates/template_fbcm/css/img/loading.gif',
			fileBottomNavCloseImage : 'fileadmin/templates/template_fbcm/css/img/close.gif',
			overlayOpacity : 0.8,
			resizeSpeed : 'fast',
			strings : {
				prevLinkTitle: 'Vorheriges Foto',
				nextLinkTitle: 'N&auml;chstes Foto',
				prevLinkText:  '&laquo; Zur&uuml;ck',
				nextLinkText:  'Vor &raquo;',
				closeTitle: 'Foto-Galerie schliessen',
				image: 'Foto ',
				of: ' von '
			},
			disableNavbarLinks: true,
			fitToScreen: true
		});

	},

	slidePictureIcons: function(direction){
		var posLeft = parseInt($('#details-pictures-iconlist-inner').css('marginLeft'));
		var itemWidth = parseInt($('#details-pictures-iconlist-inner div.details-icon:first').outerWidth()+4);
		var totalWidth = $('#details-pictures-iconlist-inner div.details-icon').length * itemWidth;
		$('#details-pictures-iconlist-inner').width(totalWidth);
		switch (direction) {
			case 'start':
				$('#details-pictures-iconlist-inner').css('marginLeft', '0px');
				$('#details-arrowRight').addClass('details-arrowRightOn');
				$('#details-arrowRight').removeClass('details-arrowRightOff');
				break;
			case 'left':
				if (posLeft < 0) {
					$('#details-arrowLeft').unbind('click');
					$('#details-pictures-iconlist-inner').animate({
						'marginLeft': (posLeft + itemWidth) + 'px'
					}, 'fast', function(){
						$('#details-arrowLeft').click(function(){
							fbcm.slidePictureIcons('left');
							return false;
						});
					});
					$('#details-pictures-iconlist-inner').animate({
						'marginLeft': (posLeft + itemWidth) + 'px'
					}, 'fast');
					$('#details-arrowRight').addClass('details-arrowRightOn');
					$('#details-arrowRight').removeClass('details-arrowRightOff');
					posLeft += itemWidth;
				}
				if (posLeft >= 0) {
					$('#details-arrowLeft').addClass('details-arrowLeftOff');
					$('#details-arrowLeft').removeClass('details-arrowLeftOn');
				}
				break;
			case 'right':
				var totalWidth = ($('#details-pictures-iconlist-inner div.details-icon').length - 3) * itemWidth;
				if (Math.abs(posLeft) <= (totalWidth - itemWidth)) {
					$('#details-arrowRight').unbind('click');
					$('#details-pictures-iconlist-inner').animate({
						'marginLeft': (posLeft - itemWidth) + 'px'
					}, 'fast',function(){
						$('#details-arrowRight').click(function(){
							fbcm.slidePictureIcons('right');
							return false;
						});
					});
					$('#details-pictures-iconlist-inner').animate({
						'marginLeft': (posLeft - itemWidth) + 'px'
					}, 'fast');
					$('#details-arrowLeft').addClass('details-arrowLeftOn');
					$('#details-arrowLeft').removeClass('details-arrowLeftOff');
					posLeft -= itemWidth;
				}
				if (Math.abs(posLeft) > (totalWidth - itemWidth)) {
					$('#details-arrowRight').addClass('details-arrowRightOff');
					$('#details-arrowRight').removeClass('details-arrowRightOn');
				}
				break;
		}
	}

}

$(document).ready(function() {
	if ($.browser.msie && parseInt($.browser.version) < 7) {
		$('body').html('<div class="ie6main"><div class="ie6content"><h1>Warnung!</h1><p>Ihr Browser ist leider schon zu alt, um unsere unsere Seite noch korrekt darstellen k&ouml;nnen. Wir empfehlen Ihnen daher ein Upgrade auf einen modernen Browser durchzuf&uuml;hren.</p><p>Die aktuellen Browser bieten Ihnen wesentlich mehr Sicherheit und eine deutlich schnellere Geschwindigkeit beim Surfen im Internet.</p><p><b>Ihr Team vom<br />fellbacher boots- &amp; camping-markt</b></p><p><u>Download-Links:</u><br /><a href="http://www.microsoft.com/germany/windows/internet-explorer/default.aspx" target="_blank">Microsoft Internet Explorer</a><br /><a href="http://www.mozilla-europe.org/de/firefox/" target="_blank">Mozilla Firefox</a></p></div></div>');
	}
	else {
		fbcm.init();
		fbcm.vehiclesList();
		fbcm.vehiclesDetail();
	}
});


