// SLIDESHOW SETTINGS (times are in milliseconds)
var slide_pauseTime = 5000;	// Default speeds
var slide_fadeTime = 1000;
var slide_slideVer = false;
var slide_slideHor = false;
var capt_slideVer = false;
var capt_slideHor = false;

var autoplay = false;

var paginateNavTotal = 6;

//how much items per page to show  
var show_per_page = 5; 

var slide_switch;
var globalDeactivate = false;
var thumbnavDeactivate = false;

if ( typeof jQuery == 'function' ) {

jQuery(document).ready(function($){
    
/* = Gallery Enlarge
------------------------------------------------------------- */
	if ( jQuery('body').is('.gallery') ) {
		//console.log('photo-gallery');
		jQuery('.galleryshow li .photo a').hover(function(){
			jQuery(this).parent().siblings('.enlarge').children('a').addClass('hover');
			//$(this).parent().siblings('.enlarge').children('a').css('backgroundPosition', 'left bottom');
			//$('.galleryshow .enlarge').css({'background-position': 'left top'});
		}, function(){
			jQuery(this).parent().siblings('.enlarge').children('a').removeClass('hover');
			//$(this).parent().siblings('.enlarge').children('a').css('backgroundPosition', 'left top');
			//$('.galleryshow .enlarge').css({'background-position': 'left bottom'});
		});
		jQuery('.galleryshow li .photo a, .enlarge a').colorbox();
	}
    
/* = SLIDESHOW
------------------------------------------------------------- */
	
	/* Make slideshow active if js is enabled */
	jQuery('#main .galleryshow').addClass('slideshow');
	/**/
	jQuery('.slideshow').each(function(i){
		//if ( $.getUrlVar('id') ) {
		if (! jQuery(this).children('li.active').length ) jQuery(this).children('li:first').addClass('active');
		jQuery(this).children('li').css({opacity: 0.0}).filter('li.active').animate({opacity: 1.0}, slide_fadeTime);
	});
	/**/
	
	if ( jQuery('body').is('.home') ) {
		jQuery('.slideshow-nav')
			.paginateThumbs({'perPage':6});
		autoplay = true;
	} else if ( jQuery('body').is('.gallery') ) {
		/* Adds previous and next search */
		jQuery('.slideshow').after('<ul class="slideshow-search structural clearfix"><li class="prev"><a href="#" title="View Previous">Previous</a></li><li class="next"><a href="#" title="View Next">Next</a></li></ul>');
		jQuery('li.prev a').click(function(e){
			e.preventDefault();
			if (! globalDeactivate ) slide_switcher('previous');
			jQuery(this).blur();
		});
		jQuery('li.next a').click(function(e){
			e.preventDefault();
			if (! globalDeactivate ) { /*console.log(globalDeactivate);*/ slide_switcher(); }
			jQuery(this).blur();
		});
		
		/* Turn on play/pause and nav */
		jQuery('.slideshow-control, .slideshow-nav').show();
		
		/* Setup slideshow nav */
		jQuery('.slideshow-nav')
			.each(function(i){
				if (! jQuery(this).children('li.active').length ) jQuery(this).children('li:first').addClass('active');
				jQuery(this).show();
			})
			.paginateThumbs({'perPage':6});
			//.jPaginate({'items':'6', 'minimize':true, 'nav_items':'0'});
		jQuery('.slideshow-nav a').click(function(e){
			e.preventDefault();
			if (! jQuery(this).parent().hasClass('active') ) {
				//var nextClass = $(this).attr('class').split(' ')[0];
				slide_switcher($('.slideshow '+ jQuery(this).attr('href')), jQuery(this).parent());
				jQuery(this).parent().siblings('active').removeClass('active');
				jQuery(this).parent().addClass('active');
			}
			//return false;
			jQuery(this).blur();
		});
	
		/* Setup slideshow controls * /
		$('.slideshow-control .pause').click(function(){
			clearInterval(slide_switch);
			$(this).addClass('active');
			$('.slideshow-control .play').removeClass('active');
			return false;
		});
		$('.slideshow-control .play').click(function(){
			slide_switch = setInterval("slide_switcher()", slide_pauseTime);
			$(this).addClass('active');
			$('.slideshow-control .pause').removeClass('active');
			return false;
		}).addClass('active');
		/**/
	}
	
	/* Start the show */
	if ( autoplay ) slide_switch = setInterval("slide_switcher()", slide_pauseTime);
});

(function($){
	$.fn.paginateThumbs = function(opts){
		if (! opts ) opts = {};
		
		// Total per page
		var resultsPerPage = opts.perPage || 6;
		
		var $ul = $(this);
		var $li = $('li', this);
		
		// Total number of pages:
		var pagesNumber = Math.ceil($li.length/resultsPerPage);
		
		// Creating the controls div:
		var $controls = $('<div class="slideshow-nav-controls clearfix">');
		
		for ( var i=0; i<pagesNumber; i++ ) {
			// Slice a portion of the li elements, and wrap it in a swPage div:
			$li.slice(i*resultsPerPage, (i+1)*resultsPerPage).wrapAll('<div class="snc-page" />');
			
			// Add a link to the controls div:
			$controls.append('<a href="#'+ (i+1) +'" class="snc-show-page">'+ (i+1) +'</a>');
		}
		
		var widthCount = ( $li.length < resultsPerPage ) ? $li.length : resultsPerPage;
		$ul.css('width', (widthCount*145) +'px');
		$controls.css('width', i*17);
		
		// If the pages are less than two, do nothing:
		if ( pagesNumber < 2 ) return this;
		
		$ul.after($controls);
		
		// add prev / next controls
		$controls
			.prepend('<a href="#prev" class="snc-show-page-prev">Previous</a>')
			.append('<a href="#next" class="snc-show-page-next">Next</a>');
		jQuery('.snc-show-page-prev').click(function(e){
			e.preventDefault();
			jQuery('.snc-show-page-next').removeClass('inactive');
			var $active = $controls.find('a.active');
			var $next;
			/* Make it continuous * /
			$next = ( $active.prev('.snc-show-page').length ) ? $active.prev('.snc-show-page') : $controls.find('a.snc-show-page:last');
			$slider.stop().animate({'margin-left': -(parseInt($next.text()) - 1) * $ul.width()}, 'slow');
			$next.addClass('active').siblings().removeClass('active');
			/* Non continuous */
			if ( $active.prev('.snc-show-page').length ) {
				jQuery(this).removeClass('inactive');
				$next = $active.prev('.snc-show-page');
				$slider.stop().animate({'left': -(parseInt($next.text()) - 1) * $ul.width()}, 'slow');
				$next.addClass('active').siblings().removeClass('active');
				if (! $next.prev('.snc-show-page').length ) jQuery(this).addClass('inactive');
			}
			/**/
			jQuery(this).blur();
		});
		jQuery('.snc-show-page-next').click(function(e){
			e.preventDefault();
			jQuery('.snc-show-page-prev').removeClass('inactive');
			var $active = $controls.find('a.active');
			var $next;
			/* Make it continuous * /
			$next = ( $active.next('.snc-show-page').length ) ? $active.next('.snc-show-page') : $controls.find('a.snc-show-page:first');
			$slider.stop().animate({'margin-left': -(parseInt($next.text()) - 1) * $ul.width()}, 'slow');
			$next.addClass('active').siblings().removeClass('active');
			/* Non continuous */
			if ( $active.next('.snc-show-page').length ) {
				jQuery(this).removeClass('inactive');
				$next = $active.next('.snc-show-page');
				$slider.stop().animate({'left': -(parseInt($next.text()) - 1) * $ul.width()}, 'slow');
				$next.addClass('active').siblings().removeClass('active');
				if (! $next.next('.snc-show-page').length ) jQuery(this).addClass('inactive');
			}
			/**/
			jQuery(this).blur();
		});
		
		var $page = $ul.find('.snc-page');
		$page.wrapAll('<div class="snc-slider" />');
		var $slider = $ul.find('.snc-slider');
		var $links = $controls.find('a.snc-show-page');
		
		$links.click(function(e){
			e.preventDefault();
			jQuery('.snc-show-page-prev, .snc-show-page-next').removeClass('inactive');
			// If one of the control links is clicked, slide the swSlider div
			// (which contains all the pages) and mark it as active:
			jQuery(this).addClass('active').siblings().removeClass('active');
			$slider.stop().animate({'left': -(parseInt(jQuery(this).text()) - 1) * $ul.width()}, 'slow');
			
			if (! jQuery(this).prev('.snc-show-page').length ) {
				jQuery('.snc-show-page-prev').addClass('inactive');
			}
			
			if (! jQuery(this).next('.snc-show-page').length ) {
				jQuery('.snc-show-page-next').addClass('inactive');
			}
			jQuery(this).blur();
		});
		
		// Mark the first link as active the first time the code runs
		//$links.eq(0).addClass('active');
		$links.filter('a.snc-show-page:first').addClass('active');
		
		return this;
	};
})(jQuery);

}

