var Agenc = {};




Agenc.addActive = function(link){
    /*$('#menu .selected').addClass('selected');*/
    $(link).parent().addClass('active');
    $('#menu').addClass('has-active');
    var current = $(link).parent();
    while (current.parent().parent().get(0).id != 'menu') {
        current.parent().parent().addClass('has-active');
        
        current = current.parent().parent();
        if (current.children('span').size() > 0) {
            current.addClass('selected');
        }
        
    }
    
};

Agenc.removeActive = function(){
    /*$('#menu .selected').removeClass('selected');*/
    var active = $('#menu .active');
    if (active.size() == 1) {
        active.removeClass('active');
        var current = active;
        while (current.parent().parent().get(0).id != 'menu') {
            current.parent().parent().removeClass('has-active');
            current = current.parent().parent();
        }
        $('#menu').removeClass('has-active');
    }
};

Agenc.removeOneActive = function(){
    /*$('#menu .selected').removeClass('selected');*/
    var active = $('#menu .active');
    
    if (active.size() == 1) {
    
        var next = active.parent().parent();
        if (next.get(0).id != 'menu' && !next.parent().hasClass('level0')) {
            active.removeClass('active');
            next.removeClass('has-active');
            next.addClass('active');
        }
        else {
            Agenc.removeActive();
        }
        
    }
};



Agenc.initMenu = function(){

    if ($('#menu li.active').size() > 0) {
        Agenc.addActive($('#menu li.active a').get(0));
    }
    $('#menu ul a').click(function(){
        if ($(this).parent().hasClass('old-active')) {
            return false;
        }
        Agenc.removeActive();
        Agenc.addActive(this);
		return true;
    });
    $('#menu .back').click(function(){
        Agenc.removeOneActive();
        return false;
    });
    
    $('#menu span').click(function(){
        $(this).parent().toggleClass('selected');
        return false;
    });
    
};

Agenc.createLogoSwf = function(){
    $('#heading h1 span').flash({
        wmode: 'transparent',
        src: 'agenc-theme/swf/logoanim.swf',
        width: 114,
        height: 32
    }, {
        version: 9
    });
    $('#heading').append('<div id="homelink"/>').click(function(){
        window.location = 'index.php';
    });
    
};

Agenc.createImpressionSwf = function(folderPath, horImageCount, verImageCount, colorFieldCount, width, height){

    var flashvars = {
        imgpath: folderPath,
        wImg: horImageCount,
        hImg: verImageCount,
        colorImg: colorFieldCount
    };
    
    
    
    $('#content .heading img').flash({
        wmode: 'transparent',
        scale: "noScale",
        'src': "agenc-theme/swf/rastergalery.swf",
        'width': width + 'px',
        'height': height + 'px',
        'flashvars': flashvars
    }, {
        'version': 9
    });
};


Agenc.initImpression = function(){
    if ($('body#impression').size() > 0) {
        var src = $('#content .heading img').attr('src');
        var match = src.match(/(\d+)_(\d+)\/[^\/]*$/);
        var horImageCount = match ? match[1] : 8;
        var verImageCount = match ? match[2] : 7;
        var colorFieldCount = 0; /*match ? match[3] : 6;*/
        match = src.match(/^(.*\/)[^\/]*$/);
        var folderPath = match[1];
        //alert(folderPath);
        var width = horImageCount * 106 + (horImageCount - 1) * 7;
        var height = verImageCount * 70 + (verImageCount - 1) * 6;
        Agenc.createImpressionSwf(folderPath, horImageCount, verImageCount, colorFieldCount, width, height);
    }
};



Agenc.createVideoSwf = function(videoPath,width, height){

    var flashvars = {
        videoWidth: width,
        videoHeight:height,
        videoSource:videoPath
    };
            
    $('#content .heading img').flash({
        wmode: 'transparent',
        scale: "noScale",
        'src': "agenc-theme/swf/video.swf",
        'width': width + 'px',
        'height': (parseInt(height) + 21) + 'px',
        'flashvars': flashvars
    }, {
        'version': 9
    });
};

Agenc.initVideo = function(){
    if ($('body#video').size() > 0) {
        var src = $('#content .heading img').attr('src');
        var match = src.match(/(\d+)_(\d+)\.jpg$/);
        var width = match ? match[1] : 8;
        var height = match ? match[2] : 7;
        match = src.match(/^.*?([^\/]*)\.jpg*$/);
        var videoName = match[1] + '.flv';
		match = document.URL.match(/^(.*?)[^\/]*$/);
		var videoPath = '../../uploads/video/' + videoName;
        Agenc.createVideoSwf(videoPath, width, height);
    }
};

Agenc.initFadeIn = function(){
    if ($('body.header-image #content .heading img').size() > 0) {
    
        $('body.header-image #content .heading img').fadeIn(2000);
    }
};


Agenc.cleanHtml = function(){
    $('#content-two-columns .part2 img').remove().wrapAll('<div class="part2"></div>').parent().replaceAll('#content-two-columns .part2');
};


$(function(){
    $.preloadCssImages();
    Agenc.cleanHtml();
    Agenc.initMenu();
    Agenc.initImpression();
    Agenc.initFadeIn();
    Agenc.createLogoSwf();
    Agenc.initVideo();
});



