$(document).ready(function() {

	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});


	$('#menu .section')
	.mouseenter( function () {
		$('#menu').addClass('hover');
		$('#menu .hover').removeClass('hover');
		$(this).parent().addClass('hover');
	});

	$('#menu')
	.mouseleave(function() {
      	$('#menu').removeClass('hover');
		$('#menu .hover').removeClass('hover');
    });

	// Home slides

	$('#homeslide')
	.before('<div id="nav" class="nav">')
	.cycle({
        fx:     'scrollRight',
        speed:  'slow',
        timeout: 5000,
        pager:  '#nav'
    });

	$('#homeminislide').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 4500, 
		next:   '#homeminisliden',
		prev:   '#homeminislidep' 
	});

	var url=window.location;
	var anchor=url.hash;
	
	if(anchor) {
		scrollTo('#description','fast',anchor);
	}

	// Expertise slides
	
	$('#partnerslide').cycle({ 
		fx:     'scrollLeft', 
		speed:  'slow', 
		timeout: 3000,
		next:   '#parnerslidenext',
		prev:   '#parnerslidebefore'
	});

});

var testme = 0;

function scrollBack(place,speed,block){
	scroller(place,'fast');
	$(testme).fadeOut('fast');
	$('#back').fadeOut('fast');
	
	testme = 0;
}

function scrollTo(place,speed,block){

	if (testme == 0) {
		$(block).fadeIn('fast', function(){
			scroller(place,'fast');
		});
	} else {
		$(testme).fadeOut(function() {
				$(block).fadeIn('fast', function() {
					scroller(place);
				});
			}
		);
	}
	testme = block;
}

function scroller(place, speed) {
	$('html, body').animate({
		scrollTop: $(place).offset().top
	}, speed);
	$('#back').fadeIn('fast');
}

function showText(el) {
	//el.parent('.textmask').animate( { height:$('.textmask .text').height() }, 1500 );
	textHeight = el.parent().parent().children('.textmask').children('.text').height();
	el.parent().parent().children('.textmask').animate( { height:textHeight }, 1500 );
	el.parent().fadeOut( function() { el.parent().parent().children('.less').fadeIn(); } );
}

function hideText(el) {
	el.parent().parent().children('.textmask').animate( { height:0 }, 1500 );
	el.parent().fadeOut( function() { el.parent().parent().children('.more').fadeIn(); } );
}

function opencontactpopup() {
	$('#popupbg').fadeIn('fast',
		function() {
			if (typeof contactPopupOpened == 'undefined') { $('#popupwhiteb').html(contactmap); };
			$('#popupfilter').click(
				function () {
					$('#popupbg').fadeOut('fast');
				}
			);
			$('#popupbg .close').click(
				function () {
					$('#popupbg').fadeOut('fast');
				}
			);
			contactPopupOpened = 1;
		}
	);
}
