$(document).ready(function(){
    
    if($("#map_canvas").length > 0) {
        initialize();
    }
    
    if($('#slides').length > 0) {
        $('#slides').slides({
    		preload: true,
    		preloadImage: 'img/loading.gif',
    		play: 0,
    		pause: 2500,
    		hoverPause: false,
    		autoHeight: true,
    		generateNextPrev: true,
    		generatePagination: false            
    	});
    }
    
    $(window).resize(function(){
        $(".slides_container .block").css({
           width: parseInt($("#slides").width()) - 50 
        });
    });

    window.setTimeout(function(){
        $(".slides_control").css({
            height: parseInt($("#slides .block:first").css("height")) + 50
        });
    }, 500);
    
    $(".slides_container .block").css({
       width: parseInt($("#slides").width()) - 50
    });
    
    
    $(".eightcol .highlights li > a img").each(function(){
        var _this = $(this);
        window.setTimeout(function(){
            _this.animate({
               top: 0 
            }, 200);
        }, (Math.floor(Math.random() * 15 + 1) * 100));
    });
    

    
    $(".eightcol .highlights li").click(function(){
        $(".tooltip").hide();
        $(this).find(".tooltip").fadeIn(250);
        //return false;
				//console.log('hello i am here');
    }).hover(function(){
        $(".eightcol .highlights li").css({ zIndex: 10 });
        $(this).css({ zIndex: 100 }).find(".tooltip").show();
    }, function(){
        $(this).find(".tooltip").hide();
    });
    
    $(".eightcol .highlights li").each(function(i){
        
        if(i < 8) { $(this).addClass("top"); }
        
        if(i > 16) { i -= 16; }
        if(i > 8) { i -= 8; } 
        i++;
    
        if(i % 6 == 0) { $(this).find(".tooltip").addClass("sixth"); } 
        else if (i % 7 == 0) { $(this).find(".tooltip").addClass("seventh"); } 
        else if (i % 8 == 0) { $(this).find(".tooltip").addClass("eighth"); }
        
    });
    
    $(".highlights li a").hover(function(){
        $(this).find("img:not(.search)").css({
           opacity: 1 
        }).next(".search").show();
    }, function(){
        $(this).find("img:not(.search)").css({
           opacity: 0.8
        }).next(".search").hide();
    });
    
    $(".fourcol .highlights li a").fancybox({
        inline: true,
        overlayOpacity: 0.9,
        padding:0,
        margin:0,
        overlayColor: "#ffffff",
        cyclic: true,
        transitionIn: "elastic",
        transitionOut: "elastic"
    });
    
    $(".highlights li a img:not(.search)").css({
        opacity: 0.8
    });
    
    $(".clients li").hover(function(){
        $(this).find("img:not(.ext)").css({
           opacity: 1 
        }, 100);
    }, function(){
        $(this).find("img:not(.ext)").css({
           opacity: 0.6 
        });
    });
    $(".clients li img:not(.ext)").css({
        opacity: 0.6
    });
    
   $("video").VideoJS({
      controlsBelow: false, // Display control bar below video instead of in front of
      controlsHiding: true, // Hide controls when mouse is not over the video
      defaultVolume: 0.85, // Will be overridden by user's last volume if available
      flashVersion: 9, // Required flash version for fallback
      linksHiding: true // Hide download links when video is supported
    });
   
   		$(".mobile-toggle").toggle(function(){
			$("#menu").show();
			$(this).text("+ Hide menu");
		}, function(){
			$("#menu").hide();
			$(this).text("+ Show menu");
		});
    
    if(window.location.search === "?animation") {
        $("#row1").prepend("<canvas id='canvas'></canvas>");
    	var canvas = document.getElementById("canvas");
    	var context;

    	//Test if browser supports HTML5 Canvas
    	if (canvas && canvas.getContext) {
    		context = canvas.getContext('2d');

    	    var width = window.innerWidth - 20;
        	var height = 152;
        	var particles = [];
        	var noParticles = 30;
        	var vel = 0.2;
        	var size = 2;
        	var sprites = 6;

        	function init(){
        		ResizeCanvas();
        		clearTimeout(intervalID);
        		var intervalID = setInterval(loop, 1000 / 24);
        	}

        	function loop(){
        		context.clearRect(0, 0, width, height);
        		for(var i = 0; i < noParticles; i++) {
        			createParticle();
            		var particle = particles[i];	
        			particle.update();
            		particle.render(context);
        		}
        	}

        	function createParticle() {
                var particleImage = new Image();
                particleImage.src = '/wp-content/themes/vizeum/i/particle'+Math.floor (Math.random() * sprites + 1)+'.png';
                var particle = new ImageParticle(particleImage, (Math.random() * width), (Math.random() * height )); 

            	particle.velX = ((Math.random()*(vel*5))-vel);
            	particle.velY = ((Math.random()*(vel*5))-vel);
            	particle.size = 0.2*Math.random();
            	particle.alpha = Math.random();
            	particle.spin = 3*Math.random();

                particles.push(particle);
        	};

        	function ImageParticle(img, posx, posy) {

            	this.posX = posx; 
            	this.posY = posy; 
            	this.velX = 0; 
            	this.velY = 0; 
            	this.spin = 0; 
            	this.size = 1; 
            	this.alpha = 1; 
            	this.rotation = 0; 
            	this.compositeOperation = 'lighter';
            	this.img = img;

                this.update = function() {

                    //Stop particles going off screen width
                    if (this.posX > width) {
                        this.posX = -vel - Math.random();
                    }

                    //Stop particles going off bottom or top
                    if (this.posY > height) {
                        this.posY = -vel - Math.random();
                    }

            		this.posX += this.velX;
            		this.posY += this.velY; 
            		this.rotation += this.spin;
                }

                this.render = function(c) {
            		c.save(); 
            		c.translate(this.posX, this.posY);
            		c.scale(this.size,this.size);
            		c.rotate(this.rotation * (Math.PI / 180));
            		c.globalAlpha = this.alpha; 
            		c.globalCompositeOperation = "lighter";
            		c.drawImage(img,0,0);
            		c.restore();
                }
        	}

        	function ResizeCanvas() {
        		canvas.width = width;
        		canvas.height = height;
        	}

        	init();
        } else {
    		$('info').hide();
    		alert("Your browser does not support Canvas. Please upgrade");
    	}
        
    }
    

    
});





