﻿jQuery(function() {
    jQuery('div[x-modweb-designer-banner]').each(function() {

        //jQuery.jqModweb.debug.write('ff');
        var widthPixels = jQuery(this).attr('x-width');
        var heightPixels = jQuery(this).attr('x-height');
        var bannerSetID = jQuery(this).attr('x-bannersetid');
        var bannerCount = jQuery(this).children('img').length;
        var isAdmin = jQuery.jqModweb.toBool(jQuery(this).attr('x-isadmin'));


        // give container the sizing
        jQuery(this).css({ 'width': widthPixels + 'px' });
        jQuery(this).css({ 'height': heightPixels + 'px' });


        if (bannerCount == 0) {

            // render a dummy banner
            var dummy = jQuery('<div></div>');
            dummy.addClass('modweb-designer-banner-dummy');
            dummy.css({ 'width': widthPixels + 'px' });
            dummy.css({ 'height': heightPixels + 'px' });
            //dummy.width(widthPixels);
            //dummy.width(heightPixels);

            //dummy.html('No images to render in this banner set.<br /><a href="/_framework/admin/modules/BANNERS/ManageBanners.aspx?bannersetid=' + bannerSetID + '">Click here</a> to start adding banner images.');


            if (isAdmin === true) {
                dummy.text('No images to render in this banner set. Please right click this banner set to start adding images.');
            }
            else {
            }
            

            jQuery(this).append(dummy);

        }
        else if (bannerCount == 1) {
            // there is a bug with jquery.cycle-lite.js where if you have only 1 slide, it throws an error to the console and keeps the image hidden.
            jQuery(this).children('img').show();
        }
        else if (bannerCount > 1) {
            // add cycle animation
            jQuery(this).cycle({
                sync: 1
            , delay: 0
            , timeout: 7000
            , before: function() {
                //TODO: change description here.
            }
            });
        }


        // add hyperlinks
        jQuery(this).children('img[x-banner-item-hyperlink]').each(function(i) {
            jQuery(this).css({ 'cursor': 'pointer' }).click(function() {
                window.location = jQuery(this).attr('x-banner-item-hyperlink');
            });
        });


        // context menu
        if (isAdmin) {
            jQuery(this).modwebCreateContextMenu({
                onContextMenuItemClicked: function() {
                    //window.location = "/_framework/admin/modules/banners/managebanners.aspx?bannersetid=" + bannerSetID;
                    window.open("/_framework/admin/modules/banners/managebanners.aspx?bannersetid=" + bannerSetID, "_blank");
                }
            });
        }

    });
});