// SLIDESHOW. Speed is controlled through JS, but placement and dimensions are controlled through CSS.
function slide_switcher( $nextSlide, $nextNavItem ) {
	globalDeactivate = true;
	jQuery('.slideshow').each(function(i){
		clearInterval(slide_switch);
		
		if ( jQuery(this).children().size() > 1 ) {
			var $active = jQuery(this).children('li.active');
			var $next;
			
			//if ( $nextSlide )
			if ( $nextSlide == 'previous' )
				$next = ( $active.prev().length ) ? $active.prev() : jQuery(this).children('li:last');
			else if ( $nextSlide && $nextSlide != 'next' )
				$next = $nextSlide;
			else
				$next = ( $active.next().length ) ? $active.next() : jQuery(this).children('li:first');
			
			var $activeNav = jQuery('.slideshow-nav li.active');
			var $nextNav = jQuery('.slideshow-nav li.'+ $next.attr('id'));
			/** /
			var $activeNav = jQuery('.slideshow-nav li.active');
			if (! $activeNav.length ) $activeNav = jQuery('.slideshow-nav li:last');
			/** /
			if ( $nextNavItem )
				var $nextNav = $nextNavItem;
			else
				var $nextNav = ( $activeNav.next().length ) ? $activeNav.next() : jQuery('.slideshow-nav li:first');
			/** /
			if ( $nextSlide == 'previous' )
				$nextNav = ( $activeNav.prev().length ) ? $activeNav.prev() : jQuery(this).children('li:last');
			else if ( $nextSlide && $nextSlide != 'next' )
				$next = $nextSlide;
			else
				$next = ( $active.next().length ) ? $active.next() : jQuery(this).children('li:first');
			/**/
			
			$active.addClass('last-active');
			
			var width = ( slide_slideHor ) ? $active.width() : 0;
			var height = ( slide_slideVer ) ? $active.height() : 0;
			
			$active
				.animate({opacity: 0.0, top: -height, left: width}, slide_fadeTime, function(){
					//jQuery('.gallery-caption li.'+this.className.split(' ')[0]).hide();
					jQuery(this).css({top: 0, left: 0});
					jQuery(this).removeClass('active last-active');
					$nextNav.addClass('active');
					$activeNav.removeClass('active');
				});
			$next
				.css({opacity: 0.0, top: height, left: -width})
				.addClass('active')
				.animate({opacity: 1.0, top: 0, left: 0}, slide_fadeTime, function(){
					jQuery('.gallery-caption li.'+this.className.split(' ')[0]).show();
					globalDeactivate = false;
					//jQuery('.gallery-caption li').text(this.className.split(' ')[0]);
				});
				
			$activeNav.removeClass('active');
			$nextNav.addClass('active').siblings().removeClass('active');
			
			if ( jQuery('.slideshow-control .play').length ) {
				if (jQuery('.slideshow-control .play').is('.active')) {
					slide_switch = setInterval("slide_switcher()", slide_pauseTime);
				}
			} else if ( autoplay ) {
				slide_switch = setInterval("slide_switcher()", slide_pauseTime);
			}
		}
	});
};

