

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:
	{		
		'body': function($elms) {
			Cufon.replace('.nav-column1 li a', { fontFamily: 'flama-medium', hover: 'true' });
			Cufon.replace('.nav-column2 li a', { fontFamily: 'flama-regular', hover: 'true' });
			Cufon.replace('.#news .news-item h4', { fontFamily: 'flama-medium'});
			Cufon.replace('.layout-standard .summary-content h2', { fontFamily: 'flama-bold'});
			Cufon.replace('.layout-gallery .summary-content h2', { fontFamily: 'flama-bold'});

			if ($.browser.msie && $.browser.version <= 6 ){
				$($elms).supersleight({shim:'/img/clear.gif'});

				// Diplay warning if it's IE6
				checkAlert = function(){
					justVisit = $.cookie('just_visit');
					if (justVisit == null){
						text = 'We have detected you have an older version of IE, please be aware that you may not fully experience the sites features while using this browser.';
						alert(text);
						$.cookie('just_visit', 'true', { expires: 365, path: '/' });
					}
				}
				checkAlert();
			}
		
		},

		
			
		'.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. 
		 */

		/*Not needed for fineseat*/
		
		'.theme-color-aqua': function ($elms){
			$margin = $('div.summary-content').height();
			$('div.sales-contacts', $elms).attr('style', 'margin-top:'+ $margin + 'px');
		},

		/*------------------------------------------------------
			  Start: Login
		--------------------------------------------------------*/

		/*fancybox example*/
		'.layout-home-login': function ($elms){
				 
				function launchLogin(){ 
					$("#login-link").fancybox({
						//'showCloseButton' : false,
						'autoDimensions' : false,
						'width' : 283,
						'height' : 131,
						'padding' : 0
					}).trigger('click');
				}
				
				launchLogin();
		},
		
		
		'.sidiz-login-link': function ($elms){
			
			$elms.fancybox({
				'href'				: '/users/authenticate',
				//'type'				: 'iframe',	//'inline', //'iframe', // 'ajax',
				'autoDimensions'	: false,
				'titleShow'			: false,
				'scrolling'			: 'no',
				'width'				: 283,
				'height'			: 151,
				'padding'			: 0,
				'onStart'			: startFancy,
				'onClosed'			: closeFancy,
				'onComplete'		: completeFancy
			});
			
			function startFancy() {
			}
			
			function closeFancy() {
			}
			
			// Will be called once the content is displayed
			function completeFancy() {
				$('#MemberPasswordPass').bind('focus', function(){
						text = $(this).val();
						if (text=='Password'){
							$(this).val('');
						}
				});
				bindOnSubmit();
			}

			 var bindOnSubmit = function() {
				 	//Binding the form after load
					$("form.sidiz-login-form").bind("submit", 
						function() {	
							$.fancybox.showActivity();

							var data = $(this).serializeArray();
							var action	= $(this).attr('action');

							//submit form
							$('#fancybox-content').load(
								action,
								data,
								function() {
									$.fancybox.hideActivity();
									bindOnSubmit();											
								}
							);
							return false;
						}
					);			
			 }
					
			
		},

		/*------------------------------------------------------
			  End: Login
		--------------------------------------------------------*/

		/*------------------------------------------------------
			  Start: Fabrics Gallery
		--------------------------------------------------------*/

		'.theme-gallery-fabrics, .layout-gallery': function ($elms){
			$(".fabric-products a").fancybox({
				'titleShow'     : false,
				'showCloseButton' : false,
				'padding' : 24,
				'onComplete'	:	function() {
					$('#fancybox-outer').append('<a class="close-alt" onclick="$.fancybox.close();" href="javascript:;">Close</a>');
				}
			});
		}

		/*------------------------------------------------------
			  End: Fabrics Gallery
		--------------------------------------------------------*/

		
	}
}	

$(document).ready(
	function() {
		Site.onReady();
	}
);
