$.noConflict();
jQuery(document).ready(function($) { 
    var markInt;
    var header = $('.header');
    var headHeight = 140; 
    /* header animation */
    header.hover(function() {
        $(this).stop().animate({
            height:'200px'
        }, 500, 'easeOutQuart');
    }, function() { 
        $(this).stop().animate({
            height:'188px'
        }, 500, 'easeOutQuart');
    });
    $('.mark').hover(function() {
        $(this).stop().animate({color:'#555'}, 500);
    }, function() {
        $(this).stop().animate({color:'#999'}, 500);
    });
    /* scroll to top */
    $('.scroll-up').click(function() {
        $('html,body').animate({
            scrollTop:0
        }, 1000);
        return false;
    }).hide();
    header.find('.wrapper').css({background:'transparent'});
    /* fade in header */
    /* $('.body').fadeOut(0).fadeIn(500); */
    resizeWin();
    $('.portfolio .item a').hover(function() {
        $(this).parents('dl').find('span, em').stop().animate({opacity:'.6'}, 500);
    }, function() {
        $(this).parents('dl').find('span, em').stop().animate({opacity:'0'}, 500);
    }).find('span').css({opacity:'0', top:'0'}).next().css({opacity:'0', top:'69px'});
    /* all alpha btns */
    $('.alphabtn').css({opacity:'.4'}).hover(function() {
        $(this).css({opacity:'.8'});
    }, function() {
        $(this).css({opacity:'.4'});
    });

    
    
    /* navigation */
    $('.nav a').hover(function() { 
        if(!$(this).parent().hasClass('active')) { 
            var canvas = $(this).next();
            var ctx = canvas[0].getContext('2d');
            var canvasWidth = canvas.width();
            var xPos = new Array(0, canvasWidth-3, 1, canvasWidth-1, 2.5, canvasWidth, 3, canvasWidth);
            var yPos = new Array(Math.ceil(Math.random()), Math.ceil(Math.random()*2), Math.ceil(Math.random()*4), Math.ceil(Math.random()*6),Math.ceil(Math.random()),Math.ceil(Math.random()*4), Math.ceil(Math.random()*8), Math.ceil(Math.random()*8));
            var count = 0;
            ctx.save();
            ctx.beginPath();
            ctx.moveTo(0, 0);
            markInt = setInterval(function() {
                ctx.lineTo(xPos[count], yPos[count]);
                ctx.fillStyle = "rgba(255, 255, 255, 0)";
                ctx.fill();
                ctx.strokeStyle = "#09f";
                ctx.stroke();
                if(count>=7) {
                    clearInterval(markInt);
                }
                count++;
            },50);
        }
        $(this).stop().animate({'color': '#09f'});
    }, function(){
        /* clear all */
        $('.nav li[class!="active"]').each(function(i){
            clearInterval(markInt);
            ctx = $(this).find('canvas')[0].getContext('2d');
            ctx.clearRect(0,0,1000,100);
        });
        $(this).stop().animate({'color': '#222'});
    });
    /* init slideshows */
    $('.case-images').carouselSlide({
        caroControl:'.case-slideshow',
        caroWidth:'544',
        caroFullWidth:'832'
    });
    setNavCanvas();
});
jQuery.event.add(window, "resize", resizeWin);
jQuery.event.add(window, "scroll", scrollWin);
function resizeWin() {
    var canvas = jQuery('#background');
    var ctx;
    var winWidth = jQuery(document).width();
    if(canvas[0].getContext) { 
        var bg = new Image();
        canvas.attr('width', winWidth);
        ctx = canvas[0].getContext('2d');
        ctx.beginPath();
        ctx.fillStyle = "#f0f2f2";
        ctx.moveTo(0,450);
        ctx.lineTo(0,0);
        ctx.lineTo(winWidth, 0);
        ctx.lineTo(winWidth, 400);
        ctx.clip();
        ctx.closePath();
        ctx.fill();
        bg.src = "assets/stylesheets/cooper/images/darkline832.png"; 
        bg.onload = function() { 
            ctx.drawImage(bg, 74, 250, 832, 400); 
        };
    }
}
function activeNav() {
    var canvas = jQuery('.active').find('canvas');
    var ctx = canvas[0].getContext('2d');
    if(canvas[0].getContext) { 
        var canvasWidth = canvas.width();
        ctx.save();
        ctx.beginPath();
        ctx.moveTo(0.0, Math.ceil(Math.random()));
        ctx.lineTo(canvasWidth - 3, Math.ceil(Math.random()*2));
        ctx.lineTo(1, Math.ceil(Math.random()*4));
        ctx.lineTo(canvasWidth - 1, Math.ceil(Math.random()*6));
        ctx.lineTo(2.5, Math.ceil(Math.random()));
        ctx.lineTo(canvasWidth, Math.ceil(Math.random()*4));
        ctx.lineTo(3, Math.ceil(Math.random()*8));
        ctx.lineTo(canvasWidth, Math.ceil(Math.random()*8));
        ctx.fillStyle = "rgb(255, 255, 255)";
        ctx.fill();
        ctx.strokeStyle = "rgb(0, 0, 0)";
        ctx.stroke();
        ctx.restore();
    }
}

function setNavCanvas() {
    jQuery('.nav li').each(function(i) {
        var el = jQuery(this);
        if(el.width()>40 && el.width()<250) {
            el.find('canvas').attr('width', function() {
                return el.width();
            });
            activeNav();
        } else {
            setNavCanvas();
        }
    });
}
function scrollWin() {
    var val = Math.floor(jQuery(document).scrollTop());
    var el = jQuery('.scroll-up');
    if(val>=200) {
        el.fadeIn();
    } else {
        el.fadeOut();
    }
}

