$(document).ready(function() {
	
	swfobject.embedSWF('/skin/flash/map.swf', 'mapflashbig', '480', '300', '9.0.0', '/skin/flash/expressInstall.swf', false, {menu:false,wmode:'opaque'});
	
	// dropdown menu
	$('li.dropdown').hover(function () {
		newHeight = $('ul',this).height()+4;
		$('#backdrop').css('display','block');
		$('#backdrop').css('height',newHeight);
	 },
	 function () {
		$('#backdrop').css('display','none');	 
	 });
	
	/*
	$('#left-content .newsletter-item').hover(function(){
		var this_id = $(this).attr('id');
		this_id = this_id.replace('newsletter_', 'image_');
		$('#'+this_id).css({left:'0px'});
	},function(){
		var this_id = $(this).attr('id');
		this_id = this_id.replace('newsletter_', 'image_');
		$('#'+this_id).css({left:'-9999px'});
	});
	*/
	var newsletterObj = $('#newsletter-image');
	var newsletterWaitTime = 3000;
	var newsletterFadeTime = 2000;
	var previousImageId = 0;
	
	var newsletterImages = $('img', newsletterObj);
	
	if( newsletterImages.length > 1 ) {
		animateNewsletterImages(1);
	}
	
	$(newsletterImages).each(function(){
		$(this).hide().removeClass('hide');
	});
	
	$('img', newsletterObj).eq(0).each(function(){
		$(this).show().addClass('top');
	});
	
	function animateNewsletterImages(id) {
		var nextImage;
		if( id == (newsletterImages.length-1) ) {
			var nextImageId = 0;
		} else {
			var nextImageId = id+1;
		}
		var previousImage = $('img', newsletterObj).eq(previousImageId);
		var image = $('img', newsletterObj).eq(id);
		if(image) {
			$(image).delay(newsletterWaitTime).fadeTo(1,0,function(){
				$(previousImage).removeClass('top');
				$(image).addClass('top').fadeTo(newsletterFadeTime,1,function(){
					$(previousImage).fadeOut(1);
					previousImageId = id;
					animateNewsletterImages(nextImageId);
				});
			});
		}
	}
	
	


// banner rotater
	var bannerObj = $('#hero');
	var waitTime = 8000;
	var fadeTime = 3000;
	var previousImageId = 0;
	
	var bannerImages = $('img.banner-inner', bannerObj);
	
	if( bannerImages.length > 1 ) {
		animateImages(1);
	}
	
	$(bannerImages).each(function(){
		if( $(this).attr('id') == 'bannerid-0' ) {
			$(this).addClass('banner-top');
		} else {
			$(this).hide().removeClass('hide');
		}
	});
	
	function animateImages(id) {
		
		//alert('animate id: '+id);
		
		var nextImage;
		
		if( id == (bannerImages.length-1) ) {
			var nextImageId = 0;
		} else {
			var nextImageId = id+1;
		}
		
		var previousImage = $('#bannerid-'+previousImageId, bannerObj);
		var image = $('#bannerid-'+id, bannerObj);
		
		if(image) {
			$(image).delay(waitTime).fadeTo(1,0,function(){
				$(previousImage).removeClass('banner-top');
				$(image).addClass('banner-top').fadeTo(fadeTime,1,function(){
					$(previousImage).fadeOut(1);
					previousImageId = id;
					animateImages(nextImageId);
				});
			});
		}
	}

	var leftProjectContent = $('#left-project-content');
	var thisPhoto = $('img:first', leftProjectContent);
	$('.photos a.hide', leftProjectContent).hide().removeClass('hide');
	if($(thisPhoto).height()>10) {
		$('.photos', leftProjectContent).height($(thisPhoto).height());
	}
	
	$('#imagenums a').click(function(){
		$('.photos a', leftProjectContent).hide();
		$('.photos a#'+$(this).attr('rel'), leftProjectContent).show();
		var thisPhoto = $('.photos a#'+$(this).attr('rel')+' img', leftProjectContent);
		if($(thisPhoto).height()>10) {
			$('.photos', leftProjectContent).height($(thisPhoto).height());
		}
		return false;
	});

	var leftProductContent = $('#left-product-content');
	var thisPhoto = $('img:first', leftProductContent);
	$('.photos a.hide', leftProductContent).hide().removeClass('hide');
	if($(thisPhoto).height()>10) {
		$('.photos', leftProductContent).height($(thisPhoto).height());
	}
	
	$('#imagenums a').click(function(){
		$('.photos a', leftProductContent).hide();
		$('.photos a#'+$(this).attr('rel'), leftProductContent).show();
		var thisPhoto = $('.photos a#'+$(this).attr('rel')+' img', leftProductContent);
		if($(thisPhoto).height()>10) {
			$('.photos', leftProductContent).height($(thisPhoto).height());
		}
		return false;
	});
	
	setTimeout(function(){
		var thisPhoto = $('img:first', leftProjectContent);
		$('.photos a.hide', leftProjectContent).hide().removeClass('hide');
		if($(thisPhoto).height()>10) {
			$('.photos', leftProjectContent).height($(thisPhoto).height());
		}
	}, 3000);
	
	setTimeout(function(){
		var thisPhoto = $('img:first', leftProductContent);
		$('.photos a.hide', leftProductContent).hide().removeClass('hide');
		if($(thisPhoto).height()>10) {
			$('.photos', leftProductContent).height($(thisPhoto).height());
		}
	}, 3000);

});
