/*
(function($) {
	
	$('html').removeClass('no-js');
	
	$.fn.rotate = function(options) {
		
		var total = $(this).children().length - 1;
		
		var defaults = {
		 	end: total,
			which: 0,
			prev: total,
			pause: 8000,
			fadeSpeed: 1500,
			hoverPause: true,
			hovering: false
		};
			
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			
			$kids = $(this).children();
			
			$kids.find('.homepage_title').each( function() {
				
				var imgSrc = $(this).attr('src');
				$(this).css('filter', " progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "') ");
				
			});
			
			$kids.children().animate({ opacity: 0 } , 0);
			$kids.eq(options.which).children().animate({ opacity: 1 } , options.fadeSpeed);
			
			(function fadeit() {
				
				$kids.eq(options.prev).find('img.description').animate({opacity: 0}, options.fadeSpeed / 2);
				$kids.eq(options.prev).find('img.homepage_feature').animate({opacity: 0}, options.fadeSpeed);
				
				$kids.eq(options.which).find('img.homepage_feature').animate({opacity: 1},options.fadeSpeed, function() {
					$(this).next().animate({opacity: 1},options.fadeSpeed);
				});
				
				options.which = (options.which == options.end) ? 0 : options.which+1;
				options.prev = (options.which == 0) ? options.end : options.which-1;
				
				setTimeout(fadeit, options.pause);
				
			})();
				
		});
	};
})(jQuery);
*/

$(function() {
	
	var $paneTarget = $('.port_thumbs'),
			current_page = 1,
			speed = 200,
			thumbs_per_page = 7,
			page_width = parseInt($paneTarget.find('a').outerWidth(true)) * (thumbs_per_page ),
			max_thumbnails = $paneTarget.find('a').length,
			max_page = max_thumbnails/thumbs_per_page;
	
	$('#left').addClass('arrow_off').css('background-position', '0 100%');
	
	function scrollThumbnails(whichDir, maxPage) {
		
		if(whichDir == 'right') {
			if(current_page < maxPage) {
				$paneTarget.animate( { left: '-='+page_width+'px'} , 300);
				current_page++;
			}
		}
		
		//arrow_off
		if(whichDir == 'left') {
			if(current_page > 1) {
				$paneTarget.animate( { left: '+='+page_width+'px'} , 300);
				current_page--;
			}
		}
		
		$('.port_thumb_dir').removeClass('arrow_off').css('background-position', '');
		
		if(current_page == 1) {
			$('#left').addClass('arrow_off').css('background-position', '0 100%');
		}
		
		if(current_page >= maxPage) {
			$('#right').addClass('arrow_off').css('background-position', '-17px 100%');
		}
		
	}
	
	if ($('div.port_landing_images').length > 0) {
		$('div.port_landing_images').isotope({
			masonry: {
				columnWidth: 73,
				rowHeight: 73
			}
		});
	}
	
	$('.port_item img').attr('data-src', this.src);
	
	// ROTATE HOMEPAGE SLIDES
	//$('#slides').rotate();
	
	if(max_thumbnails > thumbs_per_page ) {
		$('.port_thumb_dir').css('display','block');
	}
		
	$('.port_thumb_dir').click(function() {
		var direction = $(this).attr('id');
		
		scrollThumbnails(direction, max_page);
		return false;
	});
	
	// are we on a portfolio page 
	if ($('body').hasClass('page-template-template_portfolio-php')) {
		
		// controlling main viewing portfolio
		var $port_thumbs = $('.port_thumbs')
				,	$post = $('div.post')
				, $hide = $post.find('.port_item');
		
		$hide.hide().eq(0).show();
		
		$port_thumbs.find('a').hover( function() {
			var id = this.id,
					findID = id.replace(/(.*)_(.*)/, 'port_$2');
			
			$hide.stop().fadeTo(500, 0, function() {
				$(this).hide();
			});
			$('#'+findID).stop().show().fadeTo(500, 1);
			
		}, function() {
		
		});
		
		$port_thumbs.find('a').click( function() {
			var id = this.id,
					findID = id.replace(/(.*)_(.*)/, 'port_$2');
					
			
			if ($('#'+findID).css('opacity') == 0) {
				return false;
			}
			
		});
		
	}
	
	// controlling submenu
	if (!$('body').hasClass('page-id-8')) {
		$('.menu-item-portfolio-0 .sub-menu li').not('.view').hide();
	}
	
	$('#view_by_industry, #view_by_category').click( function() {
		
		$('.menu-item-portfolio-0 .sub-menu li').not('.view').hide();
		
		var hasher = '#'+$(this)[0].id.replace('_by', '');
		if (hasher != '#view_industry') {
			$('.menu-item-portfolio-0 .sub-menu li:lt('+$('.menu-item-portfolio-0 .sub-menu li.rule').index()+')').show();
		} else {
			$('.menu-item-portfolio-0 .sub-menu li:gt('+$('.menu-item-portfolio-0 .sub-menu li.rule').index()+')').show();
		}
	
		return false;
	});
	
	// directions js
	$('.directionLinks div').hide();
	$('.directionLinks a').click( function() {
		$('.directionLinks div').stop().hide();
		$(this).next().stop().show();
		return false;
	});
	
	// what sells
	$('.sellthis').hide().animate({opacity: 0}, 0);
	
	// IE hack to make hover work
	$('.what_sells').parent().css('background', 'url(#)');
	
	$('.what_sells').parent().hover( function() {
		$('.sellthis').stop().show().animate({opacity: 1}, 200);
	}, function() {
		$('.sellthis').stop().animate({opacity: 0}, 200).hide();
	});
	
	/* RETAIL PAGE */
	$rb = $('#retail_banner');
	
	if ($('body').hasClass('page-template-templateretail-php')) {
		
		// controlling main viewing portfolio
		var $retail_thumbs = $('.retail_thumbs')
				, $retail_hide = $('.retail_item');
		
		$retail_hide.hide().eq(0).show();
		
		$retail_thumbs.find('a').hover( function() {
			var id = this.id,
					justID = id.replace(/(.*)_(.*)/, '$2'),
					findID = 'port_'+justID;
			
			$retail_hide.stop().fadeTo(500, 0, function() { $(this).hide(); });
			$('#'+findID).stop().show().fadeTo(500, 1);
			
			$rb.find('h1').html($('#word_'+justID).val()).parent().css('background', '#'+$('#color_'+justID).val());
			
		}, function() {
		
		});
		
	}
	
	// external links
	$("a[href^=http]").each( function(){
	    if(this.href.indexOf(location.hostname) == -1) { 
        $(this).attr('target', '_blank');
      }
    }
  );
	
	/*
	$("a[rel=external]").each(function() {
		$(this).click(function(){
			window.open(this.href,"mywindow");
	    	return false;
	    });
	});
	*/
	
});

//popup windows
	var myWin = null;
	function gen_popup(theUrl, theWidth, theHeight) {
		theTitle = '';
		if ((!myWin) || (myWin.closed)) {
			theDim = 'width=' + theWidth + ',height=' + theHeight + ',left=10,top=10,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,';
			myWin = window.open(theUrl, theTitle, theDim);
			myWin.opener = window;
		} else {
			myWin.location = theUrl;
			myWin.focus();
			myWin.opener = window;
		}
	}

