

$(document).ready(function(){
    $('#multi-ddm').dropDownMenu({timer: 500, parentMO: 'parent-hover', childMO: 'child-hover1'});
    //$('#multi-ddm li:first a').addClass('active');
    $('#logo').click(function(){
        window.location = '/';
    });
    
    $('#multi-ddm li a.active').animate(
        {backgroundPosition:"(right 0px)"},
        {duration:500}
    );
    


// IMAGES ============================    
if($('#image_wrapper').length > 0){
    
    var string = '';
    var c = '';
    var even;
    var j = false;
   // var counter = 0;
    var duration = 800;
    var duration1 = 500
    var period = 7000;
    //var cook = false;
    
    var images = ['main_image','trans-1','trans-2','trans-6'];
    var overimages = ['quality','creativity','dedication','excellence'];
	var overimages_counter = 0;
    
    var pos = [
               [-177,-186],
               [0,-186],
               [177,-186],
               [177,0],
               [177,186],
               [0,186],
               [-177,186],
               [-177,0]
            ];
    
    var posVert = [
               [0,-372],
               [0,372]
            ];
    
    var posHor = [
               [-900,0],
               [900,0]
            ];    

    function sloganAnimate()
    {
        var imgSize = $('#q img').width();

            $('#q').css('width', imgSize + 'px');
            $('#q').animate(
                {
                    left: 885 - imgSize + 'px'
                },
                {duration:duration}
            );
            counter = 1;
            //setCookie('alreadyin', 'yes');
    }

    function draw_slogan() {
            //var cur_slogan = overimages[Math.floor(Math.random()*overimages.length)];
			if(overimages_counter >= overimages.length) overimages_counter = 0;
            var cur_slogan = overimages[overimages_counter];
			overimages_counter++;
            $('#q').empty();
            $('#q').css('position','relative');
            $('#q').css('top', Math.floor(Math.random() * (236 - 0 + 1)) + 0 + 'px');
            $('#q').css('left', '0px');
            
            $('#q').append('<img src="resources/img/slogan/' + cur_slogan + '.png" />');
            $('#q').children().unbind('load');
            $('#q').children().load(function(){sloganAnimate();});
    }
    
    function comp_image(i) {
        $('#q').empty();
        $('.images').hide();
        $('#image').show();
        $('#image').find('td').each(function(){
            $(this).empty();
            var sliceId = $(this).attr('sid');
            var position = pos[Math.floor(Math.random()*pos.length)];
            $(this).append('<img style="position:relative; left:'+ position[0] +'px; top:'+ position[1] +'px;" src="resources/img/images/' + images[i] + '_' + sliceId + '.jpg" />');
            $(this).find('img').animate(
                {
                    left: '0px',
                    top: '0px'
                },
                {duration:duration}
            );
            
            var obj = $(this);
            setTimeout(function () {
                $('#q').empty();
                $(obj).find('img').fadeOut(duration);
            },period - (duration + duration1));
        });
        draw_slogan();
    }
    
    function comp_image_lines(i) {
        $('#q').empty();
        $('.images').hide();
        $('#image_lines').show();
        j = 0;
        var top = 0;
        $('#image_lines').find('td').each(function(){
            if(j % 2) {   
                top = -500;
            } else {
                top = 500;
            }
            $(this).css('background-position', '175px ' + top + 'px' ).attr('bgpos', top);
            j = j + 1;
        });
        
        j = 0;
        $('#image_lines').find('td').each(function(){
            if(j % 2) {
                even = 0;
            } else {
                even = 1;
            }
            $(this).empty();
            
            var sliceId = $(this).attr('sid');
            
            var sliceId1 = parseInt(sliceId) + 5;
            if (sliceId1 < 10) {
                sliceId1 = '0' + sliceId1;
            }
            
            var position = posVert[even];
            
           
            if (sliceId != '05') {
                $(this).animate(
                    {backgroundPosition:"(175px 0px)"},
                    {duration:duration1}
                );
            }
            /*
            setTimeout(function(){
                draw_slogan();
            },duration1);
            */
            var obj = $(this);
            setTimeout(function () {
                $(obj).append('<div class="img" style="left:'+ position[0] +'px; top:'+ position[1] +'px;"><img src="resources/img/images/' + images[i] + '_' + sliceId + '.jpg" /><img src="resources/img/images/' + images[i] + '_' + sliceId1 + '.jpg" /></div');
                $(obj).find('div').animate(
                    {
                        left: '0px',
                        top: '0px'
                    },
                    {duration:duration}
                );
            },duration);
            
            setTimeout(function () {
                $('#q').empty();
                $(obj).find('div').animate(
                    {
                        left: position[0] + 'px',
                        top: position[1] + 'px'
                    },
                    {duration:duration}
                );
            }, period - (duration + duration));
            
            setTimeout(function () {
                $(obj).animate(
                    {backgroundPosition: '(175px ' + $(obj).attr('bgpos') + 'px)'},
                    {duration:duration1}
                );
            }, period - (duration1 + duration1));

            j = j + 1;
        });
		setTimeout(function(){
                draw_slogan();
            },duration1);
    }
    
    function comp_image_hor_lines(i) {
        $('#q').empty();
        $('.images').hide();
        
        
        var left = 0;
        j = 0;
        $('#image_hor_lines').find('td').each(function(){
            if(j % 2) {   
                left = -900;
            } else {
                left = 900;
            }
            $(this).css('background-position', left + 'px 0px' ).attr('bgpos',left);
            j = j + 1;
            
            
        });
        
        
        j = 0;
        $('#image_hor_lines').find('td').each(function(){
            $('#q').empty();
            if(j % 2) {
                even = 0;
            } else {
                even = 1;
            }
            $(this).empty();
            
            var sliceId = $(this).attr('sid');
            
            var sliceId1 = parseInt(sliceId) + 1;
            sliceId1 = '0' + sliceId1;
            
            var sliceId2 = parseInt(sliceId) + 2;
            sliceId2 = '0' + sliceId2;
            
            var sliceId3 = parseInt(sliceId) + 3;
            sliceId3 = '0' + sliceId3;
            
            var sliceId4 = parseInt(sliceId) + 4;
            if (sliceId4 < 10) {
                sliceId4 = '0' + sliceId4;
            }
            
            var position = posHor[even];
            
           
            $(this).animate(
                {backgroundPosition:"(0px 0px)"},
                {duration:duration1}
            );
            /*
            setTimeout(function(){
                draw_slogan();
            },duration1);
            */
            var obj = $(this);
            setTimeout(function () {
                $(obj).append('<div class="img_hor" style="left:'+ position[0] +'px; top:'+ position[1] +'px;"><img src="resources/img/images/' + images[i] + '_' + sliceId + '.jpg" /><img src="resources/img/images/' + images[i] + '_' + sliceId1 + '.jpg" /><img src="resources/img/images/' + images[i] + '_' + sliceId2 + '.jpg" /><img src="resources/img/images/' + images[i] + '_' + sliceId3 + '.jpg" /><img src="resources/img/images/' + images[i] + '_' + sliceId4 + '.jpg" /></div');
                $(obj).find('div').animate(
                    {
                        left: '0px',
                        top: '0px'
                    },
                    {duration:duration}
                );
            }, duration);
            
            setTimeout(function () {
                $('#q').empty();
                $(obj).find('div').animate(
                    {
                        left: position[0] + 'px',
                        top: position[1] + 'px'
                    },
                    {duration:duration}
                );
            }, period - (duration + duration));
            
            setTimeout(function () {
                $(obj).animate(
                    {backgroundPosition: '(' + $(obj).attr('bgpos') + 'px 0px)'},
                    {duration:duration1}
                );
            }, period - (duration1 + duration1));

            j = j + 1;
        });
        $('#image_hor_lines').show();
        setTimeout(function(){
                draw_slogan();
            },duration1);
    }
    

    //Execute
    for(var j = 0; j < images.length; j = j + 1) {
        for(var k = 1; k <= 10; k = k + 1) {
            if(k < 10) {
                c = '0' + k;
            } else {
                c = k;
            }
            string += '<img src="resources/img/images/' + images[j] + '_' + c +'.jpg" />';
        }
    }
    
    for(var j = 0; j < overimages.length; j = j + 1) {
        string += '<img src="resources/img/slogan/' + overimages[j] +'.png" />';
    }
    $('#preload').append(string);
    
    
    comp_image(0);
    
    var i = 1;
    var j = 2;
    setInterval(function(){
        if(i > (images.length - 1)) {
            i = 0;
        }
        if(j > 3) {
            j = 1;
        }
        switch(j) {
            case 1:
              comp_image(i);
              break;
            case 2:
              comp_image_hor_lines(i);
              break;
            case 3:
              comp_image_lines(i);
              break;
            default:
              false;
        }
        j = j + 1;
        
        i = i + 1;
    },period);
    
}
// =======================================
    
    $('#multi-ddm li a').click(function(){

       $('#multi-ddm li a.active').removeAttr('style');
       $('#multi-ddm li a.active').removeClass('active');
        $(this).addClass('active');
        $('#multi-ddm li a.active').animate(
                {backgroundPosition:"(right 0px)"},
                {duration:500}
            );
    })

/*
            $('#multi-ddm li').hover(
            function() {
                    $('#menu div.active').removeClass('active');
                $(this).addClass('active');
                    $(this).css('backgroundPosition','right 0px');
                    var w = $('#menu div.active').width();
                    $(this).find('li').width(parseInt(w+'px'));
                $(this).find('ul').stop(true, true);
                $(this).find('ul').slideDown();
            },
            function() {
                $(this).removeClass("active");
                $(this).find('ul').slideUp('fast');
            }
        );
*/


});

