﻿jQuery(document).ready(function () {
    jQuery(window).resize(function (event) {
        if (jQuery(window).width() < 961) {
            jQuery('#Header').attr('style', 'background:transparent;');
        }
        else { jQuery('#Header').attr('style', ''); }
    });

    jQuery(".reliefFinder-list>li").hover(
        function () {
            jQuery(this).setDataValue('basic-height', jQuery(this).height());
            jQuery(this).css('height', jQuery('ul', jQuery(this)).height() + jQuery(this).height() + 10);
        },
        function () {
            jQuery(this).css('height', jQuery(this).getDataValue('basic-height'));
        }
    );
    jQuery("#Page").prepend('<div class="plane"><!--plane--></div>');
    jQuery("#Page").prepend('<div class="cloud one"><!--cloud 1 --></div>');
    jQuery("#Page").prepend('<div class="cloud one"><!--cloud 2 --></div>');

    var offset = window.screen.width / 5;
    jQuery('.cloud').each(function () {
        jQuery(this).pan({ direction: -1, speed: randomize(25, 35), initialPosition: { x: offset} });

        offset += window.screen.width / 2;
    });
    jQuery('.plane').pan({ speed: randomize(35, 60), verticalLimits: { min: 15, max: 50 }, initialPosition: { x: window.screen.width / 4} });

    jQuery('.Widget.ReliefFinder ul.reliefFinder-list>li ul li').hover(
        function () { jQuery(this).stop().animate({ left: '10px' }); },
        function () { jQuery(this).stop().animate({ left: '0px' }); }
    );
});

/*
* ###########################################################################
* Name: pan
* Description: Pans an object across the screen
* ###########################################################################
*/
jQuery.fn.pan = function (options) {
    var controller = new panController(this, options);

    this.data('pan-controller', controller);

    controller.initialize();
}

function panController(element, options) {
    this.target = element;
    this.targetWidth = 10;
    this.options = options;
    this.speed = 1.65;
    this.direction = 1;
    this.timer = null;
    this.timeout = 2000;
    this.timeoutTimer = null;
    this.needsReset = true;
    this.initialOffsetX = 0;
    this.lastTime = null;
    this.verticalLimits = { min: 10, max: 175}
    this.position = { x: 0, y: 0 };
    this.initialPosition = null;
    this.isFirstRun = true;
    this.isInFocus = true;
}

panController.prototype.initialize = function () {
    if (this.options != undefined && this.options != null) {
        if (this.options.speed != undefined) this.speed = this.options.speed;
        if (this.options.direction != undefined) this.direction = this.options.direction;
        if (this.options.initialOffsetX != undefined) this.initialOffsetX = this.options.initialOffsetX;
        if (this.options.verticalLimits != undefined) this.verticalLimits = this.options.verticalLimits;
        if (this.options.initialPosition != undefined) this.initialPosition = this.options.initialPosition;
    }

    this.reset();
    this.targetWidth = jQuery(this.target).width();

    var scope = this;

    jQuery(window).blur(function () { scope.isInFocus = false; })
    jQuery(window).focus(function () { scope.isInFocus = true; scope.lastTime = new Date().getTime(); });
    
    this.timer = setInterval(function () { scope.update(); }, 30);
};

panController.prototype.reset = function () {
    if (!this.needsReset) return;

    this.needsReset = false;

    if (this.timeoutTimer != null) {
        clearTimeout(this.timeoutTimer);
        this.timeoutTimer = null;
    }

    // If an initial position was set, use that
    // but only for the first run.
    if (this.isFirstRun && this.initialPosition != null) {
        this.position.x = this.initialPosition.x;
        this.position.y = this.initialPosition.y;
        this.isFirstRun = false;
    }
    else {
        if (this.direction < 0) {
            this.position.x = randomize(window.screen.width, window.screen.width + this.targetWidth) + this.initialOffsetX;
        }
        else {
            this.position.x = -randomize(0, this.targetWidth) + this.initialOffsetX;
        }
    }

    jQuery(this.target).css('left', this.position.x);
    jQuery(this.target).css('top', randomize(this.verticalLimits.min, this.verticalLimits.max));
    this.initialOffsetX = 0;

    this.lastTime = new Date().getTime();
};

panController.prototype.update = function () {
    if (this.needsUpdate) return;

    var ms = (new Date().getTime()) - this.lastTime;
    var seconds = ms / 1000;

    this.position.x = this.position.x + (this.speed * this.direction * seconds);

    jQuery(this.target).css('left', this.position.x);

    var scope = this;

    if ((this.direction > 0 && this.position.x > window.screen.availWidth) || (this.direction < 0 && (this.position.x + this.targetWidth) < 0)) {
        this.needsReset = true;

        this.timeoutTimer = setTimeout(function () { scope.reset(); }, this.timeout);
    }

    this.lastTime = new Date().getTime();
};


/*
* ###########################################################################
* Name: reliefRadio
* Description: Hooks radio buttons up to their respective DIVs
* Use: 
*   JS: jQuery('.rdio').reliefRadio();
*   HTM: <div class="rdio"><input type="radio" .. /><label /></div>
* ###########################################################################
*/
jQuery.fn.reliefRadio = function (options) {
    var prefix = 'relief-';

    if (options != null && options != undefined) {
        if (options.prefix != null && options.prefix != undefined) prefix = options.prefix;
    }

    jQuery('.' + prefix + 'panel').hide();

    jQuery("input", jQuery(this)).change(function () {
        var id = '#' + prefix + jQuery(this).attr('id');

        if (jQuery('.' + prefix + 'panel.Selected').length == 0) {
            jQuery('#' + prefix + 'panel-note').fadeOut(200, function () { jQuery(id).fadeIn(250, function () { jQuery(this).addClass('Selected') }); });
        }
        else {
            jQuery('.' + prefix + 'panel.Selected').fadeOut(200, function () { jQuery(id).fadeIn(250, function () { jQuery(this).addClass('Selected') }); });
        }
    });
}

