﻿(function ($) {
    $(document).ready(function () {
        $('.stackme').addClass('stacked');
        $('.selects').click(function (ev) {
            $Im.addClass($(this), 'sticky');
            ev.stopPropagation();
        }).mouseover(function (ev) {
            $Im.addClass($(this), 'active');
            ev.stopPropagation();
        }).mouseout(function (ev) {
            $Im.clearClass($(this), 'active');
            ev.stopPropagation();
        });
        $('.photos .viewer').append('<div class="handle" />');
        $('.viewer a').fancybox({'titlePosition' : 'over', 'titleFormat' : $Im.titleImage});
    });
})(jQuery);

(function() {

    if (!window.ARR) window.ARR = {};

    ARR.Images = {
    
		addClass : function (tg, cl) {
			var pa = tg.closest('.stacked'), im = $('#' + tg.attr('id') + '-image');
			if (im && !tg.hasClass(cl)) {
				if (pa) pa.find('.' + cl).removeClass(cl);
				im.addClass(cl);
				tg.addClass(cl);
			}
		},
		
		clearClass : function (tg, cl) {
			if (tg.hasClass(cl)) {
				tg.removeClass(cl);
				window.setTimeout(function() {$Im.resetClass(tg, cl);}, 750);
			}
		},
		
		resetClass : function (tg, cl) {
			var im = $('#' + tg.attr('id') + '-image');
			if (tg.hasClass(cl))
				im.addClass(cl);
			else
				im.removeClass(cl);
		},
		
		titleImage : function (ti, ca, ci, co) {
			var pa = $(ca[ci]).closest('.drawer'), dl = pa.prevAll('.drawer');
			var pg = $('#' + $(ca[ci]).parent().attr('id').replace('image', 'texts')).html();
			var h2 = pa.find('h2'), h3 = pa.find('h3'), fl = pa.hasClass('chained'), tx = [];
			
			if (pg) {
				for (var i = 0, x = dl.length; i < x && h2.length == 0; i++) {
					if (fl) {
						if (h3.length == 0) h3 = $(dl[i]).find('h3');
						fl = $(dl[i]).hasClass('chained');
					}
					if (h2.length == 0) h2 = $(dl[i]).find('h2');
				}
				tx[0] = h2.text();
				tx[1] = h3.text();
				tx[(tx[1].length ? 2 : 1)] = pg;
			}
			else
				tx[0] = ti;
			
			return	'<span id="fancybox-title-over">Image ' + (ci + 1) + ' / ' + ca.length +
					(tx.length > 2 || tx[0].length || tx[tx.length - 1].length ? ' - ' : '') +
					tx.slice((tx[0].length ? 0 : 1), (tx[tx.length - 1].length ? tx.length + 1 : -1)).join(' - ') +
					'</span>';
		}

    };

    window.$Im = window.ARR.Images;

})();


(function() {

    if (!window.ARR) window.ARR = {};

    ARR.Cookie = {
  
        create : function (nm, vl, dy) {
            var dt, ex;
            if (dy) {
                dt = new Date();
                dt.setTime(dt.getTime() + (dy * 24 * 60 * 60 * 1000));
                ex = "; expires=" + dt.toGMTString();
            }
            else
                ex = "";
            document.cookie = nm + "=" + vl + ex + "; path=/";
        },

        read : function (nm) {
            var eq = nm + "=", ca = document.cookie.split(';');
            for (var cx, i = 0; i < ca.length; i++) {
                cx = ca[i];
                while (cx.charAt(0) == ' ') cx = cx.substring(1, cx.length);
                if (cx.indexOf(eq) == 0) return cx.substring(eq.length, cx.length);
            }
            return null;
        },

        erase : function (nm) {
            $Ck.create(nm, "", -1);
        }

    };

    window.$Ck = window.ARR.Cookie;

})();


