/* HOLIDAY DEALS */
var show_deals = false;
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

if (year == 2011 && month == 12) {
	show_deals = true;
} else if (year == 2012) {
	if (month == 1 && day < 16) {
		show_deals = true;
	}
}

/* END HOLIDAY DEALS */

(function($){
    
	$.fn.navigation = function(){

		return this.each(function(){
            
			var $this = $(this);
			
			var $menu = $this.find(">ul");
			
			var $menu_items = $menu.find(">li");
			
			var $menu_all_items = $menu.find("li");
			
			var i=0;
            
			$menu_items.each(function(){
			
				var $menu_item=$(this);
				$menu_item.data("index",i); 
				$menu_item.data("parent_index",-1); 
			
				i++;
			
				var $submenu=$menu_item.find(">ul");
			
				var $submenu_items = $submenu.find(">li");
			
				$submenu_items.each(function(){
			
					var $submenu_item = $(this);
					$submenu_item.data("index",i); 
					$submenu_item.data("parent_index",$menu_item.data("index"));
			
					i++;
			
					var $submenu2=$submenu_item.find(">ul"); 
			
					var $submenu_items2 = $submenu2.find(">li");
			
					$submenu_items2.each(function(){
			
						$(this).data("index",i);
						$(this).data("parent_index",$submenu_item.data("index"));
			
						i++;
			
					});
			
				});
			});
           
			$this.find("a").each(function(){
			
				if($(this).attr("href")=='#'){
					var href="javascript:void(0)";
				}
				else {
	
		
					var href = '';
					if( $(this).attr("href").indexOf('site/') > -1) href = $(this).attr("href");
					else href = site_url+"#page/cms/"+$(this).attr("href").replace(site_url,"").replace("#","").replace(/^\//, '');
					
					$(this).attr("href",href);
		
					$(this).click(function(e){
					
						if(processing) {
							e.preventDefault();
							return false;
						}
					
						next_el = $(this).parent();
					
						$menu_all_items.removeClass("selected");
					
					
						$(this).parents("li").addClass("selected");
					
					});
				}
			});
		
		});
	
	}
    
    
$.fn.testimonial = function() {
	return this.each( function() {
		var $this=$(this);

		if( $this.find('.testimonial-slide').length <= 1) $this.find('.testimonial-link').css('display', 'none');

		var $wrapper = $this.find("div.testimonial-wrapper");
		var $slides = $this.find("div.testimonial-slide");
		var $link = $this.find("div.testimonial-link a");
		var total = $slides.size();
		var currentTestimonial = 0 ;
		var sliding = false;
		var show = function(index){
			sliding=true;
			$slides.eq(currentTestimonial).animate({"left":$wrapper.width()+100},1000,function(){
				$(this).css({"left":-$wrapper.width()-100});
			});
		
			$slides.eq(index).animate({"left":0},1000,function(){
				sliding=false;  
			});
			currentTestimonial=index;
		}
	
		$slides.eq(0).css('left',0);
	
		$link.click(function(e){
			e.preventDefault();
			if(sliding) return;
			
			var nextIndex = currentTestimonial+1;
			
			if(nextIndex==total) nextIndex = 0;
			
			show(nextIndex);
		})
	})
};

    
    $.fn.centered = function(){
             
        return this.each(function(){
            
            var $this = $(this);
            
            var w = $this.width();
            var h = $this.height();
            
            var center = function(){
		//$('#wrapper').css('margin', 0);

                var ww = $(window).width();
                var wh = $(window).height();
                
                var left = Math.max((ww-w)/2,0);
                var top = Math.max((wh-h)/2-5,0);
               //$this.stop().animate({"top":top , "left":left},10);
		$this.stop().css({'margin': 0, "top":top , "left":left},10);
                $('#copyright').css({ 'margin':0, 'top': top+$('#wrapper').height()+3, 'left':left });
            }
            
            $(window).bind("resize", center);
            
            center();
            
        });
    }
    
    $.fn.largeSlideshow = function(options){
        
        var settings = { 
            time  : 300
        }
        
        return this.each(function(){
            
            var $this = $(this);
            
            $this.find("img").wrap("<div class='slide' ></div>");
            
            var $slides = $this.find("div.slide");
            var $slideshowWrapper = $("<div class='large-slideshow-wrapper' ></div>").append($slides).appendTo($this);
            $("<div class='frame' ></div>").insertAfter($slideshowWrapper);
            var total_slides = $slides.size();
            var current_slide = 0;
            var animated = false;
            
            var next = function(){
                
                if(animated) return false;
                
                animated = true ;
                
                $slides.eq(current_slide).animate({"left":$this.width()},settings.time ,function(){$(this).hide()});
                
                current_slide+=1;
                
                if(current_slide==total_slides) current_slide = 0;
                
                $slides.eq(current_slide).css({"left":-$this.width()},settings.time).show().animate({"left":0},settings.time,function(){
                    animated = false;
                });
            }
            
            var $button = $("<a href='#' class='large-slideshow-button' ></a>").appendTo($this);
            
            $button.click(function(e){
                e.preventDefault();
                next();
            })
            
            $slides.eq(0).show();
            
        })
    }
    
    $.fn.smallSlideshow = function(options){
        
        var settings = { 
            time  : 300
        }
        
        return this.each(function(){
            
            var $this = $(this);
            
            $this.find("img").wrap("<div class='slide' ></div>");
            
            var $slides = $this.find("div.slide");
            var slide_width = $slides.eq(0).outerWidth(true);
            var $slideshowWrapper = $("<div class='small-slideshow-wrapper2' ></div>").width($slides.size()*slide_width).append($slides).appendTo($this);
            $slideshowWrapper.wrap("<div class='small-slideshow-wrapper' ></div>");
            $("<div class='small-slideshow-wrapper-round-left' ></div>").insertAfter($slideshowWrapper);
            $("<div class='small-slideshow-wrapper-round-right' ></div>").insertAfter($slideshowWrapper);
            var total_slides = $slides.size();
            var current_slide = -1;
            var animated = false;
            
            $slides.each(function(){
                
                $(this).append($("<div class='frame' ></div>"));
                
            })
            var next = function(){
                
                if(animated) return false;
                
                animated = true ;
                
                current_slide+=1;
                
                if(current_slide==total_slides) current_slide = 0;
                
                var tmp_slide = $slides.eq(current_slide).clone().appendTo($slideshowWrapper);
                $slideshowWrapper.animate({"right":-slide_width},settings.time,function(){
                    animated=false;
                    tmp_slide.replaceWith($slides.eq(current_slide));
                    $slideshowWrapper.css("right",0)
                });
            }
            
            var $button = $("<a href='#' class='small-slideshow-button' ></a>").appendTo($this);
            
            if($slideshowWrapper.width()>$this.width())
                $button.click(function(e){
                    e.preventDefault();
                    next();
                })
            
        })
    }
    
    $.fn.largeVideo = function(options){
        
        var settings = { 
            time  : 300
        }
        
        return this.each(function(){
            
            var $this = $(this);
            
            var $videos = $this.find("div.large-video-items");
            var total_videos = $videos.size();
            var current_video = 0;
            var animated = false;
            
            var next = function(){
                
                if(animated) return false;
                
                animated = true ;
                
                $videos.eq(current_video).animate({"left":$this.width()},settings.time ,function(){$(this).hide()});
                
                current_video+=1;
                
                if(current_video==total_videos) current_video = 0;
                
                $videos.eq(current_video).css({"left":-$this.width()},settings.time).show().animate({"left":0},settings.time,function(){
                    animated = false;
                });
            }
            
            var $button = $("<a href='#' class='large-video-button' ></a>").appendTo($this);
            
            $button.click(function(e){
                e.preventDefault();
                next();
            })
            
            $videos.eq(0).show();
            
        })
    }
    
    $.fn.autoSizeContent = function(){
       
        return this.each(function(){
            
            var $this = $(this);
            var $contents = $this.find("div.content-text,div.roadshow-description");
            
            $contents.each(function(){
                
                var $content_wrapper = $(this).parent();
                
                var $content = $(this);
                    
                var content_height = $content_wrapper.height()-$(this).position().top;

                var $next_el = $(this).next()
                while($next_el.size()==1){
                    content_height -= $next_el.outerHeight(true);
                    $next_el= $next_el.next();
                }

                $(this).height(Math.round(content_height/parseInt($content.css("line-height")))*parseInt($content.css("line-height")));
                    
            });
            
        })
    }
    
    $.fn.socialIcons = function(){
        
        $("#icons").show();

        $("body").addClass("hasSocialIcons");
        
        return this.each(function(){
            
            var $this=$(this);
            
            if($this.find("div.home-page").size()>0){

                var $home = $("#icons div.home-social");

                if($home.is(":hidden")){

                    $("#icons div.active").removeClass("active").fadeOut(500,function(){
                        $home.addClass("active").fadeIn(500);
                    })
                    

                }
            }
            else if($this.find("div.contact-page").size()>0){

                var $contact = $("#icons div.contact-social");

                if(!$contact.hasClass("active")){

                    $("#icons div.active").removeClass("active").fadeOut(500,function(){
                        $contact.addClass("active").fadeIn(500);
                    });
                    

                }
            }
            else if($this.find("div.purchase-page").size()>0){
                $("#icons").hide();
                
                $("body").removeClass("hasSocialIcons");
            }
            else {
                var $regular = $("#icons div.regular-social");

                if(!$regular.hasClass("active")){

                    $("#icons div.active").removeClass("active").fadeOut(500,function(){
                        $regular.addClass("active").fadeIn(500);
                    })

                }
            }
        })
    }
    
    $.fn.trips = function(){
        
        return this.each(function(){
            
           var $this = $(this);
           
           var $content = $this.find("div.trip-block-content");
           
           var default_height = $content.height();
           
           var max_height = $content.css("height","auto").height();
           
           $content.height(default_height);
           
           var $link = $this.find("a.trip-more-info");
            
           $this.data("open",false);
           
           $link.click(function(e){
               
               e.preventDefault();
               
               if($this.data("open")){
                   $content.stop().animate({height : default_height},100,function(){
                       
                       $this.data("open",false);
                       $link.text("Click here for more information.");
                       $("#page-wrapper").find("div.content-text").jScrollPane({
                           verticalDragMinHeight: 14,
                           verticalDragMaxHeight: 14,
                           verticalDragMinWidth: 15,
                           verticalDragMaxWidth: 15
                       });
                       
                   });
               }
               else {
                   $content.stop().animate({height : max_height},100,function(){
                       
                       $this.data("open",true);
                       $link.text("Click here for less information.");
                       $("#page-wrapper").find("div.content-text").jScrollPane({
                           verticalDragMinHeight: 14,
                           verticalDragMaxHeight: 14,
                           verticalDragMinWidth: 15,
                           verticalDragMaxWidth: 15
                       });
                       
                   });
               }
               
           })
        });
    }
    
    
    $.fn.defaultValues = function(){
        
        return this.each(function(){
            
            var $this = $(this);
            
            $this.data("default",$this.val());
            
            $this.blur(function(){
                
                if($this.val().replace(/[ ]+/g,"")==''){
                    $this.val($this.data("default"));
                }
                
            });
            
            $this.focus(function(){
                
                if($this.val()==$this.data("default")){
                    $this.val('');
                }
                
            });
            
        })
    }
    
    $.fn.homeSlide = function( timeout ){
        
        return this.each(function(){
            
            var $this = $(this);
            
            var $images = $this.find("img").not("#preload-image");
            var total = $images.size();
            var currentImg = -1;
            
            var display = function(index){
                
                if(currentImg!=-1)
                    $images.eq(currentImg).fadeOut(1500);
                else 
                    $this.find("#preload-image").fadeOut(1500);
                    
                $images.eq(index).fadeIn(1500,function(){
                    
                    currentImg=index;
                    
                    setTimeout(function(){
                        
                        var nextImg = currentImg+1;
                        if(nextImg==total) nextImg = 0;
                        
                        display(nextImg);
                        
                    },timeout);
                
                });
                
            }
            
            var preloadInterval = setInterval(function(){
                
                var imgs_loaded = true;
                
                $images.eq(0).each(function(){
                    var img = $(this).get(0);
                    
                    if(!img.complete ){
                        
                        imgs_loaded=false;
                    }
                    
                })
                
                if(imgs_loaded){
                    clearInterval(preloadInterval);
                    display(0);
                }
                
            },100);
            
            
        })
        
    };
    
    $.fn.roadshow = function(){
        
        return this.each(function(){
           
           var $this = $(this);
           
           var $roadshows = $this.find("div.roadshow-block");
           
           var $map_nodes = $("a.roadshow-node");
           
           var $map_info = $("#roadshow-instructions");
         
           var $hand = $("#roadshow-hand");
           
           var total = $roadshows.size();
           
           var currentRoadshow = 0 ;
           
           var show = function(index){
               
               $roadshows.eq(currentRoadshow).animate({"left":$this.width()+100},1000,function(){
                   
                   $(this).css({"left":-$this.width()-100});
               });
               
               $map_nodes.eq(currentRoadshow).removeClass("selected");
               
               var position = $map_nodes.eq(index).position();
               
               $hand.animate({"left":position.left, "top":position.top},1000);
               
               $roadshows.eq(index).animate({"left":0},1000);
               
               $map_nodes.eq(index).addClass("selected");
               
               currentRoadshow=index;
               
           }
           
           $map_nodes.each(function(i){
               
               $(this).data("location",$(this).attr("title"));
               
               $(this).removeAttr("title");
               
               $(this).click(function(e){
                   e.preventDefault();
                   show(i);});
               
               $(this).mouseover(function(){
                   $map_info.text("Click for "+$(this).data("location")).css("line-height","60px");
               });
               
               $(this).mouseout(function(){
                   
                   $map_info.empty();
                   
               })
           });
           
           $map_nodes.eq(0).addClass("selected");
           
           $roadshows.eq(0).css('left',0);
           
           $("#roadshow-next").click(function(e){
               e.preventDefault();
               
               var nextIndex = currentRoadshow+1;
               if(nextIndex==total) nextIndex = 0;
               
               show(nextIndex);
           })
           
           $hand.css({"left":$map_nodes.eq(0).position().left, "top":$map_nodes.eq(0).position().top})
            
        });
    }
    $.fn.audioPlayer = function(){
        
        return this.each(function(){
            
           var $this = $(this);
           
           var $audios = $this.find("a.audioPlayer");
           
           $("#music-container").empty();
           
           $audios.each(function(i,item){
           
                var mp3= $(this).attr("href");
                
                var audioHTML= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="niftyPlayer_'+i+'" ><param name="movie" value="assets/swf/niftyplayer.swf?file='+mp3+'&amp;as=0"><param name="quality" value="high"><embed src="assets/swf/niftyplayer.swf?file='+mp3+'&amp;as=0" quality="high" bgcolor="#FFFFFF" width="0" height="0" name="niftyPlayer_'+i+'" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';

                $("#music-container").append(audioHTML);

                $(this).data("playing", false).data("player_id","niftyPlayer_"+i);

                $(this).click(function(e){
                    e.preventDefault();
                    if($(this).data("playing")){
                         niftyplayer($(this).data("player_id")).pause();
                        $(this).data("playing",false).removeClass("playing");
                    }
                    else {
                        
                        if(currentPlaying && $(this).parents("div.page").attr("id")!=currentPlaying.parents("div.page").attr("id")){
                            niftyplayer(currentPlaying.data("player_id")).pause();
                        }
                        
                        $(this).data("playing",true).addClass("playing");
                        niftyplayer($(this).data("player_id")).play();
                        niftyplayer("niftyPlayer_"+i).registerEvent("onSongOver",'niftyplayer("'+$(this).data("player_id")+'").play()');
                        currentPlaying=$(this);
                    }
                    
                });
                
           })
           
        });
        
    }
    
    $.fn.videoPlayer  = function(){
        
        return this.each(function(){
            $(this).attr("href",$(this).attr("href").replace("watch?v=","embed/"));
            
            $(this).fancybox({
                width : 853,
                height : 510,
                autoScale :false,
                padding : 0,
                centerOnScroll : true ,
                overlayColor : "black",
                type : "iframe"
             });
            
        })
    }
    
    
    $.fn.homePlayer = function(){
        
        return this.each(function(){
            
           var $this = $(this);
           
           var $audios = $this.find("a.audioPlayer");
           
           var $play = $this.find("#home-player-play");
           
           var $pause = $this.find("#home-player-pause");
           
           $("#music-container").empty();
           
            var mp3= $this.find("div.home-mp3").html();
            var i = 0 ;
            
            var audioHTML= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="niftyPlayer_'+i+'" ><param name="movie" value="assets/swf/niftyplayer.swf?file='+mp3+'&amp;as=0"><param name="quality" value="high"><embed src="assets/swf/niftyplayer.swf?file='+mp3+'&amp;as=0" quality="high" bgcolor="#FFFFFF" width="0" height="0" name="niftyPlayer_'+i+'" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';

            $("#music-container").append(audioHTML);

            $this.data("playing", false).data("player_id","niftyPlayer_"+i);

            $play.click(function(e){
                e.preventDefault();
                if(!$this.data("playing")){
                    $this.data("playing",true).addClass("playing");
                    niftyplayer($this.data("player_id")).play();
                    niftyplayer("niftyPlayer_"+i).registerEvent("onSongOver",'niftyplayer("'+$this.data("player_id")+'").play()');
                    currentPlaying=$(this);
                }

            });
            
            $pause.click(function(e){
                e.preventDefault();
                if($this.data("playing")){
                     niftyplayer($this.data("player_id")).pause();
                    $this.data("playing",false).removeClass("playing");
                }
            })
           
        });
        
    }
    
})(jQuery);

var prev_el=null,next_el=null,direction,processing=false,last_location='',currentPlaying ;;

$(document).ready(function(){

	$("a[href='#'],a[href='"+site_url+"#']").live("click",function(e){
		e.preventDefault();
	});
	
	$("#wrapper").centered();
	
	$("#menu").navigation();
	if (show_deals) {
		window.setTimeout(function() {
			$.fancybox(
				'<div style="height:565px;width:855px;background:#fff;overflow:hidden;"><img src="assets/chateaupotelle/images/holiday_deals.jpg" style="border:none;" width="855" height="565" alt="Holiday Deals" usemap="#holidaymap" /><map name="holidaymap"><area shape="rect" coords="20,363,128,392" href="#page/cms/purchase/index" alt="Shop Now" /><area shape="rect" coords="780,363,832,392" href="mailto:matthew@chateaupotelle.com?subject=RE:%20Holiday%20VGS" alt="Email" /></map></div>',
				{
					overlayColor : 'black',
					autoScale : false,
					autoDimensions: true,
	
					padding : 0,
					hideOnContentClick : true,
					centerOnScroll: true
				}
			);
		}, 2000);
	}
	$.history.init(function(hash){
	
		if(hash=='') hash="page/cms/home";
	
		if(hash != "") {
			//window.console.log(last_location.split("?")[0] +"=="+ site_url+"#"+hash.split("?")[0]+ " = "+(last_location.split("?")[0] == site_url+"#"+hash.split("?")[0]))

			if(last_location.split("?")[0] == site_url+"#"+hash.split("?")[0]){
	
				var part_index = hash.indexOf("?")!=-1?parseInt(hash.split("?")[1]):0;
				//$("#menu").find("a").not(".no-link").filter(function(){return $(this).attr("href").replace(site_url+"#","")==hash}).trigger("click")
				$("#menu").find("a").not(".no-link").filter(function(){ return $(this).attr("href").replace(site_url+"#","")==hash }).trigger("click");
				$("#page-wrapper").find("div.page-frame").animate({"left":-$("#page-wrapper").find("div.page").eq(part_index).position().left},300);
				
				prev_el=next_el;
			}
			else {

				var $old_page=$("#page-wrapper").find("div.page-frame");
				var $new_page = $("<div class='page-frame'></div>");
				$("#menu").find("a").not(".no-link").filter(function(){ return $(this).attr("href").replace(site_url+"#","")==hash }).trigger("click");


				if(next_el==null){
	
					next_el=$("#menu").find("a").not(".no-link").filter(function(){return $(this).attr("href").replace(site_url+"#","")==hash}).trigger("click").parent();
				
				}
	
				processing=true;
	
	
				if(prev_el!=null && prev_el.data("index")>next_el.data("index")){
					$new_page.css({
						"top": -980,
						"left" : 0
					});
					$old_page.animate({"top":1980},1000,function(){
						$old_page.remove();
					});
				}
				else {
					$new_page.css({
						"top": 980,
						"left" : 0
					});
				
					$old_page.animate({"top":-1980},1000,function(){
						$old_page.remove();
					});
				}
	

				prev_el=next_el;
	
				$.post(hash,function(response){
					try { response = jQuery.parseJSON(response); } catch(e) { alert('error jq.m-415') };
					if(response.status) {
						$new_page.html(response.results.html);
						$new_page.width($new_page.children().size()*980);
						$new_page.find("a.next-page-link").unbind("click").click(function(e){
							e.preventDefault();
			
							if(next_el.parents("ul").size()==2){
							
								window.location=next_el.find(">ul >li >a").eq(0).trigger("click").attr("href");
							}
							else {
							
								if(next_el.next().size()==1){
									window.location=next_el.next().find(">a").trigger("click").attr("href");
								}
								else {
									window.location=next_el.parents("li:first").find(">a").trigger("click").attr("href");
								}
							}
						});
			
						$new_page.appendTo($("#page-wrapper"));
						
						$new_page.find("div.testimonial-box").testimonial();
						$new_page.find("div.large-slideshow").largeSlideshow();
						$new_page.find("div.small-slideshow").smallSlideshow();
						$new_page.find("div.large-video-area").largeVideo();
						$new_page.find("div.trip-block").trips();
						$new_page.find("#roadshow-wrapper").roadshow();
						$new_page.find("a.zoomLink").fancybox({
                                                    overlayColor : "black",
                                                    autoScale : false,
                                                    width : 853,
                                                    height : 510,
                                                    padding : 0,
                                                    hideOnContentClick : true,
                                                    centerOnScroll : true 
                                                });
						$new_page.find("a.youtube-video").videoPlayer();
						$new_page.autoSizeContent().socialIcons();
						$new_page.audioPlayer();

						$new_page.find("div.content-text,div.testimonial-text,div.roadshow-description,.jScrollPane").jScrollPane({
							verticalDragMinHeight: 22,
							verticalDragMaxHeight: 22,
							verticalDragMinWidth: 10,
							verticalDragMaxWidth: 10,
							showArrows: true,
							verticalArrowPositions: 'split'
						});

			
						if($new_page.find("div.home-page").size()>0){
							$("#menu div.meni_separator").hide();
							$new_page.find("#home-slides").homeSlide( 6000 );
							$new_page.find("div.home-player").homePlayer();
						}
						else {
							$("#menu div.meni_separator").show();
						}
						
						if($new_page.find("div.contact-page").size()>0){
						
							$("div.input-box input,div.text-area-box textarea",$new_page).defaultValues();
						}
						
						var part_index = hash.indexOf("?")!=-1?parseInt(hash.split("?")[1]):0;
						
						if(part_index)
							$new_page.css({"left":-$new_page.find("div.page").eq(part_index).position().left});
						
						$new_page.animate({"top":0},1000,function(){
						
							processing=false;
							document.title = response.results.pagedata.pagetitle + ' - ' + site_name;
						});
			
			
					} else {
						alert(response.error);
					}
				});
				
			}
			last_location=window.location.toString();
		}

	},

	{ unescape: ",/" });
});

function go_home(){
   window.location = site_url+"#";
   $("#menu").find("li").removeClass('selected');
   next_el = null;
   last_el = null;
}

function play(){
    
}
function pause(){
    
}

function validate_contact(){
    
    var $el = $("#first-name");
    
    if($el.val()==$el.data("default")){
        
        $el.focus();
        alert("Please enter your first name");
        
        return false;
    }
    
    if($el.val().length<3){
        
        $el.focus();
        alert("First name has to have at least 3 characters");
        
        return false;
    }
    
    $el = $("#last-name");
    
    if($el.val()==$el.data("default")){
        
        $el.focus();
        alert("Please enter your last name");
        
        return false;
    }
    
    if($el.val().length<3){
        
        $el.focus();
        alert("Last name has to have at least 3 characters");
        
        return false;
    }
    
    $el = $("#email-address");
    
    if(!$el.val().match(/^[0-9a-zA-Z_\-\.]+\@[0-9a-zA-Z_\-\.]+\.[a-zA-Z]{2,5}$/)){
        
        $el.focus();
        alert("Email isn't in valid format");
        
        return false;
    }
    
    var $confirm_el = $("#confirm-email");
    
    if($el.val()!=$confirm_el.val()){
        
        $confirm_el.focus();
        alert("Email addresses didn't match");
        
        return false;
    }
    
    $el = $("#state");
    
    
    if($el.val()==$el.data("default")){
        
        $el.focus();
        alert("Please enter your state");
        
        return false;
    }
    
    if($el.val().length<2){
        
        $el.focus();
        alert("Please enter your state");
        
        return false;
    }
    
    return true;
    
}


// Script for NiftyPlayer 1.7, by tvst from varal.org
// Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
  },

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};

