/* Author: Author: Joey Dehnert for PixelGrip Web Marketing & Development Experts 

*/
if($('#index-slider').length){
	$('#index-slider').cycle({
		fx: 'fade',
		timeout: 4000,
		speed: 1000,
		pager:  '#slider-nav-social nav',
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '#slider-nav-social nav a:eq(' + idx + ')'; 
		}
	});
}

$('#pause-play').live('click', function() {
	
	if($(this).hasClass('play')){
		$('#index-slider').cycle('resume');
		$(this).removeClass('play');
	} else {
		$('#index-slider').cycle('pause');
		$(this).addClass('play');
	}

});

if($('#interior-header-videolinks').length){
	
	var listCount = $('div#interior-header-videolinks section').length - 1;
	
	$('div#interior-header-videolinks section').each(function(i){
		$(this).addClass('vid-thumb-' + i);
	});
		
	for (i=0;i<=listCount;i++){
	
	// console.log($("section.vid-thumb-" + i + " p a.interior-view-video").attr("href"));
	
	var url = $("section.vid-thumb-" + i + " p a.interior-view-video").attr("href"),
		vidId = url.substring(17,25),
		fullUrl = "http://player.vimeo.com/video/" + vidId + "?title=0&amp;byline=0&amp;portrait=0&amp;color=ff9933";
		
	    $("section.vid-thumb-" + i + " a.interior-view-video").fancybox({
	      'padding'       : 0,
	      'autoScale'     : false,
	      'transitionIn'  : 'fade',
	      'transitionOut' : 'fade',
	      'width'         : 650,
	      'height'        : 362,
	      'href'          : fullUrl,
	      'type'          : 'iframe'
	    });
	
	}
	
}

if($('#gallery-sub-nav').length){	
		var hideTimeoutId;
		$('a.pref-text').click(function () {
			$(this).siblings('.dropdown , .jScrollPaneTrack').removeClass('hidden').addClass('visible');
			$(this).addClass('selected');
		});
		// Setting timeout for dropdown (faking the functionality of real dropdown)
		$('.dropdown , .pref-text').mouseover(function () {
			window.clearTimeout(hideTimeoutId);
		});
		
		$('.dropdown , .pref-text').mouseout(function () {
			hideTimeoutId = window.setTimeout(function () {
				$('.dropdown, .jScrollPaneTrack').addClass('hidden').removeClass('visible');
				$('.pref-text').removeClass('selected');
			}, 10);
		});
		
		// Setting the text of dropdown heading link to the text of selected option dropdown link
		$('#archives-pane li a').click(function () {
			$(this).parent().parent().parent().parent().parent().siblings().children().text( $(this).text() );
			$(this).parent().parent().parent().parent().parent().addClass('hidden');
			$(this).parent().parent().parent().parent().parent().siblings('.pref-text').removeClass('selected');
			$('.archives .jScrollPaneTrack').addClass('hide');
			
		});
}

