var J=jQuery.noConflict();
var visible = false;
function isVisible(obj) { return (obj.css('display') == 'block'); }

var sIFR = {activate:function(){}, replace: function(){}};
function cufonInit(){
    Cufon.replace([
'.header .main-menu .menu ul li.sample',
'.header .main-menu .menu ul li.select',
'.content .bottom-cont span.featured-projects-inscr',
'.content .equipment span.equipment-inscr',
'.content .bottom-cont ul.filter li.first',
'.content .bottom-cont ul.filter li.all a',
'.content .bottom-cont ul.filter li.commercial a',
'.content .bottom-cont ul.filter li.film a',
'.content .bottom-cont ul.filter li.other a',
'.content .showreel a',
'.content .tech-info-img a.other',
'.content .right-cont a.link',
'.content .right-cont span.link',
'.content .middle-cont .equip-title h2, .content .middle-cont .project-title h2, .content .middle-cont .gallery-title h2, .video-popup .equip-title h2',
'.content .right-cont p.title',
'.content .middle-cont .team-member h3, .content .middle-cont.form .contact-descr h3',
'.content .right-cont ul span.headline',
'.content .home-img .image .desc h2',
'.content .home-img .image .desc h4',
'.content .home-img .image .desc p',
'.content .middle-cont .b-c a.first',
'.content .middle-cont .b-c a.more',
'.content .middle-cont .b-c span',
'.content .left-cont ul.l-menu li.replace span',
'.content .left-cont ul.l-menu li.main span'
    ], {hover: true});
};

J(document).ready(function () {
        carousel();
        carousel2();
        carousel4();
        cufonInit();

        if (J('#proj-info').size() == 0 && J('#video-player').size() == 0){
            if( J('#gallery').size() > 0 ){
                J('#gallery').slideDown(2000);
                var a = J('.content .middle-cont A.close');
                var rel = a.attr('rel');
                var list = rel.split(' ');
                var class_name = list[1];
                var el = J('.content .middle-cont A[rel="gallery '+class_name+'"]');
                el.html('CLOSE');

                el.parent().addClass(class_name);
                el.addClass('open');
                setTimeout('carousel3();',500);
                J('#video-player').html('');
            }
        }

        var size = J('.view-projects UL LI').size();
        J('.view-projects UL').width(size*161);

        J('UL#proj-type LI A').click(function(){
            if (!J(this).parent().hasClass('main')){
                next(J(this).attr('rel'),0,'');
                return false;
            }
        });


        J('.showreel A[href="javascript:;"], #showreels LI A, .project-material LI A').click(function(){
            J('.overlay').width(J(document).width());
            J('.overlay').height(J(document).height());
            J('.overlay').show();
            if (document.getElementById('video-player') != null){
                J('#video-player').html('');
            }
            J('.video-popup').show();
            J('.video-popup A.close').click(function(){
                J('.overlay').hide();
                J('.video-popup').hide();
                if (document.getElementById('video-player') != null){
                    initPlayerGallery("themes/vfx",J("#video-player").attr("title"),"video-player");
                }
            });
            initPlayer('themes/vfx',J(this).attr("alt"),'video-player-id');
        });

        /*J('.middle-cont A.download-info').click(function(){
            var downloaderUri = '/download-pdf.php?file=';
            window.open(downloaderUri + base64_encode(J(this).attr('href')), 'pdfdownload');
            return false;
        });*/

        J('.middle-cont a.download-info').click(function(){
            window.open(J(this).attr('href'));
            return false;
        });

        J('.middle-cont .download').click(function(){
            var downloaderUri = '/download-zip.php?file=';
            var files = '';
            J('.middle-cont .technical-list INPUT:checked').each(function(i){
                files += base64_encode(J(this).val()) + ';';
            });
            if (files != ''){
                window.open(downloaderUri + files, 'pdfdownload');
            }
        });

        J('.middle-cont area').hover(function(){
            var coords = J(this).attr('coords').split(',');
            J('#popup').css('left',coords[0]+'px');
            J('#popup').css('top',coords[3]+'px');
            var desc = J(this).attr('alt').split(' | ');
            J('#popup P.name').html(desc[0]);
            J('#popup P.position').html(desc[1]);
            J('#popup').show();
            J('.descr UL LI').hide();
            J('.descr .'+J(this).attr('id')).show();
        },function(){
            J('#popup').hide();
        });

        J('.middle-cont area').click(function(){
            J('.descr UL LI').hide();
            J('.descr .'+J(this).attr('id')).show();
            return false;
        });


        J('UL#equip LI A').click(function(){
            if (!J(this).parent().hasClass('main')){
                return true;
            }
        });

        J('.content .middle-cont A.close').click(function(){
                var rel = J(this).attr('rel');
                var list = rel.split(' ');
                var class_name = list[1];
                var show = list[0];
                if (isVisible(J('#'+show))){
                    if (show == 'video-player'){
                        J('#gallery').slideDown(2000);
                        var el = J('.content .middle-cont A[rel="gallery '+class_name+'"]');
                        el.html('CLOSE');
                        el.parent().addClass(class_name);
                        el.addClass('open');
                        setTimeout('carousel3();',500);
                        J('#video-player').html('');
                    } else if (show == 'gallery'){
                        J('#video-player').show(2000, function(){
                            initPlayerGallery("themes/vfx",J("#video-player").attr("title"),"video-player");
                        });
                        var el = J('.content .middle-cont A[rel="video-player '+class_name+'"]');
                        el.html('CLOSE');
                        el.parent().addClass(class_name);
                        el.addClass('open');
                    }

                    J('#'+show).slideUp(2000);
                    J(this).html('OPEN');
                    J(this).parent().removeClass(class_name);
                    J(this).removeClass('open');
                } else {
                    if (show == 'video-player'){
                        J('#gallery').slideUp(2000);
                        var el = J('.content .middle-cont A[rel="gallery '+class_name+'"]');
                        el.parent().removeClass(class_name);
                        el.html('OPEN');
                        el.removeClass('open');
                    } else if (show == 'gallery'){
                        J('#video-player').html('');
                        J('#video-player').slideUp(2000);
                        var el = J('.content .middle-cont A[rel="video-player '+class_name+'"]');
                        el.html('OPEN');
                        el.parent().removeClass(class_name);
                        el.removeClass('open');
                    }
                    if (show == 'video-player'){
                        J('#'+show).show(2000, function(){
                            initPlayerGallery("themes/vfx",J("#video-player").attr("title"),"video-player");
                        });
                    }else{
                        J('#'+show).slideDown(2000);
                    }
                    J(this).html('CLOSE');
                    J(this).parent().addClass(class_name);
                    J(this).addClass('open');
                    setTimeout('carousel3();',500);
                }
                return false;
        });

        if (J('#gallery.close.open')){
            carousel3();
        }

        setTimeout(function(){
            if (J('.middle-cont.pre-vis .centered').length){
                var descrHeight = J('.middle-cont.pre-vis .descr').height();
                var centeredHeight = J('.middle-cont.pre-vis .centered').height();
                var winHeight = J(window).height();
                var differ = (winHeight - 526)/2;

                J(window).scroll(function () {
                    if (J.browser.msie){
                        var currentY = document.documentElement.scrollTop - differ;
                    }else{
                        var currentY = window.scrollY - differ;
                    }

                    if (currentY >= 0){
                        if ((centeredHeight + currentY) > (descrHeight + 20)){
                            return false;
                        }else{
                            //J('.middle-cont.pre-vis .centered').css('margin-top', currentY);
                            J('.middle-cont.pre-vis .centered').animate({top:currentY}, 200);
                        };
                    }else{
                        //J('.middle-cont.pre-vis .centered').css('margin-top', 0);
                        J('.middle-cont.pre-vis .centered').animate({top:0}, 200);

                    };
                });
            };
        }, 1000);


        //J.history.init(callback);

});

