// BASKET

var basket = {
	
	init: function() {
		// bind to quantity input
		$('input.basketitemquantity').live('change', function() {
			basket.changequantity($('input[name*="productid"]', $(this).parent()).val(), $('input[name*="basketitemquantity"]', $(this).parent()).val(), $('input[name*="productoptions"]', $(this).parent()).val());
			return false;
		});
		// bind to remove product
		$('a.basketremoveitembutton').live('click', function() {
			basket.removeproduct($(this).attr('class').match(/productid\-.+?\b/).toString().replace('productid-', ''), $('input[name*="productoptions"]', $(this).parent()).val());
			return false;
		});
		// bind to donation button
		$('input#donationbutton').live('click', function() {
			basket.setdonation($('input#donationamount').val());
			return false;
		});
		// bind to donation button
		$('a#donationremove').live('click', function() {
			basket.setdonation(0);
			return false;
		});
		
	},
	
	additem: function(productid) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=additem' 
				+ '&productid=' + productid 
				+ '&quantity=' + '1',
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});			
	},
	
	removeproduct: function(productid, options) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=removeproduct' 
				+ '&productid=' + productid + '&options=' + options,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});	
	},
		
	changequantity: function(productid, quantity, options) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=changequantity' 
				+ '&productid=' + productid 
				+ '&quantity=' + quantity
				+ '&options=' + options,
			success: function(xml) {
				switch ($("status", xml).text()) {
					case '0':
						alert($("message", xml).text(), 'Error');
						break;
					case '1':
						var bodyhtml = $("body", xml).text();
						$('#basket').html(bodyhtml);
						var microbaskethtml = $("microbasket", xml).text();
						$('#microbasket').html(microbaskethtml);
						break;
					default:
						alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
						break;
				}
			},
			error: function(xml, type) {
				alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});	
	},
	
	setdeliverycountryid: function(deliverycountryid) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setdeliverycountryid&deliverycountryid=' + deliverycountryid,
			success: function(xml) {
				switch ($("status", xml).text()) {
				case '0':
					alert($("message", xml).text(), 'Error');
					break;
				case '1':
					var bodyhtml = $("body", xml).text();
					$('#basket').html(bodyhtml);
					break;
				default:
					alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
				break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
		
	},
	
	setdonation: function(amount) {
		$.ajax({
			url: '/ajax/basket.php',
			data: 'mode=setdonation&amount=' + amount,
			success: function(xml) {
				switch ($("status", xml).text()) {
				case '0':
					alert($("message", xml).text(), 'Error');
					break;
				case '1':
					var bodyhtml = $("body", xml).text();
					$('#basket').html(bodyhtml);
					break;
				default:
					alert('Unexpected Response: ' + $("message", xml).text(), 'Message');
				break;
				}
			},
			error: function(xml, type) {
				//alert('There was an error establishing a connection with the server, or the server did not respond as expected. Please check your internet connection and try again.', 'Communication Error (' + type + ')');
			}
		});
		
	}		
	
};

basket.init();	

