var urladdition = "?xslt=blank.xsl";

$(document).ready(function () {
    //HANDLE ORIGINAL TEXT ON INPUTS AND TEXTAREAS
    // SET ON FOCUS VALUES FOR ALL TEXTFIELDS
    $("input.textfield, textarea, #searchfield").each(function () {
        $(this).val($(this).attr("alt"));
    });
    // CLEAR VALUES ON FOCUS FOR ALL TEXTFIELDS
    $("input.textfield, textarea, #searchfield").focus(function () {
        if ($(this).val() == $(this).attr("alt")) {
            $(this).val("");
        }
    });
    // ADD ORIGINAL VALUE IF EMPTY FOR ALL TEXTFIELDS
    $("input.textfield, textarea, #searchfield").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("alt"));
        }
    });

    $(".dailymenuframe .todaysmenu, #newsitem").each(function () {
        var html = $(this).html();
        html = html.replace(/_GF_/g, "<img src =\"/BluerangeEasy/templates/2166/images/icon_glutenfritt.gif\" align=\"absmiddle\" class=\"icon\">");
        html = html.replace(/_LF_/g, "<img src =\"/BluerangeEasy/templates/2166/images/icon_laktosfritt.gif\" align=\"absmiddle\" class=\"icon\">");
        html = html.replace(/_NP_/g, "<img src =\"/BluerangeEasy/templates/2166/images/icon_narproducerat.gif\" align=\"absmiddle\" class=\"icon\">");
        $(this).html(html);
    });

    $("a[ref='lightbox']").click(function (e) {
        e.preventDefault();
        var url = $(this).attr("href").replace("./", "/") + urladdition;
        fullscreenframemessurements();
        loadfullscreencontents(url);
    });

    $("a[ref='printmenu']").click(function (e) {
        e.preventDefault();
        var newsid = $("#weekmenuid").val();
        var href = "/?newsitem=" + newsid + "&xslt=print.xsl";
		console.log(href);
        window.open (href, "Full_productimage", "width=600,height=800,scrollbars=yes");

    });

    $("#fullscreenframe .lightboxcontainer").mouseup(function () {
        return false
    });
    $("#fullscreenframe, #fullscreenframe .closeframe").mouseup(function () {
        $("#fullscreenframe").fadeOut(300, function () {
            $("#flashcontainer").show();
            $("#flashimg").remove();
        });

    });

    $(".accordioncolumns .column a").each(function () {
        var href = $(this).attr("href").replace("./", "/");
        var ref = $(this).attr("ref");
        var newtext = $(this).html();
        var newlink = $("<font></font>").html(newtext).addClass("spanlink").attr("link", href);
        if (ref != null) {
            newlink.attr("ref", $(this).attr("ref"));
        }
        $(this).replaceWith(newlink);
    });

    $("font[ref='lightbox']").hover(function (e) {
        $(this).css("text-decoration", "underline");
    }, function (e) {
        $(this).css("text-decoration", "none");
    });

    $("font[ref='lightbox']").click(function (e) {
        e.preventDefault();
        var url = $(this).attr("link") + urladdition;
        fullscreenframemessurements();
        loadfullscreencontents(url);
    });


});

function runlightboxslider() {
	if ($("#slider").length > 0) {
		var showing = 1;
		var slides = $("#lightboxslider li").length;
		
		//show first slide
		$("#lightboxslider li:nth-child(1)").show();
		
		//changeslides
		$("#slider .rightarrow").click(function() {
			//which slide is to be shown?
			var shownow;
			if (showing == slides) {shownow = 1;}
			else {shownow = showing+1;}
			
			//do transition
			$("#lightboxslider li:nth-child("+showing+")").fadeOut(500, function () {
				$("#lightboxslider li:nth-child("+shownow+")").fadeIn(500);
			});
			
			//change showing slidenumer
			showing = shownow;
			
		});
		$("#slider .leftarrow").click(function() {
			//which slide is to be shown?
			var shownow;
			if (showing == 1) {shownow = slides;}
			else {shownow = showing-1;}
			
			//do transition
			$("#lightboxslider li:nth-child("+showing+")").fadeOut(500, function () {
				$("#lightboxslider li:nth-child("+shownow+")").fadeIn(500);
			});
			
			//change showing slidenumer
			showing = shownow;
			
		});
	}
}

function lightBoxVideo (inUrl) {

    var url = inUrl + urladdition;
    fullscreenframemessurements();
    loadfullscreencontents(url);
}
//prepare fullscreenframe to open
function fullscreenframemessurements() {
	var height = $(window).height();
	var width = $(window).width();
	$("#fullscreenframe").height(height);
	$("#fullscreenframe").width(width);
	
	//reset startvalues
	$("#lightbox").html("");
	$("#fullscreenframe .lightboxcontainer").hide();
	$("#fullscreenframe .lightboxframe").height(50);
	$("#fullscreenframe .lightboxframe").width(50);
	
	//set width and height
	$("#windowscreenframe").width(width);
	$("#windowscreenframe").height($(window).height());
}

//show the fullframe with animations
function loadfullscreencontents(url) {
    $("#flashcontainer").hide();
    $("<img />").attr("id", "flashimg").attr("src", "/" + $("#flashcontainer").attr("ref")).appendTo($("#flashframe"));

    $("#fullscreenframe").fadeIn(500, function () {
        $("#delayframe").delay(200).hide(function () {
            $("#fullscreenframe .lightboxframe").animate({ width: 850 }, 500, function () {
                $(this).animate({ height: 504 }, 500, function () {
                    $("#delayframe").delay(500).hide(function () {
                        $("#lightbox").load(url, function () {
                            $("#fullscreenframe .lightboxcontainer").fadeIn(500);
                        });
                    });
                });
            });
        });
    });
}
