JCB = {
};

JCB.CONSTS = {
	carousels: {
		images: {
			locationCarousel: '#images .carouselMechanism ul',
			locationDisplay: '#images .carouselDisplay',
			speedCarousel: 400
		},
		testimonials: {
			locationCarousel: '#testimonials .carouselMechanism ul',
			locationDisplay: '#testimonials .carouselDisplay',
			speedCarousel: 400
		},
		videos: {
			locationCarousel: '#videos .carouselMechanism ul',
			locationDisplay: '#videos .carouselDisplay',
			speedCarousel: 400
		}
	},
	navigationExplore: {
		de: 'Explore',
		en: 'Explore',
		fr: 'Explore'
	},
	navigationExploreExterior: {
		de: 'ExploreExterior',
		en: 'ExploreExterior',
		fr: 'ExploreExterior'
	},
	navigationExploreInterior: {
		de: 'ExploreInterior',
		en: 'ExploreInterior',
		fr: 'ExploreInterior'
	},
	heightVideoContainer: 474,
	itemCount: {
		de: 'Video [x] von [y]',
		en: 'Video [x] of [y]',
		fr: 'Vidéo [x] de [y]'
	},
	speedScroll: 500, 
	speedVideoContainerOpen: 1000,
	speedVideoContainerClose: 1500,
	speedVideoContainerFadeIn: 1000,
	speedVideoContainerFadeOut: 500,
	paramsVideo: {
		de: {
			closeString: 'Schliessen',
			downloadThisString: 'VIDEO DOWNLOADEN',
			nextString: 'WEITER',
			replayString: 'NOCHMAL ABSPIELEN',
			movie: null
		},
		en: {
			closeString: 'Close',
			downloadThisString: 'DOWNLOAD THIS',
			nextString: 'NEXT',
			replayString: 'REPLAY',
			movie: null
		},
		fr: {
			closeString: 'Fermer',
			downloadThisString: 'TELECHARGER',
			nextString: 'SUIVANT',
			replayString: 'REDEMARRER',
			movie: null
		}
	}
};

JCB.VARS = {
	carousels: {
		active: {
			images: null,
			testimonials: null,
			videos: null
		},
		objects: {
			images: null,
			testimonials: null,
			videos: null
		},
		timeouts: {
			images: null,
			testimonials: null,
			videos: null
		}
	},
	locationHash: null,
	panelOpening: 0
};

$(function() {
});