if($('.page-template-template_gallery-php').length){

	// $(document).ready(function() {
	// 
	//     function callback(hash)
	// 	{
	// 	   
	// 	}
	// 	$(document).ready(function() {
	// 	    $.history.init(callback);
	// 	    $("a[@rel='history']").click(function(){
	// 	        $.history.load(this.href.replace(/^.*#/, ''));
	// 	        return false;
	// 	    });
	// 	});
	// 
	// });

	$('.thumb, section.thumb-description p a, a.magnify').click(function(){
		
		var currentId = $(this).attr('id');
		// window.location.hash = '#single-' + currentId;
																				
		// Hide grid items
		$('.thumb').hide();
		$('#prev-next-general').hide();
		$('.inside-frame').removeClass('off');	
							
		// Show single items
		
		$('#gallery-single-contain').removeClass('hidden');
		$('.no-frame').addClass('gallery-frame');
		$('.gallery-frame img').attr('style', 'position: absolute; top: 0px; left: 0px;');	
				
		$('#faux-pagination').cycle({ 
		    fx: 'fade', 
		    timeout:  0, 
		    speed:    200, 
		    prev:    '.prev', 
		    next:    '.next',
		    startingSlide: currentId
		});

		$('.slideshow').cycle({ 
		   	fx: 'fade', 
		    timeout:  0, 
		    speed:    200, 
		    prev:    '.prev', 
		    next:    '.next',
		    startingSlide: currentId
		});									
	
		$('section#case-description').height($('section.active').height());	
				
	});

	$('a.next').click(function(){
		if($('section.slide:last').hasClass('active')){
			$('section#case-description').height($('section.slide:first').height());
		} else {
			$('section#case-description').height($('section.active').next().height());
		}
	});
	
	$('a.prev').click(function(){
		if($('section.slide:first').hasClass('active')){
			$('section#case-description').height($('section.slide:last').height());
		} else {
			$('section#case-description').height($('section.active').prev().height());
		}
	});
		
	$('.grid-view-btn').live('click', function(){
		
		// window.location.hash = '#grid';
		
		$('section.active').removeClass('active');
		$('#faux-pagination').cycle('destroy'); 
		$('.slideshow').cycle('destroy'); 
		
		
		// Show grid items
		$('.thumb').show();
		$('#prev-next-general').show();
		$('.inside-frame').removeClass('on');	
		
		// Hide single items
		
		$('.no-frame').removeClass('gallery-frame');
		$('#gallery-single-contain').addClass('hidden');
			
	});
	
	if($('body.page-template-template_gallery-php').length){	

		$('section.thumb-description').each(function(){
		
			var thumbDescription = $(this).find('p').find('span').text().length;
			var abbrDescription = $(this).find('p').find('span').text().substr(0,80);
					
					if($(this).find('p').find('span').text().length > 80){
						
						$(this).find('p').find('span').text(abbrDescription).append('...');
						
					} else {
						$(this).find('p').css('margin-bottom', '16px')
					}
				
		})

	}	
	
$(function() {

	//Define Global Settings
	var imagesPerPage = 9;
	var startingPageNumber = 0;
	var totalImages = parseInt($('.thumb').last().attr('id')) + 1;
	//Init: Hide all thumbs and show any for the first page.
	$('.thumb').addClass('hidden');
	for (i=0;i<imagesPerPage;i++)
	{
	$("#" + i).removeClass('hidden');
	}
	$(".prev-pager").addClass('hidden');
	if(totalImages <= imagesPerPage) $(".next-pager").addClass('hidden');

	//Main Page Changing Function.  Hide all thumbs, unhide new page.
	function changePages(pageNumber){
	var pageMaxNumbers = pageNumber + imagesPerPage;
	$('.thumb').addClass('hidden');
	for (i=startingPageNumber;i<pageMaxNumbers;i++)
	{
	$("#" + i).removeClass('hidden');
	}
	if(pageNumber <= 0) {
	$(".prev-pager").addClass('hidden');
	} else {
	$(".prev-pager").removeClass('hidden');
	}
	if(pageNumber >= (totalImages - imagesPerPage)) {
	$(".next-pager").addClass('hidden');
	} else {
	$(".next-pager").removeClass('hidden');
	}
	}
	//NEXT Click function
	$('.next-pager').live('click', function(){
	startingPageNumber += imagesPerPage;
	changePages(startingPageNumber);
	});

	//PREVIOUS Click Function
	$('.prev-pager').live('click', function(){
	if(startingPageNumber > 0) startingPageNumber -= imagesPerPage;
	changePages(startingPageNumber);
	});
	});
	
}

//------------------- for clearing and replacing text in form input fields and textareas -------------------//

// function clearText(thefield) {
//   if (thefield.defaultValue==thefield.value) { thefield.value = "" }
// } 
// function replaceText(thefield) {
//   if (thefield.value=="") { thefield.value = thefield.defaultValue }
// }

$('.wpcf7 input').focus(
	function(){
		$(this).attr('value', '');
	} 
);

$('.wpcf7 textarea').focus(
	function(){
		$(this).attr('value', '');
	} 
);

$('.wpcf7 input').blur(
	function(){
		if ($(this).val() == "") {
		var valueDefault = $(this)[0].defaultValue;
		console.log(valueDefault);
		$(this).attr('value', valueDefault);
	} 
	}
);

$('.wpcf7 textarea').blur(
	function(){
		if ($(this).val() == "") {

		var valueDefault = $(this)[0].defaultValue;
		$(this).attr('value', valueDefault);
	} 
	}	
);
