$(document).ready(function () {

	window.startSlideshow?startSlideshow():null;
	window.startProductsSlideshow?startProductsSlideshow():null;
	window.agents?agents():null;
	window.colorboxInit?colorboxInit():null;
		
	$('#productsoverview_new>li>a').hover(function() {
		$(this).animate({opacity: "0.7"});
	}, function() {
		$(this).animate({opacity: "1"});
	});

});

		
function startSlideshow() {
	if($('#slideshow:not(.disabled)').length>0){
		$('#slideshowlist>li>a').click(function() {	
				clearInterval(interval);
				photo= new Image(); 
				photo.src=""+$(this).attr('href')+"";		
				$('#slideshowlist>li>a.bullet_active').removeClass('bullet_active');
				$('#slideshowlist>li>a').addClass('bullet_inactive');
				//var title = $('#slideshow h1').html();
				$(this).addClass('bullet_active');
				$("#slideshow").append("<div class=\"photo\" style=\"background-image: url("+$(this).attr('href')+");\"><h1>"+title+"<\/h1><\/div>");
				$("#slideshow>div:last").fadeIn('slow', function(){ $("#slideshow>div:first").remove(); });
				return false;
			});		
			var title = $('#slideshow h1').html();
			var i = 1;
			var interval = window.setInterval(function() {
				photo= new Image();
				photo.src=""+$('#slideshowlist>li>a.bullet_active').attr('href')+"";
				$('#slideshowlist>li>a.bullet_active').addClass('bullet_inactive').removeClass('bullet_active').parent().next().children().addClass('bullet_active');
				$("#slideshow").append("<div class=\"photo\" style=\"background-image: url("+$('#slideshowlist>li>a.bullet_active').attr('href')+");\"><h1>"+title+"<\/h1><\/div>");
				$("#slideshow>div:last").fadeIn('slow', function(){ $("#slideshow>div:first").remove(); })
								
				i++;
				
				if (i==$('#slideshowlist>li>a').size()) clearInterval(interval);			
			
			}, 3500);
		}
	}
/* old map */
/*
function agents(){
	if($('#agents').length>0){
	$('#agents>a').click(function () {
				$('#agentsinfoclose').show();
				$(this).addClass('active');
				$('#agentsinfo').fadeIn();
				$($(this).attr('href')).fadeIn();
				return false;
			});
			
			$('#agentsinfoclose, #agentsinfo').click(function () {
				$('#agents>a').removeClass('active');
				$('#agentsinfoclose').hide();				
				$('#agentsinfo>div').hide();
				$('#agentsinfo').fadeOut();
				
				return false;
			});
	}
}*/

function agents(){
	
	if($('#agentslider').length>0){
		
		$('#agentslider>a.dot').click(function(){
				$('#agentsinfoclose').show();
				$(this).addClass('active');
				$('#agentsinfo').fadeIn();
				$($(this).attr('href')).fadeIn();
				return false;
			});

		$('#agentsinfoclose, #agentsinfo').click(function () {
				$('#agentslider>a.dot').removeClass('active');
				$('#agentsinfoclose').hide();				
				$('#agentsinfo>div').hide();
				$('#agentsinfo').fadeOut();
				
				return false;
			});
		
		$('#agentsnav a').click(function(){
			/* parse $(this).href */
			var href= $(this).attr('href');
			var marginleft = href.split('&')[0].split('=')[1]; /* get value from #x= (this is not as flexible as it should be) */
			var margintop = href.split('&')[1].split('=')[1]; /* get value from #y= (this is not as flexible as it should be) */
			$('#agentslider').animate({marginLeft: '-'+marginleft+'px',marginTop: margintop+'px'},500);
			$('#agentsnav a.active').removeClass('active');
			$(this).addClass('active');
			});
		}
	}
function startProductsSlideshow(){
	if($('#products-slideshow img').length>1){
			var productsSlideshow = setInterval( "slideProducts()", 5000 );
		}
	}
function slideProducts() {
			var active = $('#products-slideshow img.active');
		
			if ( active.length == 0 ) active = $('#products-slideshow img:last');
		
			var next =  active.next().length ? active.next()
				: $('#products-slideshow img:first');
		
			active.addClass('last-active');
		
			next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 1000, function() {
					active.removeClass('active last-active');
				});
		}
		
function colorboxInit(){
	if(jQuery().colorbox){
		$('a[rel=shadowbox]').colorbox({iframe: true,width:950,height: 580});
		}
	}

