var interval;
var stuff;

$(document).ready(function() {
	
	var to;
	var closing = false;
	
	$('.promo-panel-wrapper').hover(function(){
//console.log('mouseenter');
		closing = false;
		$(this).children('.promo-panel').stop();
		$(this).children('.promo-panel').children('.promo-content-wrapper').children('.promo-content').fadeOut(0);
		
		$(this).children('.promo-panel').animate({width: '0', height: '318', top: '-10'}, 300, 'linear', function() {
//console.log('done closing from enter');
			$(this).css('background-color', "#ffffff");
			$(this).fadeTo(0, 1);
			$(this).animate({width: '298', height: '298', top: '0'}, 300, 'linear', function() {
				if(closing == false) {
//console.log('done opening from enter');
					stuff = $(this);
					$(this).children('.promo-content-wrapper').css('display', 'block');
					//stuff.children('.promo-content-wrapper').children('.promo-content').fadeIn(200);
					to = setTimeout("stuff.children('.promo-content-wrapper').children('.promo-content').fadeIn(200)", 150);
				}
			});
		});
	}, function(){
//console.log('mouseleave');
		var base = $(this);
		closing = true;
		base.children('.promo-panel').stop();
		base.children('.promo-panel').children('.promo-content-wrapper').children('.promo-content').fadeOut(0);
		clearTimeout(to);
		
		$(this).children('.promo-panel').children('.promo-content-wrapper').children('.promo-content').fadeOut(100, function() {
//console.log('fading out content');
			base.children('.promo-panel').children('.promo-content-wrapper').css('display', 'none');
			base.children('.promo-panel').animate({width: '0', height: '318', top: "-10"}, 300, 'linear', function() {
//console.log('done closing from leave');	
				$(this).css('background-color', "#ffffff");
				$(this).fadeTo(0, .5);
				$(this).children('.promo-content-wrapper').css('display', 'none');
				$(this).animate({width: '298', height: '298', top: '0'}, 300, 'linear', function() {
//console.log('done opening from leave');
				});
			});
		});
	});
	
	
	
	//scrolling promo
	interval = setInterval("slidePromo()", 8000);// 8 secs between requests
	$('.scroll_btn').click(function() {
		clearInterval(interval);
		slidePromo(this.id.substr(11));
	});
	
	
});


function slidePromo(newslide){
	if(!newslide) {
		var i = 0;
		var ready = 0;
		while(ready == 0) {
			if($('#scrolling-block-'+i).css('display') == 'block') {
				newslide = i*1+1;

				if($('#scrolling-block-'+newslide).length == 0) newslide = 0;
				
				ready = 1;
			}
			i++;
		}
	}

	if($('#scrolling-block-'+newslide).css('display') == 'none') {
		
		if(newslide == 0) {
//console.log($('#btns'));
//console.log($('#btns')[0].childElementCount);
//alert($('#btns')[0].childElementCount);
			var container = $('#btns')[0];
			
			 var childCount = 0;

			if (container.childElementCount === undefined) {
				if (container.children) {
					childCount = container.children.length;
				}
				else {  // Firefox before version 3.5
					var child = container.firstChild;
					while (child) {
						if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
							childCount++;
						}
						child = child.nextSibling;
					}
				}
			}
			else {
				childCount = container.childElementCount;
			}
			oldslide = childCount - 1;
		}
		else oldslide = newslide*1-1;

//alert('newSlide: '+newslide+' oldSlide: '+oldslide);
		//proceed with action
		//fade out text
		
		$('#scrolling-block-'+oldslide+' p').fadeOut('slow', function(){});
		$('#scrolling-block-'+oldslide+' a').fadeOut('slow');
		
		//move images
		$('#scrolling-block-'+newslide+' img').css('left', '310px');
		$('#scrolling-block-'+newslide+' p').css('display', 'none');
		$('#scrolling-block-'+newslide+' a').css('display', 'none');
		$('#scrolling-block-'+newslide).css('display', 'block');
		
		$('#scrolling-block-'+oldslide+' img').animate({left: -310}, 'slow');
		$('#scrolling-block-'+newslide+' img').animate({left: 0}, 'slow', function() {
			//fade in new text
			$('#scrolling-block-'+oldslide).css('display', 'none');
			$('#scroll_btn_'+oldslide).removeClass('active');
			$('#scroll_btn_'+newslide).addClass('active');
			$('#scrolling-block-'+newslide+' p').fadeIn('slow');
			$('#scrolling-block-'+newslide+' a').fadeIn('slow', function() {
			});
		});
	}
}
