$(function() {
	$('#cities_up').hover(
		function() {
			stopScrolling = false;
			$('#cities ul').scrollList('down', 50, 100);
		},
		function() {
			stopScrolling = true; 
		}
	);
	$('#cities_down').hover(
		function() {
			stopScrolling = false;
			$('#cities ul').scrollList('up', 50, 100);
		},
		function() {
			stopScrolling = true; 
		}		
	);	

	var next_active = 0;
	var next_active_defined = 0;
	var first_time = true;
	
	function addClickToInactiveItem (item){
		$('.cnt', item.element ).unbind();
		$( '.cnt', item.element ).click(function (){ 
			clickInactiveItem(item);
		});			
	};
	function clickInactiveItem (item){
		//console.log("clicked inactive: " + item.index + " => " + item.CFobj._stepLock);
		if(item.CFobj._stepLock){
			next_active = item.index;
			next_active_defined = true;				
		} else {
			item.CFobj.moveToIndex(item.index);	
			item.CFobj._stopSlideshow();			
		}							
	};
	


	if (!$.browser.msie || parseInt($.browser.version, 10) > 8) {
				
		var cf = new ContentFlow('contentFlow', { 	
									reflectionHeight: 0, 
									circularFlow: true, 
									loadingTimeout: 0,
									maxItemHeight: 233, 
									relativeItemPosition: 'center center', 
									visibleItems: 1,
									flowSpeedFactor: 0.4,		        
									scaleFactor: 1, 
									scaleFactorLandscape:  3.7,
									startItem: 0,
									scrollInFrom: 0,
									endOpacity: 0.75,
									calcFontSize: function (item) {
									
										// Returns the version of Internet Explorer or a -1
										// (indicating the use of another browser).													
										function getInternetExplorerVersion(){
											var rv = -1; // Return value assumes failure.
											if (navigator.appName == 'Microsoft Internet Explorer'){
												var ua = navigator.userAgent;
												var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
												if (re.exec(ua) != null)
													v = parseFloat( RegExp.$1 );
											}
											return rv;
										}
										
										//console.log( item.size.height + " -> " + $( item.element ).css('opacity'));
										//console.log( getInternetExplorerVersion() );
										if(item.Browser.IE && getInternetExplorerVersion() == 9){
											return item.size.height;
										}else{
											var factor = 0.37;
											if(item.Browser.IE8){
												factor = 0.36;
											}
											var size = (1-item.size.height)*factor/0.25;
											//console.log( 1-item.size.height );
											return 1 - size;													
										}															            															            
							        },	
							        onclickActiveItem: function( item){
							        	//console.log("X clicked active: " + item.index);
							        	if($( item.element ).hasClass( "details" )){
											window.location = $( item.element ).attr("href"); 
										}
							        },
									onMakeActive: function (item) {
										//console.log("active: " + item.index);
										
										//on init, give all elements an click-event
										if(first_time){
											first_time = false;
											$.each(item.CFobj.items, function(index, item) {											
												addClickToInactiveItem (item);										    
											 });										
										}
										
							        	$('.cnt', item.element ).unbind();
										$( '.cnt', item.element ).click(function (){ 
											//console.log("clicked active: " + item.index);
											if($( item.element ).hasClass( "details" )){
												window.location = $( item.element ).attr("href"); 
											}
										});	
										
										if(item.CFobj._slideshow_stoped){
											$( '.cnt', item.element ).mouseout(function (){ 
												//console.log("mouseout: " + item.index);
												item.CFobj._startSlideshow();
											});											
										}

										
										
							        },	
							        onReachTarget : function (item) {
							        	//console.log("onReachTarget: " + item.index);
							        	if(next_active_defined && next_active != item.index){
							        		//console.log("onReachTarget moveto: " + next_active);
											next_active_defined = false;
											item.CFobj.moveToIndex(next_active);	
										} else if (!item.CFobj._slideshow_stoped && !item.CFobj._slideshow_locked) {
											item.CFobj._startSlideshow();
							            }
							        },	
							        onclickInactiveItem: function( item){
							        	//console.log("X clicked inactive: " + item.index);
							        	clickInactiveItem (item);
							        },
							        onMakeInactive: function (item) {
							        	//console.log("inactive: " + item.index);
							        	addClickToInactiveItem (item);																
									},
									
									
							        calcCoordinates: function (item) {
							            var rP = item.relativePosition;
							            var vI = this.conf.visibleItems; 
							
							            var f = 1 - 1/Math.exp( Math.abs(rP)*0.75);
							            var x =  item.side * vI/(vI+1)* f; 
							            var y = 1;
													
													//Korrektur der Inhaltsposition
													$( '.cnt', item.element ).css('top', "-" + this.conf.maxItemHeight*item.size.height + "px");
																									
													//Berechnung der Größen von internen Bildern
													var cntFlow = this;																										
													$.each($( 'img', $( '.cnt', item.element ) ), function() { 
														var img = this;
																												
														var height, width;																									
														//speichern der Startbildgröße
														if($( img ).attr('start_height') == undefined){															
															if($( img ).css('height') != undefined && $( img ).css('height') != '0px'){
																//wurde dem Bild eine Höhe per CSS zugewiesen, dann nimm diese														
																height = $( img ).css('height').replace("px","");
															}else{
																//wurde nix per CSS zugewiesen, dann nimm orginal Höhe
																height = img.height;
															}															
															$( img ).attr('start_height', height);
														}else{
															height = $( img ).attr('start_height');
														}															
														if($( img ).attr('start_width') == undefined){														
															if($( img ).css('width') != undefined && $( img ).css('width') != '0px'){
																//wurde dem Bild eine Breite per CSS zugewiesen, dann nimm diese
																width = $( img ).css('width').replace("px","");
															}else{										
																//wurde nix per CSS zugewiesen, dann nimm orginal Breite					
																width = img.width;
															}																
															$( img ).attr('start_width', width);
														}else{
															width = $( img ).attr('start_width');
														}		
														
														//console.log("Faktor: " + item.size.height + " - " + height + " " + width);														
														$( this ).css('height', item.size.height*height + "px"); 
														$( this ).css('width', item.size.height*width + "px");													  
													});
													
													//console.log(item.index + ": " + f + " " + x + " " + item.size.height + ": " + 	$( '.text', item.element ).css('top'));
							            return {x: x, y: y};
							        }								        
		});
		ContentFlowGlobal.setAddOnConf('slideshow', {startOnLoad: true, duration: 4000, showControlls: false});
		
		$('#offers').removeClass('noScript');
	}
});

