$(function() {
	
    //setInterval( "slideSwitch()", 5000 );
    	//This was for the rotating images on the homepage. 
    	//Now it just loads a random image out of the available five.
    $('div#newsAndTwit>p').jTruncate({length: 200});
    
    $('#logoBar h1').click(function(){window.location=$(this).find('a').attr('href');});
    
	
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    //THIS IS FOR IPHONE SCROLLING
    if (agentID) {
    
    	$('#newsPage li').css({fontSize: 10});
    
    	$('#goLeft').css({left: '-50px'});
 		clickCounter = 0;
 		max = $('ul.scroller>li').length;
 		$('div.scroller-container').css({overflow: 'hidden'});
 		$('ul.scroller li:first-child').addClass('active');
 		$('#grandchildren').append('<li>' + max + '</li>');
 		
        $('#goRight').click(function(){        	
        	
            if(clickCounter <= max-1){
            	activeWidth = $('.active').width();
            	liMargin = 6;
        		scrollWidth = activeWidth + liMargin;
        		if(clickCounter == 0 && $('body').hasClass('single-project')){
        			scrollWidth = activeWidth-40;
        			scrollWidth = scrollWidth + 120;
        		}else if(clickCounter == max-2){
        			$(this).fadeOut();
        		}
        		
        		if(clickCounter == 0){
        			$('#goLeft').show().animate({left: 0}, 300);
        		}
        		
            	$('.active').removeClass('active').next('li').addClass('active');
        		$('ul.scroller').animate({
        			left: '-='+scrollWidth+'px'
        		}, 500);
        		
        		
        		
        	}
        	clickCounter++;
        });
        
        $('#goLeft').click(function(){        	
        	
            if(clickCounter <=max-1){
            	activeWidth = $('.active').width();
            	prevWidth = $('.active').prev('li').width();
        		scrollWidth = prevWidth;
        		if(clickCounter == 0){
        			scrollWidth = activeWidth-40;
        			$('#goRight').fadeIn();
        			scrollWidth = scrollWidth + 120;
        		}
        		
            	$('.active').removeClass('active').prev('li').addClass('active');
        		$('ul.scroller').animate({
        			left: '+='+scrollWidth+'px'
        		}, 500);
        		
        		if(clickCounter == 1){
        			$(this).fadeOut();
        		}
        		
        		if(clickCounter == max - 1){
        			$('#goRight').fadeIn();
        		}
        		
        	}
        	clickCounter--;
        });
		
    } else {
    	//THIS IS FOR SCREEN SCROLLING
    	//approach!
	
		$("#goLeft").approach({left: '-50px'}, 300);
    	$('#goRight').approach({right: '-50px'},300);
		
		div = $('div.scroller-container');
    	ul = $('ul.scroller');
    	ulMargin = 60;
		
    	divWidth = div.width();
    	div.css({overflow: 'hidden'});
    	lastLi = ul.find('li:last-child');
		
    	//When user move mouse over menu
    	div.mousemove(function(e){    
    	  ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulMargin;
		
    	  left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    	  div.scrollLeft(left);
    	});
    	
    	$(window).resize(function(){
    		divWidth = div.width();
    		
    		div.mousemove(function(e){    
    	  		ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulMargin;
		
    	  		left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    	  		div.animate({left: left}, 400);
    	  		//div.scrollLeft(left);
    		});
    	});
    }
    
    //splashMenu setup
   // $('#splashMenu li ul').css({display: 'block', top: '60px', opacity: '1'}).hide();
    $('#splashMenu li:has(ul)>a').click(function(){return false;});
	
	//splashMenu drop-down animation
	/*$('#splashMenu li:has(ul)').hover(function(){
		$('>a', this).css({color: '#fff', cursor: 'default'}).animate({marinTop: '-3px'}, 100);
		$('>ul', this).show().animate({
			opacity: '1',
			top: '55px'
		}, 300);
	}, function(){
		$('>a', this).css({color: '#fff'});
		$('>ul', this).animate({
			opacity: '0',
			top: '60px'
		}, 300).hide(1)	;
	});*/
	
	$('#splashMenu #menu-lahznimmo>li>span').live("mousedown mouseout", function(event) {
		if(event.type == 'mousedown'){
			$(this).animate({left: 2, top: 2}, 100);
		} else if(event.type == 'mouseout'){
			$(this).animate({left: 0, top: 0}, 100);
		}
		
	});
	
	$('#splashMenu li ul li a').hover(function(){
		$(this).animate({paddingLeft: '5px'}, 100);
	}, function(){
		$(this).animate({paddingLeft: '0px'}, 100);
	});
	
	
	//-----MENU ANIMATION-----//

	//open parent menu of current page
	$('.current-menu-item').parent('.sub-menu').show().addClass('open');
	if($('#grandchildren').length > 0){
		$('#menu-lahznimmo>li:gt(0)').click(function(){
			$('#grandchildren').fadeOut();
		});
		$('#menu-lahznimmo>li:first').click(function(){
			$('#grandchildren').fadeIn();
		});
	}
	
	if($('#grandchildren').hasClass('single')){
		$('#menu-lahznimmo .sub-menu:first').show().addClass('open');
		$('#menu-lahznimmo li:first').addClass('current-menu-parent');
	}
	$('.sub-menu.open').show();
	$('.sub-menu:first li a').each(function(){
		a = $(this).text();
		b = $('.current-menu-item').parent('ul').attr('title');
		if(a == b){
			$(this).parent('li').addClass('current-menu-parent');
		}
	});
	
	$('.current-page-parent').parent('.sub-menu').show();
	$('.current-menu-parent').parent('ul').show();
	
	$('.sub-menu:not(.open)').css({display: 'block'}).hide();
	if($('body').hasClass('single-newspost')){
		$('#menu-lahznimmo>li:eq(2)').addClass('current-menu-item');
	}
	
	$("#globalNav li a, #globalNav li span").live("mouseover mouseout", function(event) {
		if ( event.type == "mouseover" ) {
			$(this).animate({paddingLeft: 5}, 150);
		} else {
			$(this).animate({paddingLeft: 0}, 150);
		}
	});
	
	$('#menu-lahznimmo li:has(.sub-menu)>a').each(function(){
		$(this).replaceWith('<span>' + $(this).text() + '</span>');
	});
	
	$('#menu-lahznimmo span').click(function(){
		$('.parentOfOpen').removeClass('.parentOfOpen');
		$(this).addClass('parentOfOpen');
	});
	
	
	$('#menu-lahznimmo>li>span').click(function(){
		$('.openSub').removeClass('openSub').fadeOut(100);
		$('ul.open').removeClass('open').fadeOut(300);
		$(this).next('ul').addClass('open').fadeIn(300);
	});
	
	//here we go: 3rd level of nav in the menu not projects (worked around by adding new ul after wp_nav_menu)
	$('.open>li>span').live('click', function(){
		$(this).next('ul').addClass('openSub').fadeIn(300);
	});
	
	$('#menu-lahznimmo ul ul .current_page_item').parents('.sub-menu').addClass('openSub').show();
	
	
	
	
	//auto-width for sub sub menu
	element = $('#grandchildren');
	numLinks = $('#grandchildren li').length;
		if(numLinks<9){
			$(element).removeClass('grid_12').addClass('grid_4');
		} else if(numLinks<17){
			$(element).removeClass('grid_12').addClass('grid_8');
		} else if(numLinks>25){
			$(element).removeClass('grid_12').addClass('grid_16');
		}
	
	$('#seeMoreOfMenu').click(function(){
		$(this).fadeOut('500');
		$('#grandchildren').animate({left: '-=640px'}, 500);
		$('#seeLessOfMenu').fadeIn('400');
	});
	
	$('#seeLessOfMenu').click(function(){
		$(this).fadeOut('500');
		$('#grandchildren').animate({left: '+=640px'}, 500);
		$('#seeMoreOfMenu').fadeIn('500');
	});
	
	//misc animations
	$('#projectMeta ul').hide();
	$('#projectMeta div').hover(function(){
		$('ul', this).slideDown();
	}, function(){
		$('ul', this).slideUp()
	});
	
	$('#allProjects li a').hide();
	$('#allProjects li').hover(function(){
		$('a', this).fadeIn(300);
	}, function(){
		$('a', this).fadeOut(300);
	});
	
	$('#newsPage li').hover(function(){
		$(this).animate({paddingLeft: 10}, 200);
	}, function(){
		$(this).animate({paddingLeft: 0}, 200);
	});
	$('#newsPage li').click(function(){
		window.location=$('a', this).attr('href'); return false;
	});
    
    
    //slideables!!
        
    $('.slideable .content').hide();
    	
    $('h3.handle').click(function(){
    
    	if($(this).hasClass('closed')){
    		$('.open')
    			.removeClass('open')
    			.addClass('closed')
    			.next('.content')
    			.slideUp();
    		$(this)
    			.removeClass('closed')
    			.addClass('open')
    			.next('.content')
    			.slideDown();
    	} else if($(this).hasClass('open')){
    		$(this)
    			.removeClass('open')
    			.addClass('closed')
    			.next('.content')
    			.slideUp();
    	}
    	
    });
	
	//columnise is saved until last so that it columnises everything
	//jquery has rendered as well (if you put it first, then collapsed
	//divs / ps with jQuery, the columns would be whack. DON'DOIT!
	$('.columnise').columnize({width: 306});
	$('.columnise .column:not(.first)').each(function(){
		$('p:first', this).css({marginTop: 20});
		$('.group:first', this).css({paddingTop: 20});
	});
	
});

