$(document).ready(function(){
	$('#slideshow').cycle();
	//Uzapvalinimai
	$('.roundT').each(function(){
		$(this).append('<img src="'+root+'/css/bg6.png" alt="" style="position: absolute; top: -4px; left: 0px;" />');
	});
	$('.roundB').each(function(){
		$(this).append('<img src="'+root+'/css/bg7.png" alt="" style="position: absolute; bottom: -4px; left: 0px;" />');
	});
	$('.roundWT').each(function(){
		$(this).append('<img src="'+root+'/css/bg10.png" alt="" style="position: absolute; top: -4px; left: -4px;" />');
	});
	$('.roundWB').each(function(){
		$(this).append('<img src="'+root+'/css/bg11.png" alt="" style="position: absolute; bottom: -4px; left: -4px;" />');
	});
	
	//Nespalvotu pakeitimas i spalvotus boxuose
	$('div.boxes div').bind('mouseover',function(){
		$(this).css('background',$(this).css('background').replace('_.','.'));
	});
	$('div.boxes div').bind('mouseout',function(){
		$(this).css('background',$(this).css('background').replace('.jpg','_.jpg'));
		$(this).css('background',$(this).css('background').replace('.gif','_.gif'));
		$(this).css('background',$(this).css('background').replace('.png','_.png'));
	});
	$('div.boxes:nth-child(3n)').css('margin-right',0);
	
	
	
	
	
	//Nespalvotu pakeitimas i spalvotus boxuose
	
	$('div.boxes').each(function(){
		var img = $(this).children().children('p').children();
		if (img.length > 0) {
			img.attr('rel', img.attr('src'));
			if ($.browser.msie) {
				grayscaleImageIE(img[0]);
			}
			else {
				img[0].src = grayscaleImage(img[0]);
			}
		}        
     });
	 
	 $('div.boxes').bind('mouseout',function(){
	 	var img = $(this).children().children('p').children();
			if($.browser.msie){
                grayscaleImageIE(img[0]);
            } else {
                img[0].src = grayscaleImage(img[0]);
            }           
    });
	$('div.boxes').bind('mouseover',function(){
		var img = $(this).children().children('p').children();
		img.attr('src',img.attr('rel'));
		img.css('filter','');
		
	});
	function grayscaleImageIE(imgObj){
        imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
    }

    function grayscaleImage(imgObj){
        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();
    }
	
	
});
