(function($) {
    $(function() {
        var image_width = 595; // image width for orzilla pictures
        var current_pos = 0; // initial position of the image slides
        //
        // Initiate page loader variables
        var current_model = $('a.model_loader:first').attr('href');
        var current_ozilla = $('a.ozilla_loader:first').attr('href');
        var model_content = $('#model_content');
        var ozilla_content = $('#ozilla_content');

        $.get(current_model, function(data) {
            model_content.html(data);
        });

        $.get(current_ozilla, function(data) {
            ozilla_content.html(data);
        });

        //bind page loading to links with class name model_loader and ozilla_loader
        $('a.model_loader, a.ozilla_loader').bind('click', page_loader);

        function page_loader(e) {
            // remove the default behaiour on clicking a link
            e.preventDefault();

            // get the clicked link's class name
            var class_name = $(this).attr('class');
            // get the url to load the page
            var loading_page = $(this).attr('href');

            // Load the page only to mode_content div
            if(class_name == 'model_loader') {
                // load only when a different page is clicked
                if(loading_page != current_model) {
                    current_model = loading_page;
                    model_content.hide();
                    $(this).parent().siblings().removeClass('selected');
                    $(this).parent().addClass('selected');
                    $.get(loading_page, function(data) {
                        model_content.html(data).fadeIn("slow");
                    });
                }
            }

            // load the page only to ozilla_content div
            if(class_name == 'ozilla_loader') {
                // load only when a different page is clicked
                if(loading_page != current_ozilla) {
                    current_ozilla = loading_page;
                    ozilla_content.hide();
                    $(this).parent().siblings().removeClass('selected');
                    $(this).parent().addClass('selected');
                    $.get(loading_page, function(data) {
                        ozilla_content.html(data).fadeIn("slow");
                        // load image slides function if the clicked link is the image slides page
                        if(loading_page == 'pictures.html') {
                            image_slides();
                        }
                        if(loading_page == 'specs.html') {
                            specs_slides();
                        }
                    });
                }
            }
            return false;
        }

        function image_slides() {
            var slides = $('img.slide');
            var image_num = slides.length;
            var slide_width = image_num * image_width;

            $("#image_box").css("overflow", "hidden");

            slides.wrapAll('<div id="slideInner"></div>').css({
                'float' : 'left'
            });
            $('#slideInner').css('width', slide_width);

            manage_controls(current_pos, image_num);

            $("#image_box a").bind('click', function(e){
                e.preventDefault();
                if($(this).attr('id') == 'pre') {
                    current_pos = current_pos  - 1;
                } else {
                    current_pos = current_pos + 1;
                }

                manage_controls(current_pos, image_num);

                $('#slideInner').animate({
                    'marginLeft' : image_width * (-current_pos)
                });
            });
            return false;
        }

        function manage_controls(position, image_num) {
            if(position == 0) {
                $('#pre').hide();
            } else {
                $('#pre').show();
            }
            if(position >= image_num - 1) {
                $('#next').hide();
            } else {
                $('#next').show();
            }
            return false;
        }

        function specs_slides() {
            $('#scroll').css({"overflow": "hidden", "position": "relative"});
            $('#scroll').wrapInner('<div id="specs_content" />').css({"position": "absolute"});
            var specs_headers = $("#specs_content h3 a");
            var specs_header_position = new Array();

            specs_headers.each(function(){
                specs_header_position.push($(this).position().top);
            });

            $('#specs_list li a').bind('click', function(e) {
                e.preventDefault();
                var clicked_spec = $(this).attr('id');
                specs_headers.each(function(index){
                    if($(this).attr("name") == clicked_spec) {
                        $('#specs_content').animate({
                            'marginTop' : 0 - specs_header_position[index]
                        });
                    }
                });
            });
            return false;
        }
    });
})(jQuery);
