function onBefore() {
    var thisIndex = $(this).parent().children().index(this);
    $("#nav_sequence_1 div").removeClass("button1-selected");
    $("#nav_sequence_1 div").eq(thisIndex).addClass("button1-selected");
}


// http://sorgalla.com/projects/jcarousel/

function mycarousel_itemLoadCallbackBefore(carousel, state)
{
//	console.log(state);
//	console.log(carousel.first);
	
//	var count = carousel.first + carousel.scroll - 1;
//	for( ; count < carousel.last + carousel.scroll ; count++) {
//		carousel.add(carousel.first + carousel.scroll + count, '<div>Loading...</div>');
//	}
}

function mycarousel_itemLoadCallbackAfter(carousel, state)
{
//	console.log(state);
//	console.log(carousel.first);
	
	// Check if the requested items already exist
	if (carousel.has(carousel.first, carousel.last)) {
		return;
	}
	
	// Add loading to all elements
//	var count = carousel.first-1;
//	for( ; count < carousel.last ; count++) {
//		carousel.add(carousel.first + count, '<div>Loading...</div>');
//	}
	
	// Now send GET to the server
	jQuery.get(
		'/index_scroll_ajax.php',
		{
			first: carousel.first,
			last: carousel.last,
			language: $('#language').val()
		},
		function(serverData) {
			mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, serverData);
		},
		'json'
	);
};

function mycarousel_itemAddCallback(carousel, first, last, serverData)
{
    //console.log(serverData);
    
    // Set the size of the carousel
    carousel.size(serverData.total);
	
    // Add scenes
    jQuery(serverData.scene).each(function(key, serverData) {
    	//console.log(key);
    	//console.log(serverData);
    	carousel.add(first + key, serverData);
    });
};


