$(document).ready(function(){
    
    thumb_links();

	// Toggle photo set description
	$("#about_this_set").click(function(){
		$("#photo_set_description").slideToggle(500)
		return false;
	});
	
	// end of toggle photo set description

	$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 0, /* padding for each side of the picture */
			opacity: 0.9, /* Value betwee 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'dark_square' /* light_rounded / dark_rounded / light_square / dark_square */
		});
	
	$('ul#thumbs li a').click(function(){
		var page = $(this).attr('href');
		$('div#large_image').height($('div#large_image').height());
		$('div#large_image img').fadeOut('slow');
		$('div#content_pri').load(page +' #large_image, #image_description','',function(){
		
			$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 0, /* padding for each side of the picture */
			opacity: 0.9, /* Value betwee 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'dark_square' /* light_rounded / dark_rounded / light_square / dark_square */
		
		});
			tooltip();
		}).fadeIn('slow');
		return false;
	});
	
	
	$('ul.pag li a').click(function(){
	    $('ul.pag li.cur').removeClass('cur');
        $(this).parent().addClass('cur');
		var indexes = $('ul.pag li a').length; // need to make sure the number is even
        var n       = $('ul.pag li a').index($(this));
        
        var pos = (n*344);

        $('ul#thumbs').scrollTo({top:pos+'px', left:'0px'}, 800, {axis:'xy' });

        return false;

	});
	
	// ******* Homepage slideshow ******* //
	
	$('body.home div#content_sec').flash({
		swf: '/js/slideshow/slideshow2.swf',
		width: 720,
		height: 495,
		wmode: 'transparent'
	});
 

	// ******* Tabs ******* //
	$("#tabs").tabs();
	
	
	// ******* Search field ******* //
	// set the default text
	$('.keywords').focus(function(){
		var value = this.value;
		var title = this.title;
		if (value == title) {
			$(this).val("");
		};
		$(this).addClass("selected");
	});
	
	
	// if it's blank then reset back the title attribute
	$('.keywords').blur(function(){
		$(this).removeClass("selected");
		if(!$(this).val().length){
			$(this).val(this.title);
		}
	});
	
	// ******* Validation ******* //
	
	// comment form
	
	$("#comment_form").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			comment_message: "required"
		},
		messages: {
			name: "required",
			email: {
				required: "required",
				email: "invalid"
			},
			comment_message: "a message is required"
		}
	});


	// contact form
	
	$("#freeform").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			message: "required"
		},
		messages: {
			name: "required",
			email: {
				required: "required",
				email: "invalid"
			},
			message: "a message is required"
		}
	});
	
	

	
	// load tooltips

	tooltip();
	
	
	// scrolling testimonials
	
	function fadeTestimonials() {
		$('#testimonials').innerfade({
			animationtype: 'fade',
			speed: 2000,
			timeout: 15000,
			type: 'random',
			containerheight: '1em'
		});
	}
	
	fadeTestimonials();
	
	
	// Equal height columns
	
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	
	equalHeight($("div.eq"));
	

});

function thumb_links(){
    
    var thumbs = $('ul#thumbs li a').length;
    var links = Math.ceil(thumbs / 8);
    
    $('ul#thumbs').after('<ul class="pag"></ul>');
    
    for(var i = 0; i < links; i++) {
        
        var dsp = i + 1;
        $('<li><a href="">'+dsp+'</a></li>').appendTo('ul.pag');
        
    }
    
}

// tooltips
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};