var lightbox = new Lightbox();

$(document).ready(function() {
    
	$('.newsletter a').click(function(e) {
		var lightbox = new Lightbox({
			width: 650,
			height: 565/*,
						element: $('<div id="lightbox"><div id="lightbox-close">Lukk vindu</div>' +
							'<div id="lightbox-wrap">' +
								'<div id="lightbox-iframe" name="lightbox-iframe"></div>' +
							'</div>' +
							'<div class="shadow shadow1"></div><div class="shadow shadow2"></div><div class="shadow shadow3"></div><div class="shadow shadow4"></div>' +
							'</div>'),
						onLoad: function() {
							var registrationForm = new RegistrationForm({
								element: $('form#SubscriberForm'),
								hasDatepickers: true
							});
						}*/
		});
		
		lightbox.open($(this));
		return false;
	});
	
	$('a.lightbox').click( function(){
		lightbox.open($(this));
		return false;
	});
    
    var categoryHeight = 0;
    $('#productcategories .category').each(function(){
        var image = $(this).find('.image');
        var height = $(this).height();
        if( height > categoryHeight ) {
            categoryHeight = height;
        }
    });
    
    $('#productcategories .category').css({
        height: categoryHeight + 'px'
    });

    $('#productcategories .category .image').css({
        position: 'absolute',
        left: 0,
        bottom: 0
    });

	$('#mainmenu li:not(.selected) a, #globalmenu li:not(.selected) a').hover(
		function(){
			$(this).animate({
				color: '#065d1e'
			},'fast')
		},
		function(){
			$(this).animate({
				color: '#6f9961'
			},'fast')
		}
	);

	$('.button').hover(
		function(){
			$(this).animate({
				opacity: 0.75
			},'fast')
		},
		function(){
			$(this).animate({
				opacity: 1
			},'fast')
		}
	);
    
    $('#faq #questions-content .content').hide();
    $('#faq #questions-content h4').click(function(){
        $(this).siblings().find('+.content').slideUp();
        $(this).siblings().parent().siblings().find('h4 +.content').slideUp();
        $(this).find('+.content').slideToggle();
        $(this).toggleClass('collapsed');
    }).hover(
        function(){
            $(this).addClass('hover');
        },
        function(){
            $(this).removeClass('hover');
        }
    );
    
    var cycle = function() {
        if( current >= total ) {
            current = 0;
        }

        next = current + 1;
        
        if( next >= total ) {
            next = 0;
        }
        
        $(elements[current]).css({
            zIndex: 1
        });

        $(elements[next]).css({
            display: 'none',
            zIndex: 2
        });
        
        $(elements[next]).fadeIn(2000,function(){
            $(elements[current]).css({
                display: 'none',
                zIndex: 0
            });
            current = current + 1;
        });
    } 
    
    if( document.getElementById('frontpage') ) {
        var elements = $('#frontpage #intro .image');
        var current  = 0;
        var total    = 3;
        var next     = 0;
        
        setInterval(cycle,3000);
        //cycle();
    }
});


function isLastDayOfMonth(date) {
	var daysOfMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var currentMonth = date.getUTCMonth();
	
	if ( isLeap(date.getFullYear()) ) {
		daysOfMonth[1] += 1; // give feb 29 days
	}
	           
	function isLeap(year) {
		if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
			return true;
	    }
		return false;
	}
	return daysOfMonth[currentMonth] == date.getDate();
}