$(function() {
    $(document).pngFix();
    
    $background = $('#background');
    if($background.length) {
        $background.resizeImage();
        $(window).bind('resize', function(ev) {
            $background.resizeImage();
        });
    }
    
    $(window).bind('resize', function(ev) {
        vertical();
    });
    
  /*   $('#vertical_line').css('height', $(document).height()); */
    
    $('#menu').dropDown();
    
    $('#drop_down_arrow').hover(
        function() {
            $('#drop_down').css('display', 'block');
        },
        function() {
            $('#drop_down').css('display', 'none');
        }
    );
    
    $('document').slideShow();
    
    $('#content .n_article ul li a').each(function(){
      if($(this).text() === 'Les mer') {
        $(this).addClass('readmore');
      }
    });
    
    $('a#pageBack').click(function(){
        parent.history.back();
        return false;
    });
    
});

// slideshow

$.fn.slideShow = function() {
    $imgs = $('#project_image_slider').find('img');
    $imgs.css({float: 'left'});
    
    var total = $imgs.length;
    var output_string = '';
    
    $imgs.not(':first').hide();
    
    $imgs.each(function(i) {
        $(this).addClass('slider_'+i);
        output_string += '<a href="#" class="img_nav" data-img="slider_'+i+'">'+(i+1)+'</a>';
    });
    
    if(total > 1) {
        $('#red_text').append(output_string);
    }
    
    $('.img_nav:first').css('color', '#fff');
        
    $('.img_nav').live('click', function(ev) {
        $this = $(this);
        
        $('.img_nav').css('color', '#bbb');
        $this.css('color', '#fff');
        
        $imgs.hide();
        $('.'+$this.attr('data-img')).show();
        
        return false;
    });
};


// resize image
$.fn.resizeImage = function() {
    var $img = $(this).find('img');
    var wh = $(window).height();
    var ww = $(window).width();
    
    if($img.height() < wh) {
	   $img.css({height:'100%', width:'auto'});
    }
		
	if($img.width() < ww) {
	   $img.css({width:'100%', height:'auto'});
    }
};

// dropdown
$.fn.dropDown = function() {
    $this = $(this);
    $this.css('z-index', '9999');
    $uls = $this.find('ul');
    $uls.css('visibility', 'hidden');
    // position subs, hide subs on load.
    $this.find('ul').each(function(i) {  
        // if sub    
        if(!$(this).closest('li').hasClass('top_level')) {
            var mT = $('#ie6').length ? '-37px' : '-7px';
            $(this).css({ marginLeft: $(this).parent('li').width()+"px", marginTop: mT });
            
            if($(this).closest('li').find('ul').length > 0 && $(this).closest('li').find('ul').find('li').length > 0) {
                $(this).closest('li').find('a:first').append('<span class="sub">&raquo;</span>');
            }
        }     
    }).css('display', 'none');
    
    $this.find('li').hover(
        function() {
            $(this).closest('li').find('ul:first').css({ display: 'block', visibility: 'visible' });
        },
        function() {
            $(this).closest('li').find('ul:first').css({ display: 'none' });
        }
    );
    
    $(this).children('ul').hide();
};

window.onload = function() {
    resize();
    $background.resizeImage();
}

function resize() {

	if($('#ie6').length && !$('#prosjekt').length) {
		var c_top = $('#content').offset().top;
        var c_height = $('#content').height();
        var r_top = $('#red_stripe_wrapper').offset().top;
        var r_height = $('#red_stripe_wrapper').height();
        
        var dy = r_top - c_top;
        var new_height = (c_height - Math.floor(dy));
        $('#red_stripe_wrapper').css({height:new_height + "px"});
		/* ($('#red_stripe_wrapper').css({ height: $('#content').height()+30+"px" }); */
	}

    if($('body#prosjekt').length) {
        $('#red_stripe_wrapper').css({ marginTop:-$('#red_stripe_wrapper').offset().top+ 334+"px" });
        
        var c_top = $('#content').offset().top;
        var c_height = $('#content').height();
        var r_top = $('#red_stripe_wrapper').offset().top;
        var r_height = $('#red_stripe_wrapper').height();
        
        var dy = r_top - c_top;
        var new_height = (c_height - Math.floor(dy));
    
        $('#red_stripe_wrapper').css({height:new_height + "px"});  
    }
    vertical();
}

function vertical() {
	$('#vertical_line, #vl').css('height', $(document).height());
}