function carousel(){
    J(".content .image").jCarouselLite({
            btnNext: ".home-img .next",
            btnPrev: ".home-img .prev",
            auto: 5000,
            visible:1,
            speed: 2000,
            easing: "easeOutCirc"
        });
}

function carousel2(){
    J("#carousel-equip").jCarouselLite({
            btnNext: ".purple-line .bottom",
            btnPrev: ".purple-line .top",
            visible:4,
            vertical: true,
            speed: 1000
        });
}

function carousel4(){
    if (J(".project-carousel UL LI").size() > 2){
        J(".project-carousel").jCarouselLite({
                btnNext: ".purple-line .bottom",
                btnPrev: ".purple-line .top",
                visible:3,
                vertical: true,
                speed: 1000
            });
    }
}


function carousel3(){
    if (!visible){
        if (J("#gallery .carousel UL LI").size() > 8){
        J("#gallery .carousel").jCarouselLite({
                btnNext: ".next-btn",
                btnPrev: ".prev-btn",
                visible:8.37,
                speed: 1000
            });
        }
        initStilGallery();
        visible = true;

    }
}
var src = '';

function initStilGallery(){

    src = J('#gallery IMG.disp-bl').attr('src');

    J("#gallery .carousel UL LI A").click(function(){
        J('#gallery IMG.disp-bl').attr('src',J(this).attr('rel'));
        src = J('#gallery IMG.disp-bl').attr('src');
    });

    J("#gallery .carousel UL LI A").hover(function(){
        J('#gallery IMG.disp-bl').attr('src',J(this).attr('rel'));
    },function(){
        J('#gallery IMG.disp-bl').attr('src',src);
    });
}
function nextEquip(page,all){
    J('#equip-content').load('equipajax?page='+page+'&all='+all);
    //J.history.load('equipajax?page='+page+'&all='+all);
}
function callback(path){
    J('#equip-content').load(path);
}

function next(cat,page,all){
    J('#middle-cont').load('galleryajax',
            {cat:cat,
            page:page,
            all:all
            }
    );
}

function nextProject(cat,page,all){
    J('#middle-cont').load('projectajax',
            {cat:cat,
            page:page,
            all:all
            }
    );
}

/*  Service Functions  */
function base64_encode(data){

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

    do {
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
    return enc;
}

function initPlayer(path,file,id){
        var swf = new SWFObject(path+"/swf/player2.swf","player","643","392","9","#fff");
        swf.addParam("movie", path+"/swf/player2.swf");
        swf.addParam("allowfullscreen", "true");
        swf.addParam("allowscriptaccess", "always");
        swf.addParam("flashvars", "file="+file+"&autostart=true&skin="+path+"/swf/vfx.swf");
        swf.setAttribute('redirectUrl','http://get.adobe.com/flashplayer');
        swf.write(id);
}

function initPlayerGallery(path,file,id){
        var swf = new SWFObject(path+"/swf/player2.swf","player","643","392","9","#fff");
        swf.addParam("movie", path+"/swf/player2.swf");
        swf.addParam("allowfullscreen", "true");
        swf.addParam("allowscriptaccess", "always");
        swf.addParam("flashvars", "file="+file+"&autostart=true&skin="+path+"/swf/vfx-gallery.swf");
        swf.setAttribute('redirectUrl','http://get.adobe.com/flashplayer');
        swf.write(id);
}



