/**
	To make your life easier, all the scripts that are loaded once the DOM has fully loaded have been written here.
	These scripts are linked to the Era Theme @ ThemeForest.net by Christopher 'crozer' Honninger
**/

$(document).ready(function() { // Everything within here will be loaded as soon as the document is ready
	
	eraFixes();
	
	/** Start the Era Slider **/
	$('div.era-slider').css('opacity', 0);
	$('div.era-slider').find('img').bind('load', function() {
		$('div.era-slider').animate({opacity: 1}, 600);
	});
	$('div.era-slider ul.slider').roundabout({
		duration: 800,
		minOpacity: 0.4,
		minScale: 0.8,
		btnPrev: '.previous',
		btnNext: '.next',
		reflect: true
	});
	
	/** Start the PrettyPhoto plugin **/	
	$('a[rel^="modal"]').prettyPhoto({
		animation_speed: 'normal', /* fast/slow/normal */
		opacity: 0.90, /* Value between 0 and 1 */
		show_title: true, /* true/false */
		theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		deeplinking: false,
		changepicturecallback: function(){if($('.pp_description').text() == 'undefined') $('.pp_description').css('display', 'none')},
		social_tools: false /* html or false to disable */
	});
	
	/** Start the Twitter-feed plugin **/
	$('.tweets').getTwitter({
		userName: "crozer",
		// The following are all default, so you can remove them if you like ;-)
		numTweets: 2,
		loaderText: "Loading tweets ..",
		fadeIn: true,
		truncate: 100
	});
			
	/** Dropdown/Submenu navigation **/
	$('div.navigation > ul > li:has(ul)').find('a:first').addClass('sub-link');
	$('.sub-link').append('<span class="sub-link-arrow"></span>');
	$('div.navigation > ul > li').hover(function() {
		if( $(this).find('ul') ) {
			$(this).find('ul').fadeIn(200);
			$(this).find('ul li ul').hide();
			$(this).find('a.sub-link').css('color', '#e16640')
			.children('.sub-link-arrow').css('background-position', '0 -3px').animate({marginTop: '17px'}, 200);
		}
	}, function() {
		if( $(this).find('ul') ) {
			$(this).find('ul').fadeOut(200);
			$(this).find('a.sub-link').css('color', '#444')
			.children('.sub-link-arrow').css('background-position', '0 0').animate({marginTop: '9px'}, 200);
		}
	});
	
	/** Start the Crew Accordion plugin **/
	if($('div.crew').length) {
		$('div.crew').vaccordion({
			animEasing: false,
			accordionW		: 960,
			accordionH		: 704,
			visibleSlices	: 4,
			expandedHeight	: 312,
			animSpeed		: 400,
			animOpacity		: 0.4,
			contentAnimSpeed: 900
		});
	}
  	
	/** All sorts of fixes ;-) **/
	function eraFixes() {
		// Era Slider controls
		$('div.era-slider').each(function() {
			var li = $('ul.slider').find('li'), i = 0, thumbs = $(this).find('li.thumbs').append('<ul></ul>');
			for(i=0; i<li.length; i++) {
				thumbs.find('ul').append('<li class="'+ i +'"></li>');
			}
			
			thumbs.find('ul li').each(function() {
				var slideClass = $(this).attr('class').split(' ')[0];
				$(this).click(function() {
					li.parent().roundabout_animateToChild(slideClass);
					return false;
				});
			});
			thumbs.find('ul li').first().addClass('active');
		});
		// Era Slider & jCycle controls' width
		$('ul.controls').each(function() {
			var width = 0, controls = $(this).find('.next, .previous, .thumbs');
			controls.each(function() {
				width += $(this).outerWidth(true);
			});
			$(this).css('width', width);
		});
		// Single Project effects
		$('div.portfolio-single div.right-col > a:has(img), div.post-entry a.post-image, div.post-entry a.full-image, div.post-entry a.left-image, div.post-entry a.right-image')
		.prepend('<div class="main-overlay"></div>');
		$('div.main-overlay').css('opacity', 0).hover(function() {
			$(this).stop().animate({opacity: 1}, 300);
			$(this).next('img').stop().animate({opacity: .6}, 300);
		}, function() {
			$(this).stop().animate({opacity: 0}, 300);
			$(this).next('img').stop().animate({opacity: 1}, 300);
		});
		$('div#comments .disqus-plugin li, #about-flickr li').hover(function() {
			$(this).stop().animate({opacity: .8}, 200);
		}, function() {
			$(this).stop().animate({opacity: 1}, 200);
		});
		// Start our scrolling-to-anchor function
		if(!($.browser.msie && $.browser.version == 7.0)) {
			$('a.scroll').click(function(event){
				event.preventDefault();
				var fullUrl = this.href,
					parts = fullUrl.split("#"),
					trgt = parts[1],
					targetOffset = $('#'+trgt).offset(),
					targetTop = targetOffset.top;
		
				$('html, body').animate({scrollTop: targetTop}, 800);
			});
		}
		// Enable our Portfolio filter
		$('div.portfolio-filter li a').first().addClass('active').end().addClass('gccolori');
		$('div.portfolio-filter li').not(':last').each(function() {
			$(this).append('<span>/</span>');
		}).end().find('a').mousedown(function() {
			if($(this).hasClass('active')) {
				// do nothing
			} else {
				$('div.portfolio-filter li').find('a.active').removeClass('active');
				$(this).addClass('active');
				var filterSelector = $(this).attr('data-filter');
				$('div.portfolio-grid').isotope({ 
					filter: filterSelector
				});
				$('div.portfolio-2col, div.portfolio-3col, div.portfolio-4col, div.portfolio-fullwidth, div.portfolio-erablocks')
				.isotope({ 
					filter: filterSelector,
					layoutMode: 'fitRows'
				});
									
			}
			return false;
		});
		$('div#era-blocks-container[class^=portfolio-] .item, div.content.about ul.portfolio-excerpt li').each(function() {
			if($(this).parent().hasClass('portfolio-2col')) $(this).find('.overlay').addClass('2col');
			if($(this).parent().hasClass('portfolio-3col')) $(this).find('.overlay').addClass('3col');
			if($(this).parent().hasClass('portfolio-4col')) $(this).find('.overlay').addClass('4col');
			if($(this).parents().hasClass('content about')) $(this).find('.overlay').addClass('4col');
			if($(this).parent().hasClass('erablocks-alt')) {
				$('.item:nth-child(4n-1)').addClass('alt'); $('.item:nth-child(4n)').addClass('alt');
				$('.item.alt').find('.left-col').addClass('left-col-swapped');
			}
		});
		// Truncate our text
		$('div.content.blog').each(function() {
			$(this).not('.blog-post').find('div.post-entry').each(function() {
				var p = $(this).find('p'), trunc = p.html().substr(0, 200) + ' ..', length = p.html();
				if(length.length >= trunc.length) p.html(trunc);
			});
			$(this).find('ul.more-posts li').each(function() {
				var p = $(this).find('p').not('.small'), trunc = p.html().substr(0, 180) + ' ..', length = p.html();
				if(length.length >= trunc.length) p.html(trunc);
			});
			$(this).find('div.recent-posts.widget li').each(function() {
				var p = $(this).find('p'), trunc = p.html().substr(0, 70) + ' ..', length = p.html();
				if(length.length >= trunc.length) p.html(trunc);
			});
			$(this).find('.era-recent-posts.widget li .overlay').each(function() {
				var p = $(this).find('a'), trunc = p.html().substr(0, 20) + ' ..', length = p.html();
				if(length.length >= trunc.length) p.html(trunc);
			});
		});
		$('div.right-box div.blog-feed p').each(function() {
			var footerp = $(this),
				limit = 60,
				str = footerp.html(),
				strtemp = str.substr(0, limit);
		
			str = strtemp + '&nbsp;..&nbsp;<span class="footer-hide">' + str.substr(limit, str.length) + '</span>';
			footerp.html(str);
		});
		// Global Color fixes..
		$('div.content.blog .main ul.list a, div.content.blog .list.widget a, div.content.blog div.main h1 a, div.content.blog div.main h2 a, div.content.blog div.main h3 a, div.content.blog div.main h4 a, div.content.blog div.main h5 a, div.content.blog div.main h6 a, a.post-method').addClass('gccolori');
		$('div.content.blog .meta-list .comments a, div.post-entry a.comments, div.content.blog .recent-posts.widget a, div.content.blog .recent-comments.widget p a, ul.more-posts p.small a, div.content.portfolio-single .right-col p a, div.post-entry p a, div#comments a:not(.post-method)').addClass('gccolor');
		// IE fixes..
		if ($.browser.msie){
			$('div.portfolio-single ul.project-images li:last-child').addClass('lastChild');		
			$('ul.arrow-list li').prepend('<span>â†’</span>');
			$('div.content.contact div.bottom-text div.right-col ul.meta-list').first().addClass('firstChild');
		}
		// FireFox fix
		$('.overlay h3 a').ellipsis(true);
		/// Placeholder cross-browser fix
		function supports_input_placeholder() {
			var i = document.createElement('input');
			return 'placeholder' in i;
		}
		if ( supports_input_placeholder() == false ) {
			$("input").each(function() {
				if ($(this).val() == "" && $(this).attr("placeholder") != "") {
					$(this).val($(this).attr("placeholder"));
					$(this).focus(function() {
						if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
					});
					$(this).blur(function() {
						if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
					});
				}
			});	
		}
	}
	
	$('#globalColor').ColorPicker({
		color: '#e16640',
		livePreview: false,
		onShow: function(colpkr) {
			$(colpkr).fadeIn(400);
			return false;
		},
		onHide: function(colpkr) {
			$(colpkr).fadeOut(400);
			return false;
		},
		onChange: function (hsb, hex, rgb) {
			$('.gccolor, div.portfolio-filter li a.active').css('color', '#'+hex);
			$('div.overlay, div.va-content').css('background', '#'+hex);
			$('.gccolori').each(function() {
				var el = $(this),
				  	attrs = $(this).attr('style');
				if(typeof attrs == 'undefined') var attrs = '';
					
					el.hover(function() {
							el.attr('style', 'color: #' + hex + ' !important;' + attrs);
					}, function() {
							el.attr('style', attrs);
					});
			});
		},
		onSubmit: function (hsb, hex, rgb, colpkr) {
			$(colpkr).ColorPickerHide();
			return false;
		}
	});
	
}); // End of DOM Ready function


