var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;


$(document).ready(function() {

	$('#menu-header li li:first-child').addClass('first-child');

    if(typeof navigation_handler == 'object') {
        navigation_handler.setup();
    }

	var all_links = $('a');
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();

	all_links.map(function(i, el){
		if($(el).attr('href')) {
			if($(el).attr('href').indexOf('http://') != -1 && $(el).attr('href').indexOf(hostname) == -1) {
				$(el).attr('target', '_blank');
			}
			
			if($(el).attr('href').indexOf('.pdf') != -1 || $(el).attr('href').indexOf('.doc') != -1 || $(el).attr('href').indexOf('.docx') != -1) {
				$(el).attr('target', '_blank');
			}
		}
	});
	
	if(typeof slideshow_handler == 'object') {
		slideshow_handler.setup();
	}
	
	$("#photos-items a, #gallery-teaser-items .photo a, #publication-list .article .thumb a, a[rel=fancybox], a[rel=content-gallery]").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false,
			'titlePosition': 'over'
			//'titleShow'		: 	false
	});
	
	
	$('#menu-header li').bind('mouseover', function(event) {
		//$(event.currentTarget).addClass('over');
	});	
	$('#menu-header li').bind('mouseout', function(event) {
		//$(event.currentTarget).removeClass('over');
	});	


	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_opera) {
		$('body').addClass('browser-opera');
	}

	if(typeof sidebar_slideshow == 'object') {
		 sidebar_slideshow.setup();
	}
	
	if($('#secondary-articles').length > 0) {

		setTimeout(function(){
			var secondary_max_height = 0;
	
			$('#secondary-articles .intro').map(function(i, el){
				if($(el).height() > secondary_max_height) {
					secondary_max_height = $(el).height();
				}
			});

			$('#secondary-articles .intro').css({'height':secondary_max_height});

		}, 100);	
		
	}

	if($('#primary-articles').length > 0) {

		setTimeout(function(){
			var primary_max_height = 0;
	
			$('#primary-articles .intro').map(function(i, el){
				if($(el).height() > primary_max_height) {
					primary_max_height = $(el).height();
				}
			});

			$('#primary-articles .intro').css({'height':primary_max_height});

		}, 100);	
		
	}
	

	if(typeof content_handler == 'object') {
		content_handler.setup();
	}	
	
});


var navigation_handler = {
    items: [],
    
    setup: function()  {
        this.items = $('#menu-top-menu').children();
		
		$('#menu-header li ul').wrap('<div class="dropdown-menu"></div>');

		$(this.items).bind('mouseover', function(event) {
            if($(event.currentTarget).hasClass('over')) return;
            
            navigation_handler.items.map(function(i, el){
                if($(event.currentTarget) != el) {
                    $(el).removeClass('over');
                }
            })

			$(event.currentTarget).addClass('over');
			
            var submenu = $('ul', event.currentTarget)[0];
            if(submenu) {

                    if(!ie8 && !ie7 && !ie6) {
						$(submenu).css({'opacity':'0'});
						$(submenu).animate({
						opacity: 1.0
						  }, 300, function() {
						});
					}
            }
        });        

        $('body').bind('mouseover', function(event) {
            if($(event.target).parents('#menu-header').length > 0) return;
            navigation_handler.items.map(function(i, el){
                
				if($(el).hasClass('over')) {
					$(el).removeClass('over');
				}
				
            })
			
        });

    }
}


var slideshow_handler = {
    container: null,
	items: [],
    total_items: null,
    last_item: null,
    delay: 4000,
	animation: true,
	photos: [],
	to: null,
    
    setup: function()  {
		this.container = $('#home-slideshow');
		if(this.container.length == 0) return;
		
		this.photos = $('.photo', this.container);
		this.items = $('#slideshow-thumbs li');
		$(this.items).first().addClass('selected');
		
		this.total_items = this.items.length;
		
		this.last_item = 0;
		
		$('#slideshow-thumbs img').bind('click', function(event) {
			var parent_li = $(event.currentTarget).parents('li').first();
			if($(parent_li).hasClass('selected')) return;
			
			slideshow_handler.animation = false;
			clearTimeout(slideshow_handler.to);
			
			var current_index = $(slideshow_handler.items).index(parent_li);
			
			slideshow_handler.switchPic(current_index);
		});	

		this.to = setTimeout(function(){

			next_index = slideshow_handler.last_item + 1;
			if(next_index == slideshow_handler.total_items) {
				next_index = 0;
			}
			
			slideshow_handler.switchPic(next_index);
		}, slideshow_handler.delay);
		
		
    },
    
    switchPic: function(next_index) {
		
		$(slideshow_handler.items).removeClass('selected');
		$(slideshow_handler.items[next_index]).addClass('selected');

		var last_pic = $('#home-slideshow .active');
		$(last_pic).animate({
			queue: false,
			opacity: 0
		  }, 1000, function() {
			
		});
		setTimeout(function(){$(last_pic).removeClass('active')}, 1000);
	
		var next_pic = $('#home-slideshow .photo')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active');
		$(next_pic).animate({
			queue: false,
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});
	
		this.last_item = next_index;
		
		if(slideshow_handler.animation == true) {
			slideshow_handler.to = setTimeout(function(){
	
				next_index = slideshow_handler.last_item + 1;
				if(next_index == slideshow_handler.total_items) {
					next_index = 0;
				}
				
				slideshow_handler.switchPic(next_index);
			}, slideshow_handler.delay);
		}
		
    }	
}


var sidebar_slideshow = {
    container: null,
    total_items: 4,
    last_item: null,
    delay: 6500,
    
    setup: function()  {
		setTimeout(function(){ sidebar_slideshow.changePic(); }, sidebar_slideshow.delay);
		last_item = 0;
    },
    
    changePic: function() {
	var last_pic = $('#sidebar-gallery .active');
	var next_index = this.last_item + 1;
	if(next_index == this.total_items) {
	    next_index = 0;
	}
	
	var last_pic = $('#sidebar-gallery .active');
	$(last_pic).animate({
	    opacity: 0
	  }, 1000, function() {
	    // Animation complete.
	});
	setTimeout(function(){$(last_pic).removeClass('active')}, sidebar_slideshow.delay);

	var next_pic = $('#sidebar-gallery .photo')[next_index];
	$(next_pic).css({'opacity':'0'});
	$(next_pic).addClass('active');
	$(next_pic).animate({
	    opacity: 1
	  }, 1000, function() {
	    // Animation complete.
	});
	
	this.last_item = next_index;
	setTimeout(function(){ sidebar_slideshow.changePic(); }, sidebar_slideshow.delay);
    }
}



var content_handler = {
    setup: function() {
        $(window).resize(function() {
            content_handler.content_resize();
        });
        this.content_resize();
    },
    
    content_resize: function() {
		
		var dh = $(document).height();
		var wh = $(window).height();
		var lh = $('#layout').height()-159;

		if(lh < wh) {
			$('#layout').css({'height': wh-159});
		} else {
			$('#layout').css({'height': 'auto'});
		}
		
    }
}