function niftyplayer(name)
{
	this.obj = FlashHelper.getMovie(name);

	if (!FlashHelper.movieIsLoaded(this.obj)) return;

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};

	return this;
}





function shop_cart_add(product_id, qty) {
	$.post('site/purchase/x_cart-add', { 'product_id':product_id, 'quantity':qty }, function(response) {
		try { response = jQuery.parseJSON(response); } catch(e) { alert('error jq.m-1098') };
		if( response.success ) {
			document.location = '/#page/cms/purchase/cart';
		}
	});
}


function shop_register_update_field(that) {
	var f = document.forms['frm_shop_register'];
	that = $(that);

	var chk = f.shop_copy_billing;
	if(!chk) return;
	if(!chk.checked) return;

	var name = that.attr('name').split(/_/);
	name.shift()
	var which = name.shift();
	name = name.join('_');

	var fld = '';
	if(which == 'billing') fld = 'shipping';
	else if(which == 'shipping') fld = 'billing';
	if(!fld) return;

	fld = f['customer_'+fld+'_'+name];

	if(fld) fld.value = that.attr('value');

}

function shop_register_update_fields() {
	var chk = $('#chk_shop_copy_billing');
	if(!chk) return;	


	$('input,select').each(function() {
		if($(this).attr('name') && ($(this).attr('name').indexOf('customer_billing_')==0)) {
			shop_register_update_field($(this));
		}
	});
}











//var site_url ="http://localhost/winery/";
//var site_url = "http://dev2.chateaupotelle.com/";
