

var Site = {
		
	fullBaseUrl: null,
	
	onReady: function() {
	
		// Set base path
		this.fullBaseUrl = document.location.protocol + '//' + document.location.host;
		
		for (var selector in this.elements) {
			var $elms = $(selector);
			
			if ($elms.size() > 0) {
				this.elements[selector]($elms);
			}
		}
	},
	
	elements:
	{		
		'.cufon_replacement': function($elms) {
			Cufon.replace('.cufon_replacement', { hover: true });
		},
			
		'.product-gallery': function($elms) {
			
			//	Fetch elements
			var $preview = $('.product-preview', $elms);
			var $imageLinks = $('.product-thumb-gallery a', $elms);
			var $images = $('img', $imageLinks);
			var $counter = $('.product-navigation .image-count', $elms);
			var $prevLink = $('.product-navigation .previous-link', $elms);
			var $nextLink = $('.product-navigation .next-link', $elms);
			
			//	Runtime variables
			var width = $preview.children('img').width();
			var height = $preview.children('img').height();
			var imagesCount = $images.length;
			var activeImageIndex = 0;
			
			//	Functions
			var init = function() {
				$images.css({'opacity': .63 });
				$images.hover(
					function() {
						$(this).stop().animate(
							{'opacity': 1},
							{'duration': 'fast'}
						);
					},
					function() {
						
						var $this = $(this);
						if ($this.parents('a.active-image').length == 0) {
							$this.stop().animate(
								{'opacity': .63},
								{'duration': 'fast'}
							);
						}
					}
				);
				
				//	Bind links
				$imageLinks.bind(
					'click', 
					function(e) {
						showImage($(this));
						e.preventDefault();
						e.stopPropagation();
						return true;
					}
				);
				$prevLink.bind(
					'click',
					function(e) {
						prevImage();
						e.preventDefault();
						e.stopPropagation();
						return true;
					}
				);
				
				$nextLink.bind(
					'click',
					function(e) {
						nextImage();
						e.preventDefault();
						e.stopPropagation();
						return true;
					}
				);
				
				//	Preload images
				$imageLinks.preload();
				
				//	Set initial status
				if ($imageLinks.length > 0) {
					updateStatus($imageLinks.eq(0));
				}
			};
			
			var nextImage = function() {
				if (activeImageIndex+1 < imagesCount) {
					activeImageIndex++;
					showImage($imageLinks.eq(activeImageIndex));
				}
			};
			
			var prevImage = function() {
				if (activeImageIndex-1 >= 0) {
					activeImageIndex--;
					showImage($imageLinks.eq(activeImageIndex));
				}
			};
			
			var showImage = function($imageLink) {
				
				//	Clear old images
				$preview.children('img:not(img:last)').stop().remove();
				
				//	Insert new image, at 0 opacity
				$("<img style='position:absolute; top:0; left:0;' src='"+$imageLink.attr('href')+"' />").appendTo($preview);
				var $newImage = $preview.children('img:last').css('opacity', 0);
				
				//	Fade out old image, fade in new
				var $oldImage = $preview.children('img:first').stop().animate(
					{ 'opacity': 0 },
					'fast',
					function() {
						$newImage.animate(
							{ 'opacity': 1.0 },
							'medium'
						);
						$oldImage.remove();
					}
				);
				
				updateStatus($imageLink);
			}
			
			var updateStatus = function($imageLink) {
				//	Update counter
				activeImageIndex = $imageLinks.index($imageLink);
				$counter.text((1+activeImageIndex)+'/'+imagesCount);
				
				//	Update highlighted thumbnail
				$imageLinks.removeClass('active-image').children('img').stop().animate(
					{'opacity': 0.78},
					{'duration': 'fast'}
				);
				$imageLink.addClass('active-image').children('img').stop().animate(
					{'opacity': 1},
					{'duration': 'fast'}
				);
			}
						
			init();
			
		},
		
		'.product-category img.gallery-image': function($images) {
			
			$images.css({'opacity': .78 });
			$images.hover(
				function() {
					$(this).stop().animate(
						{'opacity': 1},
						{'duration': 'fast'}
					);
				},
				function() {
					$(this).stop().animate(
						{'opacity': .78},
						{'duration': 'fast'}
					);
				}
			);
		},
		
		'#ProductSearchForm': function($elms) {
			
			var initPhrase = 'search';
			
			var onFocus = function(e) {
				if ($(this).val() == initPhrase) {
					$(this).val('');
				}
				return true;
			};
			
			var onBlur = function(e) {
				if ($(this).val() == '') {
					$(this).val(initPhrase);
				}
				return true;
			};
			
			$('#ProductKeyword', $elms).bind('focus', onFocus).bind('blur', onBlur).val(initPhrase);
		},
		
		/*
		 * Fix the contact location on contact page. 
		 */
		
		'.theme-color-aqua': function ($elms){
			$margin = $('div.summary-content').height();
			$('div.sales-contacts', $elms).attr('style', 'margin-top:'+ $margin + 'px');
		}
		
		
	}
}	

$(document).ready(
	function() {
		Site.onReady();
	}
);
