(function($)
{
	var load_btn = false;
	var sl = 
	{
		 thumbwidth: 179
		,init:function()
		{
			sl.slides = $(".images-thumbs-container");
			if(sl.slides.length == 0) return;
			sl.slides.each(function(i)
			{
				$(this).find("a").bind("click",sl.move)
						.colorbox({
							rel:'gallery-'+i,
							scalePhotos: true,
							maxHeight: '85%',
							top: 50
							}).eq(0)
						.addClass('active-thumb');
						
				$(this).data("count",$(this).find("a").length);
			});
			
			$(".images-thumbs-enlarge").bind("click",sl.trigger);
		}
		,move:function(e)
		{
			if(typeof e.altKey == "undefined") return;
			//e.preventDefault();
			var $self = $(this);
			var parent = $self.parent();
			var $as = parent.find("a").removeClass('active-thumb');
			if($as.length ==1) return;
			
			var index = ($self.next("a").length==0) ? 0 : $as.index(this)+1;
			$as.eq(index).addClass('active-thumb');
			parent.animate({marginLeft:-(index*sl.thumbwidth)+'px'},300);
			$("#"+parent.attr("id")+'-pagination').html((index+1)+" of "+parent.data("count"));
			return false;
		}
		,trigger:function(e)
		{
			e.preventDefault();
			$($(this).attr("rel")).find(".active-thumb").trigger("click");
		}
	};
	
	var gl =
	{
		 thumbwidth: 757
		,init:function()
		{
			gl.slides = $(".gallery-container1");
			if(gl.slides.length == 0) return;
			gl.slides.each(function(i)
			{
				$(this).find("a").bind("click",gl.move)
						//.colorbox({rel:'gallery-'+i}).eq(0)
						.addClass('active-thumb');
						
				$(this).data("count",$(this).find("a").length);
			});
			
		}
		,move:function(e)
		{
			if(typeof e.altKey == "undefined") return;
			//e.preventDefault();
			var $self = $(this);
			var parent = $self.parent();
			var $as = parent.find("a").removeClass('active-thumb');
			if($as.length ==1) return;
			
			var index = ($self.next("a").length==0) ? 0 : $as.index(this)+1;
			$as.eq(index).addClass('active-thumb');
			parent.animate({marginLeft:-(index*gl.thumbwidth)+'px'},300);
			//$("#"+parent.attr("id")+'-pagination').html((index+1)+" of "+parent.data("count"));
			return false;
		}
	}
	
	var gl2 =
	{
		 thumbheight: 700
		,interval: false
		,init:function()
		{
			gl2.slides = $(".gallery-container2");
			if(gl2.slides.length == 0) return;
						
			gl2.slides.find(".slide").css({top:'-'+gl2.thumbheight+'px'}).eq(0).css({top:0}).addClass("active-thumb");
			
			
			$(".gallery2").bind("click",function(){location.href = '/products';})
			.bind("mouseover",function()
			{
				clearInterval(gl2.interval);
			})
			.bind("mouseout",function()
			{
				gl2.interval = setInterval(gl2.move,4500);
			})
			.trigger("mouseout");
			
		}
		,move:function(e)
		{
			if(e&&e.preventDefault) e.preventDefault();
			var $self = gl2.slides.find(".active-thumb").removeClass("active-thumb");
			var $next = $self.next(".slide");
			
			if($next.length==0) $next = gl2.slides.find(".slide:first");
			
			$next.addClass("active-thumb");
			
			$self.animate({top:gl2.thumbheight+'px'},300,'',function(){$(this).css({top:'-'+gl2.thumbheight+'px'})});
			$next.animate({top:0},300);
			
		}
	}
	
	
	$(window).bind("load",function()
	{
		sl.init();
		gl.init();
		gl2.init();
		$(".clickclear").clickclear().trigger("blur");

		if(!/\.com\/$/g.test(location.href))
		{
			$btn = $('<a href="http://itunes.apple.com/ca/app/neuvo/id367256426?mt=8" id="app-button" target="_blank" />');
			$("body").append($btn);
			setTimeout(function(){$btn.animate({width:'122px'},200)},1000);
			
			$btn2 = $('<a href="http://www.neuvomonde.com/distribution/" id="dist-button" />');
			$("body").append($btn2);
			setTimeout(function(){$btn2.animate({width:'122px'},200)},850);

		}
		
	});
		
	$(document).bind("ready",function()
	{
		
		// features hover
		$("#products .product-features h4").hover(function(e){
			$(e.currentTarget).parent().addClass('hover');
			$(e.currentTarget).siblings('.info').stop().fadeIn(100);
		}, function(e){
			$(e.currentTarget).parent().removeClass('hover');
			$(e.currentTarget).siblings('.info').stop().fadeOut(200,function(){
				$(e.currentTarget).siblings('.info').css({opacity:1});	
			});
		})
		
		//distribution form validation
		$(document.contact).validate();
		
		
		// stocklist map
		if($("#stocklist_map").length){
			$("#stocklist_map").css({
				height: 500,
				width: '100%'
			});
			var myLatLng = new google.maps.LatLng(45.5004082,-73.5727303);
			stocklist_map.init('#stocklist_map', myLatLng, 4);
			
			var markers_xml = $("#markers_xml").html();
			
			
			stocklist_map.placeMarkers(markers_xml);
		}
		
	});
	
	
	jQuery.send = function()
	{
		var $form = $(document.contact);
		var formvalid = $form.valid();
				
		if (!formvalid) return false;
		
		$.ajax({url:'http://www.danieljulien.com/neuvo-assets/contact.php?jsonpcallback=?'
		,type:"GET",dataType:"jsonp",jsonp:"jsonpcallback"
		,data:'_rand='+Math.random()+"&"+$form.serialize()
			,success: function(data)
		{
			debug(data);
			alert(data.msg);
			if(data.success) $form.get(0).reset();
			
		}
		,error:function(data){debug(data);}});
	}
	
	
	var stocklist_map = {
	  bounds: null,
	  map: null
	}
	stocklist_map.init = function(selector, latLng, zoom) {
	  var myOptions = {
	    zoom:zoom,
	    center: latLng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	  }
	  this.map = new google.maps.Map($(selector)[0], myOptions);
	  this.bounds = new google.maps.LatLngBounds();
	}
	stocklist_map.placeMarkers = function(filename) {
	  
	  xml = filename;
	  //$.get(filename, function(xml){
	    $(xml).find("marker").each(function(){
	      var name = $(this).find('name').text();
	      var address = $(this).find('address').text();
	      var website = $(this).find('website').text();
	      // create a new LatLng point for the marker
	      var lat = $(this).find('lat').text();
	      var lng = $(this).find('lng').text();
	      var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
	      // extend the bounds to include the new point
	      stocklist_map.bounds.extend(point);
	      // add the marker itself
	      var marker = new google.maps.Marker({
	        position: point,
	        map: stocklist_map.map,
	        icon: 'http://www.danieljulien.com/neuvo-assets/images/neuvo-marker.png'
	      });
	      // create the tooltip and its text
	      var infoWindow = new google.maps.InfoWindow();
	      var html='<b>'+name+'</b><br />'+address;
	      if(website != '') html += '<br /><br /><a target="_blank" href="' + website + '">View website</a>';
	      // add a listener to open the tooltip when a user clicks on one of the markers
	      google.maps.event.addListener(marker, 'click', function() {
	        infoWindow.setContent(html);
	        infoWindow.open(stocklist_map.map, marker);
	      });
	    });
	    // Fit the map around the markers we added:
	    stocklist_map.map.fitBounds(stocklist_map.bounds);
	  //});
	}
	
	
})(jQuery);


