function UpdateScroll() {
    $('body').css('width',$('html').css('width'));
    $('body').css('height','100%');
    alert($('html').css('width'));
    alert($('body').css('width'));
}

function setUpIntroText() {
    $(".intro").find("p").find("a").each(function() {
        var _this = $(this);
        $(this).click(function() {
            window.open(_this.attr("href"));
        });
    });

}


$(window).resize(function() {
    //UpdateScroll();
});


function slideDown() {
    $(".text").click(function() {        
        if($(this).find(".bodyText").is(":hidden")) {
            $(this).find(".bodyText").slideDown("fast");
        } else {
            $(this).find(".bodyText").slideUp();
        }
        return false;
    });
}

function setUpBrowsers() {

    if ($.browser.msie && $.browser.version < 8) {
        $('div#content div.newslayout1 div.box2').css('position', 'inherit');
    }
    else {
        $('div#content div.newslayout1 div.box2').css('position', 'relative');
        $('div#content div.newslayout1 div.box2').css('top', '0');
        $('div#content div.newslayout1 div.box2').css('left', '0');
    }

}




/*
Find all projects and appende imagetoggle
also toggle the images

*/
function initProjects() {

    $("#content .project").each(function(i) {

        //This is the project
        var currentProject = $(this),
        count = currentProject.find(".list").find("div").size();
        
        if (count <= 1) {
            return;
        }
        
        $(this).find(".images").find("img").each(function(imgIndex) {
            var _this = $(this);
            $(this).click(function() {
                //This can be achived in a smarter way

                //If there is no sibling length == 0
                var nextImgIndex = imgIndex + 1;
                _this.removeClass("active");
                if (_this.next("img").length !== 0) {
                    _this.next("img").addClass("active");
                    //Need to find the associated number
                }
                else {
                    //End of list start over.
                    _this.siblings('img:first').addClass("active");
                    nextImgIndex = 0;
                }
                
                //$("div:nth-child("+nextImgIndex+")")
                currentProject.find(".list").find("div").each(function(numberIndex) {
                    $(this).removeClass("active");
                    if (nextImgIndex ==  numberIndex) {
                        $(this).addClass("active");
                    }
                });

            });
        });

        $(this).find(".list").find("div").each(function(numberIndex) {

            $(this).click(function() {
                currentProject.find(".list").find("div").each(function(num) {
                    $(this).removeClass("active");
                });

                $(this).addClass("active");
                currentProject.find(".images").find("img").each(function(imgIndex) {

                    $(this).removeClass("active");
                    if (imgIndex ==  numberIndex) {

                        $(this).addClass("active");
                    }
                });

            });

        });


    });

}


//This function Ajax loads images for the slideshow
    //Since I put out one image from xslt i dont use the first image. 
    function initLoadImagesForSlideShow() {
        $("#content .project").each(function(i) {
            $(this).find(".images").each(function() {
                var imgElement = $(this);
                //alert($(this).attr("class"));
                var mediafolder = $(this).find(".mediafolder").val();
                //alert(mediafolder);
                $.getJSON('/Services/ImageJson.ashx?mediafolder=' + mediafolder, function(data) {
                    var images = data;
                    for (var i = 1; i < images.length; i++) {
                        var img = $("<img src='" + images[i] + "' alt=''/>");
                        imgElement.append(img);
                    }
                    //yes bad i reinitiate every project...but it works...need to be redone...someday
                    initProjects();
                });
            });
        });
        
    }
    $(window).ready(function() {
        $(".bodyText").hide();
        slideDown();
        //initProjects();
        setUpIntroText();
        initLoadImagesForSlideShow();
    });
