
$(document).ready(function() {
    $(window).resize(resize);
    resize();

    $(".divtab").mouseover(function() { Menu.show($(this).siblings(".divmenu"));});
    $(".divmenu").mouseover(function(){ Menu.show($(this)); });
    $(".divtab, .divmenu").mouseout(function() { Menu.hide();});   
    $(".tooltip_anchor").hover(
        function() { showtooltip(this.id);  }, 
        function() { hidetooltip(); }); 
});

function resize() {
	
	function checkAdSize(name) {
		$(name).css("height", "");
		$(name+" .container").css("height", "");		
	    if( $(name+" .container").height()<200) {
	        $(name).height(200);
	        $(name+" .container").height(191);
	    }		
	}
	
    $("#tableOuter").css("width", "");
    if($("#tableOuter").width() > 1400)
        $("#tableOuter").width(1400);
		
	checkAdSize("#banner2");
	checkAdSize("#banner4");
	checkAdSize("#banner5");	
}

Menu = {timer : null, current : null};

Menu.show= function(obj) {
    if(this.timer) clearTimeout(this.timer);
    if (this.current!=null && obj.attr("id")!=this.current.attr("id"))
        this.doHide();
    obj.slideDown(300);
    this.current= obj;
}

Menu.hide= function() {
    this.timer = setTimeout("Menu.doHide()",300);
}

Menu.doHide = function(){
    if(this.current!=null){
	this.current.fadeOut(300);	
	this.current = null;
    }
}


var sshotidx= 0;
var SSHOT_TOUT= 8000;


function fade() {
    $("#random").show(1);
    $("#randomback").show();
    $("#random").fadeOut(2500, function() {
        $("#ss_img").attr("src", $("#bss_img").attr("src"));    
	$("#random").show(1);
	if (++sshotidx>=shots[0].length)
            sshotidx= 0;
        updateSshot(sshotidx);
        $("#randomback").hide();
        setTimeout("fade()", SSHOT_TOUT);
    });
}


function canvasSshot() {

        new canvasUtil.rotateAnim(document.getElementById('shot'), shots[1][sshotidx], shots[1][(sshotidx+1) % shots[0].length]);
        setTimeout("canvasSshot()", SSHOT_TOUT);
	if (++sshotidx>=shots[0].length)
            sshotidx= 0;
        updateSshot(sshotidx);
}


function updateSshot(idx) {
    $("#ss_a").attr("href", shots[0][idx]);
    $("#ss_a").attr("title", shots[3][idx]);
    $("#ss_a2").html(shots[2][idx]+" screenshot");
    $("#bss_img").attr("src", shots[1][(idx+1)%shots[0].length].src);
    $("#ss_img").attr("src", shots[1][idx].src);
    $("#ss_a").lightBox( {imageBtnClose: '/css/lightbox/lightbox-btn-close.gif'});
}

function showtooltip(obj) {    
    tooltipShown = $('#tooltip_'+obj);   
    tooltipShown.css("top", $("#"+obj).offset().top+20 + 'px');
    tooltipShown.css("left", $("#"+obj).offset().left + 'px');
    tooltipShown.fadeIn(300);
}
    
function hidetooltip() {
    if (tooltipShown==null)
        return;
    tooltipShown.fadeOut(500);
    tooltipShown= null;
}

function unentityfy(text) {
    return $("<div>"+text+"</div>").text();
}

