Cufon.replace('#bottom-nav a', { "fontSize": '10px', hover: { color:'#ffffff' } });
Cufon.replace('#footer p span');
Cufon.replace('#side-full p');
Cufon.replace('#top-nav a');
Cufon.replace('#second-nav a');
Cufon.replace('.entry h2');
Cufon.replace('.entry p');
Cufon.replace('.inner-page #navigation ul li a', { fontSize:'11px', hover: { color:'#ffffff' } });
Cufon.replace('.popup h2');
Cufon.replace('.popup p');
Cufon.replace('.contact label');
Cufon.replace('.contact-info p');
Cufon.replace('.contact-buttons a');

Cufon.replace('.page-home #navigation ul li a', { fontSize:'22px', hover: { color:'#590202' } });


$( document ).ready( function(){
	
	
	
	$('.view-clients').click(function(){
		$('#clients').fadeIn();
		return false;
	});
	
	$('.close').click(function(){
		$(this).parent().fadeOut();
		return false;
	});
	
	$('.clear-form').click(function(){
		$('#contactform input[type=text], #contactform textarea').val('');
		return false;
	});
	$('.submit-form').click(function(){
		$('#contactform').submit(); 
		return false;
	});
	var i = 0;
	$('#logos li').each(function(){
		if( i < 7 ) {
			$('#logos ul').append( $(this).clone() );
			i++;
		}else {
			return false;
		}
	});
	
	//alert(top);
	_animate();
	
	$('#logos ul a').hover(
		function(){
			$('#logos ul').stop();
		},
		function(){
			_animate(direction);
		}
	);
	
	
	_scroller();
	$('#scroller li img')
		.live('mouseover', function(){
			_remove_gray( this );
		})
		.live('mouseout', function(){
			_gray( this );
		})
	
	$('#scroller li img').each(function(){ 
		var img_obj = this;
		this.old_src = this.src;
		
		//preloading
		img = new Image();
		img.onload = function(){ _gray( img_obj ); }
		img.src = this.src;
	});
	
	
	if(FlashDetect.installed){
    	$('#bottom-nav').addClass('flash-installed');
		$('.cms-nav').addClass('cms-navFLASH');
    }else {
    	$('#bottom-nav').addClass('flash-not-installed');
		$('.cms-nav').addClass('cms-navNOFLASH');
    }
	
	
});

var direction;
function _animate( direct ) {
	var ul_top = parseInt($('#logos ul').css('top'));
	var top = -1 * ($('#logos ul').height()-1024) + 'px';
	
	direction = ul_top == 0 ? 'top' : 'bottom';
	
	if( typeof(direct) != 'undefined' ) {
		direction = direct;
	}
	
	if(direction == 'bottom' ) {
		//top = 0;
	}
	
	var speed = Math.abs(parseInt(top) - parseInt(ul_top)) * 10;
	
	$('#logos ul').animate(
		{ 'top' : top }, 
		speed, 
		'linear',
		function(){
			$('#logos ul').css({ 'top' : '-80px' });
			_animate();
		}
	);
}






function _scroller(){
	var parent = $('#scroller');
	var ul = parent.find('ul:eq(0)');
	var li = parent.find('li');
	var clip = $('#scroller-clip');
	
	if( li.length <= 5 ) 
		return;
	
	
	var direction = 'right';
	var li_w = li.outerWidth();
	var ul_w = li.length * li_w + parseInt(ul.css('padding-right'));
	var clip_w = clip.outerWidth();
	var clip_x = clip.offset().left;
	ul.css({ 'width': ul_w + 'px' });
	
	var time = li.length * 500;
	
	var $t = this;
	var animated = false;
	var direct;
	
	clip.mousemove(function(e){
		var x = e.pageX - clip_x;
		var left_x = clip_w / 2 - 70;
		var right_x = clip_w / 2 + 70;
		//var rigth_x
		
		if( x > clip_w / 2 ) {
			direct = 'right';
		}else {
			direct = 'left';
		}
		
		if( direction != direct || !animated ){
			direction = direct;
			$t._animate();
		}
		
		if( x > left_x && x < right_x ) {
			$t._stop();
		}
		
	});
	
	clip.hover(function(){}, function(){ $t._stop(); });
	
	_stop = function(){
		ul.stop();
		animated = false;
	}
	
	
	_animate = function(){
		if( direction == 'left' ) {
			left_position = 0;
		}else {
			left_position = clip_w - ul_w;
		}
		
		var space_left = Math.abs( left_position - ul.position().left )
		time = Math.ceil(space_left / ul_w * 20000);
		
		ul.stop();
		ul.animate({ 'left' : left_position + 'px' }, time, 'linear');
		animated = true;
	}
	
}



function _gray( img ) {
	if($.browser.msie){
		grayscaleImageIE( img );
	} else {
		img.src = grayscaleImage( img );
	}
}

function _remove_gray( img ) {
	if($.browser.msie){
		remove_grayscaleImageIE( img );
	} else {
		img.src = img.old_src;
	}	
}


function grayscaleImageIE(imgObj){
	imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
}

function remove_grayscaleImageIE( img ) {
	img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=0)';
}

function grayscaleImage(imgObj){
	imgObj.old_src = imgObj.src;
	
    var canvas = document.createElement('canvas');
    var canvasContext = canvas.getContext('2d');
    
    var imgW = imgObj.width;
    var imgH = imgObj.height;
    canvas.width = imgW;
    canvas.height = imgH;
    
    
    
    canvasContext.drawImage(imgObj, 0, 0);
    var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
    
    for(var y = 0; y < imgPixels.height; y++){
        for(var x = 0; x < imgPixels.width; x++){
            var i = (y * 4) * imgPixels.width + x * 4;
            var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
            imgPixels.data[i] = avg; 
            imgPixels.data[i + 1] = avg; 
            imgPixels.data[i + 2] = avg;
        }
    }
    
    canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
    return canvas.toDataURL();
}
