﻿(function(jq) {
    jq(document).ready(function() {
        // store for item css pre enlarge;
        var old = {};
        old.item = { height: 84 };
        old.div = {};
        old.img = {};
        // hide carousel descriptions;
        jq('.info p', "#carousel").hide();
        // listen for custom events from carousel;
        jq.each(['init', 'preanimate', 'complete'], function(i, val) {
            jq(document).bind(val, function(e, a) {
                if (val == 'preanimate') {
                    resetItem(a);
                } else if (val == 'init' || val == 'complete') {
                    killLinks();
                    resizeItem(a);
                }
            });
        });
        // remove existing links from the info panels to prevent tabbing issues;
        function killLinks() {
            jq(".info a").each(function() {
                // store link url in parents rel attr for resurrection later;
                jq(this).parent().attr("rel", this.href);
                jq(this).replaceWith(jq(this).html());
            });
        }
        // resurect link when panel active;
        function resurrectLink(elem) {
            var href = jq('.info', elem).attr("rel");
            jq('.info', elem).wrapInner("<a href='" + href + "'></a>");
        };
        // collect old styles for reset;
        function resetCSS(el, obj, selectors) {
            return jq.each(selectors, function(i, v) {
                var style = jq(el).css(v);
                if ((v == 'top' || v == 'left') && style == 'auto')
                    style = 0;
                else if (v == 'background' && (style == undefined || style == ""))
                    style = 'none';
                obj[v] = style;
            });
        };
        function popItem(el) {
            var t = parseInt(old.item.top) - 10 || -10, l = parseInt(old.item.left) - 12;
            jq(el).css({
                zIndex: 3,
                padding: 2,
                background: '#fff'
            }).animate({ top: t, height: 104, left: l, width: 187 }, 50, 'linear', function() {
                setTimeout(function() {
                    jq('.info p', el).show('fast', function() {
                        jq(document).trigger('showComplete');
                    }), 1200
                });
            });
        };
        function resizeItem(item) {
            var selectors = ['zIndex', 'top', 'left', 'background', 'padding', 'width'];
            resetCSS(item, old.item, selectors);
            popItem(item);
            // size info panel;    
            selectors = ['left', 'bottom', 'width' ];
            resetCSS(jq('.info', item), old.div, selectors);
            jq('.info', item).css({
                left: 7,
                bottom: 7,
                width: 177
            });
            // size info panel background;
            jq('.info .ft', item).css({ width: 177 });
            // size image;
            selectors = [ 'height', 'width', 'background' ];
            resetCSS(jq('img', item), old.img, selectors);
            jq('img', item).css({
                height: 104,
                width: 187,
                background: 'none'
            });
            resurrectLink(item);
        };
        function resetItem(el) {
            jq(el).css( old.item );
            jq('img', el).css( old.img );
            jq('.info', el).css( old.div );
            jq('.info .ft', el).css({width: 141});
            jq('.info p', el).hide('fast');
        };
        // do enhanced panels;
        jq(".subPanel").panel();
        // do carousel;
        jq("#carousel").carousel({ scrollIncrement: 1 });
    });
})(jQuery);