var canvasUtil= {
    support: function() {
        return !!document.createElement('canvas').getContext;
    },
    rotateAnim: function(canvas, img, img2) {
        var startTime= new Date().getTime();
        canvas.width= 175;
        canvas.height= 130;
        var buffer= document.createElement('canvas');
        buffer.width= canvas.width;
        buffer.height= canvas.height
        var buffer2= document.createElement('canvas');
        buffer2.width= img2.height;
        buffer2.height= img2.width;

        animLoop();

        function easing(t,d,c,b) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}	
        }

        function draw(t) {
            if (t>90) t=90;

            var context= canvas.getContext('2d');
            var buffer_context= buffer.getContext('2d');
            buffer_context.clearRect(0,0, buffer.width, buffer.height);
            buffer_context.save();
            buffer_context.rotate(3.1415926535*easing(t,90,90,0)/180);
            buffer_context.drawImage(img, 0,0);

            buffer2_context= buffer2.getContext('2d');
            buffer2_context.save();
            buffer2_context.translate(img2.width/2, img2.width/2);
            buffer2_context.rotate(3.1415926535*-90/180);
            buffer2_context.drawImage(img2,-img2.width/2, -img2.width/2);    
            buffer_context.drawImage(buffer2, 0, -img2.width);

            context.clearRect(0,0,canvas.width,canvas.height);
            context.drawImage(buffer,0,0);

            buffer_context.restore();           
            buffer2_context.restore();           
        }

        function animLoop() {
            var t= (new Date().getTime() - startTime)*90 / 1500;
            draw(t);
            if (t<90)
                setTimeout(animLoop, 1000/60);    
        }
    },
    alphaWavesElem:null,
    alphaWavesTout: null,
    alphaWaves: function (id, damping) {
        var t=0;        

        clearTimeout(canvasUtil.alphaWavesTout);
        canvas= document.getElementById(id);
        canvas.width= canvas.style.width.substring(0, canvas.style.width.length -2 );
        canvas.height= canvas.style.height.substring(0, canvas.style.height.length -2 );

        var current= new Array();
        for (i=0; i<canvas.height; i++) {
            current[i]= new Array();
            for(j=0; j<canvas.width; j++)
                current[i][j]=0;
        }
        var old= new Array();
        for (i=0; i<canvas.height; i++) {
            old[i]= new Array();
            for(j=0; j<canvas.width; j++)
                old[i][j]=0;
        }
        splash(parseInt(canvas.width/2 + Math.random()*canvas.width/3-canvas.width/6), parseInt(canvas.height/2), canvas.height>40 ? 20 : parseInt(canvas.height/2));


        draw();
        setTimeout(animLoop, 10);

        function splash(ox, oy, radius) {
            for (y= oy-radius; y<oy+radius; y++)
                for (x= ox-radius; x<ox+radius; x++) {
                    dy= y-oy;
                    dx= x-ox;
                    dist= Math.sqrt(dx*dx + dy*dy);
                    if (dist<radius) 
                        old[y][x]= current[y][x]= 256 - 256*dist/radius;
                }
        }

        function ripples(x, y) {
            old[y][x]= (current[y-1][x] + current[y][x-1] + current[y+1][x] + current[y][x+1] )/1.999 - old[y][x];
            old[y][x]= old[y][x] * damping;
            return current[y][x]*3;
        }
        
        function draw() {
            var context= canvas.getContext('2d');
            context.fillStyle= '#808080';
            context.fillRect(0,0,canvas.width,canvas.height);
            var data= context.getImageData(0,0,canvas.width,canvas.height);

            for (y=1; y<canvas.height-1; y++) {
               var offset= y*canvas.width*4 + 4;
               for (x=1; x<canvas.width-1; x++) {
                   data.data[offset++]= 128 - old[y][x]/4;
                   data.data[offset++]= 128 - old[y][x]/4;
                   data.data[offset++]= 128 + (current[y][x] > 0 ? current[y][x] : 0);
                   data.data[offset++]= 256 - ripples(x,y);
                }
            }

            tmp= old;
            old= current;
            current= old;
            
            context.putImageData(data,0,0);

        }

        function animLoop() {
            t++;
            draw();
            if (t<600) {
                clearTimeout(canvasUtil.alphaWavesTout);
                canvasUtil.alphaWavesTout= setTimeout(animLoop, 1000/60);
            }
        }

    }
};


function alphaWaves(elem) {
    if (canvasUtil.alphaWavesElem==elem)
        return;

    clearTimeout(canvasUtil.alphaWavesTout);

    canvasUtil.alphaWavesElem= elem;

    var canvas= $(elem).parent().find("canvas:eq(0)");
    canvas.css("left", $(elem).position().left + ($(elem).hasClass("level3") ? 8 : 0) );
    canvas.css("top", $(elem).position().top);
    canvas.width($(elem).width());
    canvas.height($(elem).height());
    canvas.show();
    alphaWavesInstance= new canvasUtil.alphaWaves(canvas.attr("id"), 0.995);
}



