function curry () {
	var fn = arguments[arguments.length -1];
    var args = [];
    for (var i=0; i < arguments.length - 1; ++i) {
        args.push(arguments[i]);
    };
    return function() {
	    fn.apply(null, args);
    };
}

$(document).ready(function() {
		
		$('#canvas').prepend(
			'<div id="bgCanvasOverlay"></div>' + 
			'<div id="canvasOverlay">' +
			'</div>'
		);
		
		if ($('#ctr4cs').length > 0) {
			
			$('#ctr4cs').css('visibility', 'hidden');
			
			$('#ctr4cs').append('<ul class="clearfix"></ul><div id="ctrTxt4cs" class="clearfix"><div id="minHeight4cs"></div></div');
			$('.t4cs').each(function(i) {
				$('#ctr4cs UL').append('<li>' + $(this).children('H2').text() + '</li>');
				$('#ctrTxt4cs').append('<div class="txt4cs ' + $(this).children('H2').text() + '"></div>');
				$(this).find('P').each(function() {
					$('.txt4cs').eq(i).append('<p>' + $(this).text() + '</p>');																
				});
			});
			
			$('.txt4cs').each(function() {
				$(this).children('P:first').addClass('intro');												 
			});
			
			$('.t4cs').hide();
			$('.txt4cs').hide();
			
			$('#ctr4cs').css({height:'auto', overflow:'hidden'});
			
			$('#ctrTxt').css('margin-bottom', $('#ctr4cs').height() + 'px');
			
			$('#ctr4cs SPAN').width($('#ctr4cs UL').width());
			$('#ctr4cs').css('visibility', 'visible');
			
			$('#ctr4cs UL LI').click(function() {
				$('#ctrTxt4cs').css('background', '#cc9');
				var c = $(this).text();
				
				$('#ctr4cs UL LI').not(this).animate({color: '#fff', backgroundColor: '#8c824d'});
				$(this).animate({color: '#8c824d', backgroundColor: '#cc9'});
				
				$('#ctrTxt').css('margin-bottom', $('#ctr4cs').height() + 'px');
				
				$('.txt4cs').hide();
				$('.' + c).fadeIn('slow');
				
				$('#ctrTxt').css('margin-bottom', $('#ctr4cs').height() + 'px');
				
			});
			
		}
		
		$('#loader').height($('#ctrContent').height());
		
		var docHeight = $('#canvas').height() + parseInt($('#canvas').css('padding-top').slice(0, $('#canvas').css('padding-top').length - 2));
		
		function resizeOverlayBg(ctr) {
			if ($('#bgCanvasOverlay').is(':visible')) {
				var overlayHeight = $(ctr).height() + parseInt($(ctr).css('margin-top').slice(0, $(ctr).css('margin-top').length - 2));	
				
				if ($(window).height() > docHeight || $(window).height() > overlayHeight) {
					$('#bgCanvasOverlay').height($(window).height());
				}
				else {
					if (docHeight > overlayHeight) {
						$('#bgCanvasOverlay').height(docHeight);
					}
					else {
						$('#bgCanvasOverlay').height(overlayHeight + 20);
					}
				}	
				
			}
		}
		
		$('#contact').click(function() {
			
			$('#canvasOverlay').empty().prepend(
				'<div id="ctrContactOverlay">' +
					'<div id="contactOverlay">' +
						'<div id="contentContactOverlay"></div>' +
						'<span id="btnClose"></span>' +
					'</div>' +
				'</div>'
			);	
			
			var url = $(this).attr('href');
			$(document).scrollTop(0);
			
			$('#bgCanvasOverlay').height($(window).height())
				.addClass('overlayLoader')
				.css('opacity', '0.6')
				.fadeIn('fast');
			
			$('#contentContactOverlay').load(url + ' .contact', function() {		
				$('#bgCanvasOverlay').removeClass('overlayLoader');
						$('#canvasOverlay').height(docHeight)
							.fadeIn('fast', function() {
								$('#contactOverlay').show(500, function() {
																												
									var flashvars = {};
									var params = {
										wmode: "transparent"
									};
									var attributes = {};
									swfobject.embedSWF("/assets/flash/product-overlay-stars.swf", "stars", "507", "40", "9.0.0", "/assets/flash/expressInstall.swf", flashvars, params, attributes);
									
									resizeOverlayBg('#ctrContactOverlay');
									
									$(window).resize(function() {
										resizeOverlayBg('#ctrContactOverlay');
									});
									
								});
							});
			
				$('#btnClose').click(function() {
					$('#contactOverlay').hide();
					$('#canvasOverlay').hide();	
					$('#bgCanvasOverlay').fadeOut('fast');													
				});
				/*$('#btnClose').click(function() {
					$('#contactOverlay').hide(500, function() {
						$('#canvasOverlay').hide();	
						$('#bgCanvasOverlay').fadeOut('fast');
					});														
				});*/
			
			});
		
			return false;
		});
		
		$('#openingHours').click(function() {
			
			$('#canvasOverlay').empty().prepend(
				'<div id="ctrOpeningHoursOverlay">' +
					'<div id="openingHoursOverlay">' +
						'<div id="contentOpeningHoursOverlay"></div>' +
						'<span id="btnClose"></span>' +
					'</div>' +
				'</div>'
			);	
			
			var url = $(this).attr('href');
			$(document).scrollTop(0);
			
			$('#bgCanvasOverlay').height($(window).height())
				.addClass('overlayLoader')
				.css('opacity', '0.6')
				.fadeIn('fast');
			
			$('#contentOpeningHoursOverlay').load(url + ' .openingHours', function() {	
				$('#bgCanvasOverlay').removeClass('overlayLoader');
						$('#canvasOverlay').height(docHeight)
							.fadeIn('fast', function() {
								$('#openingHoursOverlay').show(500, function() {
																												
									resizeOverlayBg('#ctrOpeningHoursOverlay');
									
									$(window).resize(function() {
										resizeOverlayBg('#ctrOpeningHoursOverlay');
									});
									
								});
							});
			
				$('#btnClose').click(function() {
					$('#openingHoursOverlay').hide();
					$('#canvasOverlay').hide();	
					$('#bgCanvasOverlay').fadeOut('fast');														
				});
				/*$('#btnClose').click(function() {
					$('#openingHoursOverlay').hide(500, function() {
						$('#canvasOverlay').hide();	
						$('#bgCanvasOverlay').fadeOut('fast');
					});														
				});*/
			
			});
		
			return false;
		});
		
		$('#sitemap').click(function() {
			
			$('#canvas').prepend(
				'<div id="ctrSitemapOverlay">' +
					'<div id="sitemapOverlay">' +
						'<div id="contentSitemapOverlay"></div>' +
					'</div>' +
				'</div>'
			);	
			
			var url = $(this).attr('href');
			$(document).scrollTop(0);
			
			$('#contentSitemapOverlay').load(url + ' .sitemap', function() {																												 
				$('.sitemap').height($('#ctrContent').height())
					.fadeIn('500', function() {
																	 
					});
			
			});
		
			return false;
		});
		
		if ($('#templateD').length > 0) {
			
			function getLeftPadding() {
				if ($(window).width() > ($('#ctrTxtLeft').width() + $('#ctrTxtRight').width())) {
					var paddingLeft = ($(window).width() - $('#ctrTxtLeft').width() - $('#ctrTxtRight').width()) / 2;
					$('#ctrTxtLeft').css('padding-left', paddingLeft + 'px');
					$('#ctrTxtRight').css('left', ($('#ctrTxtLeft').width() + paddingLeft) + 'px');
				}
			}
			
			$('#ctrTxtLeft').prepend('<div id="dateHolder"></div>');
			
			var itemsPerPage = 4;
			var newsItemCount = $('#ctrNewsList UL LI').size();
			var newsPageCount = Math.ceil(newsItemCount / itemsPerPage);
			var listHTML = [ ];
			for (var i = 0; i < newsItemCount; i++) {
				listHTML[i] = '';
			}
			var itemsLoaded = 0;
			
			$('#ctrNewsList UL LI').each(function(i) {
				var url = $(this).children('A').attr('href');
				$('#dateHolder').load(url + ' .date', curry(i, $(this).html(), function(i, li) {
					var dateTxt = $('.date').text();
					
					if ((i + 1) % itemsPerPage == 0) { 
						listHTML[i] = '<li><h2>' + dateTxt + '</h2>' + li + '</li></ul><ul class="nav" id="list-' + (Math.ceil(i / itemsPerPage) + 1) + '" style="display:none">';
					}
					else {
						listHTML[i] = '<li><h2>' + dateTxt + '</h2>' + li + '</li>';
					}
					
					itemsLoaded++;
					if (itemsLoaded == newsItemCount) {
						$('#ctrNewsList').empty().append('<ul class="nav" id="list-1">' + listHTML.join('') + '</ul>');
					
						var firstList = $('#ctrNewsList UL').eq(0);
						var lastList = $('#ctrNewsList UL').eq($('#ctrNewsList UL').size() - 1);
						lastList.addClass('last');
						firstList.addClass('first');
						
						firstList.children('LI').eq(0).children('A').addClass('on');
						
						if ($('#ctrNewsList UL').size() > 1) {
							$('#next').css({color:'#fff', cursor:'pointer'});
						}
						
						$('#next').click(function() {
							if (!(lastList.is(':visible'))) {
								var thisList = $('#ctrNewsList UL:visible');
								var nextList = $('#ctrNewsList UL:visible').next();
								var nextListNum = parseInt($('#ctrNewsList UL:visible').next().attr('id').split('-')[1]);
								$('#pageNum SPAN EM').text(nextListNum < 10 ? '0' + nextListNum : nextListNum);
								thisList.hide();
								nextList.fadeIn('fast');
								if (nextList.hasClass('last')) {
									$('#next').css({color:'#575130', cursor:'default'});
								}
							}
							if ($('#ctrNewsList UL').size() > 1) {
								$('#prev').css({color:'#fff', cursor:'pointer'});
							}
						});
						$('#prev').click(function() {
							if (!(firstList.is(':visible'))) {	
								var thisList = $('#ctrNewsList UL:visible');
								var prevList = $('#ctrNewsList UL:visible').prev();
								var prevListNum = parseInt($('#ctrNewsList UL:visible').prev().attr('id').split('-')[1]);
								$('#pageNum SPAN EM').text(prevListNum < 10 ? '0' + prevListNum : prevListNum);
								thisList.hide();
								prevList.fadeIn('fast');
								if (prevList.hasClass('first')) {
									$('#prev').css({color:'#575130', cursor:'default'});
								}
							}
							if ($('#ctrNewsList UL').size() > 1) {
								$('#next').css({color:'#fff', cursor:'pointer'});
							}
						});
						
						
						$('#ctrNewsList UL LI A').click(function() {
							url = $(this).attr('href');	
							
							$('#ctrNewsList UL LI A').each(function() {
								$(this).removeClass('on');										
							});
							
							$(this).addClass('on');
							
							loadNewsArticle(url);
							
							return false;
						});
						
					}
					
				}));
			});
			
			$('#pageNum SPAN STRONG').text(newsPageCount < 10 ? '0' + newsPageCount : newsPageCount);
			
			var url = $('#ctrNewsList UL LI:first A').attr('href');
			$('#ctrNewsList UL LI:first A').addClass('on');
			
			function loadNewsArticle(url) {
				$('#ctrNewsItem').append('<div id="newsLoader"></div>');
				$('#newsItem').load(url + ' .content', function() {
																   
					$(this).find('H1').hide();
					$('#newsLoader').fadeOut(500, function() {
						$(this).remove();
					});
				});	
			}
			
			getLeftPadding();
			
			if ($('#ctrNewsList UL LI').size() > 0) {
				loadNewsArticle(url);
			}
			$(window).resize(getLeftPadding);
			
		}
		$('UL.nav:eq(0) LI UL').addClass('png');
		$('.nav:eq(0)').children('LI').each(function() {
			if ($(this).hasClass('nav-path-selected')) {
				var subNavLeftPos = $('#hdr').width() - ($('.nav:eq(0)').width() + parseInt($('.nav:eq(0)').css('margin-right').slice(0, $('.nav:eq(0)').css('margin-right').length - 2)) - parseInt($('.nav:eq(0) LI A').css('padding-left').slice(0, $('.nav:eq(0) LI A').css('padding-left').length - 2)));
				
				var subNavOn = '';
				
				$(this).find('UL').children('LI').each(function() {
					if ($(this).hasClass('nav-path-selected')) {
						subNavOn = ' class="on"';	
					}
					else {
						subNavOn = '';
					}
					$('#subNav UL').append('<li' + subNavOn + '>' + $(this).html() + '</li>');
				});
				
				var subNavWidth = 0;
				var extraSubNavItems = new Array();
				var extraSubNavItemWidths = new Array();
				var isWider = false;
			
				$('#subNav UL LI').each(function() {
					var subNavItem = this;
					var subNavItemWidth = $(this).width() + parseInt($('#subNav UL LI').css('margin-right').slice(0, $('#subNav UL LI').css('margin-right').length - 2));
					
					
					
					if ((subNavWidth + subNavItemWidth) > ($(window).width())) {
						extraSubNavItems.push('<li>' + $(subNavItem).html() + '</li>');
						extraSubNavItemWidths.push(subNavItemWidth);
						$(subNavItem).remove();
						isWider = true;
					}
					else {
						subNavWidth = subNavWidth + subNavItemWidth;
					}
				});
				
				if (isWider) {
					var lastSubNavItem = $('#subNav UL LI').eq($('#subNav UL LI').size() - 1);
					extraSubNavItems.unshift('<li>' + lastSubNavItem.html() + '</li>');
					extraSubNavItemWidths.unshift(lastSubNavItem.width() + parseInt(lastSubNavItem.css('margin-right').slice(0, lastSubNavItem.css('margin-right').length - 2)));
					$(lastSubNavItem).replaceWith('<li id="more">More &#62;&#62;</li>')
					
					subNavWidth = 0;
					
					$('#subNav UL LI').each(function() {
							subNavWidth = subNavWidth + $(this).width() + parseInt($('#subNav UL LI').css('margin-right').slice(0, $('#subNav UL LI').css('margin-right').length - 2));														 
					});
					
					$('#ctrSubNav').append('<ul id="moreNav"></ul>')
					
					for (var i = 0;i < extraSubNavItems.length;i++) {
						$('#moreNav').append(extraSubNavItems[i]);
					}
					
					$('#more').click(function() {
						if ($('#moreNav').is(':visible')) {
							$('#moreNav').slideUp('fast');
						}
						else {
							$('#moreNav').slideDown('fast');
						}
					});
					
				}
				
				$('#subNav UL').width(subNavWidth);
				
				function positionSubNav() {
					
					var subNavLeftMargin = subNavLeftPos + (($(window).width() - $('#hdr').width()) / 2);
					
					if (($('#subNav UL').width() + subNavLeftMargin) > $(window).width()) {
						$('#subNav UL').css('margin', '0 auto');
					}
					else {
						$('#subNav UL').css('margin', '0 0 0 ' + subNavLeftMargin + 'px');
					}	
				}
				
				positionSubNav();
				
				$('#subNav UL LI').hide();
				
				$(window).resize(function() {
																	
					positionSubNav();												
																	
					if (isWider) {
						//s$('#moreNav').hide();
						
						//for (var i = 0;i < extraSubNavItemWidths.length;i++) {
							//alert($('#subNav UL').width() + $(extraSubNavItems[i]).width());
							/*if (($('#subNav UL').width() + extraSubNavItemWidths[0]) < $(window).width()) {
								//$('#moreNav').append(extraSubNavItems[i]);
								$('#more').before(extraSubNavItems[0]);
								extraSubNavItems.shift();
								extraSubNavItemWidths.shift();
								$('#moreNav LI:first').remove();
								
								if (extraSubNavItems.length == 0) {
									$('#more').hide();
									$('#moreNav').hide();
								}
								$('#subNav UL').width($('#subNav UL').width() + extraSubNavItemWidths[0]);
							}*/
							/*else {
								var lastSubNavItem = $('#subNav UL LI').eq($('#subNav UL LI').size() - 2);
								var lastSubNavItemWidth = lastSubNavItem.width() + parseInt(lastSubNavItem.css('margin-right').slice(0, lastSubNavItem.css('margin-right').length - 2));
								extraSubNavItems.unshift('<li>' + lastSubNavItem.html() + '</li>');
								extraSubNavItemWidths.unshift(lastSubNavItemWidth);
								lastSubNavItem.remove();
								$('#moreNav').append(extraSubNavItems[0]);
								
							}*/
						//}
						 
						
					}
					
				});
				
			}
		});
		
		
		
		
		if ($('#templateE').length > 0) {
			
			if ($('#products LI').size() > 0) {
				
			
				var h = 0;
		
				$('#products LI P').each(function(){		 
						if ($(this).height() > h) {
							h = $(this).height();
						}	
				});
				$('#products LI P').height(h);
				
				var prodMargin = parseInt($('#products LI').css('margin-right').slice(0, $('#products LI').css('margin-right').length - 2));
				var prodWidth = $('#products LI').width() + prodMargin;
				var numProds = $('#products LI').size();
				var prodsWidth = prodWidth * numProds;
				
				$('#products').width(prodsWidth);
				
				var endRightPos = (prodsWidth - $(window).width() + parseInt($('#products').css('padding-right').slice(0, $('#products').css('padding-right').length - 2)));
				var endLeftPos = parseInt($('#products').css('left').slice(0, $('#products').css('left').length - 2));
				//var onLoadLeftPos = (prodsWidth / 2) - ($(window).width() / 2) - (prodMargin / 2);
				var onLoadLeftPos = endLeftPos;
				//var startPos = (onLoadLeftPos / 4) * 3;
				var startPos = (onLoadLeftPos * (numProds / 4));
				var noScrollLeftPos = (($(window).width() - prodsWidth) / 2) + (prodMargin / 2);
				var finishedMovingAfterloading = false;
				var speed = 4;
				var slowdown = 300;
				var leftPos = onLoadLeftPos;
				var duration = prodsWidth + endLeftPos;
				var direction = '';
				var rightPos = duration - endRightPos;
				
				if (prodsWidth > $(window).width()) {	
					$('#products').css('left', '-' + startPos + 'px');
					//duration = prodsWidth - leftPos;
					$("#products").animate({left: onLoadLeftPos + 'px'}, {duration:(duration / 2), complete:function() {																																																																
						//$('#scrollRight').css('background-position', '-400px 40px');
						$('#scrollLeft').css('background-position', '384px 40px');
						//leftPos = '-' + leftPos;
						finishedMovingAfterloading = true;
						
						//$('#scrollRight').html('left: ' + leftPos + ', right: ' + rightPos);
						//$('#scrollLeft').html('duration: ' + (duration * speed) + ', endRightPos: ' + endRightPos);
						
						
					}});
				}
				else {
					$("#products").css('left', noScrollLeftPos + 'px');
					$('#scrollRight SPAN').html('<strong>' + $('#products LI').size() + 'items</strong>');
				}
				
				
				
				/*function positionProds() {
					if (prodsWidth < $(window).width()) {
						isProdsWiderThanWindow = false;
						leftPos = (($(window).width() - prodsWidth) / 2) + (prodMargin / 2);
						$('#products').css('left', leftPos + 'px');
					} else {
						isProdsWiderThanWindow = true;
					}
					endRightPos = ($('#products').width() - $(window).width() + parseInt($('#products').css('padding-right').slice(0, $('#products').css('padding-right').length - 2)));
				}*/
			
				function scrollProdsLeft() {
					if (finishedMovingAfterloading) {
						$("#products").stop();	
						//$('#scrollLeft').html('duration: ' + (duration * speed) + ', endRightPos: ' + endRightPos);
						
						direction = 'left';
						$('#scrollRight').css('background-position', '-400px 40px');
						if (leftPos >= (- + endRightPos)) {
							$("#products").animate({left: '-' + endRightPos}, {duration:(duration * speed), easing:'easeOutSine', complete:function() {
								$('#scrollLeft').css('background-position', '-41px 40px');		
							}});
						}
					}
				}
				
				function scrollProdsRight() {	
					if (finishedMovingAfterloading) {
						$("#products").stop();	
						direction = 'right';
						$('#scrollLeft').css('background-position', '384px 40px');
						duration = rightPos - leftPos;
						
						//$('#scrollLeft').html('duration: ' + (duration * speed) + ', endRightPos: ' + endRightPos);
						
						$("#products").animate({left: endLeftPos + 'px'}, {duration:(duration * speed), easing:'easeOutSine', complete:function() {
							$('#scrollRight').css('background-position', '25px 40px');	
						}});
					}
				}
				
				function stopProds() {
					if (finishedMovingAfterloading) {
						//alert('animated!');
						var sLeftPos = parseInt($('#products').css('left').slice(0, $('#products').css('left').length - 2));
						if (direction == 'left') {
							//alert(sLeftPos + ' ' + (-endRightPos));
							if (sLeftPos > (-endRightPos + slowdown)) {
								$("#products").animate({left: '-=' + slowdown + 'px'}, {queue:false, duration:(2800), easing:'easeOutSine', complete:function() {
									$("#products").stop();			
								}});
							}
						}
						else {
							//alert(sLeftPos + ' ' + endLeftPos);
							if (sLeftPos < (endLeftPos - slowdown)) {
								$("#products").animate({left: '+=' + slowdown + 'px'}, {queue:false, duration:(2800), easing:'easeOutSine', complete:function() {
									$("#products").stop();			
								}});
							}
						}
						//$("#products").stop();
						leftPos = parseInt($('#products').css('left').slice(0, $('#products').css('left').length - 2));
						duration = prodsWidth + leftPos;
						
						//$('#scrollRight').html('left: ' + leftPos + ', right: ' + rightPos);
						//$('#scrollLeft').html('duration: ' + (duration * speed) + ', endRightPos: ' + endRightPos);
						
					}
				}
				
				function validateCallbackForm() {
					
					$('#ctrCallbackForm').append('<iframe id="salesforce" width="0" height="0" style="visibility:hidden;overflow:hidden"></iframe>');
					
					$("#salesforce").attr('src', '/salesforce-form.html');
					
					
					$('#callbackForm').validate({
						rules: {
							first_name: 'required',
							last_name: {
								required: function(element) {
									return $("#first_name").val() != '';
								}
							},
							phone: {
								required: function(element) {
									return $("#first_name").val() != '' && $("#last_name").val() != '' && $("#email").val() == '';
								}
							},
							email: {
								required: function(element) {
									return $("#first_name").val() != '' && $("#last_name").val() != '' && $("#phone").val() == '';
								},
								email: function(element) {
									return $("#first_name").val() != '' && $("#last_name").val() != '' && $("#phone").val() == '';
								}
							},
							comment: {
								required: function(element) {
									return $("#first_name").val() != '' && $("#last_name").val() != '' && ($("#phone").val() != '' || $("#email").val() != '');
								}
							}
						},
						
						messages: {
							first_name: 'First name required!',
							last_name: 'Last name required!',
							phone: 'Phone/email required!',
							email: {
								required: 'Email/phone required!',
								email: 'Valid email required!'
							},
							comment: 'Comment required!'
						},
						
						errorLabelContainer: "#ctrErrors",
						
						submitHandler: function() {
							
							var post = {
								name: $("#first_name").val() + ' ' + $("#last_name").val(), 
								telephone: $("#phone").val(),
								email: $("#email").val(),
								priceRange: $("#price_range").val(),
								comment: $("#comment").val(),
								productCode: $("#productCode").val(),
								productName: $("#productName").val()
							};
							
							$.ajax({
									type: 'POST',
									url: '/contact-form-handler',
									data: post,
									dataType: 'json',
									error: function(){         
											//alert('Unexpected response!');
											return false;
									},
									success: function(responseData){
										if(responseData.status=='ok'){
												$('#wrprCallbackForm').hide();
              					$('#thxCallbackForm').fadeIn('slow');
										}
									}
							});
							
							$("#salesforce").contents().find("#00N20000002MQKX").attr('value', $("#productCode").val());
							$("#salesforce").contents().find("#00N20000002MQKc").attr('value', $("#productName").val());
							$("#salesforce").contents().find("#00N20000002MQKm").attr('value', $("#productDescription").val());
							$("#salesforce").contents().find("#00N20000002MQKh").attr('value', $("#productCategories").val());
							$("#salesforce").contents().find("#first_name").attr('value', $("#first_name").val());
							$("#salesforce").contents().find("#last_name").attr('value', $("#last_name").val());
							$("#salesforce").contents().find("#phone").attr('value', $("#phone").val());
							$("#salesforce").contents().find("#email").attr('value', $("#email").val());
							$("#salesforce").contents().find("#00N20000001wWqE").attr('value', $("#price_range").val());
							$("#salesforce").contents().find("#description").attr('value', $("#comment").val());
							$("#salesforce").contents().find("#lead_source").attr('value', 'Website');
							//console.log($("iframe").contents().find("#salesforceForm"));
							$("#salesforce").contents().find("#first_name").focus();
							$("#salesforce").contents().find("#salesforceForm").submit();
							
						}
					});
					
				}
				
				$(window).resize(function() {
					//positionProds();
					endRightPos = ($('#products').width() - $(window).width() + parseInt($('#products').css('padding-right').slice(0, $('#products').css('padding-right').length - 2)));
					//$('#scrollLeft').html('duration: ' + duration + ', endRightPos: ' + endRightPos);
					
				});
				
				//positionProds();
				
				$('#scrollRight').hover(scrollProdsRight, stopProds);
				$('#scrollLeft').hover(scrollProdsLeft, stopProds);
				
				$('#products LI').css('cursor', 'pointer');
				
				$('#products LI').hover(
					function() {
						$(this).find('P').css('background-position', '-157px 0');
					},
					function() {
						$(this).find('P').css('background-position', '0 0');
					}
				);
				
				$('#products LI A').hide();
				
				$('#scrollRight SPAN STRONG').text($('#products LI').size() + ' items');
				
				$('#products LI').click(function() {
																				 
					$('#canvasOverlay').empty().prepend(
						'<div id="ctrProductOverlay">' +
							'<div id="productOverlay">' +
								'<div id="contentProductOverlay"></div>' +
								'<span id="btnClose"></span>' +
							'</div>' +
						'</div>'
					);															 
					
					var url = $(this).find('A').attr('href');
					$(document).scrollTop(0);
					
					$('#bgCanvasOverlay').height($(window).height())
						.addClass('overlayLoader')
						.css('opacity', '0.6')
						.fadeIn('fast');
					
					function showProdDetail() {
						$('#bgCanvasOverlay').removeClass('overlayLoader');
								$('#canvasOverlay').height(docHeight)
									.fadeIn('fast', function() {
										$('#name').css('background', '#cc9');
										$('#productOverlay').show(500, function() {
										
											var flashvars = {};
											var params = {
												wmode: "transparent"
											};
											var attributes = {};
											swfobject.embedSWF("/assets/flash/product-overlay-stars.swf", "stars", "650", "40", "9.0.0", "/assets/flash/expressInstall.swf", flashvars, params, attributes);
											
											resizeOverlayBg('#ctrProductOverlay');
											
											$(window).resize(function() {
												resizeOverlayBg('#ctrProductOverlay');
											});
											
											var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
											$.getScript(gaJsHost + 'google-analytics.com/ga.js');
											try {
												var pageTracker = _gat._getTracker("UA-11580230-1");
												pageTracker._trackPageview(url);
											} catch(err) {}
											
											$.getScript('/assets/js/libraries/jquery.validate.js', validateCallbackForm);
											
											$('#first_name').focus();
											
											$('.focus').focus(function() {
												$('.focus').each(function() {
													$(this).css('background', '#fff');													
												});
												$(this).css('background', '#cc9');
											});
											
											$('.shot').click(function() {
																								
													var url = $(this).attr('href');
													
													$('#ctrProdImg').append('<div class="loading"></div>');
																								
													var img = new Image();
													
													$(img).load(function() {
														
															$('#ctrProdImg').html(this);
															$('.loading').fadeOut('fast', function() {
																$(this).remove();																			
															});
															
													})
														.attr('src', url);
											
													return false;										
											});
											
											$('.shotAssoc').click(function() {
													
													var url = $(this).attr('href');
													
													$('#bgCanvasOverlay').addClass('overlayLoader');
													
													$('#productOverlay').hide();
													
													$('#contentProductOverlay').load(url + ' .product', showProdDetail);
											
													return false;										
											});
											
										});												 
									});
									
									$('#btnClose').click(function() {
										$('#productOverlay').hide();
										$('#canvasOverlay').hide();	
										$('#bgCanvasOverlay').fadeOut('fast');														
									});
									/*$('#btnClose').click(function() {
										$('#productOverlay').hide(500, function() {
											$('#canvasOverlay').hide();	
											$('#bgCanvasOverlay').fadeOut('fast');
										});														
									});*/
									
									if ($('#p1').length > 0 && $('#p2').length > 0) {
										$('#p2').next('SPAN').hide();
									}
									
									if (!($('#p1').length > 0) && $('#p2').length > 0) {
										$('.hdrAdditionalProds').eq(0).hide();
									}
									
									$('.hdrAdditionalProds SPAN').click(function() {
										window.print();																						 
									});
									
					}
					
					$('#contentProductOverlay').load(url + ' .product', showProdDetail);
					
				});
			}
		}
		
		// Mouseover function for nav submenu dropdowns.
		$('.nav:eq(0) LI:has(UL)').hover(
			function() {
				if (!($(this).hasClass('nav-path-selected'))) {
					$(this).children('A').addClass('over');
					$(this).find('UL').slideDown(300);
				}
			},
			function() {
				if (!($(this).hasClass('nav-path-selected'))) {
					var a = $(this).children('A');
					$(this).find('UL').slideUp(0, function() {
						a.removeClass('over');																	
					});
				}
			}
		);
		
		$(window).load(function() {
			
			function loader() {
					$('#loader').fadeOut('fast', function() {
						
						if ($('#templateA').length > 0) {
							/*$('.callout DIV').each(function(i) {	
								$(this).slideDown(500 * (i + 1), function() {
									$(this).find('UL').slideDown(500);
								});
							});*/
						}
						else {
							$('#ctrSubNav UL LI').each(function(i) {
								$(this).slideDown(50 * i)																		
							});
						}
						
					});
			}
			
			window.setTimeout (loader, 200);
			
    });
});