window.onbeforeunload = function () {
    //makes sure page is reloaded when using  back-/forwards buttons
}
window.onunload = function () {
    //SAFARI only
    //makes sure page is reloaded when using  back-/forwards buttons
}

$(document).ready(function () {
    onLoad();

    var url = window.location.toString();
    if (url.indexOf('foretag') && url.indexOf('blogg')) {
        $('#facebookSharerA').attr('href', function (i, attr){
            
            var newUrl = attr.replace('privat', 'foretag');
            return newUrl;
        });
    }
});

$(window).scroll(function () {
    if ($(window).scrollTop() > ($(document).height() - $(window).height() - 200)) {
        //loadblogitem();
    }
});

function onLoad() {
    $("#content").hide();
    $("#content").fadeIn(500);

    //$(".hiddengallerypage").css({ opacity: 0 });

    if ($(".gallerypage").length > 0 && window.location.href.match("#")) {
        var galleryid = window.location.href.split("#")[1];
        if ($("div[rel='" + galleryid + "']").length > 0) {

            var showgalleryid = "gallery_" + galleryid;
            $(".gallerypageframe").fadeOut(1, function () {
                $("#" + showgalleryid).show().css("height", "auto").animate({ opacity: 1 }, 1);
            });
        }
    }

    onAjaxLoad();
}

function onAjaxLoad() {
    //$(".opacity10").animate({ opacity: 0.4 }, 0);
    $(".opacity15").animate({ opacity: 0.35 }, 0);
    //$(".opacity40").animate({ opacity: 0.4 }, 0);
    //$(".opacity80").animate({ opacity: 0.8 }, 0);

    $("#topmenu a[ref='dropdown']").click(function (e) {
        e.preventDefault();
        var submenu = $(this).next(".submenu");
        submenu.parent().css("z-index", 1000)
        submenu.css("z-index", 1000).fadeIn(200);
    });
    $(".s_dropdowncontainer > a").click(function (e) {
        e.preventDefault();
        var submenu = $(this).next(".s_dropdownmenu");
        submenu.parent().css("z-index", 1000)
        submenu.css("z-index", 1000).fadeIn(200);
    });


    $(".fadeintext").delay(200).animate({ opacity: 0.8 }, 500, function () {
        $(this).delay(3000).animate({ opacity: 0.35 }, 500);
    });

    $(".fadeintext").hover(function () {
        $(this).stop(true, true).animate({ opacity: 0.8 }, 500);
    }, function () {
        $(this).stop(true, true).animate({ opacity: 0.35 }, 500);
    });

    $(".slider li").hover(function () {
        $(this).removeClass("inactive");
        $("li.inactive").stop(true, true).delay(200).animate({ opacity: 0.5 }, 400);
		
		$(this).children(".image").children("a").children(".imageinfo").stop(true, true).delay(200).fadeIn(400);
		
    }, function () {
        $(this).addClass("inactive");
        $("li.inactive").stop(true, true).delay(30).animate({ opacity: 1 }, 400);
		
		$(this).children(".image").children("a").children(".imageinfo").stop(true, true).delay(200).fadeOut(400);   
    });

    $(".blogframe .rightcolumn").delay(100).fadeIn(500);

    $(".blogframe .mainlistitem").hover(function () {
        $(this).find(".iteminfo").stop(true, true).delay(200).fadeIn(400);
    }, function () {
        $(this).find(".iteminfo").stop(true, true).delay(200).fadeOut(400);
    });

    $(".gallerycategory .slider li .image a").click(function (e) {
        e.preventDefault();
        var galleryframe = $(this).parents(".gallerypageframe");
        var showgalleryid = "gallery_" + $(this).attr("rel");
        var gallerypage = $("#" + showgalleryid);

        galleryframe.fadeOut(200, function () {
            //reset gallerypage
            gallerypage.css("width", "").css("height", "auto").css("overflow", "visible");

            //show the gallery
            gallerypage.animate({ opacity: 1 }, 200);
        });
    });


    $(".mainlistitem a, .listframe a").click(function (e) {
        e.preventDefault();
        var url = $(this).attr("href");
        onBlogClick(url);
    });


    $(".submenu, .s_dropdownmenu").mouseup(function () {
        return false
    });
    $(document).mouseup(function () {
        $("#darkframe").fadeOut(400);
        $("#lightboxframe").fadeOut(400);
        $(".submenu").fadeOut(100);
        $(".s_dropdownmenu").fadeOut(100);
    });

    $(".backtogalleries").click(function (e) {
        e.preventDefault();
        var showgalleryid = "gallery_" + $(this).attr("rel");
        var gallerypage = $("#" + showgalleryid);
        gallerypage.animate({ opacity: 0 }, 200, function () {
            $(this).css("width", 1).css("height", 1).css("overflow", "hidden");
            $(".gallerypageframe").fadeIn(200);
        });

    });
}