$(function() {
	
	$("#control").bind('dblclick', function() {
		$("#control").fadeOut();
		return false;
	});
	
	$('a[href*=#]').click(function() {
		if($(this).parent().attr('class')=='scrollable_navigation' || $(this).attr('href').indexOf('javascript')>-1) return;
		var duration=800;
		var easing='swing';
		var newHash=this.hash;
		var oldLocation=window.location.href.replace(window.location.hash, '');
		var newLocation=this;
		if(oldLocation+newHash==newLocation) {
			var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;
			if(target > $(document).height()-$(window).height()) {
				target=$(document).height()-$(window).height();         
			}
			if($.browser.safari) {
				var animationSelector='body:not(:animated)';
			} else {
				var animationSelector='html:not(:animated)';
			}
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {
				window.location.href=newLocation;
			});
			return false;
		}
	});
	
	$('.clickable').click(function() {
		if ($('a', this).attr('target')) {
			window.open($('a', this).attr('href'), $('a', this).attr('target'));
		} else {
			location.href = $('a', this).attr('href');
		}
		return false;
	}).css('cursor', 'pointer');
	
	$('ul.featurenav li a').live('click mouseover', function() {
		if (!$(this).parent().hasClass('active')) { 
			var index = $(this).parent().index();
			$("ul.featurenav li").removeClass('active');
			$("ul.featurenav li").eq(index).addClass('active');
			$("ul.feature li").removeClass('active');
			$("ul.feature li").eq(index).addClass('active');
			$("ul.feature li:not(.active)").stop().animate({opacity:0});
			$("ul.feature li.active").stop().animate({opacity:1});
			$("ul.feature li").css('z-index', 1);
			$("ul.feature li").eq(index).css('z-index', 10);
		}
	});
	
	$('ul.featurenav li a').live('click', function() {
		clearInterval(featureInterval);	
		return false;
	});	
	
	if ($('ul.featurenav li a').length > 0) {

		var featureInterval = setInterval(function() {
			var featureItems = $("ul.feature li").length -1;
			var featureCurrent = $("ul.feature li").index($("ul.feature li.active"));
			if (featureCurrent == featureItems) {
				var featureNext = 0;
			} else {
				var featureNext = featureCurrent + 1;
			}
			$("ul.featurenav li").removeClass('active');
			$("ul.featurenav li").eq(featureNext).addClass('active');
			$("ul.feature li").removeClass('active');
			$("ul.feature li").eq(featureNext).addClass('active');
			$("ul.feature li:not(.active)").stop().animate({opacity:0});
			$("ul.feature li.active").stop().animate({opacity:1});
			$("ul.feature li").css('z-index', 1);
			$("ul.feature li").eq(featureNext).css('z-index', 10);
		}, 5000); 
	}
	
	$('div.widget.howtohelp > div.content > ul > li').each(function() {
		var contentid = $(this).attr('class').toString().replace(' active' ,'').replace('contentid-' ,'');
		$('> a', this).prepend('<img src="/template/theme/raystede/image/icon-contentid-' + contentid + '.png" alt="" />');
	});
	
	var hdslider = {
			
		element: false,
		items: false,
		itemcount: 0,
		currentitem: 0,
		itemwidth: 0,
		visibleitems: 1,
			
		init: function(element, visibleitems) {
			hdslider.element = $(element);
			hdslider.visibleitems = visibleitems;
			hdslider.items = $('div.items > ul > li', hdslider.element);
			hdslider.itemcount = hdslider.items.length;
			hdslider.itemwidth = $(hdslider.items[0]).outerWidth(true);
			
			$('a.previous', hdslider.element).live('click', function() {
				hdslider.previous();
				return false;
			});
			$('a.next', hdslider.element).live('click', function() {
				hdslider.next();
				return false;
			});
		},
	
		previous: function() {
			if (hdslider.currentitem > 0) {
				hdslider.currentitem--;
				hdslider.animate();
			}
		},
		
		next: function() {
			if ((hdslider.currentitem + hdslider.visibleitems) < hdslider.itemcount) {
				hdslider.currentitem++;
				hdslider.animate();
			}
		},
		
		animate: function() {
			var offset = hdslider.currentitem * (hdslider.itemwidth / -1);
			$('div.items > ul', hdslider.element).stop().animate({'margin-left':offset});
		}
			
	}
	
	hdslider.init('.horizontalslider.animals', 4);
	
	// Featured Animal Filter
	$('#animalsortby').change(function() {
		if ($(this).val() == '') {
			$('li', '.horizontalslider.animals div.items > ul').show();
		} else {
			$('li', '.horizontalslider.animals div.items > ul').hide();
			$('li.' + $(this).val(), '.horizontalslider.animals div.items > ul').show();
		}
		$('div.items > ul', '.horizontalslider.animals').stop().css({'margin-left':0});
	});
	
	// Rehome Animal Filter
	$('#rehomesortby').change(function() {
		window.location = $(this).val();
	});
	
	var hdnewsslider = {
			
		element: false,
		items: false,
		itemcount: 0,
		currentitem: 0,
		itemwidth: 0,
		visibleitems: 1,
			
		init: function(element, visibleitems) {
			hdnewsslider.element = $(element);
			hdnewsslider.visibleitems = visibleitems;
			hdnewsslider.items = $('div.items > ul > li', hdnewsslider.element);
			hdnewsslider.itemcount = hdnewsslider.items.length;
			hdnewsslider.itemwidth = $(hdnewsslider.items[0]).outerWidth(true);
			
			if (hdnewsslider.itemcount > 0) {
			
				$('a.previous', $('div.newsslidercontrols.' + hdnewsslider.element.attr('class').replace('newsslider ', ''))).live('click', function() {
					hdnewsslider.previous();
					return false;
				});
				
				$('a.next', $('div.newsslidercontrols.' + hdnewsslider.element.attr('class').replace('newsslider ', ''))).live('click', function() {
					hdnewsslider.next();
					return false;
				});
			
			}
		},
		
		previous: function() {
			if (hdnewsslider.currentitem > 0) {
				hdnewsslider.currentitem--;
				hdnewsslider.animate();
			}
		},
		
		next: function() {
			if ((hdnewsslider.currentitem + hdnewsslider.visibleitems) < hdnewsslider.itemcount) {
				hdnewsslider.currentitem++;
				hdnewsslider.animate();
			}
		},
		
		animate: function() {
			var offset = hdnewsslider.currentitem * (hdnewsslider.itemwidth / -1);
			$('div.items > ul', hdnewsslider.element).stop().animate({'margin-left':offset});
		}
			
	}
		
	hdnewsslider.init('.newsslider.latestnews', 1);
	
	var hdeventsslider = {
			
			element: false,
			items: false,
			itemcount: 0,
			currentitem: 0,
			itemwidth: 0,
			visibleitems: 1,
			
			init: function(element, visibleitems) {
				hdeventsslider.element = $(element);
				hdeventsslider.visibleitems = visibleitems;
				hdeventsslider.items = $('div.items > ul > li', hdeventsslider.element);
				hdeventsslider.itemcount = hdeventsslider.items.length;
				hdeventsslider.itemwidth = $(hdeventsslider.items[0]).outerWidth(true);
				
				if (hdeventsslider.itemcount > 0) {
				
					$('a.previous', $('div.newsslidercontrols.' + hdeventsslider.element.attr('class').replace('newsslider ', ''))).live('click', function() {
						hdeventsslider.previous();
						return false;
					});
					
					$('a.next', $('div.newsslidercontrols.' + hdeventsslider.element.attr('class').replace('newsslider ', ''))).live('click', function() {
						hdeventsslider.next();
						return false;
					});
				
				}
			},
			
			previous: function() {
				if (hdeventsslider.currentitem > 0) {
					hdeventsslider.currentitem--;
					hdeventsslider.animate();
				}
			},
			
			next: function() {
				if ((hdeventsslider.currentitem + hdeventsslider.visibleitems) < hdeventsslider.itemcount) {
					hdeventsslider.currentitem++;
					hdeventsslider.animate();
				}
			},
			
			animate: function() {
				var offset = hdeventsslider.currentitem * (hdeventsslider.itemwidth / -1);
				$('div.items > ul', hdeventsslider.element).stop().animate({'margin-left':offset});
			}
			
	}
	
	hdeventsslider.init('.newsslider.upcomingevents', 1);
	
	var hdanimalsslider = {
			
			element: false,
			items: false,
			itemcount: 0,
			currentitem: 0,
			itemwidth: 0,
			visibleitems: 1,
			
			init: function(element, visibleitems) {
				hdanimalsslider.element = $(element);
				hdanimalsslider.visibleitems = visibleitems;
				hdanimalsslider.items = $('div.animallists > ul', hdanimalsslider.element);
				hdanimalsslider.itemcount = hdanimalsslider.items.length;
				hdanimalsslider.itemwidth = $(hdanimalsslider.items[0]).outerWidth(true);
				
				$('a.previous', $('div.animalslidercontrols')).live('click', function() {
					hdanimalsslider.previous();
					return false;
				});
				
				$('a.next', $('div.animalslidercontrols')).live('click', function() {
					hdanimalsslider.next();
					return false;
				});
				
			},
			
			previous: function() {
				if (hdanimalsslider.currentitem > 0) {
					hdanimalsslider.currentitem--;
					hdanimalsslider.animate();
				}
			},
			
			next: function() {
				if ((hdanimalsslider.currentitem + hdanimalsslider.visibleitems) < hdanimalsslider.itemcount) {
					hdanimalsslider.currentitem++;
					hdanimalsslider.animate();
				}
			},
			
			animate: function() {
				var offset = hdanimalsslider.currentitem * (hdanimalsslider.itemwidth / -1);
				$('div.animallists', hdanimalsslider.element).stop().animate({'margin-left':offset});
			}
			
	}
	
	hdanimalsslider.init('.animalslider', 1);
	
	
	if ($('#sidebar').children().size() == 0) {
		$('#sidebar').remove();
	}
	
	$('form.meettheanimals').live('change', function() {
		var select = $('select', this);
		if (select.val() != '') {
			window.location = select.val();
		}
	});
	
	$('div.map-point').each(function() {
		var infowindow = $('div.outer', $(this)); 
		infowindow.css({'margin-top':'-' + infowindow.outerHeight() + 'px'});
	});
	
	$('div.map-point').hover(function() {
		$(this).addClass('active');
		var infowindow = $('div.outer', $(this)); 
		infowindow.show().css({'margin-top':'-' + infowindow.outerHeight() + 'px'});
	}, function() {
		$(this).removeClass('active');
		var infowindow = $('div.outer', $(this)); 
		infowindow.hide();
	});
	
	$('li.map-key').hover(function() {
		var mappoint = $('div.map-point-' + $(this).attr('class').replace('map-key map-key-', ''));
		mappoint.addClass('active');
		var infowindow = $('div.outer', mappoint); 
		infowindow.show().css('margin-top', '-' + infowindow.outerHeight() + 'px');
	}, function() {
		var mappoint = $('div.map-point');
		mappoint.removeClass('active');
		var infowindow = $('div.outer', mappoint);
		infowindow.hide();
	});
	
	$("a.lightbox").colorbox();
	$("a[rel='lightbox']").colorbox();
	$("a.help").colorbox({width:"540px", maxHeight:"80%"});
	
	$("a.popupanimallink").live('click', function() {
		var divref = '#popupanimal' + $(this).parent().parent().attr('class').match(/animalid\-.+?\b/).toString().replace('animalid-', '');
		$.colorbox({width:"482px", inline:true, href:divref});
		return false;
	});
	
	$('div.gallery ul.thumbs li a').live('click', function() {
		var imageurl = $(this).attr('href');
		$('a.large', $(this).parent().parent().parent()).attr('href', imageurl);
		$('a.large img', $(this).parent().parent().parent()).attr('src', imageurl.replace('/800/', '/302/'));
		return false;
	});
	
	/* Sitemap */
	$('ul.sitemap li:has(ul)').prepend('<span class="expander"></span>');
	
	$('ul.sitemap li:has(ul) span.expander').live('click', function(event) {
		if ($('> ul', $(this).parent()).is(':hidden')) {
			$(this).removeClass('contracted'); 
			$(this).addClass('expanded'); 
		} else {
			$(this).removeClass('expanded'); 
			$(this).addClass('contracted'); 
		}
		$('> ul', $(this).parent()).toggle();
		return false;
	}).addClass('contracted').css({cursor:'pointer'});
	$('ul.sitemap li ul').hide();
	
	$.ajaxSetup({
		type: 'POST',
		cache: false
	});
	
	$("body").ajaxStart(function () {
		$('body').addClass('loading');
	});
	
	$("body").ajaxStop(function () {
		$('body').removeClass('loading');
		$('.ajaxtrigger').removeClass('ajaxtrigger');
	});
	
	$('.cmscontent img').load(function() {
		if($(this).width() > 100) {
			$(this).addClass('large');
		}
	});
	
});
