﻿/// <reference path="common.js" />

$().ready(function ()
{
    HRGBannerInit();
});

/*
* Coverflow banner
*/
function HRGBannerInit()
{
    // Ensure we have a coverflow
    if (jQuery('#coverflow-slider').length > 0)
    {
        var initLayout = function ()
        {
            $('#coverflow-slider').spacegallery({
                loadingClass: 'loading',
                border: 0,          // border arround the image
                perspective: 155,   // perpective height (3 slides: 112, 12 slides: 155)
                minScale: 0.5,      // minimum scale for the image in the back
                duration: 1000,     // animation duration
                interval: 5000      //interval between image changes
            });

            InitNavigation();

            // When everything is loaded show the coverflow
            jQuery('.coverflow').removeClass('loading');
        };
        EYE.register(initLayout, 'init');

        // Once all images on the first slide have loaded we're ready to animate the slides
        jQuery('#coverflow-slider').onImagesLoad(
        {
            selectorCallback: function (firstSlide)
            {
                $(EYE.init);
            }
        });
    }
}

function InitNavigation()
{
    var frameCount = jQuery('#coverflow-slider img.sg-holder').length;

    $("#coverflow-navigation-slider").slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: frameCount-1,
        value: frameCount-1
    });
}

function SyncNavigation(currentFrame, src)
{
    if (src != 'gotoStep')
    {
        $("#coverflow-navigation-slider").slider({ value: currentFrame });
    }
}


/**
*
* Zoomimage
* Author: Stefan Petre www.eyecon.ro
* 
*/
(function ($)
{
    var EYE = window.EYE = function ()
    {
        var _registered = {
            init: []
        };
        return {
            init: function ()
            {
                $.each(_registered.init, function (nr, fn)
                {
                    fn.call();
                });
            },
            extend: function (prop)
            {
                for (var i in prop)
                {
                    if (prop[i] != undefined)
                    {
                        this[i] = prop[i];
                    }
                }
            },
            register: function (fn, type)
            {
                if (!_registered[type])
                {
                    _registered[type] = [];
                }
                _registered[type].push(fn);
            }
        };
    } ();

})(jQuery);