function loadSliders() {
    $("ul.slider").each(function (e) {
        var slides = 3;
        if ($(this).hasClass("companyportfolio")) {
            slides = 4;
        }
        $(this).bxSlider({
            displaySlideQty: slides,
            prevText: '',
            nextText: '',
            infiniteLoop: false,
            moveSlideQty: slides,
            speed: 500,
            onAfterSlide: function (currentSlideNumber, totalSlideQty, currentSlideHtmlObject) {
                if (parseInt(currentSlideNumber + slides) >= totalSlideQty) {
                    $(".bx-next:eq(" + e + ")").fadeOut(200);
                }
                if (currentSlideNumber == 0) {
                    $(".bx-prev:eq(" + e + ")").fadeOut(200);
                    if (totalSlideQty > slides) {
                        $(".bx-next:eq(" + e + ")").fadeIn(200);
                    }
                } else {
                    $(".bx-prev:eq(" + e + ")").fadeIn(200);
                }
            }
        });
        //$(this).parents(".hiddengallerypage").hide();
    });
}

function initSlider(sliderid) {
        var slides = 3;
        if ($(this).hasClass("companyportfolio")) {
            slides = 4;
        }
        $("#"+sliderid).bxSlider({
            displaySlideQty: slides,
            prevText: '',
            nextText: '',
            infiniteLoop: false,
            moveSlideQty: slides,
            speed: 500,
            onAfterSlide: function (currentSlideNumber, totalSlideQty, currentSlideHtmlObject) {
                if (parseInt(currentSlideNumber + slides) >= totalSlideQty) {
                    $(".bx-next:eq(" + e + ")").fadeOut(200);
                }
                if (currentSlideNumber == 0) {
                    $(".bx-prev:eq(" + e + ")").fadeOut(200);
                    if (totalSlideQty > slides) {
                        $(".bx-next:eq(" + e + ")").fadeIn(200);
                    }
                } else {
                    $(".bx-prev:eq(" + e + ")").fadeIn(200);
                }
            }
        //$(this).parents(".hiddengallerypage").hide();
    });
}

function loadblogitem() {
    if ($(".blogframe").length > 0) {
        var lastitemid = $(".blogitempage:last").attr("ref");
        var nextitemid = parseInt($("#avalibleitems input[value='" + lastitemid + "']").next("input").val());

        //is there a next item?
        if (nextitemid > 0) {
            var container = $(".nextitem:last");
            
            //load next item
            container.css("visibility", "visible");
            $.ajax({
                url: "/-blog-/?blogview=loadblogitem&itemid=" + nextitemid + "&xslt=blogitem.xsl",
                success: function (data) {
                    container.css("background", "none");
                    container.html(data);
                    onAjaxLoad();
                }
            });
        }
    }
}

function onBlogClick(url) {
    try {
        var parentli = $(this).parent(".mainlistitem");
        blinkContent(parentli, 25);
    } catch (err) { }

    $("#delayframe").delay(400).fadeOut(0, function () {
        showLoading(500);
        newBlogAnimation(url);
    });
}
function loadLightBox(imageurl) {

    $("#darkframe").width($(window).width()).height($(window).height());
    $("#darkframe").fadeIn(400, function () {
        $.ajax({
            url: imageurl,
            success: function (data) {
                $("#lightboxinnerframe").html(data);
                $("#lightboxframe").width($(window).width()).height($(window).height());
                $("#lightboxframe").fadeIn(800);
            }
        });
    });

}

function newContentAnimation(url) {
    $(".galleryframe:first").animate({ left: -2000 }, 1500);
    $(".galleryframe:last").animate({ right: -2000 }, 1500, function () {
        loadContent(url);
    });
}
function newBlogAnimation(url) {
    $(".maincolumn").fadeOut(500); //.animate({ left: -2000 }, 1500);
    $(".rightcolumn").fadeOut(500, function () {//.animate({ right: -2000 }, 1500, function () {
        $("#delayframe").animate({ opacity: 0 }, 500, function () {
            loadContent(url);
        });
    });
}
function showLoading(delaytime) {
    var left = parseInt((parseInt($(window).width()) / 2) - 25) + "px";
    var top = parseInt((parseInt($(window).height()) / 2) - 25) + "px";

    $("#loadinganimation").css("top", top).css("left", left);
    $("#loadinganimation").delay(delaytime).fadeIn(300);
}

function loadContent(url) {
    window.location = url;
}

function blinkContent(content, time) {
	content.animate({opacity: 0.5},time);
	content.delay(time).animate({opacity: 1},time);
	content.delay(time*3).animate({opacity: 0.5},time);
	content.delay(time*3).animate({opacity: 1},time);
}

function loadBloglist(template, parentcategoryid) {
    var url = "/-blog-/?blogview=" + template + "&blogcategory=" + parentcategoryid + "&xslt=blank.xsl";
    $.ajax({
        url: url,
        success: function (data) {
            //$("#" + template).height(0).css("overflow", "hidden");
            $("#" + template).html(data);
            //var containerheight = $("#" + template + " .ajaxcontainer").outerHeight();
            //$("#" + template).animate({ height: containerheight }, 300);
            $("#" + template + " a").hover(function () {
                $(this).stop(true, true).animate({ opacity: 0.8 }, 300);
            }, function () {
                $(this).stop(true, true).animate({ opacity: 0.4 }, 500);
            });
            $("#" + template + " a").click(function (e) {
                e.preventDefault();
                var url = $(this).attr("href");
                onBlogClick(url);
            });
        }
    });
}

function loadCatCount(id, container) {
    var url = "/-blog-/?blogview=blogCatCount&blogcategory=" + id + "&xslt=blank.xsl";
    $.ajax({
        url: url,
        success: function (data) {
            $("#" + container).html(data);
        }
    });
}
