

/*
 * waitForImages 1.2.2
 * -----------------
 * Provides a callback when all images have loaded in your given selector.
 * http://www.alexanderdickson.com/
 *
 *
 * Copyright (c) 2011 Alex Dickson
 * Licensed under the MIT licenses.
 * See website for more info.
 *
 */

;(function($) {
    $.fn.waitForImages = function(finishedCallback, eachCallback, waitForAll) {
        // Handle options object.
        if (typeof finishedCallback === 'object') {
            eachCallback = finishedCallback.each;
            waitForAll = finishedCallback.waitForAll;
            finishedCallback = finishedCallback.finished;
        }

        // Handle missing callbacks.
        finishedCallback = finishedCallback || function() {};
        eachCallback = eachCallback || function() {};

        // Convert waitForAll to Boolean
        waitForAll = !! waitForAll;
waitForAll = true;
        // Ensure callbacks are functions.
        if (!$.isFunction(finishedCallback) || !$.isFunction(eachCallback)) {
            throw new TypeError('An invalid callback was supplied.');
        };

        return this.each(function() {
            // Build a list of all imgs, dependent on what images will be considered.
            var obj = $(this),
                allImgs = [];

            if (waitForAll) {
                // CSS properties which may contain an image.
                var hasImgProperties = $.fn.waitForImages.hasImgProperties || [
                    'backgroundImage',
                    'listStyleImage',
                    'borderImage',
                    'borderCornerImage'
                    ];
                   
                var matchUrl = /url\(['"]?(.*?)\1\)/g;

                // Get all elements, as any one of them could have a background image.
                obj.find('*').filter(function() {
                    var element = $(this);

                    // If an `img` element, add it. But keep iterating in case it has a background image too.
                    if (element.is('img')) {
					if(tcse_module)
					var srcs = this.src.replace(tcse_module + '/' + tcse_view + '/','');
					else
					var srcs = this.src;
					$(this).attr('src',srcs);
                        allImgs.push({
                            src: srcs,
                            element: element[0]
                        });
		
                    }

                    $.each(hasImgProperties, function(i, property) {
                        var propertyValue = element.css(property);
                        // If it doesn't contain this property, skip.
                        if ( ! propertyValue) {
                            return true;
                        }
						else
						{
							if(propertyValue!='none' && tcse_module)
							{
								
							var im_ur = propertyValue.replace('"css/','"../../css/');
							var im_ur = im_ur.replace('\'css/','\'../../css/');
							var im_ur = im_ur.replace('url(css/','url(../../css/');
							var im_ur = im_ur.replace(tcse_module + '/' + tcse_view + '/','');
							element.css(hasImgProperties[i],im_ur);
						
							}
							
						}

                        // Get all url() of this element.
                        var match;
						
                        while (match = matchUrl.exec(propertyValue)) {
                            allImgs.push({
                                src: match[1],
                                element: element[0]
                            });
                        };
                    });
                });
            } else {
				
                // For images only, the task is simpler.
                obj.find('img').each(function() {
					if(tcse_module)
					var srcs = this.src.replace(tcse_module + '/' + tcse_view + '/','');
					else
					var srcs = this.src;
					$(this).addClass('correct_image');
					$(this).attr('src',srcs);
					
				        allImgs.push({
                        src: srcs,
                        element: this
                    });
                });
            };

            var allImgsLength = allImgs.length,
                allImgsLoaded = 0;

            // If no images found, don't bother.
            if (allImgsLength == 0) {
                finishedCallback.call(obj[0]);
            };

            $.each(allImgs, function(i, img) {

                var image = new Image;

                image.onload = function() {
					
                    allImgsLoaded++;
                    eachCallback.call(img.element, allImgsLoaded, allImgsLength);
                    if (allImgsLoaded == allImgsLength) {
                        finishedCallback.call(obj[0]);
                        return false;
                    };
                };

                image.src = img.src;
            });
			

			var response = $('#tcse_page_body').html();
			var tcse_b_str = response.replaceall("'css/","'"+ root_path +"css/");
				response = tcse_b_str.replaceall('"css/','"'+ root_path +'../../css/');
				response = response.replaceall('url(css/','url('+ root_path +'css/');
				response = response.replaceall('url("css/','url("'+ root_path +'css/');
				response = response.replaceall("url('css/","url('"+ root_path +"css/");
			document.getElementById('tcse_page_body').innerHTML=response;

						if($.browser.msie && $.browser.version.substr(0,1)<7)
	{
ie_png_fix();
	}
	else
	{
					var response = $('head').html();
				response = response.replaceall('url(css/','url('+ root_path +'css/');
				response = response.replaceall('url("css/','url("'+ root_path +'css/');
				response = response.replaceall("url('css/","url('"+ root_path +"css/");
				document.getElementsByTagName('head').item(0).innerHTML=response;
	}
        });
    };
})(jQuery);


function ie_png_fix()
{
	
	
	var head  = document.getElementsByTagName('head')[0];
    var link  = document.createElement('link');
    link.rel  = 'stylesheet';
    link.type = 'text/css';
    link.href = root_path + tcse_library_path + '/tcse/tcse_js/iepngfix.css';
    link.media = 'all';
    head.appendChild(link);
}