/**
*
* Spacegallery
* Author: Stefan Petre www.eyecon.ro
* 
*/
(function ($)
{
    var imageOnTop = 0;     //no of the image on top of the stack i.e. the one showing
    var ptrImage = 0;       //pointer to the current image during an animation
    var lastImage = 0;
    var animationFinished = 1;
    var lastStepSrc = '';

    EYE.extend({

        spacegallery: {

            //default options (many options are controled via CSS)
            defaults: {
                border: 0,          // border arround the image
                perspective: 135,   // perpective height
                minScale: 0.5,      // minimum scale for the image in the back
                duration: 800,      // animation duration
                interval: 5000,     //interval between image changes
                loadingClass: null, // CSS class applied to the element while looading images
                before: function ()
                {
                    var myclass = $("#coverflow-slider img:last").attr("class");
                },
                after: function ()
                {
                    var myclass = $("#coverflow-slider img:last").attr("class");
                    $("div." + myclass).fadeIn("slow");
                    SyncNavigation(imageOnTop, lastStepSrc);
                }
            },

            animated: false,
            timer: 0,


            //reposition images
            positionImages: function (el)
            {

                var top = 0;
                EYE.spacegallery.animated = false;
                $(el)
					.find('a')
						.removeClass(el.spacegalleryCfg.loadingClass)
						.end()
					.find('img')
						.removeAttr('height')
						.each(function (nr)
						{
						    var newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * el.spacegalleryCfg.asins[nr];
						    $(this)
								.css({
								    top: el.spacegalleryCfg.tops[nr] + 'px',
								    marginLeft: -parseInt((newWidth + el.spacegalleryCfg.border) / 2, 10) + 'px',
								    //marginLeft: parseInt((newWidth - 170) * 0.8) + 'px',
								    opacity: 1 - el.spacegalleryCfg.asins[nr]
								})
								.attr('width', parseInt(newWidth));
						    this.spacegallery.next = el.spacegalleryCfg.asins[nr + 1];
						    this.spacegallery.nextTop = el.spacegalleryCfg.tops[nr + 1] - el.spacegalleryCfg.tops[nr];
						    this.spacegallery.origTop = el.spacegalleryCfg.tops[nr];
						    this.spacegallery.opacity = 1 - el.spacegalleryCfg.asins[nr];
						    this.spacegallery.increment = el.spacegalleryCfg.asins[nr] - this.spacegallery.next;
						    this.spacegallery.current = el.spacegalleryCfg.asins[nr];
						    this.spacegallery.width = newWidth;
						})

            },



            autoNext: function (el, src)
            {
                if (EYE.spacegallery.animated === false)
                {
                    lastStepSrc = src;

                    var newDuration = el.spacegalleryCfg.duration;
                    var newEasing = 'easeOut';
                    if (lastStepSrc == 'gotoStep') 
                    {
                        newEasing = 'linear';
                        newDuration = 150;
                    }

                    EYE.spacegallery.animated = true;
                    el.spacegalleryCfg.before.apply(el); // run the "before" function
                    $(el)
						.css('spacegallery', 0)         //set spacegallery property to 0 (why?)
						.animate({
						    spacegallery: 100           //animate the spacegallery property from 0 to 100, over 'duration' millisecs
						}, 
                        {
                            easing: newEasing,
						    duration: newDuration,
						    complete: function ()
						    {     
                                // callback: after animation is complete, move the front image to the back
						        $(el)
									.find('img:last')   // find the last image in this div
									.prependTo(el);     // and stick it at the beginning

						        imageOnTop = $(el.children[el.children.length - 1]).attr('image-no');

						        EYE.spacegallery.positionImages(el);    // run "position images"

						        el.spacegalleryCfg.after.apply(el);     // run the "after" function						        

						        while ((ptrImage < lastImage) && (animationFinished == 1))
						        {
						            ptrImage++;
						            animationFinished = 0;
						            EYE.spacegallery.autoNext(el, src);
						        }
						        animationFinished = 1;
						    },
						    step: function (now)
						    {
						        $('img', this)
									.each(function (nr)
									{
									    var newWidth, top, next;
									    if (nr + 1 == el.spacegalleryCfg.images)
									    {
									        top = this.spacegallery.origTop + this.spacegallery.nextTop * 4 * now / 100;
									        newWidth = this.spacegallery.width * top / this.spacegallery.origTop + 100;
									        $(this)
												.css({
												    top: top + 'px',
												    opacity: 0.7 - now / 100,
												    marginLeft: -parseInt((newWidth + el.spacegalleryCfg.border) / 2, 10) + 'px'
												    //marginLeft: parseInt((newWidth - 170) * 0.8) + 'px'
												})
												.attr('width', newWidth);
									    } else
									    {
									        next = this.spacegallery.current - this.spacegallery.increment * now / 100;
									        newWidth = this.spacegallery.origWidth - (this.spacegallery.origWidth - this.spacegallery.origWidth * el.spacegalleryCfg.minScale) * next; $(this).css({
									            top: this.spacegallery.origTop + this.spacegallery.nextTop * now / 100 + 'px',
									            opacity: 1 - next,
									            marginLeft: -parseInt((newWidth + el.spacegalleryCfg.border) / 2, 10) + 'px'
									            //marginLeft: parseInt((newWidth - 170) * 0.8) + 'px'
									        })
											.attr('width', newWidth);
									    }
									});
						    }
						});
                }

                return false;
            },

            gotoStep: function(imageToShowID, el)
            {
                EYE.spacegallery.pause();

                var imageOnTopID = parseInt(imageOnTop.toString());
                if (imageOnTopID < imageToShowID)
                {
                    imageOnTopID += el.spacegalleryCfg.images;  //wrap around
                }

                if (imageToShowID < imageOnTopID)
                {
                    ptrImage = imageToShowID + 1;
                    lastImage = imageOnTopID;
                    EYE.spacegallery.autoNext(el, 'gotoStep');
                }

                EYE.spacegallery.resume(el);
            },

            pause: function()
            {
                clearInterval(timer);
            },

            resume:function(el)
            {
                timer = setInterval(function () { EYE.spacegallery.autoNext(el, 'auto'); }, el.spacegalleryCfg.interval);
            },

            //constructor
            init: function (opt)
            {

                opt = $.extend({}, EYE.spacegallery.defaults, opt || {});
                return this.each(function ()
                {
                    var el = this;
                    if ($(el).is('.spacegallery'))
                    {
                        el.spacegalleryCfg = opt;
                        el.spacegalleryCfg.images = el.getElementsByTagName('img').length;   //no of images
                        el.spacegalleryCfg.loaded = 0;
                        el.spacegalleryCfg.asin = Math.asin(1);  //the arcsin of '1'
                        el.spacegalleryCfg.asins = {};
                        el.spacegalleryCfg.tops = {};
                        el.spacegalleryCfg.increment = parseInt(el.spacegalleryCfg.perspective / el.spacegalleryCfg.images, 10);
                        _el = el;

                        var top = 0;


                        $('img.sg-holder').each(function (nr)
                        {             
                            //need to match up each of the links with corres images
                            $(this).attr('image-no', nr);
                        });


                        $('img', el)                                        //build up image data
							.each(function (nr)
							{
							    var imgEl = new Image();
							    var elImg = this;
							    el.spacegalleryCfg.asins[nr] = 1 - Math.asin((nr + 1) / el.spacegalleryCfg.images) / el.spacegalleryCfg.asin;
							    top += el.spacegalleryCfg.increment - el.spacegalleryCfg.increment * el.spacegalleryCfg.asins[nr];
							    el.spacegalleryCfg.tops[nr] = top;
							    elImg.spacegallery = {};
							    imgEl.src = this.src;

							    if (imgEl.complete)
							    {
							        el.spacegalleryCfg.loaded++;
							        elImg.spacegallery.origWidth = imgEl.width;
							        elImg.spacegallery.origHeight = imgEl.height
							    } else
							    {
							        imgEl.onload = function ()
							        {
							            el.spacegalleryCfg.loaded++;
							            elImg.spacegallery.origWidth = imgEl.width;
							            elImg.spacegallery.origHeight = imgEl.height
							            if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images)
							            {

							                EYE.spacegallery.positionImages(el);
							            }
							        };
							    }

							});


                        //
                        // bind the event handlers
                        //

                        // stop animating on mouseover
                        $(this).hoverIntent(
                        function ()
                        {
                            EYE.spacegallery.pause();
                        },
                        //recommence animating on mouseout
                        function ()
                        {
                            EYE.spacegallery.resume(el);
                        });

                        // When a new page is requested
                        $("#coverflow-navigation-slider").bind("slidechange", function(event, ui)
                        {
                            if (event.originalEvent)
                            {
                                //var gotoFrame = ((frameCount-1) - ui.value); // for reversing the scale
                                var gotoFrame = ui.value;
                                EYE.spacegallery.gotoStep(gotoFrame, el);
                            }
                        });

                        // bind onclick action
                        $('img.sg-holder').bind('click', function (nr)
                        {
                            if (($(this).attr('target') != null) && ($(this).attr('target').toString() != ""))
                            {
                                window.location.href = $(this).attr('target');
                            }
                        });

                        el.spacegalleryCfg.asins[el.spacegalleryCfg.images] = el.spacegalleryCfg.asins[el.spacegalleryCfg.images - 1] * 1.3;
                        el.spacegalleryCfg.tops[el.spacegalleryCfg.images] = el.spacegalleryCfg.tops[el.spacegalleryCfg.images - 1] * 1.3;

                        if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images)
                        {       //if no images loaded = no images

                            EYE.spacegallery.positionImages(el);                            //rearrange the images
                            imageOnTop = el.spacegalleryCfg.images - 1;                     //the image on top of the stack
                            timer = setInterval(function () { EYE.spacegallery.autoNext(el, 'auto'); }, el.spacegalleryCfg.interval);   //start timer
                        }
                    }
                });


            }
        }
    });

    $.fn.extend({

        /**
        * Create a space gallery
        * @name spacegallery
        * @description create a space gallery
        * @option	int			border			Images' border. Default: 6
        * @option	int			perspective		Perpective height. Default: 140
        * @option	float		minScale		Minimum scale for the image in the back. Default: 0.2
        * @option	int			duration		Animation duration. Default: 800
        * @option	string		loadingClass	CSS class applied to the element while looading images. Default: null
        * @option	function	before			Callback function triggered before going to the next image
        * @option	function	after			Callback function triggered after going to the next image
        */
        spacegallery: EYE.spacegallery.init
    });
    $.extend($.easing, {
        easeOut: function (x, t, b, c, d)
        {
            return -c * (t /= d) * (t - 2) + b;
        }
    });


})(jQuery);