$(window).load(function() { // Everything within here will be loaded once the window has loaded

	$('body').css('overflow-x', 'visible');
	

	
	/** Era Grid Styling/functionality **/
	$('div#era-blocks-container .item, div.content.about ul.portfolio-excerpt li').each(function() {
		var el = $(this),
			imgWidth = $(this).find('img').width(),
			ratio = 0.38,
			newWidth = (imgWidth*ratio).toFixed(0);
			
		// Set the Era Blocks' proportions
		if($('div#era-blocks-container .item').length) {
			if(newWidth < 200 && newWidth > 180) {
				el.addClass('small');
			} else if(newWidth > 200 && newWidth < 300) {
				el.addClass('medium');
			} else {
				el.addClass('large');
			}
		}
			
		var p = el.find('.overlay p').not('.small'),
			title = el.find('.overlay h3 a').not('.2col h3 a, .3col h3 a, .4col h3 a'),
			titlePortf2 = el.find('.2col h3 a'),
			titlePortf3 = el.find('.3col h3 a'),
			titlePortf4 = el.find('.4col h3 a'),
			date = el.find('.overlay p.small');
		
		// Styling of specific layouts
		if(title.width() >= el.width()-44) $(title, el).css({ 'max-width': el.width()-60, 'text-overflow': 'ellipsis'});
		if(date.width() >= $(this).width()-44) $(date, el).css({ 'max-width': el.width()-60, 'text-overflow': 'ellipsis'});
		if(titlePortf2.width() >= el.width()-250) $(titlePortf2, el).css({ 'max-width': el.width()-282, 'text-overflow': 'ellipsis'});
		if(titlePortf3.width() >= el.width()-60) $(titlePortf3, el).css({ 'max-width': el.width()-60, 'text-overflow': 'ellipsis'});
		if(titlePortf4.width() >= el.width()-60) $(titlePortf4, el).css({ 'max-width': el.width()-60, 'text-overflow': 'ellipsis'});
		
		var img = $(this).find('img');
		if(img.height() < el.height()) $(img, this).css({width: 'auto', height: '100%'});
		
		// Show only fullscreen button if image is toosmall
		if($('div#era-blocks-container .item').length) {
			if(p.height() >= el.height()-42) {
				el.find('p, h3, p.small').css('display', 'none');
				el.find('a.fullscreen').css({
					position: 'absolute',
					marginLeft: (el.width()/2)-5,
					marginTop: (el.height()/2)-5
				});
			}
		}
	});
	
	/** Start the Isotope plugin **/
	if($('div#era-blocks-container').length) {
		$('div#era-blocks-container').isotope({
			itemSelector: '.item',
			masonry: {
				columnWidth : 1
			}
		});
	}
		
	/* Truncate our text.. AFTER Isotope has loaded ;-) **/
	$('div#era-blocks-container div.item, div.content.about ul.portfolio-excerpt li')
	.not('.portfolio-2col .item, .portfolio-fullwidth .item').each(function() {
		var el = $(this),
			p = $(this).find('p').not('.small');
			if($(this).parent().hasClass('portfolio-3col') || $(this).parent().hasClass('portfolio-4col') ||
			$(this).parents().hasClass('content about')) {
				var limit = 70;
			} else if($(this).parent().hasClass('portfolio-erablocks')) {
				var limit = 90;
			} else {
				var limit = (p.offset().top+p.height() >= $('p.small', el).offset().top) ? 60 : 100;
			}
			
			var str = p.html(),
				strtemp = str.substr(0, limit);
			
		// Truncate text based on overlay's size
		if(limit == 70 && str.length <= limit) {
			str = strtemp;
		} else { str = strtemp + '&nbsp;..&nbsp;<span class="overlay-hide">' + str.substr(limit, str.length) + '</span>'; }
		p.html(str);
		
	}), $('div[class^=portfolio-] .item, ul.project-images li, div.content.about ul.portfolio-excerpt li, div.content.blog .era-recent-posts.widget li').each(function() {
		var el = $(this);
		// Hide and enable Hovering of our overlay
		$('div.overlay').css('display','none');
		el.hover(function() {
			$('div.overlay', el).stop().fadeTo(400, .9);
		}, function() {
			$('div.overlay', el).stop().fadeTo(400, 0);
		});
		
	});
	
	// Portfolio Single & Blog image-oriented effects
	$('div.portfolio-single div.right-col ul.project-images li, div.content.about ul.portfolio-excerpt li, #about-flickr li, div.post-entry a.post-image, div.post-entry a.full-image, div.post-entry a.left-image, div.post-entry a.right-image')
	.each(function() {
		var el = $(this), img = $(this).find('img');
		if(img.height() < el.height()) $(img, this).css({width: 'auto', height: '100%'});
	});
	
	/** Start the jCycle plugin **/
	if($('div.jcycle').length) {
		$('div.jcycle').cycle({
			fx: 'scrollHorz',
			speed: 600,
			timeout: 0,
			pager: '.thumbs ul',
			next: '.controls .next',
			prev: '.controls .previous'
		});
	}
	
	// Final IE fix
	if ($.browser.msie){
		$('h3.era-slider-caption, div.big-message h3').css('font-family', 'Helvetica, Arial, sans-serif');
	}
	
}); // End of Window Load function
