(function ($) {
	$.extend(jQuery.browser, {
		ipad : navigator.userAgent.toLowerCase().indexOf('ipad') > -1
	});
})(jQuery);

var App = function () {}

App.prototype = {
	
	getView: function (opts) {
		var margins = $.extend({
				top: 0,
				bottom: 0,
				left: 0,
				right: 0,
				maxWidth: 0,
				maxHeight: 0
			}, (opts || {})),
			view = {
				width: $(window).width() - (margins.left + margins.right),
				height: $(window).height() - (margins.top + margins.bottom)
			};
			
		if (margins.maxWidth > 0 && view.width > margins.maxWidth) {
			view.width = margins.maxWidth;
		}
		if (margins.maxHeight > 0 && view.height > margins.maxHeight) {
			view.height = margins.maxHeight;
		}
		
		return view;
	},
	
	resizeThumbs: function () {
		var w = this.getView({ left: 30, right: 30, top: 70, bottom: 70, maxWidth: 997 }),
			gallery = this.getView({ top: 70, bottom: 70, maxWidth: 997 });
			
		$('#scrollable').css('height', w.height);
		
		if ( $('#gallery-pictures').height() < gallery.height) {
			/*$('#gallery-pictures').css({
				top: '50%',
				left: '50%',
				marginLeft: -($('#gallery-pictures').width() / 2),
				marginTop: -($('#gallery-pictures').height() / 2)
			});*/
		}
		
		$('#page.gallery').css('height', gallery.height);
		
		if ( w.width < 684 ) {
			w.width = 684;
		}
		
		$('header, #page, footer').css('width', w.width);
		
		// pages
		$('.column').not('.small').css('width', (w.width - 150) / 4);
	},
	
	initHover: function () {
		if ( $('#gallery-pictures').length ) {
			$('#gallery-pictures a').hover(function () {
				$(this).find('img').css('opacity', 0.4);
			}, function () {
				$(this).find('img').css('opacity', 1);
			});
			
			$('.video').each(function () {
				$('<div class="play-button">').appendTo($(this));
			});
		}
	},
	
	fullscreen: function () {
		var v = this.getView({ left: 100, right: 100, top: 50, bottom: 50 });
		
		$('#fullscreen').css({
			width: v.width,
			height: v.height,
			marginTop: -(v.height / 2),
			marginLeft: -(v.width / 2)
		});
		
		var $img = $('#fullscreen img:not(#poster), #fplayer');
		if ( $img.length ) {
			if ( ! $img.data('o') ) {
				$img.data('o', {
					width: $img.width(),
					height: $img.height()
				});
			}
			var i = $img.data('o'),
				h = v.height,
				w = v.height * (i.width / i.height);
			
			if ( w > $('#fullscreen').width() ) {
				w = $('#fullscreen').width();
			}
			if ( w > i.width ) {
				w = i.width;
			}
			h = w * (i.height / i.width);
			
			if ( h > i.height ) {
				h = i.height;
				w = h * (i.width / i.height);
			}
			
			if ( $img.is('img') ) {
				$img.attr({
					height: h,
					width: w
				});
				if ( ! $('#onimgPrev, #onimgNext').length ) {
					$('<a id="onimgNext">').attr('href', $('#next').attr('href')).insertAfter($img);
					$('<a id="onimgPrev">').attr('href', $('#prev').attr('href')).insertAfter($img);
				}
				$('#onimgPrev, #onimgNext').css({
					height: h,
					width: w / 2,
					marginTop: -(h / 2),
					marginLeft: -(w / 2)
				});
				$('#onimgNext').css('marginLeft', 0);
			} else {
				$img.css({
					width: w,
					height: h
				});
			}
			
			$img.css({
				marginTop: -(h / 2),
				marginLeft: -(w / 2)
			});
		}
		
		$('.title').css('marginTop', h + $img[0].offsetTop + 7);
		
		$('#close').css({
			marginTop: $img[0].offsetTop,
			marginLeft: (w / 2) + 2
		});
	},
	
	filter: function () {
		if ( $('#filter').length ) {
			var stored = $.cookie('filter'),
				$filter = $('#filter');
			if ( $('.filter').length == 0 ) {
				return false;
			}
			
			if ( ! stored ) {
				$('.filter').addClass('active');
			} else {
				var checked = stored.split('|');
				$('.filter', $filter).each(function () {
					if ( $.inArray(this.className.split(' ').pop(), checked) != -1 ) {
						$(this).addClass('active');
					}
				});
			}
			
			function checkFilter() {
				var $active = $('.filter.active'),
					cookie = [];
				$active.each(function () {
					cookie.push(this.className.replace('filter ', '').replace(' active', ''));
				});
				
				$.cookie('filter', cookie.join('|'));
				
				$('#gallery-pictures a').each(function () {
					var classes = this.className.split(' '),
						$img = $(this);
					$.each(classes, function (i, c) {
						if ($.inArray(c, cookie) != -1 || c == '') {
							$img.show();
							return false;
						}
						$img.hide();
					});
				});
			}
			
			$('.filter').click(function () {
				$(this).toggleClass('active');
				checkFilter();
			});
			checkFilter();
		}
	}
	
};

var app = new App();

$(function () {
	app.resizeThumbs();
	app.initHover();
	app.filter();
});
$(window).resize(function () {
	app.resizeThumbs();
});



/**
 * 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;
};



