$.noConflict();
jQuery(document).ready(function() {

// External Links
	//jQuery('a[href^="http://"]').attr("target", "_blank");
	jQuery('a[href^=""]').each(function(){ 
		if (this.hostname !== location.hostname) {
			jQuery(this).attr("target", "_blank");
		}
	});
	
// Ajax Wrappers
	jQuery('#top').before('<div id="top-loader" class="loader section" />');
	jQuery('#footer').after('<div id="load-2" class="loader section" />');
	jQuery('#footer').after('<div id="base" class="section" />');
	
// Show more blog posts
function bloggle() {
	jQuery('.page-item-9.current_page_item').live('click', function(event) {
		event.preventDefault();
		event.stopPropagation();
		if ( jQuery(this).hasClass('expanded_page_item') == false ) {
			jQuery('#topbar .widget').animate({ height: '327px' }, 'medium');
			jQuery('#more-blogs').text('\u25B2');
			jQuery('#topbar .perma-blog-link').slideDown('medium');
		} else {
			jQuery('#topbar .widget').animate({ height: '108px' }, 'medium');
			jQuery('#more-blogs').text('\u25BC');
			jQuery('#topbar .perma-blog-link').slideUp('medium');
		}
		jQuery(this).toggleClass('expanded_page_item');
		return false;
	});
}

	jQuery('.single.cat-3-id .page-item-9').click(function() {
		jQuery('.cat-3-id.single #top .cat-post').delay(250).css('display', 'block').slideDown('medium');
	});

// Hover meta info 
	//jQuery('.init .meta > a, .post.category-blogg .meta > a, .cat-post .post .meta > a').animate({ opacity: 0 }, 'fast');
	//jQuery('.post.init, .post.category-blogg, .cat-post .post').hover(function() {
	//	jQuery(this).find('.meta > a').animate({ opacity: 1 }, 'fast');
	//},
	//function() {
	//	jQuery(this).find('.meta > a').animate({ opacity: 0 }, 'fast');
	//});
	
// Hover cat info 
	jQuery('.cat-post .cat-description').css('display', 'none');
	jQuery('.cat-post .widgettitle').hover(function() {
		jQuery(this).parent().find('.cat-description').fadeIn('fast');
	},
	function() {
		jQuery(this).parent().find('.cat-description').fadeOut('medium');
	});
	
	
// Open post loading state
	jQuery('.init.post .title a, .init.post .meta a.more-link, .init.post .image-wrapper a').click(function() {
		jQuery(this).parent().parent().addClass('loading').animate({opacity: 0.5}, 'slow');
	});
	
	
// Hover image details 
	jQuery('.quintet .image-details').animate({ opacity: 0 }, 'fast');
	jQuery('.quintet .image-wrapper').hover(function() {
	  jQuery(this).find('.image-details').animate({ opacity: 1 }, 'fast');
	},
	function() {
	  jQuery(this).find('.image-details').animate({ opacity: 0 }, 'fast');
	});


	//jQuery('#page-nav-1 li li a').addClass('target');


// Add blog expansion toggle
	jQuery('#page-nav-1 li li').first().addClass('current_page_item').prepend('<a href="#" id="more-blogs">&#9660;</a>');
	bloggle();

	jQuery('#page-nav-1 li li').first().click(function() {
		// jQuery(this).parent().prepend('<a href="#" id="more-blogs">&#9660;</a>');
		// bloggle();
	});
	
// Ajax top panel
	jQuery('#page-nav-1 li.page_item a').live('click', function(event) {
		console.log(jQuery(this));
		if (!jQuery(this).parent().hasClass('current_page_item')) {
			jQuery('#page-nav-1 li').removeClass('current_page_item');
			jQuery(this).parent().addClass('loading');
			jQuery('#top-loader').slideDown('medium');
			jQuery('#page-nav-1 #more-blogs').remove();
			jQuery('#topbar').slideUp('medium');
			var goto = jQuery(this).attr('href');
			var gotoid = goto + ' #pages';
			var menuitem = jQuery(this).parent();
			jQuery('#topbar').load(gotoid, function() {
				jQuery('#topbar').slideDown('medium');
				jQuery('#top-loader').slideUp('medium');
				jQuery(menuitem).removeClass('loading').addClass('current_page_item');
				jQuery('#topbar .content p strong').parent().addClass('strong-paragraph');
				contactsw();
			});
			event.preventDefault();
			//pageTracker._trackPageview(goto);
		}
	});


// Auto load first page in footer menu
	jQuery('#page-nav-2 li li').first().find('a').each(function() {
		jQuery(this).parent().addClass('loading');
		jQuery('#load-2').fadeIn('medium');
		var goto = jQuery(this).attr('href');
		var gotoid = goto + ' #pages';
		var gotoanchor = '<a href="' + goto + '" class="page-permalink">&raquo;</a>';
		var menuitem = jQuery(this).parent();
		jQuery('#base').load(gotoid, function() {
			jQuery(this).slideDown('medium');
			jQuery('#load-2').fadeOut('medium');
			jQuery(menuitem).removeClass('loading').addClass('current_page_item');
		});
	});
	
// Redir toppblog
function redirblog() {
	jQuery('#page-nav-1 .page-item-1301 .page-permalink').click(function() {
		window.location = 'http://toppmotet.se/kategori/blogg/';
	});
}

	jQuery('#logotype .logo').click(function() {
		window.location = 'http://toppmotet.se/';
	});


// Ajax base panel
	jQuery('#page-nav-2 li.page_item a').click(function(event) {
		jQuery('#page-nav-2 li').removeClass('current_item');
		jQuery(this).parent().addClass('loading');
		jQuery('#load-2').slideDown('medium');
		jQuery('#base').slideUp('medium');
		var goto = jQuery(this).attr('href');
		var gotoid = goto + ' #pages';
		var menuitem = jQuery(this).parent();
		jQuery('#base').load(gotoid, function() {
			jQuery('#base').slideDown('medium');
			jQuery('#load-2').slideUp('medium');
			jQuery(menuitem).removeClass('loading').addClass('current_item');
		});
		//pageTracker._trackPageview(goto);
		event.preventDefault();
	});
	
// Children Dropdown
	jQuery('.cat-nav li').hover(function() {
		jQuery(this).find('.children').fadeIn(100);
	},
	function() {
		jQuery(this).find('.children').fadeOut('fast');
	});
	
// Search Placeholder
	jQuery('#searchform input').val('S\u00F6k p\u00E5 toppmotet');
	
	jQuery('#searchform input').focus(function() { 
		if(jQuery(this).val() == 'S\u00F6k p\u00E5 toppmotet')
		{
			jQuery(this).val('');
		}
	});
	
	jQuery('#searchform input').blur(function() {
		if(jQuery(this).val() == '')
		{
			jQuery(this).val('S\u00F6k p\u00E5 toppmotet');
		} 
	});

	
// Title sizer
	jQuery('.post .title').each(function() {
		var titletitle = jQuery(this).text().toString().substring(0).length;
		var titlelen = jQuery(this).text().toString().split(/\s+/).length;
		if(titletitle < 15) {
			jQuery(this).addClass('super-type');
		} else if(titletitle < 30) {
			jQuery(this).addClass('large-type');
		} else if(titletitle < 45) {
			jQuery(this).addClass('medium-type');
		} else if(titletitle < 60) {
			jQuery(this).addClass('small-type');
		} else {
			jQuery(this).addClass('mini-type');
		}
		var titleclass1 = 'words' + titlelen;
		var titleclass2 = 'chars' + titletitle;
		var titleclass = titleclass1 +' '+ titleclass2;
		jQuery(this).addClass(titleclass);
	});
		
// Comment focus
	jQuery('.commentlist > li:gt(2)').css("display", "none");
	
	jQuery('.commentlist > li:nth-child(4)').after('<h5 class="more-comments grid_7 prefix_1">Visa fler kommentarer...</h5>');
	
	jQuery('.more-comments').click(function () {
		jQuery('.commentlist > li:gt(2)').slideDown('slow');
		jQuery(this).slideUp('slow');
	});
	
// Facebook info 
	jQuery('#respond .fb-info-knapp').toggle(function() {
		jQuery('#facebook-info').fadeIn('medium');
	},
	function() {
		jQuery('#facebook-info').fadeOut('medium');	
	});
	
// Comment Rules info
	jQuery('#comment').click(function() {
		jQuery('#comment-accept').slideDown('medium');
	});
	
	jQuery('.comment-rules a').toggle(function() {
		jQuery('#comment-rules-info').slideDown('medium');
	},
	function() {
		jQuery('#comment-rules-info').slideUp('medium');
	});
	
// Auto bolder p 
	jQuery('.content p strong').parent().addClass('strong-paragraph');
	
// First content p 
	jQuery('.content p:eq(0)').addClass('first-p');
	
// First content p 
	jQuery('.content h3:eq(0)').addClass('first-p');


// Has inline image
	jQuery('.content div img').parent().parent().addClass('has-image');
	
// Get autop:ed images
	jQuery('.content p > img').parent().addClass('img-paragraph');
	jQuery('.content p > img.size-large').parent().addClass('img-large');
	jQuery('.content p > img.size-medium').parent().addClass('img-medium');
	jQuery('.content p > img.size-thumbnail').parent().addClass('img-thumbnail');

	jQuery('.content p a > img').parent().parent().addClass('img-paragraph');
	jQuery('.content p a > img.size-large').parent().parent().addClass('img-large');
	jQuery('.content p a > img.size-medium').parent().parent().addClass('img-medium');
	jQuery('.content p a > img.size-thumbnail').parent().parent().addClass('img-thumbnail');


// Feature portrait post
	jQuery('.post .image-large, .post .image-medium').each(function() {
		var imageheight = jQuery(this).height();
		var imagewidth = jQuery(this).width();
		if( imageheight > imagewidth ) {
			jQuery(this).parent().parent().addClass('portrait-image');
		} else {
		}
	});


// Gridalize images
	jQuery('.image-wrapper a img').each(function() {
	    var height = jQuery(this).height() + 0;
	    var divheight = height / 24;
	    var roundheight = Math.round(divheight) * 24 + "px";
	    jQuery(this).parent().css("height", roundheight);
	});
	jQuery('.image-wrapper div img').each(function() {
	    var height2 = jQuery(this).height() + 0;
	    var divheight2 = height2 / 24;
	    var roundheight2 = Math.round(divheight2) * 24 + "px";
	    var addheight2 = roundheight2 + 24;
	    jQuery(this).parent().css("height", roundheight2);
	});
	//jQuery('.wp-caption img').each(function() {
	//    var height = jQuery(this).height() / 12;
	//    var roundheight = Math.round(height) * 12 + "px";
	//    jQuery(this).parent().css("height", roundheight);
	//});
		
// Decide if height is good for multicolumn layout
	var contentheight = jQuery('.single .content').height();
	var windowheight = jQuery(window).height();
	if( contentheight < 100 ) {
		jQuery('.post.select .content').addClass('single-col');
	} else if( contentheight < windowheight ) {
		if (jQuery('.post.select .content a').hasClass('video-wrapper')) {
		
		} else {
			jQuery('.post.select .content').addClass('multi-col');
			jQuery('.post.select').addClass('has-multi-col');
		}
	} else {
		jQuery('.post.select').addClass('has-single-col');
		jQuery('.post.select .content').addClass('single-col');
	}

	
// Show/hide more blog posts
	jQuery('.blog-toggle').toggle(
		function () {
			jQuery('.topp-blog').animate({ height: '360px' }, 'slow');
			jQuery('.blog-description').fadeIn('slow');
			jQuery('.blog-link').fadeIn('slow');
		},
		function () {
			jQuery('.topp-blog').animate({ height: '120px' }, 'fast');
			jQuery('.blog-description').fadeOut('fast');
			jQuery('.blog-link').fadeOut('fast');
		}
	);
	
// Contact swichboard
function contactsw() {
	jQuery('.contact-item a').click(function() {
		var gotom = jQuery(this).attr('href').split('kontakt/');
		jQuery('.contact').slideUp('medium');
		jQuery(gotom[1]).slideDown('medium');
		jQuery('.contact-item').removeClass('active');
		jQuery(this).parent().addClass('active');
		return false;
	});
}

contactsw();

});
