(function( $, undef ) {

var IE = (function() {
    var v = 3,
        div = document.createElement( 'div' );
    while (
        div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
        div.getElementsByTagName('i')[0]
    );
    return v > 4 ? v : undef;
}() );

window.ta = {
    
    init: function() {
	
    	var _link = $('#menu li a')		// Anchor in title
	
    	_link.each(function() {
    		var _relative = this.getAttribute('href',2)
    		if (window.location.href.indexOf(this.href) == 0 && _relative.length > 0 && _relative != '#') {
    		    
    			$(this).parents('li').addClass('active');
    			
    			// so dirty
    			if ($(this).parent().parent().hasClass('main')) {
    			    $(this).parent().append($('.sub'));
    			}
    		}
    	});
	
    	var _hr = $(document.createElement('div')).addClass('hr');
    	
    	$('#primary').append(_hr);
	
        if($(".news_images").length) {
        	$('.news_images li:first-child').addClass('active');
        	$('.news_images').galleria({
        		history:false,
        		onThumb:function(thumb){thumb.css('display','none').fadeIn();}
        	});
        }
    
    	$('#menu ul.expandable>li:not(.active)').addClass('hidden')

    	// Expandable menu events
    	$('#menu ul.expandable>li').click( function() {
		
    		// manage classes
    		$('#menu ul.expandable>li.active').removeClass('active').addClass('hidden')
    		$(this).removeClass('hidden').addClass('active')
		
    	});
    	
    	var selectors = 'h1, #primary h2, #secondary h2, #menu ul.main li, h5, #menu>ul.sub li span';
        var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        
	    if ( /mac/i.test(navigator.platform) || chrome ) {
	        $(selectors).each(function() {
	            var size = parseInt( $(this).css('font-size') );
	            $(this).css({
	                fontSize: size+1+'px',
	                fontFamily: 'NobelLight,arial,sans-serif'
	            });
	        });
	    } else {
	        Cufon.replace( selectors );
	        
	        if ( IE == 7 ) {
	            $(selectors).each(function() {
	                
	                return;
	                var a = $(this).children('a');
	                
	                if (!a.length) {
	                    a = $(this).find('>span>a');
	                }
	                
	                if (!a.length) {
	                    return;
	                }
	                
	                var href = a.attr('href');
	                
	                a.attr('href','#').click(function(e) {
	                    e.preventDefault();
	                });
	                
	                $(this).css('cursor','pointer').attr('title', href).click(function(e) {
	                    e.preventDefault();
	                    window.location = href;
	                });
	                //console.log(a.attr('href'))
	            })
	        }
	    }
    }
}

})( jQuery );