$(document).ready(function() {
	if ($("#navigationMain").length > 0) {
		$("#navigationMain .fancybox").fancybox({type: 'iframe', width: 932, height: 492, scrolling: 'no', padding: 0, overlayColor: '#000', overlayOpacity: 0.7});
	}
	JCB.VARS.locationHash = location.hash.replace(/\#/g, "");
	if ($('.contentExplore').size() > 0) {
		JCB.GENERIC.loadFlashExplore();
	}
	if (JCB.VARS.locationHash == JCB.CONSTS.navigationExploreExterior[navigationLanguage]) {
		navigationActive = 'navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage];
	}
	JCB.GENERIC.navigationIntercept();
	JCB.GENERIC.highlightNavigation();
	if ($('#images').size() > 0) {
		JCB.CAROUSELS.initCarousel($(JCB.CONSTS.carousels.images.locationCarousel), 'images');
		$('#images .jcarousel-skin-JCB li:eq(0) a').trigger('click');
	}
	if ($('#testimonials').size() > 0) {
		JCB.CAROUSELS.initCarousel($(JCB.CONSTS.carousels.testimonials.locationCarousel), 'testimonials');
		JCB.CAROUSELS.setCarouselStart('testimonials', 1);
		$('#testimonials .testimonial').css('display', 'inline');
	}
	if ($('#videos').size() > 0) {
		JCB.CAROUSELS.initCarousel($(JCB.CONSTS.carousels.videos.locationCarousel), 'videos');
	}
	JCB.GENERIC.bindDocument();
	//$("a, h1, h2, h3, h4, h5, h6, li, p, span").ieffembedfix();
});

// *******************************************************************************************

JCB.GENERIC = {
	scrollRightColumn: function(scrollTarget) {
		$('.contentRight').scrollTo($('#' + scrollTarget), JCB.CONSTS.speedScroll);
	},
	
	hashChanged: function (e){
		JCB.VARS.locationHash = location.hash.replace(/\#/g, "");
		if (JCB.VARS.locationHash == JCB.CONSTS.navigationExploreExterior[navigationLanguage]) {
			navigationActive = 'navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage];
		}
		else {
			navigationActive = 'navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage];
		}		
		JCB.GENERIC.highlightNavigation();
		JCB.FLASH.changeView();
	},

	highlightExploreExterior: function() {
	//	navigationActive = 'navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage];
		location.hash = JCB.CONSTS.navigationExploreExterior[navigationLanguage];
	//	JCB.GENERIC.highlightNavigation();
	},
	
	highlightExploreInterior: function() {
	//	navigationActive = 'navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage];
		location.hash = JCB.CONSTS.navigationExploreInterior[navigationLanguage];
	//	JCB.GENERIC.highlightNavigation();
	},

	highlightNavigation: function() {
		JCB.VIDEOS.panelClose();
		$('#navigationSub li').removeClass('active');
		$('#navigationSub li').removeClass('arrowed');
		$('#' + navigationActive).addClass('active');
		$('#' + navigationActive).addClass('arrowed');
		$('#' + navigationActive).parents('li').addClass('active');
	},
	
	navigationIntercept:function() {
		if ((navigationActive == 'navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage]) || (navigationActive == 'navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage])) {
			$('#navigation' + JCB.CONSTS.navigationExplore[navigationLanguage] + ' a, #navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage] + ' a, #navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage] + ' a').unbind('click');
			$('#navigation' + JCB.CONSTS.navigationExplore[navigationLanguage] + ' a, #navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage] + ' a, #navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage] + ' a').bind('click', function() {
				var navigationClicked = $(this);
				
				var navigationParts = navigationClicked.attr('href').split('#');
				location.hash = navigationParts[1];
				/*navigationActive = 'navigation' + navigationParts[1];
				JCB.GENERIC.highlightNavigation(navigationActive);
				JCB.FLASH.changeView();*/
				return false;
			});
		}
	},
	
	loadFlashExplore:function() {
		var flashvarHash = 'home';
		if (JCB.VARS.locationHash == JCB.CONSTS.navigationExploreExterior[navigationLanguage]) {
			flashvarHash = 'exterior'
		}
		if (JCB.VARS.locationHash == JCB.CONSTS.navigationExploreInterior[navigationLanguage]) {
			flashvarHash = 'interior'
		}
		var flashvars = {hash:flashvarHash,navigationLanguage:navigationLanguage};
		var params = {allowScriptAccess: "always", menu: "false"};
		params.wmode = "transparent";
		swfobject.embedSWF("../swf/site.swf", "flash", "980", "514", "9.0.115", "../swf/expressInstall.swf", flashvars, params);
		$('#flash img').css('visibility','visible');
	},

	bindDocument: function() {
		$(document).unbind('click');
		$(document).bind('click', function(click) {
			var elementClicked = $(click.target);
			if (elementClicked.attr('class').indexOf('carouselDisplay') >= 0) {
				JCB.VIDEOS.panelClose();
			}
		});
		
		$(window).bind( 'hashchange', JCB.GENERIC.hashChanged );
	}
};

// *******************************************************************************************

JCB.FLASH = {
	changeView: function() {
		 var flashMovie = null;
         if (navigator.appName.indexOf("Microsoft") != -1) {
             flashMovie = window["flash"];
         } else {
			flashMovie = document["flash"]; 
		}
		if (navigationActive == 'navigation' + JCB.CONSTS.navigationExploreExterior[navigationLanguage]) {
			flashMovie.updateSection('exterior');
		}
		if (navigationActive == 'navigation' + JCB.CONSTS.navigationExploreInterior[navigationLanguage]) {
			flashMovie.updateSection('interior');			
		}
	},
	
	reactivateFlash: function() {
		 var flashMovie = null;
         if (navigator.appName.indexOf("Microsoft") != -1) {
             flashMovie = window["flash"];
         } else {
			flashMovie = document["flash"]; 
		}
		if (flashMovie) {		
			flashMovie.reactivateHotspots();
		}
		
	}
};

// *******************************************************************************************

JCB.CAROUSELS = {
	initCarousel: function(objectToCarousel, carouselType) {
		objectToCarousel.jcarousel({
			animation: JCB.CONSTS.carousels[carouselType].speedCarousel,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			itemFirstInCallback: JCB.CAROUSELS.getInViewFirst,
			itemLastInCallback: JCB.CAROUSELS.getInViewLast,
			scroll: 1
		});
		JCB.VARS.carousels.objects[carouselType] = $(objectToCarousel).data('jcarousel');
		JCB.VARS.carousels.objects[carouselType].carouselType = carouselType;
		if (JCB.VARS.carousels.objects[carouselType].size() > 7) {
			JCB.CAROUSELS.bindControls(carouselType);
		} else {
			$('#' + carouselType + ' .carousel-next').css({
				'display':'none'
			});
			$('#' + carouselType + ' .carousel-prev').css({
				'display':'none'
			});
		}
	},
	
	bindControls: function(carouselType) {
		$('#' + carouselType + ' .carousel-next').unbind('mouseenter');
		$('#' + carouselType + ' .carousel-next').bind('mouseenter', function() {
	        JCB.VARS.carousels.objects[carouselType].mouseDown = '1';
	        JCB.CAROUSELS.carouselNext(carouselType);
	    });
		$('#' + carouselType + ' .carousel-next').unbind('mouseleave');
		$('#' + carouselType + ' .carousel-next').bind('mouseleave', function() {
			JCB.VARS.carousels.objects[carouselType].mouseDown = '0';
			clearTimeout(JCB.VARS.carousels.timeouts[carouselType]);
		});
		$('#' + carouselType + ' .carousel-prev').unbind('mouseenter');
		$('#' + carouselType + ' .carousel-prev').bind('mouseenter', function() {
	        JCB.VARS.carousels.objects[carouselType].mouseDown = '1';
	        JCB.CAROUSELS.carouselPrev(carouselType);
	    });
		$('#' + carouselType + ' .carousel-prev').unbind('mouseleave');
		$('#' + carouselType + ' .carousel-prev').bind('mouseleave', function() {
			JCB.VARS.carousels.objects[carouselType].mouseDown = '0';
			clearTimeout(JCB.VARS.carousels.timeouts[carouselType]);
		});
	},
	
	carouselNext: function(carouselType) {
		if ((JCB.VARS.carousels.objects[carouselType].mouseDown == 1) && (!JCB.VARS.carousels.objects[carouselType].animating)) {
			JCB.VARS.carousels.objects[carouselType].next();
		}
		JCB.VARS.carousels.timeouts[carouselType] = setTimeout("JCB.CAROUSELS.carouselNext('" + carouselType + "');", 10);
	},

	carouselPrev: function(carouselType) {
		if ((JCB.VARS.carousels.objects[carouselType].mouseDown == 1) && (!JCB.VARS.carousels.objects[carouselType].animating)) {
			JCB.VARS.carousels.objects[carouselType].prev();
		}
		JCB.VARS.carousels.timeouts[carouselType] = setTimeout("JCB.CAROUSELS.carouselPrev('" + carouselType + "');", 10);
	},

	getInViewFirst: function(carousel, item, idx, state) {
		carousel.inViewFirst = idx;
	},

	getInViewLast: function(carousel, item, idx, state) {
		carousel.inViewLast = idx;
	},

	setCarouselStart: function(carouselType, newPosition) {
		carouselObject = JCB.VARS.carousels.objects[carouselType];
		if (newPosition == null) {
			newPosition = 1;
		}
		var speedCarouselCurrent = carouselObject.options.animation;
		carouselObject.options.animation = 0;
		carouselObject.scroll(newPosition);
		carouselObject.options.animation = speedCarouselCurrent;
		JCB.CAROUSELS.makeCarouselItemActive(carouselType, newPosition, 1);
	},

	changeDisplayedItem: function(carouselType, newAsset, theLink) {
		var newPosition = theLink.parents('li').attr('jcarouselindex');
		JCB.CAROUSELS.makeCarouselItemActive(carouselType, newPosition, 0);
		if (carouselType == 'images') {
			JCB.CAROUSELS.changeDisplayedImage(carouselType, newAsset);
		}
		if (carouselType == 'testimonials') {
			JCB.CAROUSELS.changeDisplayedTestimonial(carouselType, newAsset);
		}
		if (carouselType == 'videos') {
			JCB.CAROUSELS.changeDisplayedVideo(carouselType, newAsset);
		}
	},

	changeDisplayedImage: function(carouselType, newAsset) {
		$('#' + carouselType + ' .carouselDisplayItem').css('display', 'none');
		$('#' + newAsset).css('display', 'inline');
	},

	changeDisplayedTestimonial: function(carouselType, newAsset) {		
		swfobject.removeSWF("testimonialPlayerContainer");		
		$('.carouselDisplayItem').prepend('<div id="testimonialPlayerContainer"></div>');
		JCB.CONSTS.paramsVideo[navigationLanguage].movie = newAsset;
		JCB.CONSTS.paramsVideo[navigationLanguage].type = 'testimonials';
		swfobject.embedSWF("../swf/videoplayer.swf", "testimonialPlayerContainer", "616", "347", "9.0.115", false, JCB.CONSTS.paramsVideo[navigationLanguage], {id:"videoPlayer", allowFullScreen: "true", menu: 'false', wmode: 'transparent'}, {styleclass: 'sample'});
		var itemCountTemp = JCB.CONSTS.itemCount[navigationLanguage].replace(/\[x\]/g, JCB.VARS.carousels.active[carouselType]);
		itemCountTemp = itemCountTemp.replace(/\[y\]/g, JCB.VARS.carousels.objects[carouselType].size());
		$('p.itemCount').html(itemCountTemp);
		var newText = '<h2>' + details_title[JCB.VARS.carousels.active[carouselType]-1] + '</h2>';
		newText += '<h3>' + details_credit[JCB.VARS.carousels.active[carouselType]-1] + '</h3>';
		newText += '<p>' + details_text[JCB.VARS.carousels.active[carouselType]-1] + '</p>';
		//newText += '<div class="layout videoFlag"><img src="../images/flag_' + details_language[JCB.VARS.carousels.active[carouselType]-1] + '.png" alt="" /></div>';
		$('.carouselDetails').html(newText);
	},
	
	changeDisplayedVideo: function(carouselType, newAsset) {		
		swfobject.removeSWF("videoPlayerContainer");		
		$('.carouselDisplayItem').prepend('<div id="videoPlayerContainer"></div>');
		JCB.CONSTS.paramsVideo[navigationLanguage].movie = newAsset;
		JCB.CONSTS.paramsVideo[navigationLanguage].type = 'videos';
		swfobject.embedSWF("../swf/videoplayer.swf", "videoPlayerContainer", "616", "347", "9.0.115", false, JCB.CONSTS.paramsVideo[navigationLanguage], {id:"videoPlayer", allowFullScreen: "true", menu: 'false', wmode: 'transparent'}, {styleclass: 'sample'});
		var itemCountTemp = JCB.CONSTS.itemCount[navigationLanguage].replace(/\[x\]/g, JCB.VARS.carousels.active[carouselType]);
		itemCountTemp = itemCountTemp.replace(/\[y\]/g, JCB.VARS.carousels.objects[carouselType].size());
		$('p.itemCount').html(itemCountTemp);
	},
	
	videoPlayAll: function(carouselType, newAsset) {
		var carouselList = $(JCB.CONSTS.carousels[carouselType].locationCarousel);
		$('li', carouselList).removeClass('active');
		$('.videosAll a').addClass('active');
		JCB.CAROUSELS.changeDisplayedVideo(carouselType, newAsset);
	},
	
	makeCarouselItemActive: function(carouselType, activeItem, triggerClick) {
		JCB.VARS.carousels.active[carouselType] = activeItem;
		var carouselList = $(JCB.CONSTS.carousels[carouselType].locationCarousel);
		$('.videosAll a').removeClass('active');
		$('li', carouselList).removeClass('active');
		$('li', carouselList).each(function() {
			var theElement = $(this)
			if (theElement.attr('jcarouselindex') == activeItem) {
				theElement.addClass('active');
				if (triggerClick == 1) {
					$('a', theElement).trigger('click');
				}
			}
		});
	},

	incrementDisplayed: function(carouselType, itemIncrement) {
		var carouselObject = JCB.VARS.carousels.objects[carouselType];
		if (!carouselObject.animating) {
			var itemToDisplay = parseInt(JCB.VARS.carousels.active[carouselType]) + itemIncrement;
			if (itemToDisplay < 1) {
				itemToDisplay = carouselObject.size();
			}
			if (itemToDisplay > carouselObject.size()) {
				itemToDisplay = 1;
			}
			if (!((itemToDisplay >= carouselObject.inViewFirst) && (itemToDisplay <= carouselObject.inViewLast))) {
				if (itemIncrement > 0) {
					carouselObject.scroll(itemToDisplay - (carouselObject.inViewLast - carouselObject.inViewFirst));
				}
				if (itemIncrement < 0) {
					carouselObject.scroll(itemToDisplay);
				}
			}
			$('li', $(JCB.CONSTS.carousels[carouselType].locationCarousel)).each(function() {
				var theElement = $(this)
				if (theElement.attr('jcarouselindex') == itemToDisplay) {
					$('a', theElement).trigger('click');
				}
			});
		}
	}

};

// *******************************************************************************************

JCB.VIDEOS = {
	panelOpenAt: function(newPosition) {
		if(newPosition == "airvent") newPosition = "aircon";
		var startIndex = $('.jcarousel-skin-JCB li').index($('#'+ newPosition))+1;
		//console.log(newPosition + " " + startIndex);
		JCB.CAROUSELS.setCarouselStart('videos', startIndex);
		JCB.VIDEOS.panelOpen();
	},

	panelOpen: function() {
		JCB.VARS.panelOpening = 1;
		var panelHeight = $('#videos').height();
		if (panelHeight == 0) {
			$('#videos .video').css('display', 'none');
			$('#videos').animate({
				height: JCB.CONSTS.heightVideoContainer + 'px'
			}, JCB.CONSTS.speedVideoContainerOpen, function() {
				$('#navigationSub').addClass('navigationSubDark');
				$('#videosOverlay').fadeIn(JCB.CONSTS.speedVideoContainerFadeIn, function() {
					$('#videos .video').css('display', 'inline');
					JCB.VARS.panelOpening = 0;
				});
			});
		}
	},
	
	panelClose: function() {
		if ($('#videos').size() > 0) {
			var panelHeight = $('#videos').height();
			if ((panelHeight > 0) && (JCB.VARS.panelOpening == 0)) {
				$('#videosOverlay').fadeOut(JCB.CONSTS.speedVideoContainerFadeOut);
				$('#navigationSub').removeClass('navigationSubDark');
				swfobject.removeSWF("videoPlayerContainer");
				$('#videos .video').css('display', 'none');
				$('#videos').animate({
					height: '0px'
				}, JCB.CONSTS.speedVideoContainerClose, function() {
					$('#videosOverlay').css('display', 'none');
					JCB.FLASH.reactivateFlash();
				});
			}
		}
	}
};

// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************
// *******************************************************************************************

