$(document).ready(function(){
	//Start jQuery
	$('div.selectOptions').hide();
	
	$('div.selectContainer').click(function() {
		//Click dropdown
		$(this).children(".selectOptions").slideDown();
	});
	
	$('div.selectContainer').mouseleave(function() {
		$(this).children(".selectOptions").slideUp();
	});
	
	//selectOptions selectCheckbox
	$('div.selectCheckbox div, div.selectOptions div').hover(function() {
		//highlight div
		$(this).addClass('selectHighlight');
	}, function() {
		//unhighlight div
		$(this).removeClass('selectHighlight');
	});
	
	$('div.selectCheckbox div, div.selectOptionsList div').hover(function() {
		//highlight div
		$(this).addClass('selectHighlight');
	}, function() {
		//unhighlight div
		$(this).removeClass('selectHighlight');
	});
	
	//Exhibitions
	$('div#selectFirst div.dropselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "first", true );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "first", true );
		}
	});
	
	$('div#selectSecond div.dropselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "second", true );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "second", true );
		}
	});
	
	$('div#selectThird div.dropselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "third", true );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "third", true );
		}
	});
	
	//Cases
	$('div#selectFirst div.listselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "first", false );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "first", false );
		}
	});
	
	$('div#selectSecond div.listselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "second", false );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "second", false );
		}
	});
	
	$('div#selectThird div.listselector').click(function() {
		//check/uncheck checkbox
		var checkbox = $(this).children(':checkbox');
		if( checkbox.is(':checked') ) {
			checkbox.attr('checked', false);
			sortItems( $(this), "third", false );
		} else {
			checkbox.attr('checked', true);
			sortItems( $(this), "third", false );
		}
	});
});

var sortPager = false;

function sortItems( target, type, dropdown ) {
	if(dropdown) {
		target.children(".custom_checkbox").toggleClass("checked");
	} else {
		target.toggleClass("selected");
	}
	
	var classes = target.attr('class').split(" ");
	var selected_class = "";
	for(var i=0;i<classes.length;i++) {
		if( classes[i].substr(0, 4) == "tag_" ) {
			var selected_class = classes[i];
			break;
		}
	}
	if(type == "third") {
		$('div#sortingHolderThird').toggleClass(selected_class);
		location.hash = '#page=1';
	} else if(type == "second") {
		$('div#sortingHolderSecond').toggleClass(selected_class);
		location.hash = '#page=1';
	} else if(type == "first") {
		$('div#sortingHolderFirst').toggleClass(selected_class);
		location.hash = '#page=1';
	}
	
	showPager();
};

function showPager() {
	
	var filterFirst = $('div#sortingHolderFirst').attr('class').replace(/ /g, ", ").replace(/tag_/g, ".tag_");
	
	var filterSecond = $('div#sortingHolderSecond').attr('class').replace(/ /g, ", ").replace(/tag_/g, ".tag_");
	
	var filterThird = $('div#sortingHolderThird').attr('class').replace(/ /g, ", ").replace(/tag_/g, ".tag_");
	
	var sortItems = $("div.sortingItem");
	
	sortItems.hide();
	
	if(filterFirst != "") {
		sortItems = sortItems.filter(filterFirst);
	}
	
	if(filterSecond != "") {
		sortItems = sortItems.filter(filterSecond);
	}
	
	if(filterThird != "") {
		sortItems = sortItems.filter(filterThird);
	}
	
	sortItems.show();
	
	if( sortItems.length == 0 ) {
		$('.filter_error').show();
		
	} else {
		$('.filter_error').hide();
		
	}
	
	if(!sortPager || sortItems.length == 0 ) {
		$("#casePager").html( "" );
		return;
	}
	//var sortItems = $("div.sortingItem:visible");
	
	sortItems.hide();
	var hash = location.hash.replace('#', '');
	var hashArray = new Array();
	var page = 1;
	
	hashArray = hash.split("&");
	
	for(var i=0;i<hashArray.length;i++) {
		if( hashArray[i].indexOf('page=') > -1 ) {
			page = parseInt( hashArray[i].substr(5) );
			break;
		}
	}
	
	for(var i = ((page-1)*5);i<(page*5);i++) {
		$(sortItems[i]).show();
	}
	
	var pagerString = "";
	
	if( page > 1 ) {
		pagerString += '<a href="#page='+(page-1)+'">&laquo;</a>';
	} else {
		pagerString += '&laquo; ';
	}
	
	for(var i = 0;i<Math.ceil(sortItems.length/5);i++) {
		//pagerString += ' <a href="#page='+(i+1)+'">'+(i+1)+'</a>';
		//pagerString += ' <a href="#page='+(i+1)+'" '+((i==page-1)?'class="currentPage"':"")+'>'+(i+1)+'</a>';
		
		pagerString += (i==page-1)?' '+(i+1):' <a href="#page='+(i+1)+'">'+(i+1)+'</a>';
	}
	
	if( page < Math.ceil(sortItems.length/5) ) {
		pagerString += ' <a href="#page='+(page+1)+'">&raquo;</a>';
	} else {
		pagerString += ' &raquo;';
	}
	
	$("#casePager").html( pagerString );
	
}

function submitEvent(id) {
	$("#eventHtml").val( $("#event_"+id).html() );
	$("#eventID").val( id );
	$("#eventForm").submit();
};

$(function(){
    $.extend($.fn.disableTextSelect = function() {
        return this.each(function(){
            if($.browser.mozilla){//Firefox
                $(this).css('MozUserSelect','none');
            }else if($.browser.msie){//IE
                $(this).bind('selectstart',function(){return false;});
            }else{//Opera, etc.
                $(this).mousedown(function(){return false;});
            }
        });
    });
    $('#selectFirst').disableTextSelect();//No text selection on elements with a class of 'noSelect'
	$('#selectSecond').disableTextSelect();//No text selection on elements with a class of 'noSelect'
	$('#selectThird').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});
