
	/*
		Johnstons of Elgin
		--------------------------
		Author:			Sam Hampton-Smith
		Web:			www.hampton-smith.com
		--------------------------
		Version:		1.0
		Date:			12th March 2011
	*/
	$.noConflict();


	var movement = 0;
	var currentframe = 0;
	var timer = 100; // default to 10fps
	var t;

	jQuery(document).ready(function($){


		$("#pressclippings .leftarrow").click(function(){
			$("ul.pressclippings").stop().clearQueue();
			// scroll everything to the right
			$("ul.pressclippings li:first").before($("ul.pressclippings li:last"));
			$("ul.pressclippings").css({marginLeft:-220}).animate({marginLeft:0},1000);
		});

		$("#pressclippings .rightarrow").click(function(){
			$("ul.pressclippings").stop().clearQueue();
			// scroll everything to the right
			$("ul.pressclippings").animate({marginLeft:-220},1000,function(){
				$("ul.pressclippings li:last").after($("ul.pressclippings li:first"));
				$(this).css({marginLeft:0});
			});
		});

		$("ul.pressclippings li").click(function(){
			// dim the screen, insert the <div> content.
			$("body").append("<div class='blackbox'></div>");
			$(".blackbox").css({display:"block",opacity:0}).animate({opacity:1},500);
			$(".blackbox").click(function(){
				// close the close view, hide the black box
				$("#lightbox").hide();
				$("#lightboxcaption").hide();
				$(this).animate({opacity:0},500,function(){
					$(this).remove();
					$("#lightbox").remove();
					$("#lightboxcaption").remove();
				});
			});
			innercontent = $(this).find("div").html();
			caption = $(this).find("span").html();
			$(".blackbox").after("<div id='lightbox'>"+innercontent+"</div>");
			$("#lightbox").after("<div id='lightboxcaption'><div id='closebox'>Click outside the article to close</div>"+caption+"</div>");
		});


		$("#crossfader").each(function(){
			fade = setInterval(function(){
				$("#crossfader li:last").animate({opacity:0},1500,function(){
					$("#crossfader li:first").before(this);
					$(this).css({opacity:1});	
				})
			},5500);
		});	


		$("#millsubmit").click(function(){
			var dataString = 'title=' + $("#title").val() + '&firstname='+ $("#firstname").val() + '&email=' + $("#contactemail").val() + '&surname=' + $("#surname").val() + '&contactnumber=' + $("#contactnumber").val() + '&dateofarrival=' + $("#dateofarrival").val() + '&timeofarrival=' + $("#timeofarrival").val() + '&numberofpeople=' + $("#numberofpeople").val() + '&nationality=' + $("#nationality").val(); 
			//alert(dataString); 
			$.ajax({  
				type: "POST",  
  				url: "http://newservertest.johnstonscashmere.com/milltour.php",  
				data: dataString,  
  				success: function() {  
					$('#milltourform').html("<div id='message'></div>");  
					$('#milltourform').html("<h2>Mill Tour request Submitted!</h2>")  
					.append("<p>We will be in touch soon.</p>")  
					.hide()  
					.fadeIn(1500, function() {  
						$('#milltourform').append("<img id='checkmark' src='images/check.png' />");  
					});  
				}  
			})
		});


		$(".video").each(function(){
			linkurl = $("#mediaplayer a").attr("rel");
			videourl = $("#mediaplayer a").attr("href");
			imageurl = $("#mediaplayer a img").attr("src");
			playerwidth = parseInt($("#mediaplayer a img").width());
			playerheight = parseInt($("#mediaplayer a img").height());
			jwplayer('mediaplayer').setup({
	    		'id': 'playerID',
	    		'width': playerwidth,
	    		'height': playerheight,
	    		'file': videourl,
	    		'image': imageurl,
	    		'modes': [
	        		{type: 'html5'},
	        		{type: 'flash', src: '/player.swf'}
	    		]
	  		});

			/* $(".video").click(function(){
				jwplayer().play()
			});

			if (linkurl!='') {
				$(".video").click(function(){
					document.location.href=linkurl;
				});	
			} */
		});

		$(".homepanel img").css({zIndex:100});
		$(".homepanel").hover(function(){
			$(this).find("p").animate({opacity:1});
			$(".homepanel").not(this).stop().animate({opacity:0.5});
			$(".homepanel").not(this).find("p").stop().css({zIndex:1,opacity:0.5});
		},function(){
			$(".homepanel p").stop().animate({opacity:1});			
			$(".homepanel").not(this).stop().animate({opacity:1});			
		});

		$(".magazinepanel").hover(function(){
			$(".magazinepanel").not(this).stop().animate({opacity:0.3});
		},function(){	
			$(".magazinepanel").not(this).stop().animate({opacity:1});			
		});
		
		$("#highlightedoffer").css({marginTop:-40}).delay(5000).animate({marginTop:0},600,function(){
			var offers=0;
			$("#highlightedoffer p").each(function(n){
				offers = n;
			});
	
			if (offers>0) {
				var offerstimer = setInterval(function(){
					$("#highlightedoffer p:first").animate({marginTop:-30},1000,function(){
						$("#highlightedoffer p:first").css({marginTop:0});
						$("#highlightedoffer p:last").after($("#highlightedoffer p:first"));					
					});	
				},5000);
			}
		});
/*
		// Nicer fading navigation
		$("#nav ul li").has("div").hover(function(){
			$(this).find("div").stop(true).show().css({opacity:1});
			$("#nav ul li").not(this).removeClass("ihover").find("div").hide();
		},function(){
			$(this).addClass("ihover");
			$(this).find("div").not("#right").not("#left").stop(true).delay(1000).fadeOut(400,function(){$(this).parent("li").removeClass("ihover");$(this).parent("li").find("#left").remove();
			$(this).parent("li").find("#right").remove();});

		});

		$("#nav ul li ul li a").click(function(){$(this).blur()});
		$("#nav ul li ul").stop().css({opacity:1});
		$("#nav ul li ul li").hover(function(){
			$(this).stop().css({opacity:1});
			$("#nav ul li ul li").not(this).stop().animate({opacity:0.3});
			adjustoffset = $("#container").offset();
			curroffset = $(this).offset();
			currentleft = curroffset.left-adjustoffset.left;

			tmp = $(this).parent("ul").find("li:last").offset();

			if (tmp.left-adjustoffset.left>640) 
				$(this).parent("ul").parent("div").parent("li").append("<a id='right'></a>");

			if (parseInt($(this).parent("ul").css("marginLeft"))<0)
				$(this).parent("ul").parent("div").parent("li").append("<a id='left'></a>");

			if (currentleft>640) {
				totaltoscroll = 640-currentleft;
				scrollto = parseInt($(this).parent("ul").css("marginLeft"))+totaltoscroll;
				$(this).parent("ul").delay(2).animate({marginLeft:scrollto},600,function(){
					mytmp = $(this).find("li:last").offset();
					if (mytmp.left-adjustoffset.left<691) 
						$(this).parent("div").parent("li").find("#right").remove();
				});
			}
			if (currentleft<0) {
				totaltoscroll = adjustoffset.left-curroffset.left;
				scrollto = parseInt($(this).parent("ul").css("marginLeft"))+totaltoscroll;
				if ($(this).parent("ul").find("li").index($(this))>0) scrollto=scrollto+40;
				if (scrollto>0) scrollto = 0;
				$(this).parent("ul").delay(2).animate({marginLeft:scrollto+10},600,function(){

					if (!parseInt($(this).css("marginLeft"))>-10)
						$(this).parent("div").parent("li").find("#left").remove();


				});
			}
			
		},function(){
			$("#nav ul li ul li").not(this).stop().animate({opacity:1});
		});

*/

$("#nav ul li div").prepend("<div class='divider'></div>");
		$("#nav>ul>li>div").append("<span class='tmpdescription'></span>");
		$(".tmpdescription").css({display:"none"});
		$("#nav ul li").has("div").hover(function(){
			$("li:first",this).each(function(){
				$(this).stop().css({opacity:1}).addClass("navsecondactive");
				$("#nav ul li ul li").not(this).removeClass("navsecondactive").stop().animate({opacity:0.3});
				$(this).find("img").each(function(){
					$(this).parent("a").parent("li").parent("ul").css({background:"#1a1a1a url("+$(this).attr("src")+") no-repeat 10px 10px !important"});
				});
				temphtml = $(this).find("p.cat-description").html();
				$(this).parent("ul").parent("div").find(".tmpdescription").html(temphtml);
			});
			$(this).find("div").stop(true).show().css({opacity:1});
			$("#nav ul li").not(this).removeClass("ihover").find("div").hide();
			$(this).addClass("ihover");
		},function(){
			$(this).removeClass("ihover");
			$(this).find("div").not("#right").not("#left").stop(true).delay(1000).fadeOut(400,function(){$(this).parent("li").removeClass("ihover");$(this).parent("li").find("#left").remove();
			$(this).parent("li").find("#right").remove();});

		});

		$("#nav ul li ul li a").click(function(){$(this).blur()});
		$("#nav ul li ul").stop().css({opacity:1});
		$("#nav ul li ul li").hover(function(){
			$(this).stop().css({opacity:1}).addClass("navsecondactive");
			$("#nav ul li ul li").not(this).removeClass("navsecondactive").stop().animate({opacity:0.3});
			$(this).find("img").each(function(){
				$(this).parent("a").parent("li").parent("ul").css({background:"#1a1a1a url("+$(this).attr("src")+") no-repeat 10px 10px !important"});
			});
			temphtml = $(this).find("p.cat-description").text();
			$(this).parent("ul").parent("div").find(".tmpdescription").html(temphtml);
		},function(){
			$("#nav ul li ul li").not(this).stop().animate({opacity:1});
		});

	
			var t;
			$("#carosel #left").mouseover(function(){
				/* Do something */
				t = setInterval(function(){
					lefttarget = parseInt($("#carosel ul li:first").width());					
					if (parseInt($("#carosel ul").css("marginLeft"))<=0-lefttarget-40) {
						$("#carosel ul li:last").after($("#carosel ul li:first"));
						$("#carosel ul").css({marginLeft:0});
					} else {
						$("#carosel ul").css({marginLeft:parseInt($("#carosel ul").css("marginLeft"))-2});
					}
				},10);
			}).mouseout(function(){
				clearInterval(t);

			});

			$("#carosel #right").mouseover(function(){

				t = setInterval(function(){
					if (parseInt($("#carosel ul").css("marginLeft"))>=0) {
						goleft = parseInt($("#carosel ul li:last").width())+40;
						$("#carosel ul li:first").before($("#carosel ul li:last"));
						$("#carosel ul").css({marginLeft:0-goleft});
					} else {
						$("#carosel ul").css({marginLeft:parseInt($("#carosel ul").css("marginLeft"))+2});
					}
				},10);
			}).mouseout(function(){
				clearInterval(t);

			});


		$(".expander").each(function(){
			currheight = $(this).height();
			$(this).data("openheight",parseInt(currheight)+50);
			$(this).css({overflow:"hidden", height:50});
			$(this).hover(function(){
				$(this).stop().animate({height:$(this).data("openheight")});	
			},function(){
				$(this).stop().animate({height:50});
			});
		});
/*
		$(".csr .cols6").each(function(){
			p1= $("p:first",this);
	    	p= p1[0].textContent.replace(/^\s+/, "").replace(/^"/, "").replace(/^'/, "");
	    	c= p.charAt(0);
	    	p1.html('<span class="dropcap tk-voluta-script-pro">' + c  + '</span>' + p.substring(1, p.length));
		});
*/
		$("#view360").click(function(){
			$("#image360").show();			
		});

		$(".thumblist a").not("#view360").click(function(){
			$("#image360:visible").hide();
			label = $("span",this).text().toLowerCase();
			$("#attribute129 option").each(function(n){
				if (label==$(this).text().toLowerCase()){
					$(this).attr("selected","selected");
					$("#attribute129 option").not(this).attr("selected","");
					if (document.createEvent) {
	        			var evt = document.createEvent('HTMLEvents');
	        			evt.initEvent("change", true, true);
						document.getElementById("attribute129").dispatchEvent(evt);
	    			}
					if (this.fireEvent)
				        document.getElementById("attribute129").fireEvent('onchange');
				}
			});
			$("#attribute135 option").each(function(n){
				if (label==$(this).text().toLowerCase()){
					$(this).attr("selected","selected");
					$("#attribute135 option").not(this).attr("selected","");
					if (document.createEvent) {
	        			var evt = document.createEvent('HTMLEvents');
	        			evt.initEvent("change", true, true);
						document.getElementById("attribute135").dispatchEvent(evt);
	    			}
					if (this.fireEvent)
				        document.getElementById("attribute135").fireEvent('onchange');
				}
			});
		});
				
		$("#image360").css({ WebkitUserSelect: 'none', WebkitBackgroundSize: "2760px 2760px"});
				
				
		$("#image360").mouseover(function(){
			t = setTimeout("rotate()",timer);
			// Mouse clicked and held down inside image area
			$(this).mousemove(function(e){
				posx = e.pageX;
				containeroffset = $(this).offset();
				containerleft = containeroffset.left;
				containerwidth = $(this).width();
				posx = posx - containerleft;
				if (posx<(containerwidth*0.4)) {
					movementpercent = posx/(containerwidth/2);
					timer = 40+parseInt(160*movementpercent);
					movement = 1;
				} else if (posx>(containerwidth*0.6)) {
					movementpercent = (posx-(containerwidth/2))/(containerwidth/2);
					timer = 200-parseInt(160*movementpercent);
					movement = -1;
				} else if (posx>(containerwidth*0.4) && posx<(containerwidth*0.6)) {
					movement = 0;
					timer = 200;
				}
			});					
		}).bind("mouseup mouseout",function(){
			// Mouse released or moved out of image area
			movement = 0;
			clearTimeout(t);
		});
/*
		$("#image360").swipeleft(function(){
			clearTimeout(t);
			movement = -1;
			timer=100;
			t = setTimeout("rotate()",timer);
		}).swiperight(function(){
			clearTimeout(t);
			movement = 1;
			timer=100;
			t = setTimeout("rotate()",timer);
		}).tap(function(){
			clearTimeout(t);
			movement = 0;
			timer=200;
		});
*/				




		/* Update image when colour is selected */

		$("#attribute129").change(function(){
			selectedcolour = $("option:selected",this).text().toLowerCase();
			$(".thumblist a span").each(function(){
				if ($(this).text().toLowerCase()==selectedcolour){
					$(this).parent("a").click();
				}
			});
		});


		/* Parallax Timelines */
				var viewportwidth = parseInt($("#parallax").width());
				var viewportheight = parseInt($("#parallax").height());
				$("#parallax").append("<div id='marker'></div>");
				$("#parallax").mousemove(function(e) {
					newX = (e.pageX- $("#parallax").offset().left);
					newY = (e.pageY- $("#parallax").offset().top);
					if (newY>0) { //400) {
						$("#parallax .moves").each(function(){
							rangeX = ($(this).width()-viewportwidth);						   					
							thisX = 0-(rangeX*(newX/viewportwidth));
						 	rangeY = ($(this).height()-viewportheight);						   						
							thisY = 0-(rangeY*(newY/viewportheight));
							$(this).css({left:thisX});
						});
						$("#marker").css({left:newX});
					}
				});
				$("#parallax ul li img").click(function(){
					tempcontent = $(this).parent("li").html();
					$("#parallax").append("<div id='indetail' style='cursor:pointer;' title='Click here to return'>"+tempcontent+'<span style="position:absolute;background:#1a1a1a;background:rgba(0,0,0,0.6);bottom:90px;left:390px; height:1em; width:180px;text-align:center;padding:3px;border:1px solid white;display:block">Click to return</span></div>');
					$("#indetail").css({
						position:		"absolute",
						left:			0,
						top:			0-parseInt($("#parallax").height()),
						width:			940,
						height:			parseInt($("#parallax").height())-20,
						padding:		10,
						overflow:		'hidden',
						opacity:		0.2	
					});
					$("#indetail").animate({
						top:			0,
						opacity:		1	
					},500).click(function(){
						$(this).animate({opacity:0},400,function(){
							$(this).remove();
						});						
					});
				});





			/* ################################################# */

			/* BEGIN WHOLESALE */

			/* ################################################# */


			var wholesaletimer;
			$(".wholesalehomepanel > ul > li:first").addClass("active");
			$(".wholesalehomepanel > ul > li.active a").css({bottom:-80}).delay(1500).animate({bottom:0},1000);
			$(".wholesalehomepanel > ul > li.active").mouseover(function(){
				clearInterval(wholesaletimer);
			}).mouseout(function(){
				wholesaletimer = setInterval(function(){
					negative = 0-(parseInt($(".wholesalehomepanel > ul > li.active ul li:first").width())+4);
					$(".wholesalehomepanel > ul > li.active ul").animate({marginLeft:negative},1000,function(){
						$(".wholesalehomepanel > ul > li.active ul li:last").after($(".wholesalehomepanel > ul > li.active ul li:first"));
						$(this).css({marginLeft:0});
					});
				},6000);
			}).mouseout();  

			$(".wholesalesupportpanel").mouseover(function(){
				$(".wholesalesupportpanel").not(this).stop().animate({opacity:0.2},600);
				//$(this).css({opacity:1});
			}).mouseout(function(){
				$(".wholesalesupportpanel").stop().animate({opacity:1},600);

			});


			/* ################################################# */

			/* BEGIN START */

			/* ################################################# */


			var starttimer;
			starttimer = setInterval(function(){
				$(".homehomepanel ul").animate({marginLeft:-964},1000,function(){
					$(".homehomepanel ul li:last").after($(".homehomepanel ul li:first"));
					$(this).css({marginLeft:0});
				});
			},4000);
		
			var foreground;
			foreground = setInterval(function(){
				$("#foreground").animate({marginLeft:-100},500,function(){
					$(this).animate({marginLeft:0},500);
				});
			},4000);

	});

	function rotate() {
	
		clearTimeout(t);
		if (movement!=0) {
			currentframe = currentframe + movement;
			if (currentframe==36)
				currentframe = 0;
			if (currentframe==-1)
				currentframe = 35;
			column = currentframe % 6;
			row = (currentframe-column)/6
			xpos = 0-(460*column);
			ypos = 0-(460*row);
			jQuery("#image360").css({backgroundPosition:xpos+"px "+ypos+"px"});
		}
		t = setTimeout("rotate()",timer);
	}	



