$(document).ready(function() {

    // get language
    var language = $('body').attr('id');
    
    // set active menu
    $('#menu_' + linklog.settings.menu).addClass('active');
    
    // hooking up logo link
    $('#logo').click(function() {
        location.href = $('#menu_frontpage a').attr('href')
    });
    
    // inserting frontpage flash
    $('#frontpage_flash').flash({
        src: '/media/flash/'+ language +'_linklog_frontpage.swf',
        width: 958,
        height: 340,
        wmode: 'transparent'
    }).css({
        marginTop: -50,
        marginLeft: -20,
        overflow:'hidden',
        zoom: 1
    });

    // inserting flash clocks
    $('#header_clocks').empty().flash({
        src: '/media/flash/clocks3.swf?clockurl=/_clocks/',
        width: 335,
        height: 101,
        wmode: 'transparent'
    })

    // insert shadow on box
    $('.box').wrap('<div class="box_wrapper_4"></div>')
             .wrap('<div class="box_wrapper_3"></div>')
             .wrap('<div class="box_wrapper_2"></div>')
             .wrap('<div class="box_wrapper_1"></div>');
    

    // setting up login box
    $('#login input').each(function(index) {
        
        $(this).focus(function() {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            };
        }).blur(function() {
           if ($(this).val() == '') {
               $(this).val($(this).attr('title'));
           };
        });
        
    });
    
    // Setting up cases page
    
    $('body.cases').each(function(index) {
        
        $('#sub_menu a').each(function(i, value) {
            $(this).click(function() {                
                $('#cases_large img').css('display', 'none');
                $('#cases_large img:eq('+ i +')').fadeIn(800);
                $(this).closest('li').addClass('active').siblings('li').removeClass('active');
                return false;
            });
            
            if (i == 0) {
                $(this).click();
            };

        });
       
       
       $('#cases_list_images').each(function(index) {
            
            var $me = $(this);
            
            $me.html('');
            
            $.getJSON('/_logos/',{},
             function(json){
                 $(json).each(function(i, val) {
                     var $img = $('<img src="'+ val.url +'" alt="'+ val.name +'" class="cases_list_image" />');
                     $me.append($img);
                     $img.reflect({
                         opacity: 0.3,
                         height:0.3
                     })
                 });
                 
                 


            });
            
            
       });
       
        
    });

    // getting oil data
    $('body.oil').each(function(index) {
        
        $.getJSON('/_oil/',{},
         function(json){
             
             $(json).each(function(i, item) {
                 var value = item.value;
                 
                 // switching to . for english page
                 if (language == 'en') {
                     value = value.replace(",",".");
                 };

                 if (i < 3) {
                     $('#oil_table').append('<tr><th>'+ item.month +'</th><td>'+ value +' %</td></tr>');
                 };                
             });
        });
        
        
    });

    // login error message
    var act = gup('act');
    var msg = gup('msg');
    
    if (act == 'error' && msg == 'wronginfo') {
        if (language = 'da') {
            alert('Brugernavn eller adgangskode er ikke korrekt. Prøv venligst igen eller ring på +45 7010 4500');
        } else {
            alert('Username or password is not valid. Try again or contact us via Phone: +45 7010 4500');
        };
    };

    // setting up about page
    $('#about_us_show_all').click(function() {
        $(this).parent().siblings('*').show();
        $('#about_content').css('padding-bottom', 19);
        $('#picture_mette').after('<br />').after($('#mette_signature'));
        $('#mette_signature').css('margin-top', 20);
        $(this).remove();
        
    });

    // setting up consultant page
    $('#consultant_show_all').click(function() {
        $(this).parent().siblings('*').show();
        $('#about_content').css('padding-bottom', 19);
        $('#picture_mads').after('<br />').after($('#mads_signature'));
        $('#mads_signature').css('margin-top', 20);
        $(this).remove();
        $('.hide_on_click').hide();
    });
    
    // running about why animation
    $('#about_why_animation').each(function(index) {
        
        // showing first
        setTimeout(function() {
            $('#about_why_animation').find('img:eq(0)').fadeIn(500);
        }, 500);
        
        // showing second after delay
        setTimeout(function() {
            $('#about_why_animation').find('img:eq(1)').fadeIn(500);
        }, 3000);

        // showing third after delay
        setTimeout(function() {
            $('#about_why_animation').find('img:eq(2)').fadeIn(500);
        }, 6000);
        
    });

    // booking thumbs
    
    $('#booking_thumbs li').fadeTo(200, 0.3).eq(1).fadeTo(200, 1);
    
    $('#booking_thumbs').jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        speed: 1000,
        beforeStart: function(elements) {
            $('#booking_thumbs li').fadeTo(200, 0.3);
        },
        afterEnd: function(elements) {
            var $main = $(elements[1]);        
            $main.fadeTo(200, 1);
        }
        
    });

    // adding reflections to screenshots
    $('#booking_thumbs img').reflect({
        opacity: 0.6,
        height: 0.25
    });

    // setting up reference footer
    $('#reference_cycle').cycle({
        fx: "fade",
        random: 1
    });

    // setting focus in login box
    $('#login_username').focus();
    
});


function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}