(function ($) { /* tracking code */
    var track = function (category, action, label) {
        try {
            var $this = $(this);
            var pageTracker = _gaq._getAsyncTracker();
            if (pageTracker) pageTracker._trackEvent(category, action, label);
            /*if (typeof $this.data('egt-href') == 'string') {
                setTimeout(function () {
                    window.location = $this.data('egt-href');
                }, 100);
            }*/
            //console.log('TRACK(' + category + ',' + action + ',' + label + ')');
        } catch (e) { };
    };
    $('*[data-egt]').each(function () {
        var data = $(this).data('egt');
        if (typeof data == 'string') {
            var s = data.split(',');
            var event = s[0];
            if (event == 'hover') event = 'mouseenter';
            if (event == 'click') event = 'mouseup';
            /*if (event == 'click' && typeof $(this).attr('href') == 'string') {
                $(this).data('egt-href', $(this).attr('href'));
                $(this).attr('href', 'javascript:void(0)');
            }*/
            if (s.length >= 3) {
                $(this).bind(event, _.bind(track, this, s[1], s[0], s[2]));
            }
        }
    });
})(jQuery);

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

(function ($) {

    var $A = window.App = {
        IE: $('body').hasClass('ie'),
        IEO: $('body').hasClass('ieo')
    };

    if ($A.IEO && typeof $FLASH_INTRO == 'undefined' && $.cookie('msg_ie7_incomp') != '1') {
        $(function() {
            alert('We have detected you are using IE Compatiblity View.  Please turn this setting off for proper viewing.');
            $.cookie('msg_ie7_incomp', '1', { path: '/' });
        });
    }

    $A.Utils = {
        Fade: function (e, show) {
            if (show) {
                $(e).css({ opacity: 0, display: 'block' })
                    .animate({ opacity: 1 }, { queue: false, duration: 200 });
            } else {
                $(e).animate({ opacity: 0 }, {
                    queue: false,
                    duration: 200,
                    complete: function() {
                        $(this).css({ opacity: 1, display: 'none' })
                    }
                });
            }
        }
    }

    $A.MainView = Backbone.View.extend({
        el: $('body'),
        initialize: function () {
            var bodyId = this.el.attr('id');

            $('a').live('mousedown', function () {
                $(this).css('outline-width', '0');
            });

            switch (bodyId) {
                case 'page-home':
                    new $A.HomeView({ el: $('.view-home') });
                    break;
                case 'page-products':
                case 'page-products-glas':
                case 'page-products-edge':
                    new $A.ProductsView({ el: $('#content') });
                    break;
                case 'page-products-glas-details':
                case 'page-products-edge-details':
                    new $A.ProductDetailsView({ el: $('#content') });
                    break;
                case 'page-glas':
                case 'page-edge':
                    new $A.RoomView({ el: $('#content') });
                    break;
            }
        }
    });

    $A.RoomView = Backbone.View.extend({
        initialize: function () {
            new $A.Expandable({ el: $('.e-expandable') });

            this.$('.room-pois > li').fadeIn(1000)
                .hover(function () {
                    $A.Utils.Fade($(this).find('.room-poi-tip'), true);
                }, function () {
                    $A.Utils.Fade($(this).find('.room-poi-tip'));
                });
        }
    });
    $A.Expandable = Backbone.View.extend({
        events: {
            'click .toggle': 'toggle'
        },
        initialize: function() { var $this = this;
            this._height = this.el.height();
            this._expandHeight = this.el.addClass('expand').height();
            this.el.removeClass('expand')
                .hide()
                .css('visibility', 'visible')
                .fadeIn(1000);

            this._expandAnimate = {
                height: this._expandHeight
            };
            this._collapseAnimate = {
                height: this._height
            };

            this._expandAnimateOpt = {
                duration: 500,
                queue: false,
                complete: function() {
                    $this.el.addClass('expand');
                }
            };
            this._collapseAnimateOpt = {
                duration: 500,
                queue: false
            };
        },
        toggle: function () {
            if (this.el.hasClass('expand'))
            {
                this.el.removeClass('expand');
                this.el.animate(this._collapseAnimate, this._collapseAnimateOpt).css('overflow','visible');
            }
            else
            {
                this.el.animate(this._expandAnimate, this._expandAnimateOpt).css('overflow','visible');
            }
        }
    });

    $A.ProductDetailsView = Backbone.View.extend({
        initialize: function () {
            new $A.Tabular({ el: $('.e-tabular') });
        }
    });
    $A.Tabular = Backbone.View.extend({
        initialize: function () {
            var $this = this;
            var lis = this.$('> li');
            lis.each(function () {
                var li = $(this);
                li.find('a.tab').click(function () {
                    lis.removeClass('active');
                    li.addClass('active');
                });
            });
        }
    });

    $A.ProductsView = Backbone.View.extend({
        initialize: function () {
            new $A.DropdownLinks({ el: $('.e-dropdownlinks') });
            new $A.ViewSelector({ el: $('.e-viewselector') });
        }
    });
    $A.ViewSelector = Backbone.View.extend({
        events: {
            'click a.grid': 'grid',
            'click a.list': 'list'
        },
        grid: function () {
            //$('#content').removeClass('view-list').addClass('view-grid');
            $.cookie("frontendListView", null, { path: "/" });
            window.location = window.location;
        },
        list: function () {
            //$('#content').removeClass('view-grid').addClass('view-list');
            $.cookie("frontendListView", "true", { path: "/" });
            window.location = window.location;
        }
    });
    $A.DropdownLinks = Backbone.View.extend({
        initialize: function () { var $this = this;
            this._list = this.$('ul');
            this._height = this._list
                .css('visibility', 'none')
                .show()
                .height();
            this._list.height(0);

            var animateOpt = {
                duration: 400,
                queue: false
            }
            var showAnimate = {height:this._height};
            var hideAnimate = {height:0};

            this.el.click(function() {
                if ($this._list.height() > 0)
                    $this._list.animate(hideAnimate, animateOpt);
                else
                    $this._list.animate(showAnimate, animateOpt);
            });
        }
    });

    $A.HomeView = Backbone.View.extend({
        initialize: function () {
            var p1 = $('#promo1 .promo-background');
            var p2 = $('#promo2 .promo-background');
            var p3 = $('#promo3 .promo-background');

            var p1t, p2t, p3t;
            if (!$A.IE) {
                var p1c = p1.find('.promo-color-background, .promo-text');
                var p2c = p2.find('.promo-color-background, .promo-text');
                var p3c = p3.find('.promo-color-background, .promo-text');
            } else {
                var p1c = p1.find('.promo-color-background, .promo-text a, .promo-text h3, .promo-icon, hr');
                var p2c = p2.find('.promo-color-background, .promo-text a, .promo-text h3, .promo-icon, hr');
                var p3c = p3.find('.promo-color-background, .promo-text a, .promo-text h3, .promo-icon, hr');
            }


            var animateOpt = { duration: 300, queue: false };

            p1.hover(function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p1.animate({ width: '350px', left: 0, right: 60 }, animateOpt).css('overflow', 'visible');
                p2.animate({ width: '260px', left: 60, right: 30 }, animateOpt).css('overflow', 'visible');
                p3.animate({ width: '260px', left: 30, right: 0 }, animateOpt).css('overflow', 'visible');

                p1c.animate({ opacity: 1 }, animateOpt);
                p2c.animate({ opacity: 0 }, animateOpt);
                p3c.animate({ opacity: 0 }, animateOpt);
            }, function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p1.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p2.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p3.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');

                p1c.animate({ opacity: 1 }, animateOpt);
                p2c.animate({ opacity: 1 }, animateOpt);
                p3c.animate({ opacity: 1 }, animateOpt);
            });

            p2.hover(function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p2.animate({ width: '330px', left: -20, right: 20 }, animateOpt).css('overflow', 'visible');
                p1.animate({ width: '270px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p3.animate({ width: '270px', left: 20, right: 0 }, animateOpt).css('overflow', 'visible');

                p2c.animate({ opacity: 1 }, animateOpt);
                p1c.animate({ opacity: 0 }, animateOpt);
                p3c.animate({ opacity: 0 }, animateOpt);
            }, function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p2.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p1.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p3.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');

                p2c.animate({ opacity: 1 }, animateOpt);
                p1c.animate({ opacity: 1 }, animateOpt);
                p3c.animate({ opacity: 1 }, animateOpt);
            });

            p3.hover(function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p3.animate({ width: '350px', left: -60, right: 0 }, animateOpt).css('overflow', 'visible');
                p1.animate({ width: '260px', left: 0, right: -60 }, animateOpt).css('overflow', 'visible');
                p2.animate({ width: '260px', left: -30, right: 0 }, animateOpt).css('overflow', 'visible');

                p3c.animate({ opacity: 1 }, animateOpt);
                p1c.animate({ opacity: 0 }, animateOpt);
                p2c.animate({ opacity: 0 }, animateOpt);
            }, function () {
                p1.stop(); p1c.stop();
                p2.stop(); p2c.stop();
                p3.stop(); p3c.stop();

                p3.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p1.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');
                p2.animate({ width: '290px', left: 0, right: 0 }, animateOpt).css('overflow', 'visible');

                p3c.animate({ opacity: 1 }, animateOpt);
                p1c.animate({ opacity: 1 }, animateOpt);
                p2c.animate({ opacity: 1 }, animateOpt);
            });

            if ($A.IEO) {
                /*var header = $('#content-header h1').each(function(i) {
                    $(this).css('opacity', 0)
                        .css('visibility', 'visible')
                        .animate({opacity:1}, {duration:1000});
                });*/
            } else {
                var header = $('#content-header h1 > *').each(function(i) {
                    var t = $(this);
                    setTimeout(function() {
                         t.css('opacity', 0)
                            .css('visibility', 'visible')
                            .animate({opacity:1}, {duration:1000});
                    }, 50*i);
                });
            }
        }
    });

    new $A.MainView;

})(jQuery);

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var dataReady = function () { };
$(document).ready(function () {
	/* data callback */
	dataReady = function (data) {/*
		if (0 != data.length) {
			$.each(data, function () {
				if ("undefined" != typeof (console)) console.log(this["Product Name"]);
				var output = "<li class=\"output_text\">"
				$("#product_list").append(output + this["Product Name"] + "</li>");
			});

			if ("undefined" != typeof (console)) {
			console.log("called");
			console.log(data["0"]["Product Name"]);
			}

		}*/
	}
});

