
$(document).ready(function() {

    if ($('#mycarousel').length > 0) {

        $('#mycarousel').jcarousel({
            visible: 3,
            scroll : 3,
            
            /*
			itemLoadCallback: mycarousel_itemLoadCallback,
            itemVisibleOutCallback: {
            onAfterAnimation: mycarousel_itemVisibleOutCallback,
			*/
            buttonNextHTML: null,
            buttonPrevHTML: null

            });
    }
    

    externalLinks();
	initRunningDays();
	hideRunningDays();


	




});





function mycarousel_itemLoadCallback(carousel, state) {

	if (state == 'init') {
	    return;
        }

        $.getJSON('/custom/windowajaxload.php', {
            index: carousel.first
        }, function(json) {
            addObject(carousel, carousel.first, json.window);
			externalLinks();
        });


}

function addObject(carousel, item, afterData) {

    carousel.add(item, afterData);

}

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {

        carousel.remove(i);

}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}


function hideRunningDays() {
	$("#CONT_leftbox div.leftbox_top").each(function(index, value) {
		if (!$(value).hasClass('current_day')) {
			$(value).next().hide();
			
		}

		$(value).css('margin-bottom', '2px');
	
	});



}



function resetRunningDays() {
	$("#CONT_leftbox div.leftbox_top").each(function(index, value) {
		if ($(value).hasClass('current_day')) {
			$(value).removeClass('current_day');
		}
		$(value).next().hide();

	});

}


function initRunningDays() {
	$("#CONT_leftbox div.leftbox_top").each(function(index, value) {

		$(value).click(function() {
			//$(this).nextAll('.leftbox_cont').toggle();
			//alert(this.tagName);
			if (!$(value).hasClass('current_day')) {
				resetRunningDays();
				$(value).addClass('current_day');
				if ($(value).hasClass('current_day')) {
					if ($(this).next().is(':hidden')) {
						$(this).next().slideDown("normal");

					} else {
						//$(this).next().hide();
					}

				} else {
					event.cancel();
				}
			}
		});


		$(value).mouseover(function() {
			//$(this).nextAll('.leftbox_cont').toggle();
			//alert(this.tagName);
			if (!$(value).hasClass('current_day')) {
				$(value).css('text-decoration', 'underline');
				$(value).css('cursor', 'pointer');
				$(value).attr('title', 'Visa tider');
			}

		});
		
		$(value).mouseout(function() {
			//$(this).nextAll('.leftbox_cont').toggle();
			//alert(this.tagName);
			
				$(value).css('text-decoration', 'none');
				$(value).css('cursor', 'default');
				$(value).removeAttr('title');
			
		});



	});

}




