var scr = new Object();
scr.isPhone = scr.isTablet = scr.isSmall = scr.isLarge = false;
scr.is = ""


$.fn.reorder_frontpage = function(ordering) {
    // remove container
    $(".container", this).children().unwrap();

    // reorder    
    var _container = this;
    $.each(ordering, function(i, v) { 
        if (i == 0) { 
            $(ordering[0], _container).prependTo(_container);
        } else {
            $(ordering[i], _container).insertAfter(ordering[i-1]);
        }
    });    

    // rebuild container
    if (scr.isLarge) { 
        $("#vestigingen, #links", this).wrapAll("<div class='container' />"); 
        $(".container", this).insertAfter(".producten:last");
    }
    if (scr.isSmall) { 
        $("#vestiging, #vestigingen", this).wrapAll("<div class='container' />"); 
        $(".container", this).insertAfter(".producten:last");
    }
}

$(window).smartresize(function(event) {
    scr.isPhone = scr.isTablet = scr.isSmall = scr.isLarge = false;
    var w = $(window).width();
    var old_scr_is = scr.is;
    if (w < 720) { scr.isPhone = true; scr.is = "phone"; };
    if (w > 720 && w < 960) { scr.isTablet = true; scr.is = "tablet"; };
    if (w > 960 && w < 1200) { scr.isSmall = true; scr.is = "small"; };
    if (w > 1200) { scr.isLarge = true; scr.is = "large"; };
    if (old_scr_is != scr.is) {
        if (typeof(ie) != "undefined") {
            var width = document.documentElement.clientWidth;
            if ((width > 300) && (width <= 720)) {
                var csshref = media_url + 'css/phone.css';
            } else if ((width > 720) && (width <= 960)) {
                var csshref = media_url + 'css/tablet.css';
            } else if ((width > 960) && (width <= 1200)) {
                var csshref = media_url + 'css/small.css';
            } else if (width > 1200) {
                var csshref = media_url + 'css/large.css';          
            }
            $("#multiscreen").attr("href", csshref)
        }
        $(window).trigger("scr_changed");
    }
});

function resizeimages() {
   var txtwidth = $(".txt").width()
    $(".txt img").not(".referentie img").each(function(i, e) {
        if ($(e).width() > txtwidth) {
            $(e).width(txtwidth);
        }
    });
}

function maximizeColumn() {
    $(".col_mid_middle").css("height", "");

    if (!scr.isPhone && !scr.isTablet) {
        var leftHeight = $("#contentwrapper>.col_left").height();
        var midHeight = $("#contentwrapper>.col_mid_small, #contentwrapper>.col_mid_wide").height();
        var rightHeight = $("#contentwrapper>.col_right").height();
        
        var colHeight = Math.max(leftHeight, rightHeight);
        
        if (colHeight > midHeight) {
            $(".col_mid_middle").height(colHeight - 40);        
        } 
    }
}

$(window).load(function() {
    resizeimages();
})

