$.fn.contentLoader = function(options) {

    var defaults = {
        content : "homepage.html",
        speedIn	: 750,
        speedOut: 300, 
        slide 	: true,
        addclass: 'homepage'
    };
    var options = $.extend(defaults, options);
	
    function showLoader() {
        $('body').append('<div id="loader"></div>');
        $('#loader').fadeIn(300);
    }

    function hideLoader() {
        $('#loader').fadeOut(300, function() {
            $('#loader').remove();
        });
    }

    return this.each(function(index, dom) {
		
        showLoader();
		
        $(dom).fadeOut(options.speedOut, function() { 
			
            $(this).html('').load(options.content, function() {
					
                $(this).addClass(options.addclass).fadeIn(options.speedIn, function() {
						
                    if (options.slide == true) {
                        var targetOffset = $('#main').offset().top - 20;
                        $('html,body').animate({
                            scrollTop: targetOffset
                        }, 1000);
                    }
						
                    hideLoader();


                    // change rate button color
                    var currentcol = $("#center h1").css("color");
                    $("#rate").fadeOut(300, function() {
                        
                        if ($("#content").attr('class') != "update" && $("#content").attr('class') != "index update") {
                         
                         
                            $.ajax({
                                url: "/site/get_class/index/"+ $("#content").attr("class") +"/" + $("#campaign_interval_id").val(),
                                success: function(txt) {
                                    $("#poll_id").val(txt);
                                }
                            });
                            
                            $(this).css({
                                'background-color' : currentcol
                            }).fadeIn(500);
                            
                        }
                    });

                    $("#ratemodal h2").css({
                        'color' : currentcol
                    });
                    $("#ratemodal a.button").css({
                        'background-color' : currentcol
                    });
                    
                    
                    //check if we are on mobile device - then force resize of #content.left
                    if ($("body").width() < 640) {
                        
                        $("#content .left").css("width", "100%");
                        $("#accordion").css("width", ($("#center h1").width() - 20) + "px" );
                        $("#accordion a").css({"height": "auto","padding-right": "25px"});
                        $("#accordion div a").css({"height": "auto","padding-right": "0"});
                        
                        $("#accordion div").css("border", "0");
                        
                        $("#center.equal, #center p, #center h2").css({"width":"100%", "margin":"10px 0"});
                        
                        $("#ssavers").css({"background": "none", "padding": 0});
                        
                    }
                    
                    
					
                });
                
                $("#accordion a").basicAccordion();
                
                
                if (options.addclass == "healthyeating") {
                    
                    counter = 1;

                    $('#questions').addQuestion({
                        question: 'The UK recommended daily calcium intake for an adult woman is 700mg per day. Which of these foods contains around 700mg of calcium?', 
                        options: ['3.5oz (100g) of cheddar cheese','3.5oz (100g) of low fat fruit yoghurt', '3.5oz (100g) of boiled spinach', '3.5oz (100g) of dairy ice cream, with 3.5oz (100g) of dried apricots'], 
                        answer: 1, 
                        explanation: "The correct answer is 3.5oz (100g) of cheddar cheese, which contains an estimated 739mg of calcium. All the other foods in the list are good sources of calcium, but they don't come near the daily amount. Other foods very high in calcium are certain fish, including whitebait and sardines."
                    });
                    $('#questions').addQuestion({
                        question: 'Which of these vitamins is produced naturally by your body when your skin is exposed to sunlight and can also be obtained from some foods, such as oily fish?', 
                        options: ['vitamin A','vitamin B', 'vitamin C', 'vitamin D'], 
                        answer: 4, 
                        explanation: "The correct answer is vitamin D. Vitamin D is needed by the body to absorb calcium, which helps maintain healthy bones. However, most people don't get enough from food or the sun. This is especially true if you live in a region that is nearer the North or South Pole than the equator (for example the UK, Canada or southern Argentina), where the sunlight to make vitamin D is only strong enough during the summer. If you're aged 65 years or over, if you completely cover yourself for religious reasons, for example by wearing a burka, or you never go outside into the sunlight you're more likely to be deficient in vitamin D."
                    });	
                    $('#questions').addQuestion({
                        question: 'If a food label in the UK says "contains organic ingredients" what proportion of the product must be organic?', 
                        options: ['100%','95-100%', '70-95%', 'at least 50%'], 
                        answer: 3, 
                        explanation: "The correct answer is 70-95%. However, if a product is simply labelled 'organic' then it has to contain at least 95% organic ingredients."
                    });	
                    $('#questions').addQuestion({
                        question: 'What is the difference between a "use by" date and a "best before" date? ', 
                        options: ['a "best before" date shows how long a shop can sell a food, "use by" shows when you need to eat it','they mean exactly the same, but "best before" is used for packaged foods', '"best before" gives a deadline for freezing food, "use by" gives a date for eating it', 'a "use by" date is given for foods that could be dangerous if you eat them after expiry, a "best before" date just shows it might not taste as good'], 
                        answer: 4, 
                        explanation: "The correct answer is that a 'use by' date is given for foods that could be dangerous if you eat them after expiry and that a 'best before' date just shows that it might not taste as good. Foods, such as meat and fish can cause food poisoning if they aren't eaten within a certain time limit, even if they look and taste fine. These foods are marked with a 'use by' date. Other foods, such as coffee granules and biscuits have a 'best before' date because they may not look or taste as good after the expiry date."
                    });
                    $('#questions').addQuestion({
                        question: 'According to studies, what proportion of UK adults have a food intolerance? ', 
                        options: ['1-2%','5%', '15-20%', '50%'], 
                        answer: 1, 
                        explanation: "The correct answer is 1-2%. However, an estimated 10-20% of UK adults claim to be intolerant or 'allergic' to a food. "
                    });	
                    $('#questions').addQuestion({
                        question: 'As part of a healthy diet, which of the following foods has been highlighted as helping to cut your risk of a coronary heart disease?', 
                        options: ['pasta','lean steak', 'oily fish', 'skinless chicken'], 
                        answer: 3, 
                        explanation: "The correct answer is oily fish. Oily fish, including salmon, mackerel and sardines are rich in a special type of fat known as omega 3 fatty acids. A good intake of omega 3 fatty acids has been shown to substantially reduce a person's risk of a fatal heart attack. The UK Government recommends eating one to two portions of oily fish every week."
                    });	
                    $('#questions').addQuestion({
                        question: 'Which of the following counts as one portion towards the recommended five plus portions of fruit and vegetables per day?', 
                        options: ['a dessert bowl of salad','a 150ml glass of fruit juice', 'three heaped tablespoons of beans or pulses', 'all of the above'], 
                        answer: 4, 
                        explanation: "The correct answer is all of the above! All of these foods represent a portion towards your five-a-day."
                    });	
                    $('#questions').addQuestion({
                        question: 'For a general healthy diet, approximately what percentage of your daily calories should be made up from starchy foods like bread and potatoes?', 
                        options: ['10%','33%', '66%', '75%'], 
                        answer: 2, 
                        explanation: "The correct answer is 33%. We should get our main source of energy from starchy foods. Of course, some carbohydrate foods are better than others - wholegrain rice, bread and pasta are the healthier choices, whereas sugary foods should be kept to an occasional treat."
                    });	
                    $('#questions').addQuestion({
                        question: 'The recommended daily salt intake in the UK is 6g per day. But how much salt does the average man and woman in Britain actually consume?', 
                        options: ['men consume an average 7g, women an average 6g','men and women both have about 6g per day', 'men consume an average 11g, women an average 8g', 'men and women consume an average 15g'], 
                        answer: 3, 
                        explanation: "The correct answer is that men consume an average 11g of salt and women an average of 8g. Even if you don't add sprinkle salt over your dinner, eating more than the recommended daily allowance is alarmingly easy - 75% of the salt we consume is already in the foods we eat, such as ready-made soups, breads and ready meals. High salt intake is linked to high blood pressure, so it makes sense to cut down."
                    });	
                    $('#questions').addQuestion({
                        question: 'According to nutrition experts what percentage of body weight lost in fluids may result in reduced mental and physical performance?', 
                        options: ['1%','2%', '5%', '10%'], 
                        answer: 2, 
                        explanation: "The correct answer 2%. Of course, the amount of fluid we need to drink each day varies from person to person, and depends on what we're doing and the climate where we live. Excessive fluid intake should be avoided as it can cause serious health problems."
                    });	

                    $("#questions").checkTest();

                }
                
                else if (options.addclass == "win") {
                    
                    if($.browser.msie) {
                        $("#hidder").attr('style', '');
                        $("#uploadnow").remove();
                        
                    }
                    

                    function beforeUpload() {

                        if ($('#uploadfile').val() != '' && $('#description').val() != '') {

                            $('#uploadForm').slideUp(500);
                            $('h1.sub').slideUp(500);
                            $('#uploadnow').slideUp(500);
                            $('#output').slideDown(500);

                            return true;
                        }

                        else {
                            alert('Please complete the form.');

                            return false;
                        }
                    }


                    function afterUpload() {
                        $('#output').css('color', '#939905');
                    }


                    var uploadoptions = { 
                        target:        '#output',
                        beforeSubmit:  beforeUpload,
                        success:       afterUpload,
                        timeout:   30000 
                    }; 

                    $('#uploadForm').submit(function() { 
                        $(this).ajaxSubmit(uploadoptions); 

                        return false; 
                    });
                    
                    
                    $("#uploadnow").click(function() {
			$("#uploadfile").click();
                        return false;
                    });
                    
                    
                     $("h5").toggle(function(){
                        $("#termsandconditions").show(500);
                    },
                    function(){
                        $("#termsandconditions").hide(500);
                    });
                        
                }
                
                
                else if (options.addclass == 'yourclients') {
                 
                // broker clients page
                $('table.display').dataTable({
                                            "sPaginationType": "full_numbers",
                                            "aoColumns": [
                                                            null,
                                                            null,
                                                            { "asSorting": false },
                                                            { "asSorting": false }
                                                         ]
                                            });

                }
					
            });
			
        });
    });
}
