// Initialize
$(document).ready(function() {
	var checked_items = new Array();
		
	$('.add_to_cart_toggle').click(function() {		
		if(checked_items.length == 0) {
			return false;
		}
		else {
			return true;
		}
	});
	
	$('.add_to_lightbox_button').click(function() {		
		if(checked_items.length == 0) {
			return false;
		}
		else {
			return true;
		}
	});
	
	// Pop Window
	$('.popup').click(function() {
		var window_width = 800;
		var window_height = 850;
		var left = (screen.width - window_width) / 2;
		var top = (screen.height - window_height) / 2;
		
		var iLivePopup = window.open(this.href,'','width='+ window_width +',height=' + window_height +',resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,location=no,screenX='+ left +',screenY='+ top +',left='+ left +',top='+ top);
		return false;
	});
	
	// Pop Window
	$('.popup_iLive').click(function() {
		var window_width = 980;
		var window_height = 740;
		var left = (screen.width - window_width) / 2;
		var top = (screen.height - window_height) / 2;
		
		window.open(this.href,'','width='+ window_width +',height=' + window_height +',resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,location=no,screenx='+ left +',screeny='+ top +',left='+ left +',top='+ top);
		return false;
	});
	
	$("#lightbox_quicklink_imagesLive").click(function() {
		var window_width = 960;
		var window_height = 740;
		var left = (screen.width - window_width) / 2;
		var top = (screen.height - window_height) / 2;
		
		var selected_id = $("#lightboxes").find('option').filter(':selected').attr('ref');
			
		var url = "http://images.slatterymedia.com/imagesLive/lightbox/" + selected_id;
				
		window.open(url,'','width='+ window_width +',height=' + window_height +',resizable=yes,scrollbars=yes,menubar=yes,toolbar=no,location=no,screenx='+ left +',screeny='+ top +',left='+ left +',top='+ top);
		return false;
	});
	
	// Ajax Bindings
	$('.binding').each(function() {
		var action_name = "binding" + $(this).attr('id');
		$(this).load('index.php', { ajax: '1', action: action_name, app: 'gspimages_search' });
	});
	// Image check boxes
	$('.image_check').attr('disabled', false);
	
	$('.image_check').click(function() {
		if($(this).attr('checked')) {
			checked_items.push($(this).val());
		}
		else {
			checked_items.pop();
		}
		if(checked_items.length == 0) {
			// disable buttons
			$('.add_to_cart_toggle').addClass('disabled');
			$(".add_to_lightbox_button_span").addClass("disabled");
		}
		else {
			// enable buttons
			$('.add_to_cart_toggle').removeClass('disabled');
			$(".add_to_lightbox_button_span").removeClass("disabled");
		}
	});
	
	$('.show_advanced_search').click(function() {
		var current_src = $('.show_advanced_search_icon').attr('src');
		if($("#advanced_search").is(":hidden")) {
			$('#advanced_search').show();
			$('.show_advanced_search').html('Hide options');
			$('.show_advanced_search_icon').attr('src', current_src.replace('bullet_down', 'bullet_up'));
		}
		else {
			$('#advanced_search').hide();
			$('.show_advanced_search').html('More options');
			$('.show_advanced_search_icon').attr('src', current_src.replace('bullet_up', 'bullet_down'));
		}
		//$('#advanced_search').toggle();
	});
	
	$('#more_event a').click(function() {
		if($("#more_event_item").is(":hidden")) {
			$('#more_event_item').fadeIn(500);
			$('#more_event a').html('Fewer Events');
		}
		else {
			$('#more_event_item').hide();
			$('#more_event a').html('More Events');
			$.scrollTo($('#results'), { speed:800, easing:'easeOutSine' });
		}
		return false;
	});

	$('#people_show_popular').click(function() {
		$('#people_popular').fadeIn(500);
		$('#people_sorted').hide();	
		return false;
	});
	$('#people_show_sorted').click(function() {
		$('#people_sorted').fadeIn(500);	
		$('#people_popular').hide();
		return false;
	});

	$('a.back_to_top').click(function() {
		$.scrollTo($('#people_section'), { speed:800, easing:'easeOutSine', offset: -30 });
	});
	
	$('.confirm_terms').click(function() {
		var result = confirm('Click ok to agree to the terms and conditions of use. Click cancel to view the terms and conditions.');
		if(result) {
			return true;
		}
		else {
			window.location.href = "http://images.slatterymedia.com/terms/";
			return false;
		}
	});
	
	$(".external").attr("target", "_blank");
	
	if ($(".tooltip").length > 0) {
		tooltip();
	}
	
	// lightbox panel, fixed at bottom of page, displayed only on certain pages. see footer.php
	if ($("#lightbox_panel").length != 0)
		$("#lightbox_panel").css('display', 'block');
	
	// if we have something to tell the user about cart activity
	//if ($(".cartAlert").length > 0) {
	//	$(".cartAlert").fadeIn(300).fadeOut(600).fadeIn(600).fadeOut(600);
	//}	
		
	// $(document).pngFix();
	
	
});

// Functions
function setFixedToFluid() {
	$('.fixed').removeClass('fixed');
	$('#container').css("width", "100%"); /* needed to add this because IE6 can't handle absolutes in the header without a set width. */
	$(document).ready(function() {
		$('.fixed').removeClass('fixed');
		$('#container').css("width", "100%");
	});
}

function setResultCount(count) {
	if(!count) count = 'No';
	$('.result_count').html(count);
}

function setCurrentType(type) {
	if(!type) return;
	$('#type_'+ type).addClass('current');
}
