﻿// Feature object
//---------------------------------
/*
Feature is used to rotate home feature
---------------------------------------------
*/
var Feature = {
    // variables
    // variables
    currentImage: 1,
    totalItems: 0,
    curImgHeight: 0,
    curImgWidth: 0,
    current: false,
    interval: {
        delay: 5000,
        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
			)
        }
    },

    Display: function () {
        var feature = this.features[0][this.current];

        //flare blogpost player images
        var curImage = $("#FeaturePodImg-" + Feature.currentImage);
        if (curImage[0]) {
            this.curImgHeight = curImage[0].height;
            this.curImgWidth = curImage[0].width;
            
            if (this.curImgHeight > this.curImgWidth) {
                curImage.addClass('centered');
            }
            else {
                curImage.addClass('centeredTop').attr('style', 'width:310px');
            }
        }

        //add margin to photo
        $("#" + feature.div).fadeIn('fast');

        var trigger = feature.div;
        var $index = trigger.indexOf("-");
        Feature.currentImage = trigger.substring($index + 1);
        Feature.updateStatus();

        Feature.List(this.current, 0);
        if ($('#pageCount').length) {
            var $margin = (425 - parseInt($("#td-Content-" + Feature.currentImage).width())) / 2;
            var $height = (425 - parseInt($("#td-Content-" + Feature.currentImage).height())) / 2.3;
            //if ($margin > 100) {
            // $margin = 81;
            // }
            $(".td-featImage").css('margin-left', $margin + 'px').css('margin-top', $height + 'px');
        }
        curImage.hide();
    },

    Bind: function (prop) {

        var obj = $("a[trigger='" + prop + "']")
        obj.bind(
			'click',
			function () {

			    Feature.Switch($(this).attr("trigger"))

			}
		)

    },

    Switch: function (trigger) {
        //window.alert(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();
    },

    Kill: function () {
        clearInterval(this.interval.rotate);
    },

    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)


    },
    play: function (rotate) {
        Feature.Switch("feature-1");
        this.interval.rotate = setInterval(
				this.Rotate,
				1000
			)

    },

    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);



        }

    }
}
