$(function() {
	var pageType = $("#main").attr("rel");
	$("nav li." + pageType).addClass("current");
	
	$("#bid").bind("mouseover mouseout click", hoverBid);
	if(!$.browser.webkit){
		$("#call-today a").attr("href", "#");
	}
	
	if($("#contact-form").length){
		$("#contact-submit").click(function(e){
			e.preventDefault();
			$(this).submit();
		});
		$("#contact-form input:text, #contact-form textarea").toggleVal({ populateFrom: "label", removeLabels: true });
		$.validator.addMethod("default", function(value, element){
			return this.optional(element) || value != $(element).data("defText");
		});
		$("#contact-form").validate({	
			errorElement: "span",
			submitHandler: function(form){
				$(form).ajaxSubmit({ 
					success: function(){ 
						$("#contact-form").fadeOut(function(){
							$("#success").fadeIn();
						});
					},
					error: function(a, b, e){ throw e; } 
				});
				return false;
			}
		});
	}
	
	function hoverBid(e){
		var clip = $("#clipboard"), 
			marginTop = clip.data("def-margin-top"),
			speed = "fast";
			
		if(!marginTop){
			marginTop = clip.css("margin-top"),
			clip.data("def-margin-top", marginTop);
		}
		
		if(e.type === "click"){
			return;	
		}
		
		if(e.type === "mouseover"){
			clip.stop().animate({ "margin-top": "-45px" }, speed);
			return;
		}
		clip.stop().animate({ "margin-top": marginTop }, speed);
	}

	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: "facebook", 
		animationSpeed:'slow',
		slideshow: 10000
	});
	
	if($("#bid-form").length){
		$("#bid-close").click(function(e){
			e.preventDefault();
			$("#bid-form").bPopup().close();
		})
		$("#bid-submit").click(function(e){
			e.preventDefault();
			$(this).submit();
		});
		
		$("#bid").click(function(e){
			e.preventDefault();
			$($(this).attr("href")).bPopup({
				modalClose: true
			});
			return false;
		});
		
		$("#bid-form-form input:text, #bid-form-form textarea").toggleVal({ populateFrom: "label", removeLabels: true });
		$.validator.addMethod("default", function(value, element){
			return this.optional(element) || value != $(element).data("defText");
		});
		$("#bid-form-form").validate({	
			errorElement: "span",
			submitHandler: function(form){
				$(form).ajaxSubmit({ 
					success: function(){ 
						$("#bid-form-form").fadeOut(function(){
							$("#bid-success").fadeIn();
						});
					},
					error: function(a, b, e){ throw e; } 
				});
				return false;
			}
		});
	}
	
	if($('#home-slider').length){
		function thumbClick(e, cycle, pause){
			$(this).siblings().removeClass("current");
			$(this).addClass("current");
			if(cycle===undefined||cycle){
				$('#home-slider .home-cycle').cycle($(this).index());
				log(pause);
				if(pause === undefined || pause){
					$('#home-slider .home-cycle').cycle("pause");
				}
			}
		}
		
		function onBefore(currSlideElement, nextSlideElement, options, forwardFlag){
			thumbClick.call($("#home-slider li:eq("+$(nextSlideElement).index()+")"), null, false);
			var newText = $(nextSlideElement).attr("alt");
			$("#home-slider-caption").animate({ "marginTop": "344px", 
												"height": 0,
												"padding-top": 0,
												"padding-bottom": 0
											}, 800, function(){
				$(this).text(newText);
				$("#home-slider-caption").animate({ "marginTop": "294px", 
													"height": "30px",
													"padding-top": "15px",
													"padding-bottom": "5px"}, 800);
			});
		}
		
		$('#home-slider .home-cycle').cycle({
			timeout: 5*1000, 
			speed: 1600,
			before: onBefore,
			fx: "fade"
		});
					
		if(!$.browser.msie||($.browser.msie&&$.browser.version==="9.0")){
			$("#home-testimonials").cycle({
				timeout: 8*1000, 
				speed: 2500,
				fx: "fade"
			});
		}
		
		$("#home-slider .home-thumbs").fadeIn(function(){
			var first = $("#home-slider .home-thumbs li").click(thumbClick).first();
			thumbClick.call(first, null, null, false)
			$("#home-slider .home-cycle").fadeIn(function(){
				$('#home-slider').css("background", "none");
			});
		});	
	}
	
	if($('#thumbnailSlider').length){
		var $thumbnailSlider= $('#thumbnailSlider'),
		$pg_photos= $('#pg_photos'),
		$pg_container	= $('#pg_container'),
		$pg_photos_elem	= $pg_photos.children(),
		$pg_photos_img	= $pg_photos_elem.find('img'),
		is_safari = navigator.userAgent.toLowerCase().indexOf('safari') > -1 && navigator.userAgent.toLowerCase().indexOf('chrome') == -1,
		pg_photos_width	= 0;
		
		function init(){
			$("#wall-wrapper").css("background", "#fff");
			initThumbnailSlider();
			configGallery();
			initEvents();
		}
		//init the thumbs slider plugin
		function initThumbnailSlider(){
			$thumbnailSlider.thumbnailSlider({ thumb_width	: 75,thumb_height: 75});
			var container = $("ul.ts_container"),
				width = $pg_photos_elem.length * 17
				left =$(window).width()/2 - (width / 2);
			container.css({ left: left , width: width }).fadeIn();
		}
		function configGallery(){
			//we need to set the width of the $pg_photos,
			//which is the sum of its childrens width
			$pg_photos_elem.each(function(){
				pg_photos_width += $(this).outerWidth(true) + 55;
			});
			$pg_photos.css({width: pg_photos_width+'px'});
		}
		function initEvents(){
			//if we mouse over a photo, show the description sored in the alt attr
			$pg_photos_elem.bind('mouseenter',function(e){
				var $e		= $(this);
					$img	= $e.find('img'),
					descr	= $img.attr('alt');
				
				$('<span>'+ descr +'</span>').hide().insertAfter($img).fadeIn();
			}).bind('mouseleave',function(e){
				var $e		= $(this);
					$descr	= $e.find('span');
				$descr.fadeOut();
			});
			$(window).hashchange(function(){
				//the index in the url hash
				var idx = location.hash.split('#')[1];
				if(!idx){
					//we assume the current is the first one,
					//so set the first thumb nav elem as selected
					$thumbnailSlider.thumbnailSlider('update',0);
					return;
				}
				//set the thumb nav elem as selected
				$thumbnailSlider.thumbnailSlider('update',idx-1);
				//based on the current left value for the pg_photos, 
				//and the left of the element, we know how much to slide
				var	current_left  = parseFloat($pg_photos.css('left'),10) + ( $(window).width()/2),
					elem_left = $pg_photos_elem.eq(idx-1).offset().left + ($pg_photos_elem.eq(idx-1).width()/2);
				//slide to element
				$pg_photos.stop().animate({left: current_left - elem_left + 'px'},800,'easeInOutExpo');
			});
			location.hash = "#1";
			$(window).hashchange();
		}
		
		//preload the images				
		$pg_photos_img.preload({
			onComplete	: init
		});
	}
});

