////////////////////////////////////////
// Over Label
////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.not('.readonly').css("display", "none");
            }
            control.focus(function () {label.not('.readonly').css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};
$(document).ready(function(){
	// external links
	$('a[rel=external]').click(function(){
		window.open(this.href);
		return false;   
	});
	
	// Image Fade
	$('#homeSlide img').hide();
	$(window).bind('load', function(){
		$('#homeSlide img').fadeIn();
	});
	
	// Accordian Nav
	$('ul.accordian>li>a').click(function(){
		if ($(this).parents('li').hasClass('open')) {
			$(this).parents('li').removeClass('open').find('ul').slideUp();
		} else {
			$(this).parents('li').siblings()
			.removeClass('open').find('ul').slideUp()
			.end()
			.end()
			.end()
			.parents('li')
			.addClass('open').find('ul').slideDown();
		}
	}); 
	
	
	// Slide Shows
	if(jQuery.fn.cycle){
		var slides = $('#slide img').length;
		var itemPic = $('.itemPic img').length;
		var html = ' ';

		html += '<div class="slideControls">';
		html += '<span class="prev"><a href="#">&lt;</a></span>';
		html += '<span class="count"><span>1</span> / '+ slides +'</span>';
		html += '<span class="next"><a href="#">&gt;</a></span>';
		html += '</div>';

		function updateCount(idx, slide) {
			$('.count span').text(slide + 1);
		}

		if (slides > 1) {
			$('#slide').after(html).cycle({
				fx: 'fade',
				timeout: 0,
				speed: 500,
				prev: '.prev',
				next: '.next',
				prevNextClick: updateCount
			});
			
		}
		if (itemPic > 1) {
			$('.itemPic').cycle({
				fx: 'fade',
				timeout: 0,
				speed: 500,
				prev: '.prev',
				next: '.next',
				pager : '.itemTypes',
				pagerAnchorBuilder: function(idx, slide) { 
					return '.itemTypes li:eq(' + idx + ') a'; 
				}
			});
			
		}

	}
	
	// Thumb Overlays
	if(jQuery.fn.fancybox){
		$('.imageGrid li a').fancybox({
			overlayOpacity : .85,
			padding : 0
		});
		$('a.box').fancybox({
			overlayOpacity : .85,
			padding : 20,
			frameWidth : 400,
			frameHeight : 580,
			hideOnContentClick : false, 
			callbackOnShow : function(){
				$('label').overlabel();	
			}
		});
	}
	
	// Forms
	$('form label').overlabel();
	
	// News Feed
	$('.feed a.thumb, #shopItems li a.thumb').hover(function(){
		$(this).parents('li').addClass('hover');
	}, function(){
		$(this).parents('li').removeClass('hover');	
	});
	

	// Flexi Pagination
	if(jQuery.fn.flexiPagination){
	    	
  	$(".images-news-feed").flexiPagination({
  		url: "/images-news?ajax=1",
  		currentPage: 1,
  		totalResults: 10000,
  		perPage: 10,
  		container: ".images-news-feed",
  		pagerVar : "p",
  		loaderImgPath: "/includes/style/images/loader.gif",
  		debug : 0
  	});
  };
  
  //Show / Hide photographer Bio
  $('#photo-bio-link').bind('click', function(){
    $('#slideshow').hide();
    $('.imageGrid').hide();
    $('#photo-bio').show();  
  });
  
  $('#slideshow-link').bind('click', function(){
    $('#photo-bio').hide();
    $('#slideshow').show();    
    $('.imageGrid').show();
  });
	
	//jQ Validate
	$('#contact-form').validate();
	
	//Blog post Images caption
	$('.blog-post img').each(function(){
    $(this).after("<p class='blog-caption'>"+$(this).attr("title")+"</p>");
	});
  
});

