var filter = 'all';
var category = 'ALL';

$(document).ready(function(){
	$('#gallery img').mouseover(function(){
		if ($(this).hasClass('disabled')) return;
		var color = '#fac304';
		var width = parseInt($(this).width(), 10);
		var height = parseInt($(this).height(), 10);
		if ($.browser.msie) {
			width += 3;
			height += 3;
		}
		var border = 3;
		if ($(this).attr('hasBr') == undefined) {
			var over = '<div class="gallery_over" rel="'+$(this).attr('rel')+'" style="width: '+width+'px; height: '+height+'px; margin-top: -'+height+'px;">';
		} else {
			var over = '<div class="gallery_over" rel="'+$(this).attr('rel')+'" style="width: '+width+'px; height: '+height+'px; margin-top: -'+(height*2)+'px;">';
		}
		over += '<div style="background-color: '+color+'; width: '+border+'px; height: '+height+'px; position: absolute;"></div>';
		over += '<div style="background-color: '+color+'; width: '+border+'px; height: '+height+'px; position: absolute; margin-left: '+(width-border)+'px"></div>';
		over += '<div style="background-color: '+color+'; width: '+width+'px; height: '+border+'px; position: absolute;"></div>';
		over += '<div style="background-color: '+color+'; width: '+width+'px; height: '+border+'px; position: absolute; margin-top: '+(height-border)+'px"></div>';
		over += '</div>';
		$(this).parent().append(over);
	});
	$('.gallery_over').live('mouseleave', function(){
		$(this).remove();
	});
	$('.gallery_over').live('mouseout', function(){
		$(this).remove();
	});
	$('.gallery_over').live('click', function(){
		var title = $('#'+$(this).attr('rel')+'_title').html();
		var source = $('#'+$(this).attr('rel')+'_source').html();
		var type = $('#'+$(this).attr('rel')+'_type').html();
		var description = $('#'+$(this).attr('rel')+'_description').html();
		switch (type) {
			case 'video':
				showVideo(source, description, title);
				break;
			case 'image':
				showImage(source, description, title);
				break;
		}
	});
	$('.filter_radio').click(setFilter);
	$('.category_radio').click(setCategory);
	setScrollbar();
});

function inspiration() {
	$('#inspiration').fadeIn();
	$('.filter_radio').removeClass('filter_radio_selected');
	$('#reset_filter').addClass('filter_radio_selected');
	$('.category_radio').removeClass('category_radio_selected');
	$('#reset_category').addClass('category_radio_selected');
	$('#gallery img').css({opacity: 1}).removeClass('disabled');
	filter = 'all';
	category = 'ALL';
}

function setScrollbar() {
	
	$('#scroller').draggable({ containment: "parent", scroll: false, axis: 'x' });
	$('#scroller').mousedown(function(){ $(this).stop(); });
	$('#scroller').bind("drag", function(event, ui) {
		var left = 0;
		var differenza = parseInt($('#gallery_table').width(),10) + 2 - parseInt($(window).width(),10);
		left = parseInt($(this).css('left'),10) * differenza / (parseInt($('#scrollbar').width(),10) - parseInt($('#scroller').width(),10));
		$('#gallery').css({marginLeft: '-'+left+'px'});
	});
	
	if (isiPad()) {
		$('#scroller').live("touchstart", function(event){
			if (event.originalEvent.targetTouches.length != 1) return false;
			this.startX = event.originalEvent.targetTouches[0].clientX;
		})
		$('#scroller').live("touchmove", function(event){
			
			event.originalEvent.preventDefault();
			
			var scroller_left = event.originalEvent.targetTouches[0].pageX - this.startX;
			if (scroller_left < 0) scroller_left = 0;
			if (scroller_left > parseInt($('#scrollbar').width(),10) - parseInt($('#scroller').width(),10)) scroller_left = parseInt($('#scrollbar').width(),10) - parseInt($('#scroller').width(),10);
			$(this).css({left: scroller_left});
			
			var left = 0;
			var differenza = parseInt($('#gallery_table').width(),10) + 2 - parseInt($(window).width(),10);
			left = parseInt($(this).css('left'),10) * differenza / (parseInt($('#scrollbar').width(),10) - parseInt($('#scroller').width(),10));
			$('#gallery').css({marginLeft: '-'+left+'px'});
			
		});
		$('#scroller').live("touchend", function(event){
			var left = 0;
			var differenza = parseInt($('#gallery_table').width(),10) + 2 - parseInt($(window).width(),10);
			left = parseInt($(this).css('left'),10) * differenza / (parseInt($('#scrollbar').width(),10) - parseInt($('#scroller').width(),10));
			$('#gallery').css({marginLeft: '-'+left+'px'});
		});
	}
	
}

function setFilter() {
	if (filter != $(this).attr('type')) {
		filter = $(this).attr('type');
		$('#gallery img').each(function(){
			var type = $('#'+$(this).attr('rel')+'_type').text();
			var cat = $('#'+$(this).attr('rel')+'_category').text();
			if (type != filter && filter != 'all') {
				$(this).css({opacity: .1}).addClass('disabled');
			} else {
				if (cat != category && category != 'ALL') {
					$(this).css({opacity: .1}).addClass('disabled');
				} else {
					$(this).css({opacity: 1}).removeClass('disabled');
				}
			}
		});
		$('.filter_radio').removeClass('filter_radio_selected');
		$(this).addClass('filter_radio_selected');
	}
}

function setCategory() {
	if (category != $(this).attr('category')) {
		category = $(this).attr('category');
		$('#gallery img').each(function(){
			var type = $('#'+$(this).attr('rel')+'_type').text();
			var cat = $('#'+$(this).attr('rel')+'_category').text();
			if (type != filter && filter != 'all') {
				$(this).css({opacity: .1}).addClass('disabled');
			} else {
				if (cat != category && category != 'ALL') {
					$(this).css({opacity: .1}).addClass('disabled');
				} else {
					$(this).css({opacity: 1}).removeClass('disabled');
				}
			}
		});
		$('.category_radio').removeClass('category_radio_selected');
		$(this).addClass('category_radio_selected');
	}
}
