var no_of_pages = 1;
var page_size = 10;

// Run when the page has loaded
$('document').ready(function() {
	
	var model_base = {
		'titleShow' : false,
		'speedIn'		: 500,
		'speedOut'		:	200,
		'overlayShow' : true,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000',
		'type' : 'iframe',
		'autoDimensions' : false,
		'width' : 480,
		'height' : 250,
		'showCloseButton' : true
	}
	
	
	// Tall modal conf
	var modal_tall = $.extend({}, model_base);
	modal_tall.height = 550;
	
	$("a.general_modal").fancybox(model_base);
	$("a.tall_modal").fancybox(modal_tall);

	// Close fancybox
	$('a[href=#close_fb]').click(function() {
		parent.$.fancybox.close();
	});
	
	// Print this window
	$('a.print').click(function() {
		window.print();
		return false;
	});
	
	// Confirmation message
	$('a.confirm').click(function() {
		
		var message = "Are you sure?";

		if ($(this).data('confirm') != undefined && $(this).data('confirm') != '') {
			message = $(this).data('confirm');
		}
		
		return confirm(message);
		
	});

	// Table zebra
	$('table.item_list').each(function() {
		$(this).find('tr:odd').addClass('even');
	});
	
	$('a[href=#back]').click(function() {
		history.go(-1);
		return false;
	});
	
	
	// Search update trigger
	$('.ws_form select').live('change', update_saerch);
	
	// Running the initial search on page load
	// Also fixes issues with refreshing
	update_saerch();
	
	
	$('.hz_scroll_pager a:first').parent().addClass('active');
	
	$('.hz_scroll_pager a').click(function() {
		
		// Make appropriate li active
		$(this).parent().addClass('active');
		$(this).parent().siblings().removeClass('active');
		
		// Determine which item was clicked on
		var item = $(this).parent().prevAll().length;
		
		jump_to_hz_item(item);
		
		return false;
		
	});
	
	
	// Default - do nothing when reset is clicked
	
	$('a[href=#reset_search]').css('cursor', 'default');
	
	$('a[href=#reset_search]').live('click', function() {
		return false;
	});
	
	
	// Prevent fader images from flashing when page loads
	$('.fader').find('img:gt(0)').hide();
	
});


function update_saerch() {

	var post_vals = $('.ws_form').serializeArray();

	$('#content_search').load(
		$('.ws_form').attr('action') + ' #content_search',
		post_vals,
		function() {
			
			$('a[href=#reset_search]').fadeTo(0, 0.3);
			
			$('.ws_form select').each(function() {
				if ($(this).val() != '0') {
				
					$('a[href=#reset_search]').css('cursor', 'pointer');
					
					$('a[href=#reset_search]').click(function() {

						$('.ws_form select').val('0');
						update_saerch();

						return false;

					});
				
					$('a[href=#reset_search]').fadeTo(0, 1);
				}
			});
			
			
			no_of_pages = Math.ceil($('ul.search_results li').length / page_size);
			
			show_page_navigation();
			
		},
		{ type: 'POST' }
	);
	
}

function show_page_navigation() {
	
	$('div.pagination').html('<ul class="hz menu"></ul>');
	
	for (a=1; a <= no_of_pages; a++) {
		$('div.pagination ul').append('<li><a href="#jump_to" data-page-no="' + a + '">' + a + '</a></li>');
	}
	
	$('div.pagination a').click(function() {
		jump_to_page($(this).data('page-no'));
		return false;
	});
	
	$('div.pagination ul').append('<li><a href="#show_all">All</a></li>');
	$('a[href=#show_all]').click(function() {
		show_all();
		return false;
	});
	
	$('div.pagination li a').click(function() {
		
		var nav_index = $(this).parents('div.pagination').find('li').index($(this).parent());
		
		$('div.pagination li a').removeClass('active');
		
		$('div.pagination').each(function() {
			$(this).find('li').eq(nav_index).find('a').addClass('active');
		});
		
	});
	
	$('div.pagination').append('<p></p>');
	
	$('div.pagination:eq(0) li').eq(0).find('a').click();
	
}

function jump_to_page(page_no) {
	
	$('ul.search_results li').hide();
	
	var start = (page_no - 1) * page_size;
	var end = start + page_size;
	
	$('ul.search_results li').slice(start, end).show();
	
	var last_item = start + page_size;
	if (last_item > $('ul.search_results li').length) last_item = $('ul.search_results li').length;
	
	$('div.pagination p').html('Showing results <strong>' + (start + 1) + '</strong> to <strong>' + last_item + '</strong> of <strong>' + $('ul.search_results li').length + '</strong>');
	
}

function show_all() {
	$('ul.search_results li').show();
	$('div.pagination p').html('Showing all <strong>' + $('ul.search_results li').length + '</strong> results')
}


// Image Fader
$(window).load(function () {

	// Only run if there's more than 1 news item
	$('.fader').each(function() {

		var max_img_height = 0;
		$(this).find('img').each(function() {
			if ($(this).height() > max_img_height) {
				max_img_height = $(this).height();
			}
		});

		$(this).height(max_img_height);

		var cur_fader = this;

		if ($(this).find('img').length > 1) {
	
			$(this).find('img:eq(0)').addClass('current');
			$(this).find('img:gt(0)').hide();
	
			setInterval(function() {
				fade_to_next_photo(cur_fader);
			}, 6000);
		}
	});

});




function jump_to_hz_item(item_number) {
	
	$('.hz_scroll_panel').stop();
	
	var offset = 0;

	$('.hz_scroll_panel > .item:lt(' + item_number + ')').each(function() {
		offset += $(this).width() + 20;
	});
	
	$('.hz_scroll_panel').animate({
		left: offset * -1
	}, 500, 'swing');
	
}

function fade_to_next_photo(fader_ref) {
	
	mark_current_fader_photo(fader_ref);

	$(fader_ref).find('img').not('.current').fadeOut(1000);

	$(fader_ref).find('img.current').fadeIn(1000);
	
}

function mark_current_fader_photo(fader_ref) {
	$(fader_ref).find('img').not(get_next_fader_photo(fader_ref).addClass('current')).removeClass('current');
}

function get_next_fader_photo(fader_ref) {
	
	// Determine next item
	var next_photo = $(fader_ref).find('img.current:last').next();
	
	// Defatult to first item if no items are selected
	if (next_photo.length < 1) next_photo = $(fader_ref).find('img:first');
	
	return next_photo;
}

