var base_url = '/';

$(document).ready(function() {
	$('input[type=text]').focus(function() {
		$(this).val('');
	});
	
	$('a[href=#]').click(function(e) {
		e.preventDefault();
	});
	
	$('a[href=#submit]').click(function(e) {
		e.preventDefault();
		$(this).closest('form').submit();
	});
	
	//Initalise dynamic defaults
	$('#content_gallery_slider .next a').attr('href', '#' + $('#content_gallery_slider .choices li:nth-child(2)').attr('id'));	
    
    //Setup the product image slider
    $('#content_gallery a').click(function(e) {
    	e.preventDefault();
    	
    	var target_href = $(this).attr('href');
    	
    	if(target_href == '#toggle_thumbs') {
    		$('#content_gallery_thumbs').toggleClass('hide');
    	} else if(target_href == '#toggle_description') {
    		$('#content_gallery_description').toggleClass('hide');    	
    	} else {
	    	var container = '#content_gallery_slider .choices';
			var target = container + ' li' + target_href;
			
			try {
				var offset = $(target).position().left;
				
				$(container).animate({left: -offset}, 0);
								
				$('#content_gallery_slider .prev a').attr('href', '#' + $(target).prev().attr('id'));
				$('#content_gallery_slider .next a').attr('href', '#' + $(target).next().attr('id'));
				
				if(offset == $(container + ' li:first-child').position().left) {
					$('#content_gallery_slider .prev').hide();
					$('#content_gallery_slider .next').show();
				} else if(offset == $(container + ' li:last-child').position().left) {
					$('#content_gallery_slider .prev').show();
					$('#content_gallery_slider .next').hide();
				} else {
					$('#content_gallery_slider .prev').show();
					$('#content_gallery_slider .next').show();
				}
				
				updateDescription(target_href);
			} catch(e) {}
		}
	});
	
	//Setup the thumbnail image slider
	$('#content_gallery_thumbs').mousemove(function(e) {
		var container = $(this);
		var choices = container.find('.choices');
		var x = e.pageX - container.offset().left;
		var y = e.pageY - container.offset().top;
		var scrollzone = 75;
		var scrollwidth = container.width();
		var min_x = 0;
		var max_x = min_x - choices.width() + scrollwidth - 10;
		var speed = 0.05;

		if(max_x < min_x) {
			if(y > 20) {
				if(x < scrollzone) {
					var t = Math.abs(choices.offset().left - min_x) / speed;
					choices.animate({left: min_x}, t);
				} else if(x > (scrollwidth - scrollzone)) {
					var t = Math.abs(max_x + choices.offset().left) / speed;
					choices.animate({left: max_x}, t);
				} else {
					choices.stop(true);
				}
			} else {
				choices.stop(true);		
			}
		}
	}).mouseout(function(e) {
		var container = $(this);
		var x = e.pageX - container.offset().left;
		var y = e.pageY - container.offset().top;

		if((x < 0) || (y < 0) || (x > container.width()) || (y > container.height())) {
			$(this).stop(true);
		}
	});
	
	//Setup the child image slider
	$('#content_gallery_child').mousemove(function(e) {
		var container = $(this);
		var choices = $(this).find('.choices');
		var x = e.pageX - container.offset().left;
		var y = e.pageY - container.offset().top;
		var scrollzone = 125;
		var scrollwidth = container.width();
		var min_x = 0;
		var max_x = min_x - choices.width() + scrollwidth - 12;
		var min_y = choices.position().top;
		var max_y = choices.position().top + choices.height() * 1.5;
		var speed = 0.2;


		if(max_x < min_x) {
			if(y > min_y && y < max_y) {
				if(x < scrollzone) {
					var t = Math.abs(choices.position().left - min_x) / speed;
					$('#content_gallery_child .next').show();

					choices.animate({left: min_x}, t, 'linear', function() {
						$('#content_gallery_child .prev').hide();
					});
				} else if(x > (scrollwidth - scrollzone)) {
					var t = Math.abs(max_x + choices.position().left) / speed;
					$('#content_gallery_child .prev').show();
					
					choices.animate({left: max_x}, t, 'linear', function() {
						$('#content_gallery_child .next').hide();
					});
				} else {
					choices.stop(true);
				}
			} else {
				choices.stop(true);		
			}
		}
	}).mouseout(function(e) {
		var container = $(this);
		var choices = $(this).find('.choices');
		
		var x = e.pageX - container.offset().left;
		var y = e.pageY - choices.offset().top;

		if((x < 0) || (y < 0) || (x > container.width()) || (y > choices.height())) {
			choices.stop(true);
		}
	});
	
	$('#footer_navigation_media a').mouseover(function() {
		if($(this).attr('href') != '#') {
			$(this).parent().siblings('.' + $(this).parent().attr('class') + '_popup').fadeIn('fast');
		}
	}).mouseout(function() {
		$(this).parent().siblings('.' + $(this).parent().attr('class') + '_popup').fadeOut('slow');
	});
});

$(window).load(function() {
	//Initialise child gallery width
	var child_gallery_width = 0;
	$('#content_gallery_child .choices li').each(function(i) {
		child_gallery_width += $(this).outerWidth(true);
	});
	$('#content_gallery_child .choices').width(child_gallery_width);
});

function updateDescription(href) {
	try {
		var id = href.substr(href.lastIndexOf('_') + 1);
	
		if(href.indexOf('category') == 1) {
			$('#content_gallery_description').load(base_url + 'catalog/category_description/' + id);
		} else {
			$('#content_gallery_description').load(base_url + 'catalog/product_description/' + id);
		}
	} catch(e) {}
}

Shadowbox.init({
	language: "en",
});