(function ($) {

    var name_space = 'locolopreloader';

    $.fn[name_space] =
    function (options) {
        var defaults =
        {
            //delay: 200,
            //preload_parent: "a",
            check_timer: 300,
            //ondone: function () { },
            //oneachload: function (image) { },
            fadein: 300
        };

        options = $.extend(defaults, options);

        // variables declaration and precaching images and parent container
        var counter = 0, i = 0, checkFlag = [];
        var togglecounter = 0;
        var root = $(this);
        var images = root.find("img");
        var delaySum = options.delay;

        $("#main").css("opacity", 0);
        $("#indtable").css("opacity", 0);

        // *************************************************************************************** //
        var init =
            function () {
                timer = setInterval(function () {

                    // stop if all images have been loaded
                    if (counter >= checkFlag.length) {
                        clearInterval(timer);
                        clearInterval(load_timer);
                        $("#main").animate({ opacity: 1 }, options.fadein);

                        if (togglecounter % 2 == 1) {
                            $('#indtable').show("clip", {}, 1000);
                        }
                        $("#indtable").animate({ opacity: 0 }, options.fadein);

                        //post processing
                        enableBubblePopup();
                        return;
                    }

                    for (i = 0; i < images.length; i++) {
                        if (images[i].complete == true) {
                            if (checkFlag[i] == false) {
                                checkFlag[i] = true;
                                //options.oneachload(images[i]);
                                counter++;

                                //delaySum = delaySum + options.delay;
                            }
                        }
                    }

                }, options.check_timer)
            };

        // *************************************************************************************** //
        // add class property "preloader" to parent class
        images.each(function () {
            //            if ($(this).parent(options.preload_parent).length == 0)
            //                $(this).wrap("<a class='preloader' />");
            //            else
            //                $(this).parent().addClass("preloader");

            checkFlag[i] = false;
            i++;
        });

        images = $.makeArray(images);

        // *************************************************************************************** //
        //        // Call init() after loading icon image is loaded
        //        var icon = jQuery("<img />", {

        //            id: 'loadingicon',
        //            src: 'image/89.gif'

        //        }).hide().appendTo("body");

        var load_timer = setInterval(
                    function () {
                        $('#indtable').toggle("clip", {}, 1000);
                        togglecounter++;
                    },
                    options.check_timer
                );
        $("#indtable").css("opacity", 1);

        init();


        //        timer = setInterval(
        //            function () {
        //                if (icon[0].complete == true) {
        //                    clearInterval(timer);
        //                    init();
        //                    icon.remove();
        //                    return;
        //                }
        //            },
        //            100
        //        );
        // *************************************************************************************** //
    } //

})(jQuery);


var enableBubblePopup = function(){

	$('.reelimg, .openreelimg, .memimg, .bookimg').CreateBubblePopup({
			position : 'top',
			align	 : 'center',
			distance : '20px',
			innerHtmlStyle: {
								color:'#fff', 
								'text-align':'center'
							},
			themeName: 	'all-black',
			themePath: 	'ver2/js/jquerybubblepopup-theme',
            closingDelay: 100
		});

    $('.reelimg').SetBubblePopupInnerHtml( 'Blog (Link)' );
    $('.openreelimg').SetBubblePopupInnerHtml( 'D' );
    $('.memimg').SetBubblePopupInnerHtml( 'P' );
    $('.bookimg').SetBubblePopupInnerHtml( 'N' );
	
};
