(function($) {
	
	$.fn.simplyShadow = function() {
		return this.each(function() {
			$(this).addClass('dropshadow-vert-right').wrap('<div class="dropshadow"></div>').before('<div class="dropshadow-corner-rb"></div><div class="dropshadow-end-bl"></div><div class="dropshadow-horz-bottom"></div>');
			$(this).parent().find('.dropshadow-horz-bottom').css('width',$(this).find('li').outerWidth()-6 + 'px');
			$(this).parent().parent().hide().css('visibility','visible');
		});
	}
	
	$.fn.simplyDropdown = function() {
		return this.each(function() {
			//vertical align
			$(this).find('span').css('margin-top','-' + $(this).find('span').outerHeight()/2 + 'px');
			if ($(this).find('a').length) {
				$(this).hover(
					function() { $(this).css('background-color','#FFF'); $("div.subnav",this).show();},
					function() { $(this).css('background-color',''); $("div.subnav",this).hide();}
				);
			}
		});
	}
	
	var regionSelector = function() {
		
		var timeout = null,
			hideLang = function() {
				$('#lang a').show();
				$("#lang-selector").hide();
			};
		
		$('#lang a').click(function() {
			$(this).hide();
			$("#lang-selector").show();
			return false;
		});
		
		$("#lang-selector a").hover(
			function() { clearTimeout(timeout);},
			function() { timeout = setTimeout(hideLang,100);}
		);
	}
	
	$.fn.products = function () {
		return this.each(function() {
								  
			
								  
			var self = this,
				timeout,
				active = false;
			
			var funcIn = function() {
				active = true;
				if (!$.browser.msie) {
					$(self).find('img').fadeTo(500,0.6);
				}
				$(self).find('p.title').animate({marginTop:"-20px"},500,
					function() {
						if (active) {
							!$.browser.msie ? $(self).find('.subcat-overlay').fadeIn() : $(self).find('.subcat-overlay').show();
						}
					}
				);
			};
			var	funcOut = function() {
				!$.browser.msie ? $(self).find('.subcat-overlay').fadeOut() : $(self).find('.subcat-overlay').hide();
				
				$(self).find('p.title').animate({marginTop:0},500);
				if (!$.browser.msie) {
					$(self).find('img').fadeTo(500,1);
				}
				active = false;
			};
								  
			$(this).find('.subcat-overlay-inner').addClass('dropshadow-vert-right').wrap('<div class="dropshadow"></div>').before('<div class="dropshadow-corner-rb"></div><div class="dropshadow-end-bl"></div><div class="dropshadow-horz-bottom"></div>');
			$(this).find('.subcat-overlay-inner').parent().find('.dropshadow-horz-bottom').css('width',$(this).find('.products').outerWidth()-4 + 'px');
			$(this).find('.subcat-overlay').hide().css('visibility','visible');
				
			$(this).hover(function() {
				if (active) {
					clearTimeout(timeout);
				} else {
					
					funcIn();
				}
				//$(this).find('.subcat-overlay').show();					   
			},function() {
				timeout = setTimeout(funcOut,250);
				
			});
			
			$(this).find('.subcat-overlay').mouseover(function() {
				clearTimeout(timeout);
			});
			
		});
	};
	
	$.fn.preview = function() {
	
		var index = 0,
			zoomIndex = 0,
			draggable = null,
			defaultColour = $(this).attr('class').match(/[0-9]{2}-[0-9]{3}-[0-9A-Z]{2}$/)[0],
			currentColour = defaultColour,
			loadedColours = [],
			imagePath = '/images/imprint2010/product_shots_cached/',
			imageFilenames = ['_RGB_front_LR.jpg','_RGB_front_HR.jpg','_Back_RGB_back_LR.jpg','_Back_RGB_back_HR.jpg','_RGB_tb_front.jpg','_Back_RGB_tb_back.jpg'],
			self = this;
			
		$(this).prepend('<div class="containment-helper"></div>');
		
		//remove draggable instance and reset position
		var destroyDraggable = function(resetPos) {
			if (draggable!==null) {
				$("#preview-image div:eq("+index+") img").eq(1).draggable('destroy');
				if (resetPos) {
					$("#preview-image div:eq("+index+") img").eq(1).css({top:0,left:0})//.css({top:"-180px",left:"-152px"});
				}
				draggable=null;
			}
		};
		
		//clear previous zoom
		var clearZoom = function() {
			$(self).find('.image-zoom').removeClass('image-zoom-out');
			$("#preview-image div:eq("+index+") img").hide().eq(0).show();
			destroyDraggable(true);
		};
		
		//stop dragging of non-draggable images (firefox)
		$(this).find("#preview-image div").each(function(){
			$(this).find("img").eq(0).mousedown(function(){return false;});
		});
		
		//thumbnail selection
		$(this).find("#preview-thumbs .preview-thumb").click(function() {
			//set preview
			index = $("#preview-thumbs .preview-thumb").index(this);
			$("#preview-image div").hide().eq(index).show();
			
			
			clearZoom();
		});
		
		//zoom in/out - init draggable on large (2nd) image
		$(this).find('.image-zoom').click(function() {
			$(this).toggleClass('image-zoom-out');
			
			zoomIndex = $(this).hasClass('image-zoom-out') ? 1 : 0;
			$("#preview-image div:eq("+index+") img").hide().eq(zoomIndex).show();
			
			if (zoomIndex === 1) {
				
				draggable = $("#preview-image div:eq("+index+") img").eq(1).css({cursor:'move'}).draggable({containment:$("#preview .containment-helper"),cursor:'move'});
				
			} else {
				destroyDraggable(false);
			}
			
		});
		
		//colour selector
		$('.colour-container .colour').click(function() {
			var selectedColour = $(this).attr('id').match(/[0-9]{2}-[0-9]{3}-[0-9A-Z]{2}$/)[0];
			var loadCount = 0;
			var cacheTimeout;
				
			if (selectedColour == currentColour) {
				clearZoom(); //just reset
				return;	
			}
			
			var imageRegisterLoad = function() {
				loadCount++;
				if (loadCount==imageFilenames.length) {
					imageLoad();	
				}
			}
			
			var imageLoad = function(e) {

				if ($.browser.msie) {
					clearTimeout(cacheTimeout);
				}
				
				$(self).find('.image-load').hide();
				$("#preview-thumbs .preview-thumb:eq(0) img, #preview-thumbs .preview-thumb:eq(1) img").fadeOut(500);
				$("#preview-image div:eq("+(index?0:1)+")").hide();
				$("#preview-image div:eq("+index+")").fadeOut(500,function() {
					
					//reset selected image & drag and zoom
					index = 0;
					clearZoom();
					
					//replace images
					$("#preview-image div:eq(0) img:eq(0)").attr('src',imagePath + selectedColour + imageFilenames[0]);
					$("#preview-image div:eq(0) img:eq(1)").attr('src',imagePath + selectedColour + imageFilenames[1]);
					$("#preview-image div:eq(1) img:eq(0)").attr('src',imagePath + selectedColour + imageFilenames[2]);
					$("#preview-image div:eq(1) img:eq(1)").attr('src',imagePath + selectedColour + imageFilenames[3]);
					$("#preview-thumbs .preview-thumb:eq(0) img").attr('src',imagePath + selectedColour + imageFilenames[4]);
					$("#preview-thumbs .preview-thumb:eq(1) img").attr('src',imagePath + selectedColour + imageFilenames[5]);
				
					//show
					$("#preview-image div:eq(0)").fadeIn(500,function() {
						$(self).find('.image-zoom').show();
						$("#preview-image div:eq(1)").show();
					});
					$("#preview-thumbs .preview-thumb:eq(0) img, #preview-thumbs .preview-thumb:eq(1) img").fadeIn(500);
					
					loadedColours.push(selectedColour); //register loaded colour
					currentColour = selectedColour;
				});
				
				

			};
					
			$(self).find('.image-zoom').hide();
			$(self).find('.image-load').show();
			
			if (selectedColour != defaultColour && !inArray(selectedColour, loadedColours)) { 
				
				//preload images
				$.each(imageFilenames,function(k,v) {
					$('<img>').attr('src', imagePath + selectedColour + v).load(imageRegisterLoad);
				});
				
				if ($.browser.msie) {
					//IE doesn't fire all load events for cached images...
					cacheTimeout = setTimeout(imageLoad,1000);
				}
				
				
			} else {
				imageLoad(); 
			}
			
		});
	}
	
	var openWin = function(loc,name,width,height,scrollbars) {
		var sY = (screen.height - height) / 2 ; var sX = (screen.width - width) / 2;
		var popupWin = window.open(loc,name,'scrollbars='+scrollbars+',top='+sY+',left='+sX+',height='+height+',width='+width+''); //not global
		popupWin.focus();
	}
	
	var inArray = function (needle, haystack) {
		var key = '';
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		}
		return false;
	}
	
	var ie_lt_7 = $.browser.msie && parseInt($.browser.version, 10) < 7 && parseInt($.browser.version, 10) > 4;
	var ie_lt_8 = $.browser.msie && parseInt($.browser.version, 10) < 8 && parseInt($.browser.version, 10) > 4;
	
	$(function() { //DOM load
	
		//common
		if(typeof sIFR == "function" && !$.browser.msie){
			sIFR.replaceElement("#nav a span",
				named({sFlashSrc:'/avantgarde_medium.swf', sColor:'#333333', sWmode: "transparent", sFlashVars: "textalign=center" })
			);
		}
		

		$("ul#nav li").simplyDropdown();
		$(".subnav ul").simplyShadow();
		regionSelector();
		
		$("a[rel=external]").click(function() {
			var w = window.open($(this).attr('href'),'_blank');
			w.focus();
			return false;
		});
		
		$("a[rel=popup]").click(function() {
			openWin($(this).attr('href'),'popup',1000,700,'yes');	
			return false;
		});
		
		 $('#content img,#header img').bind('contextmenu',function() {return false;}); 
		
		//product
		if ($("#preview").length) {
			$("#preview").preview();
			
			$(".related-item").click(function() {
				document.location = $(this).find('a').attr('href');							  
			});
			$(".related-item a").click(function(e) {
				e.stopPropagation();	
				return true;
			});
		}
		
		//category
		if ($(".subcat-container").length) {
			$(".subcat").products();	
		}
		
		//sub-cat
		if ($("#subcat-listing").length && !ie_lt_7) {
			
			$("#subcat-listing li").wrapInner('<div/>').hoverIntent(function(){
				$(this).stop(true).animate({top:"-20px"},500);
			},function() {
				$(this).animate({top:0},500);
			});
		}
		
		//home	
		if ($("body.p_home").length) {
			$(window).load(function() { //onload
				$('.cycle').cycle({
					fx: 'fade',
					timeout: 5000,
					speed: 1000
				});
			});
		}
		
		//contacts
		if ($(".p_contacts .country-container").length) {
			$(".p_contacts .country-outer").addClass('dropshadow-vert-right').wrap('<div class="dropshadow"></div>').before('<div class="dropshadow-corner-rb"></div><div class="dropshadow-end-bl"></div><div class="dropshadow-horz-bottom"></div>');
			$(".p_contacts .country-outer").parent().find('.dropshadow-horz-bottom').css('width',$('.country-inner').outerWidth()-6 + 'px');
		}
		
		if ($("ul#contacts").length) {
			$("ul#contacts li:last").addClass('last');
		}
		
		//Fix z-index ordering in IE 7 and below
		if (ie_lt_8) {
			var zIndexNumber = 1000;
			$('div.subnav,ul#nav,div#header,div#lang-selector,div#system-bar,div.cycle img,div.subcat-overlay,div.subcat,div#main').each(function() {
				$(this).css('z-index',zIndexNumber);
				zIndexNumber -= 10;
			});	
		}
		
		//Transparent PNGs (< IE 6)
		if (ie_lt_7) {
			var alphaEls = ['#lang img','#lang-selector img','.item-2 img','.item-3 img','.subcat p.title','.subcat img','.subcat-new img','#subcat-listing img','#preview .image-zoom','ul#contacts li','.view-usa','#landing-container ul img'];
			$(alphaEls.join(",")).supersleight();	
		}
		
	});	
	
})(jQuery);

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: '/images/x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		//if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				//console.log(this);
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		//};
	});
};
/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);