﻿
function theRotator(div,interval) {
    //Set the opacity of all images to 0
    $('div#' + div + ' ul li').css({ opacity: 0.0 });

    //Get the first image and display it (gets set to full opacity)
    $('div#' + div + ' ul li:first-child').css({ opacity: 1.0 });
    $('div#' + div + ' ul li:first-child').addClass('show');

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('rotate("' + div + '")', interval);
};

function rotate(div) {
    //Get the first image
    var current = ($('div#' + div + ' ul li.show') ? $('div#' + div + ' ul li.show') : $('div#' + div + ' ul li:first-child'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#' + div + ' ul li:first-child') : current.next()) : $('div#' + div + ' ul li:first-child'));

    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0 })
    .addClass('show')
    .animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000)
    current.removeClass('show');
};
$(document).ready(
    function () {
        //load css for use with jquery
        $('<link>').appendTo('head').attr({ rel: 'stylesheet', type: 'text/css', href: 'CSS/jquery.css' });
        //page load events
        //Load the slideshows

        var rotator = $("#rotator")[0];
        var rotatorList = rotator.childNodes;
        var rotatorUl

        for (var i = 0, ii = rotatorList.length; i < ii; i++)
        {
            if (rotatorList[i].nodeType == 1)
            {
                rotatorUl = rotatorList[i];
                i = ii;
            }
        }

        var rotatorListItems = rotatorUl.childNodes;

        var rotatorListItemsFinal = 0;
        for (var i = 0, ii = rotatorListItems.length; i < ii; i++)
        {
            if (rotatorListItems[i].nodeType == 1)
            {
                rotatorListItemsFinal++;
            }
        }

        if (rotatorListItemsFinal > 1)
        {
            theRotator('rotator', 10000);
        }

        
//        $('div#rotator').hide();

//        current = $(".nav li.selected");
//        $('.nav li').click(function () {
//            current = $(this);
//        });


//        $('.nav li').live('mouseover mouseout', function (event) {
//            $this = $(this);
//            if (event.type == 'mouseover') {
//                current.removeClass("selected");
//                $this.find('ul').show();
//            } else {

//                current.addClass("selected");
//                $this.find('ul').hide();
//            }
//        });




        // Navigation
        current = $(".nav li.selected");
        $('.nav li').click(function ()
        {
            current = $(this);
        });

        $('.nav li').live('mouseover mouseout', function (event)
        {
            if (event.type == 'mouseover') {
                current.removeClass("selected");
            } else {
                current.addClass("selected");
            }
        });
        $('.nav li.about').live('mouseover mouseout', function (event)
        {
            if (event.type == 'mouseover') {
                $('.nav li.about ul').show();
            } else {
                $('.nav li.about ul').hide();
            }
        });

        $('.nav li.about ul').live('mouseover mouseout', function (event)
        {
            if (event.type == 'mouseover') {
                $('.nav li.about').addClass("selected");
            } else {
                current.addClass("selected");
                $('.nav li.about').removeClass("selected");
            }
        });
        $('.nav li.forsale').live('mouseover mouseout', function (event)
        {
            if (event.type == 'mouseover') {
                $('.nav li.forsale ul').show();
            } else {
                $('.nav li.forsale ul').hide();
            }
        });

        $('.nav li.forsale ul').live('mouseover mouseout', function (event)
        {
            if (event.type == 'mouseover') {
                $('.nav li.forsale').addClass("selected");
            } else {
                current.addClass("selected");
                $('.nav li.forsale').removeClass("selected");
            }
        });



    });
