﻿// Feature object
//---------------------------------
/*
Feature is used to rotate home feature
---------------------------------------------
*/
var Feature = {
    // variables

    // variables
    currentImage: 1,
    //changing this variable to true, code takes link from headline and adds it to image
    allowClickThrough: false,
    totalItems: 0,
    linkCollection: [],
    current: false,
    interval: {
        delay: 10000,
        rotation: {}

    },
    solid: "",
    count: 0,
    bg: "Feature-Background",
    menu: {
        node: "li",
        state: ["on", ""]
    },
    features: [],
    // methods

    Init: function(rotate) {
        var features = this.features[0];

        var first = true;



        for (prop in features) {

            if (!first) { $("#" + features[prop].div).hide(); }
            else {
                Feature.List(prop, 0);
            }
            if (!this.current) { this.current = prop; }
            this.Bind(prop);
            first = false;
        }

        if (rotate) {

            this.interval.rotate = setInterval(
				this.Rotate,
				this.interval.delay
			)

        }

        //this is an additional function that allows users to click on fp images
        if (this.allowClickThrough == true) {
            for (i = 1; i <= 4; i++) {

                $("#img" + i).bind('click', function() {
                    var y = $(this).attr('id');
                    window.location = $("#link" + y.substring(y.length - 1)).attr("href");


                });
            }
        }

        Feature.hideButtons();

    },

    Display: function() {
        var feature = this.features[0][this.current];
        //note: the fade in creates a bug in ie that pushes the page up and down.
        if ($.browser.msie) {
            $("#" + feature.div).show();

        }
        else {
            $("#" + feature.div).fadeIn("fast");

        }



        var trigger = feature.div;
        var $index = trigger.indexOf("-");

        Feature.currentImage = trigger.substring($index + 1);
        Feature.updateStatus();

        //note: this adds a link on the main image of the photo gallery




        Feature.List(this.current, 0);
        if ($('#imageHolder').length) {
            var $margin = (425 - parseInt($("#td-Content-" + Feature.currentImage).width())) / 2;
            var $height = (425 - parseInt($("#td-Content-" + Feature.currentImage).height())) / 2.3;

            $(".td-featImage").css('margin-left', $margin + 'px').css('margin-top', $height + 'px');

        }


    },

    Bind: function(prop) {

        var obj = $("a[trigger='" + prop + "']")
        obj.bind(
			'click',
			function() {

			    Feature.Switch($(this).attr("trigger"))



			}
		)




    },

    Switch: function(trigger) {

        this.current = trigger;
        var features = this.features[0];

        for (prop in features) {
            $("#" + features[prop].div).hide();
            Feature.List(prop, 1);
        }
        var $index = trigger.indexOf("-");
        Feature.currentImage = trigger.substring($index + 1);

        Feature.updateStatus();
        this.Display();
        this.Kill();
        this.hideButtons();
    },

    Kill: function() {
        clearInterval(this.interval.rotate);
        if ($(".playbutton").length) {
            $(".playbutton").attr("href", "javascript:Feature.play()").text("Play");

        }

    },

    Rotate: function() {
        var features = Feature.features[0];
        var current = Feature.current;
        var count = Feature.count;
        var next = false;
        var is = false;
        var first = false
        for (prop in features) {

            first = !first ? prop : first;
            if (is) { next = prop; }
            is = prop == current ? true : false;

            $("#" + features[prop].div).hide();
            Feature.List(prop, 1);
        }

        Feature.current = next ? next : first;
        Feature.Display();
        if (Feature.currentImage == Feature.totalItems) {
            Feature.Kill();

        }

    },

    List: function(prop, state) {
        var state = this.menu.state[state];
        var obj = $("a[trigger='" + prop + "']")

        state == "on" ? obj.addClass("on") : obj.removeClass("on");

    },

    getNext: function(rotate) {
        var $nextImage = (parseInt(Feature.currentImage) + 1);
        if ($nextImage >= Feature.totalItems) {
            $nextImage = Feature.totalItems;
        }
        Feature.Switch("feature-" + $nextImage)


    },
    getLast: function(rotate) {
        var $nextImage = (parseInt(Feature.currentImage) - 1);
        if ($nextImage < 1) {
            $nextImage = 1;
        }
        Feature.Switch("feature-" + $nextImage)

    },
    hideButtons: function() {
        if (parseInt(Feature.currentImage) == 1) {
            $("#PhotoGalleryReverse").hide();
        }
        else {
            $("#PhotoGalleryReverse").show();

        }
        if (parseInt(Feature.totalItems) == parseInt(Feature.currentImage)) {
            $("#PhotoGalleryForward").hide();
        }
        else {
            $("#PhotoGalleryForward").show();

        }

    },


    play: function(rotate) {
        Feature.Switch("feature-1");
        this.interval.rotate = setInterval(
				this.Rotate,
				1000
			)
        if ($('#pageCount').length) {
            $(".playbutton").attr("href", "javascript:Feature.Kill()").text("Pause");
        }

    },

    di: function(rotate) {
        var $nextImage = (parseInt(Feature.currentImage) - 1);
        if ($nextImage < 1) {
            $nextImage = 1;
        }
        Feature.Switch("feature-" + $nextImage)


    },
    updateStatus: function() {
        if ($('#pageCount').length) {
            $("#pageCount").html(Feature.currentImage + " of " + Feature.totalItems);


            $("a[rel='forward']").show();
            $("a[rel='reverse']").show();
           
            if (Feature.currentImage == Feature.totalItems) {

                $("a[rel='forward']").hide();
            }

            else if (Feature.currentImage == 1) {
                

                $("a[rel='reverse']").hide();

            }


        }

    }
}