$(document).ready(function() { 
    $("#mainmenu li").hover(function() { 
        if (!$(this).hasClass("selected") && !$(this).hasClass("ancestor")) {
            $("#mainmenu li").removeClass("hover"); 
            $(this).addClass("hover"); 
        }
    }, function() { 
        $("#mainmenu li").removeClass("hover"); 
    });

    $(".submenu li").hover(function() {
        if (!$(this).hasClass("selected") && !$(this).hasClass("top") && !$(this).hasClass("ancestor")) {
            $(".submenu li").removeClass("hover");
            $(this).addClass("hover"); 
        }
    }, function() { 
        $(".submenu li").removeClass("hover"); 
    });


    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search("iphone") > -1)
        $(".content .submenu li a").css("padding-top", "8px");


    if ($(".submenu").find(".sub").size() > 0) {
        $(".submenu").addClass("submenu_twolines");
    }

    $(".producten").height(
        $(".producten").map(function(i,v) { return $(this).height() }).sort().eq(-1)[0]
    );

    if ($("body.frontpage").length) {
        var reorder_function = function() {
            var orderings = {
                phone: ["#mainmenu", "#actie", "#nieuws", "#vestiging", "#vestigingen", ".producten:first", ".producten:last", "#links"],
                tablet: ["#mainmenu", "#actie", "#nieuws", "#vestiging", "#vestigingen", ".producten:first", ".producten:last", "#links"],
                small: ["#mainmenu", "#actie", "#nieuws", ".producten:first", ".producten:last", "#vestiging", "#vestigingen", "#links"],
                large: ["#mainmenu", "#actie", "#nieuws", "#vestiging", ".producten:first", ".producten:last", "#vestigingen", "#links"]
            }
            
            $("#contentwrapper").reorder_frontpage(orderings[scr.is]);      
        }
    } else if ($("body.content").length) {
        var reorder_function = function() { 
            resizeimages();
	    resizeReferenties();

            if (scr.isSmall) { $(".col_right").appendTo(".col_left"); $(".blocks").appendTo(".col_left"); }
            if (scr.isLarge) { $(".col_right").insertAfter(".col_mid_small"); $(".blocks").appendTo(".col_left"); }
            
            if (scr.isTablet) {
                $(".submenu").addClass("tabletsubmenu");
                if ($(".header").length) {
                    $(".submenu").insertAfter(".header");
                    $(".submenu").after("<div class='submenuclear' style='clear: both;'></div>");         
                    $(".sub").addClass("tabletsub").insertAfter(".submenu");
                    $('.sub:not(:first)').remove();
                } else {                    
                    $(".submenu").prependTo(".txt");
                    $(".submenu").after("<div class='submenuclear' style='clear: both;'></div>");
                    $(".sub").addClass("tabletsub").insertAfter(".submenu");
                    $('.sub:not(:first)').remove();
                }
                $(".col_right").insertAfter(".col_mid_small");
                if ($(".col_mid_wide").length) {
                    $(".blocks").insertAfter(".col_mid_wide");
                } else {
                    $(".blocks").insertAfter(".col_mid_small");
                }
                $("*[ipad_bg_url]").each(function(){ 
                    $(this).attr("originalbg", $(this).css("background-image"))
                    $(this).css("background-image", 'url("' + $(this).attr("ipad_bg_url") + '")');
                });
            } else {
                if (!$(".submenu").parent().hasClass("col_left")){
                    $(".submenu").prependTo(".col_left");
                
                    $(".submenu").removeClass("tabletsubmenu");
                    $(".submenuclear").remove();
                    $(".sub").removeClass("tabletsub");
                    
                    $(".sub").appendTo($(".selected.has_children, .ancestor"));

                    $("*[ipad_bg_url]").each(function(){ 
                        $(this).css("background-image", $(this).attr("originalbg"))
                    });
                }
            }

            if (scr.isPhone) {
                $(".first_only_iphone").insertBefore("#berichten");

                if ($(".col_mid_wide").length) {
                    $(".blocks").insertAfter(".col_mid_wide");
                } else {
                    $(".blocks").insertAfter(".col_mid_small");
                }
            } else {
                $(".first_only_iphone").appendTo("body");
            }

            maximizeColumn();
        }
    }

    $(window).bind("scr_changed", reorder_function);

    $(window).trigger("smartresize", ["execAsap"]);
})

// referenties

$(document).ready(function() {
    $(".citaten").cycle({fx: 'fade', slideResize: 0, containerResize: 0});
    $(".referentie:odd").addClass("noborder");
});

function resizeReferenties() {
    $(".referentie>img").each(function() {
	if (scr.isTablet || scr.isSmall) {
	    $(this).attr("src", $(this).attr("src_breed"));
	} else {
	    $(this).attr("src", $(this).attr("src_smal"));
	}
	if (scr.isPhone) {
	    $(".referentie").addClass("noborder");
	} else {
	    $(".referentie").removeClass("noborder");
	    $(".referentie:odd").addClass("noborder");
	}
    });
}

// afbeelding animatie

$(document).ready(function() {
    $(".afbeelding_animatie").cycle({
	fx: 'fade'
	//after: function() {
	//    $(this).parent().children("img").css("display", "block");
	//}
    });
});
