﻿jQuery(document).ready(function () {
    /* Featured Products Fader */
    jQuery('.featured-products').fader({ callback: function (i) { jQuery('.featured-products .index').html((i+1).toString()); } });
});

function initAdmin() {
    /* Catch for admin edits */
    jQuery('.admin-bar .edit.image').live(
        'click',
        function (event) {
            event.preventDefault();

            jQuery('#productId').val(jQuery(this).parentsUntil('.product').parent().attr("product"));
            jQuery('#image-dialog').dialog({ model: true, width: 400 });
        }
    );

        jQuery('#ExistingFile').live('change', onEditImageChanged);
        jQuery('#ExistingFile').live('keyup', onEditImageChanged);
}

function onEditImageChanged() {
    jQuery('#ImagePreview').attr('src', '/Images/Products/' + jQuery(this).val());
}

function getProductDetailsAsync(id, callback) {
    jQuery.ajax({
        url: '/Products/JsonDetails/' + id,
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            callback(data);
        }
    });
}

jQuery.fn.isValidEmail = function () {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    return reg.test(this.val());
};

jQuery.fn.getHref = function () { return this.attr('href'); }
jQuery.fn.getId = function () { return this.attr('id'); }

jQuery.fn.isChecked = function () {
    return this.attr('checked') == true;
};

/*
* ###########################################################################
* Name: Fader
* Description: Provides basic fading functionality for a list of elements.
* ###########################################################################
*/
jQuery.fn.fader = function (options) {
    // Get the existing controller
    var controller = this.data().controller;

    // stop existing controller
    if (controller != undefined) controller.stop();

    // Create the new controller
    controller = new faderController(this, options);

    // Add the value to persistent storage
    //this.setDataValue(this, 'controller', controller);
    this.data('controller', controller);

    // Gentleman, START YOUR ENGINES!!!!
    controller.start();
}

function faderController(element, options) {
    this.target = element;
    this.options = options;
    this.timeout = 7500;
    this.fadeTime = 700;
    this.fadeDelay = 500;
    this.timer = null;
    this.current = 0;
    this.addControls = true;
    this.callback = null;

    /* Fields used managing controls clicks out of sync */
    this.queuedIndex = null;
}

faderController.prototype.start = function () {
    // Load up options if we can
    if (this.options != undefined) {
        if (this.options.timeout != undefined) this.timeout = this.options.timeout;
        if (this.options.fadeTime != undefined) this.fadeTime = this.options.fadeTime;
        if (this.options.fadeDelay != undefined) this.fadeDelay = this.options.fadeDelay;
        if (this.options.addControls != undefined) this.addControls = this.options.addControls;
        if (this.options.callback != undefined) this.callback = this.options.callback;
    }

    // The way this works is by setting a timeout for when the next fade
    // should occur. At the end of the next fade, we will recreate
    // the timeout. This keeps things consistent.
    var scope = this;
    this.timer = setTimeout(function () { scope.update(); }, this.timeout);

    // Hide all elements except for the first
    jQuery('ul.fader > li', this.target).each(function () {
        if (jQuery(this).index() > 0) jQuery(this).hide(0);
    });

    if (this.addControls) {
        var controlsHtml = '<ul class="fader-controls">';

        jQuery('ul.fader > li', this.target).each(function () {
            controlsHtml += '<li><a href="#">' + (jQuery(this).index() + 1).toString() + '</a></li>';
        });

        controlsHtml += '</ul>';

        jQuery(this.target).append(controlsHtml);
    }

    jQuery('.fader-controls li:eq(0)').addClass('Selected');
    jQuery('.fader-controls li a').click(function (event) {
        event.preventDefault();
        scope.selectPanel(jQuery(this).parent().index());
    });
};

faderController.prototype.update = function (direction) {
    if (direction == null || direction == undefined || direction == 0)
        direction = 1;

    // Kill the existing timer to make sure it doesn't
    // continue on.
    clearTimeout(this.timer);
    this.timer = null;

    // Fade out the existing element
    jQuery('ul.fader > li:eq(' + this.current + ')', this.target).fadeOut(this.fadeTime);
    jQuery('.fader-controls li.Selected', this.target).removeClass('Selected');

    // Update the index!
    this.current = this.current + direction;

    // Verify that we haven't gone out of bounds and reset if we have.
    var liCount = jQuery('ul.fader > li', this.target).size();
    if (this.current == liCount) this.current = 0;
    else if (this.current == -1) this.current = liCount - 1;

    // Setup the fade and recreation of the timeout
    var scope = this;
    jQuery('ul.fader > li:eq(' + this.current + ')', this.target)
        .delay(this.fadeDelay)
        .fadeIn(this.fadeTime, function () { scope.updateDone(); });
    jQuery('.fader-controls li:eq(' + this.current + ')', this.target).addClass('Selected');
};

faderController.prototype.selectPanel = function (panelIndex) {
    if (this.timer == null) {
        this.queuedIndex = panelIndex;
        return;
    }

    this.selectPanelInternal(panelIndex);
};

faderController.prototype.selectPanelInternal = function (panelIndex) {
    // Kill the existing timer to make sure it doesn't
    // continue on.
    clearTimeout(this.timer);
    this.timer = null;

    // Fade out the existing element
    jQuery('ul.fader > li:eq(' + this.current + ')', this.target).fadeOut(this.fadeTime);
    jQuery('.fader-controls li.Selected', this.target).removeClass('Selected');

    // Update the index!
    this.current = panelIndex;

    // Verify that we haven't gone out of bounds and reset if we have.
    var liCount = jQuery('ul.fader > li', this.target).size();
    if (this.current == liCount) this.current = 0;
    else if (this.current == -1) this.current = liCount - 1;

    // Setup the fade and recreation of the timeout
    var scope = this;
    jQuery('ul.fader > li:eq(' + this.current + ')', this.target)
        .delay(this.fadeDelay)
        .fadeIn(this.fadeTime, function () { scope.updateDone(); });
    jQuery('.fader-controls li:eq(' + this.current + ')', this.target).addClass('Selected');
}

faderController.prototype.updateDone = function () {
    if (this.queuedIndex != null && this.queuedIndex != undefined) {
        this.selectPanelInternal(this.queuedIndex);
        this.queuedIndex = null;
        return;
    }

    if (this.callback != null) {
        this.callback(this.current);
    }

    var scope = this;

    // Recreate the timeout!
    this.timer = setTimeout(function () { scope.update(); }, this.timeout);
}

faderController.prototype.stop = function () {
    clearTimeout(this.timer);
    this.timer = null;
};