/*
* jQuery 'onImagesLoaded' plugin v1.1.1 (Updated January 27, 2010)
* Fires callback functions when images have loaded within a particular selector.
*
* Copyright (c) Cirkuit Networks, Inc. (http://www.cirkuit.net), 2008-2010.
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* For documentation and usage, visit "http://includes.cirkuit.net/includes/js/jquery/plugins/onImagesLoad/1.1.1/documentation/"
*/
(function ($)
{
    $.fn.onImagesLoad = function (options)
    {
        var self = this;
        self.opts = $.extend({}, $.fn.onImagesLoad.defaults, options);

        self.bindEvents = function ($imgs, container, callback)
        {
            if ($imgs.length === 0)
            { //no images were in selection. callback based on options
                if (self.opts.callbackIfNoImagesExist && callback) { callback(container); }
            }
            else
            {
                var loadedImages = [];
                if (!$imgs.jquery) { $imgs = $($imgs); }
                $imgs.each(function (i)
                {
                    //webkit fix inspiration thanks to bmsterling: http://plugins.jquery.com/node/10312
                    var orgSrc = this.src;
                    //if (!$.browser.msie) { // IE detection disabled (IE9 image caching breaks this and IE9 returns "8" as $.browser.version)
                    if (true)
                    {
                        this.src = ""; //ie will do funky things if this is here (show the image as an X, only show half of the image, etc)
                    }
                    $(this).bind('load', function ()
                    {
                        if (jQuery.inArray(i, loadedImages) < 0)
                        { //don't double count images
                            loadedImages.push(i); //keep a record of images we've seen
                            if (loadedImages.length == $imgs.length)
                            {
                                if (callback) { callback.call(container, container); }
                            }
                        }
                    });
                    //if (!$.browser.msie) { // IE detection disabled (IE9 image caching breaks this and IE9 returns "8" as $.browser.version)
                    if (true)
                    {
                        this.src = orgSrc; //needed for potential cached images
                    }
                    else if (this.complete || this.complete === undefined) { this.src = orgSrc; }
                });
            }
        };

        var imgAry = []; //only used if self.opts.selectorCallback exists
        self.each(function ()
        {
            if (self.opts.itemCallback)
            {
                var $imgs;
                if (this.tagName == "IMG") { $imgs = this; } //is an image
                else { $imgs = $('img', this); } //contains image(s)
                self.bindEvents($imgs, this, self.opts.itemCallback);
            }
            if (self.opts.selectorCallback)
            {
                if (this.tagName == "IMG") { imgAry.push(this); } //is an image
                else
                { //contains image(s)
                    $('img', this).each(function () { imgAry.push(this); });
                }
            }
        });
        if (self.opts.selectorCallback) { self.bindEvents(imgAry, this, self.opts.selectorCallback); }

        return self.each(function () { }); //dont break the chain
    };

    //DEFAULT OPTOINS
    $.fn.onImagesLoad.defaults = {
        selectorCallback: null,        //the function to invoke when all images that $(yourSelector) encapsultaes have loaded (invoked only once per selector. see documentation)
        itemCallback: null,            //the function to invoke when each item that $(yourSelector) encapsultaes has loaded (invoked one or more times depending on selector. see documentation)
        callbackIfNoImagesExist: false //if true, the callbacks will be invoked even if no images exist within $(yourSelector).
        //if false, the callbacks will not be invoked if no images exist within $(yourSelector).
    };
})(jQuery);