(function ($) {
	$(function() {
		var currContentHeight = 0;
		var maxContentHeight = 0;
		var currTitleHeight = 0;
		var maxTitleHeight = 0;
		var oneRowLevel = $(".oneRowLeveler");
		oneRowLevel.each(function() {
			var titleLeveler = $(this).find(".titleLeveler");
			//console.log(titleLeveler);
			titleLeveler.each(function() {
				currTitleHeight = $(this).height();
				if (currTitleHeight > maxTitleHeight) {
					maxTitleHeight = currTitleHeight;
				}
			});
			titleLeveler.height(maxTitleHeight);
			//console.log(maxTitleHeight);
			currTitleHeight = 0;
			maxTitleHeight = 0;
	
			var contentLeveler = $(this).find(".contentLeveler");
			//console.log(contentLeveler);
			contentLeveler.each(function() {
				currContentHeight = $(this).height();
				if (currContentHeight > maxContentHeight) {
					maxContentHeight = currContentHeight;
				}
			});
			contentLeveler.height(maxContentHeight);
			//console.log(maxContentHeight);
			currContentHeight = 0;
			maxContentHeight = 0;
		});
		
		$('.lightbox').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif',
			imageBlank: '/images/lightbox-btn-blank.gif',
			containerBorderSize: 10,
			containerResizeSpeed: 350
		});

		
		/*
		$("#oneCarCarousel .smallPic").hover(function() {
			var thisHREF = $(this).attr("href");
			$("#oneCar .picContainer a").attr("href", thisHREF);
			$("#oneCar .picContainer a img").fadeTo(200, 0.1, function() {
				$(this).attr("src", thisHREF).fadeTo(200, 1);
			});
		}, function() {

		});
		
		$('#video_icon').bind('click', function(){
			alert('video');
			return false;
		});
		*/
		
		/// Video Player ///
		$("#oneCarCarousel .smallPic").hover(function() {
			var thisHREF = $(this).attr("href");
			var thisALT = $(this).find("img").attr("alt");
			if ($("#oneCar .picContainer .videoContainer").is(":visible")) {
				$("#oneCar .picContainer .videoContainer").fadeOut(500, function() {
					$("#oneCar .picContainer a img").attr("src", thisHREF);
					$("#oneCar .picContainer a").attr("src", thisALT).fadeIn(500);
				});	
			}
			$("#oneCar .picContainer a").attr("href", thisHREF);
			$("#oneCar .picContainer a img").fadeTo(200, 0.1, function() {
				$(this).attr("src", thisALT).fadeTo(200, 1);
			});
		}, function() {

		});
		
		$("#oneCarCarousel .smallPic").click(function() {
			return false;
		});
		$("#oneCarCarousel .videoShower").click(function() {
			$("#oneCar .picContainer a").fadeOut(500, function () {
				$("#oneCar .picContainer .videoContainer").fadeIn(500);
			});
			return false;
		});
		/// Video Player ///
		
		
		//Scroller corousel using AJAX - 2010-02-17 - moced to template to remember scrolled pages
		/*
		$("#offersCarousel").jcarousel({
	        scroll: 5,
	        animation: 800,
	        itemVisibleOutCallback: {
	        	onAfterAnimation: function(carousel, item, i, state, evt) {
	        		carousel.remove(i);
	        	}
	        },
	        itemLoadCallback: {
	        	onBeforeAnimation: mycarousel_itemLoadCallbackBefore
				,onAfterAnimation: mycarousel_itemLoadCallbackAfter
			}
	    });
	    */
	    
	    $("#oneCarCarousel").jcarousel({
	        scroll: 3,
	        animation: 800
	    });
	    
	    $("#carList .oneCar").children().css("background-position", "0 -200px").css("background-color", "transparent");
	    $("#carList .oneCar").hover(function() {
	    	$(this).children().css("background-position", "0 0");
	    	$(this).children(".middle").css("background-color", "#2f2f2f");
	    }, function() {
	    	$(this).children().css("background-position", "0 -200px").css("background-color", "transparent");
	    });
	    
	    $("#contactUs .inputButton").click(function() {
		    $.nyroModalManual({
				content: $(".nyroContainer").html()
			});
			$.nyroModalSettings({
				width: 200,
				height: 200
			});
		});
		
		/*
		$('#sequence_1').cycle({
		    fx:     'scrollUp',
		    timeout: 3000,
		    delay:  -2000
		});
		*/
		$('#sequence_1').cycle({
			fx:     'scrollUp',
			speed:  'normal',
		    timeout: 3000,
			pager:  '#nav',
			before:  onBefore, 
			pause: 1,
			pagerAnchorBuilder: function(idx, slide) {
				// return selector string for existing anchor
				//return '#nav_sequence_1 li:eq(' + idx + ') a';
				
				//var temp = '#nav_sequence_1 div:eq(' + idx + ')';
				//alert(temp);
				
				return '#nav_sequence_1 div:eq(' + idx + ')';
		    }
		});
		
		// Here an option - stop this cycle! after displaying selected slide
		$("#nav_sequence_1 div").click(function () {
			$("#nav_sequence_1 div").removeClass("button1-selected");
			$(this).addClass("button1-selected");
			$('#sequence_1').cycle('pause');
			
			// Resume sliding after NN miliseconds - timer
			var sequence_1_temp = function(){
				$("#sequence_1").cycle('resume');
			};
			setTimeout(sequence_1_temp, 10000);
	    });
	    
	    // Here an another option - stop sliding on HOVER on the buttons!
		$("#nav_sequence_1 div").hover(
			function () {
				$("#sequence_1").cycle('pause');
			}, 
			function () {
				$("#sequence_1").cycle('resume');
			}
		);
		
		
		// button Overs
		$('.button1').hover(function() {
			$(this).removeClass("button1").addClass("button1-over");
		}, function() {
			$(this).removeClass("button1-over").addClass("button1");
		});
		
		$("input:text").hint();
		
		
	});
})(jQuery);