$(document).ready(function(){

	var site = {
		rate: 500,
		buffer: 250,
		hoverTimeout: 500,
		opacity: 0.9,
		easing: 'easeOutQuint',
		url: 'http://www.benpalmer.info/wp-content/themes/portfolio/'
	};

	if($('#masonry').length > 0){
		var $container = $('#masonry');
		
		if($('html').hasClass('ie7')||$('html').hasClass('ie8')){
			
			if($container.hasClass('work')){
				$container.masonry({
					itemSelector: '.item',
					isResizable: false,
					columnWidth: $container.width() / 3
				});
			}else{
				$container.masonry({
					itemSelector: '.item',
					isResizable: false,
					columnWidth: $container.width() / 4
				});
			}
			
		}else{

			if($container.hasClass('work')){
				
				$(window).resize(function(){
					var w = $(window).width();

					if(w > 1199){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 4
						});
					}else if(w > 959 && w < 1200){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 3
						});
					}else if(w > 767 && w < 960){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 3
						});
					}else if(w > 479 && w < 768){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 2
						});
					}else if(w < 479){
						$container.masonry('reload');
					}

				}).resize();

			}else{
				$(window).resize(function(){
					var w = $(window).width();

					if(w > 1199){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 5
						});
					}else if(w > 959 && w < 1200){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 4
						});
					}else if(w > 767 && w < 960){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 3
						});
					}else if(w > 479 && w < 768){
						$container.masonry({
							itemSelector: '.item',
							isResizable: false,
							columnWidth: $container.width() / 2
						});
					}else if(w < 479){
						$container.masonry('reload');

					}

				}).resize();
			}
			
		}

	};
	
	if($("#slideshow").length>0){

        $('#slideshow').cycle({
            fx : 'scrollHorz',
            easing : 'easeOutQuint',
            speed : 1000,
            timeout : 0,
            next:   '.pager .next', 
            prev:   '.pager .prev'
        });

		$(window).resize(function(){
			$('#slideshow img').css('width',$('#slideshow').width());
			$('#slideshow img').css('height',$('#slideshow').height());
			$('#slideshow').cycle('destroy').cycle({
	            fx : 'scrollHorz',
	            easing : 'easeOutQuint',
	            speed : 1000,
	            timeout : 0,
	            next:   '.pager .next', 
	            prev:   '.pager .prev'
	        });
		});

		/* touchSwipe support */
		$(function(){
			$("#slideshow").swipe({
				threshold : 50,
				swipeLeft : function(){
					$('#slideshow').cycle('next');
				},
				swipeRight : function(){
					$('#slideshow').cycle('prev');
				}
			});
		});

    };
	
	/* if on contact page */
	if($("#contact-form").length>0){
		$('p.warning').remove();
		$('.addy').hide();
		$('div.thanks').hide();
		$.ajax({
			type: "POST",
			url: site.url+'scripts/ajax/token.php',
			success: function(txt){
				$('fieldset#ghost').append('<input type="hidden" name="ts" value="'+txt+'" />');
			}
		});
		$("#contact-form").validate({
			rules: {
				name: "required",
				email:  {
					required: true,
					email: true
				},
				comments: "required"
			},
			errorElement : "span",
			errorPlacement : function(error,element){
				error.appendTo(element.prev())
			},
			messages: {
				name: "",
				email: "",
				comments: ""
			},
			submitHandler: function(form){
				$(form).ajaxSubmit({
					beforeSubmit: function(){
						//$('#contact-form input').attr('disabled','disabled');
						$('#contact-form').activity({ 
							color : '#999',
							align : 'right',
							valign : 'bottom',
							segments: 16, 
							steps: 3, 
							width:2, 
							space: 2, 
							length: 4,
							speed: 2
							});
					},
					success: function(){
						
						$('#contact-form').delay(site.rate*4).slideUp(site.rate, function(){ $('#contact-form').activity(false); });
						$('div.thanks').delay(site.rate*4).slideDown(site.rate);
					}
				});
			}
		});
	}
	
});