// new window popup used for opening up artwork and flash projects
function launchwin(uri,name,args){
	windowReference = window.open(uri,name,args);
	if (!windowReference.opener){
		windowReference.opener = self;
	}
	windowReference.focus();
}

function toggleReferralDivs(){
	if(document.getElementById("referralOption")){
		checkedValue = getChecked(document.forms[0].referralOption);
		if(checkedValue == "Yes"){
			document.getElementById("retail_dealer_name").style.width = "auto";
			document.getElementById("retail_dealer_name").style.display = "inline";
			document.getElementById("retail_dealer_name").style.visibility = "visible";
			
			document.getElementById("referral_type").style.width = "0px";
			document.getElementById("referral_type").style.display = "none";
			document.getElementById("referral_type").style.visibility = "hidden";
		} else {
			document.getElementById("retail_dealer_name").style.width = "0px";
			document.getElementById("retail_dealer_name").style.display = "none";
			document.getElementById("retail_dealer_name").style.visibility = "hidden";
			
			document.getElementById("referral_type").style.width = "auto";
			document.getElementById("referral_type").style.display = "inline";
			document.getElementById("referral_type").style.visibility = "visible";
		}
	}
}

function toggleVideoOptions(){
	if(document.getElementById("send_full_corporate_video")){
		if(document.getElementById("send_full_corporate_video").checked == true){
			document.getElementById("videoOptions").style.visibility = "visible";
		} else {
			document.getElementById("videoOptions").style.visibility = "hidden";
			document.getElementById("email_link").checked = false;
			document.getElementById("mail_dvd").checked = false;
		}
	}
}

function getChecked(formElementObject){
	for (i=0;i<formElementObject.length;i++){ 
		if (formElementObject[i].checked){ 
			return formElementObject[i].value; 
		} 
	}
	return false;
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = 'Character Limit: <span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

function toggleMaterialSummary(materialId){
	var divId = "materialSummary" + materialId;
	if(document.getElementById(divId)){
		if(document.getElementById(divId).style.visibility && document.getElementById(divId).style.visibility == "visible"){
			document.getElementById(divId).style.visibility = "hidden";
			document.getElementById(divId).style.height = "0px";
		} else {
			document.getElementById(divId).style.visibility = "visible";
			document.getElementById(divId).style.height = "auto";
		}
	}
}

function imageMapHover(imageId, rollover){
	if(document.getElementById(imageId)){
		if(rollover){
			if(rollover == 'pci_bellmont') pos = "0px -120px";
			else if(rollover == 'pci_amero') pos = "0px -60px";
			else if(rollover == 'pci_pacificcrest') pos = "0px 70px";
			document.getElementById(imageId).style.backgroundPosition = pos;
		} else {
			document.getElementById(imageId).style.backgroundPosition = "0px 0px";
		}
	}
}
