$(function () {

    if ($.browser.msie) {
        var pied;
        //Before print
        $(window).bind("beforeprint", function () {
            //Loop through every element and remove/store behavior CSS property where found.
            pied = [];
            $("*").each(function () {
                var $this = $(this),
                b = $this.css("behavior");
                if (b.length && b != "none") {
                    $this.data("behavior", b);
                    $this.css("behavior", "none");
                    pied.push(this);
                }
            });
        });
        //After print
        $(window).bind("afterprint", function () {
            //Loop through previously stored elements with behavior and restore property.
            $(pied).each(function () {
                var $this = $(this);
                $this.css("behavior", $this.data("behavior"));
            });
        });
    }

    $.extend($.blockUI.defaults, {
        css: {},
        overlayCSS: {
            backgroundColor: '#fff',
            opacity: 0.75
        }
    });

    $(window).resize(function () {
        if ($(window).width() < 1152)
            $("body").addClass("w1024");
        else
            $("body").removeClass("w1024");
    });

    setTimeout(function () {
        $(window).resize();
    }, 1);

    //$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_rounded'}); 

    $.fn.prettyPhoto({ theme: 'light_rounded' });
    $("a[rel^=prettyPhoto]").live("click", function () {
        $.prettyPhoto.open($(this).attr("href"), "", "");
        return false;
    });

    $("#doelgroepkeuze a, .tab a.doelgroepkeuze").click(function () {
        setCookie($(this).attr("rel"));
    });

    // a href
    $("a[href^='http://']").live('click', function () {
        externalLinkMethod($(this));
    });
    $("a[href^='www.']").live('click', function () {
        var url = "http://" + $(this).attr("href");
        externalLinkMethod($(this), url);
    });
});

function externalLinkMethod($elm, url) {
    if(url == null)
        url = $elm.attr("href");
    
    if ($elm.attr("target") == ""){
        $elm.attr("target", "_blank");
    }
}

function setCookie(id) {
    $.cookie('mmm_site_option', id, {
        expires: 7,
        path: '/'
    });
}
function initBlockPlayer(selector, width, height, autostart) {
    var startType;
    if(autostart === true)
        startType = "true";
    else
        startType = "false";
        
    var $context = $(selector);
    var $a = $("a", $context);
    var href = $a.attr('href');
    var newhref = '/Content/swf/player.swf?width='+width+'&height='+height+'&flashvars=file='+href+'&autostart='+startType ;
    $a.attr('href', newhref) ;
}

function paginate (context, selector, items_per_page, pagination_container) {
    var items = $(selector);
    var total = items.length;
    var ipp = items_per_page;
    if (total <= ipp) {
        $(pagination_container).hide();
        return;
    } else {
        items.remove();
    }
    $(pagination_container).pagination(total, {
        items_per_page:ipp,
        num_display_entries:5,
        next_text:'&gt;',
        prev_text:'&lt;',
        num_edge_entries:1,
        link_to: 'javascript:void(0);',
        callback : function (new_page_index, pagination_container) {
            var pc = $(context);
            pc.children(selector).remove();
         
            for(var i=new_page_index*ipp;i<(new_page_index+1)*ipp ;i++) {
                if (i < total) {
                    pc.append(items[i]);
                }
            }
            $('.supplier:last').css("borderBottom", "0px");
        }
    });
}
