$('document').ready(function(){

	/**
	* Front Career Tabs
	**/
	$("#frontCareerTabs li a").click(function() {
		if( !$(this).hasClass('current') ) {
			$('.tab').hide();
			$('#frontCareerTabs .tabNav li').removeClass('current prev');
			
			$(this).parent().addClass('current');
			var id = $(this).parent().attr('id').split('-');
			$("#frontCareerTabs .tab").eq(id[1]).show();
			if( id[1]!=0 ) {
				$("#frontCareerTabs .tabNav li").eq(id[1]-1).addClass('prev');
			}
		}
		return false;
	});
	

	/**
	* Top toolbar
	**/
	$(".tools li a.toggler").click(function() {
		if( !$(this).parent().hasClass('current') ) {
			$('.toolsContainer').hide();
			$('.tools li').removeClass('current');
			
			var c = $(this).parent().attr('class');
			$(this).parent().addClass('current');
			$('.toolsContainer.' + c ).show();
		}
		else {
			$('.toolsContainer').hide();
			$('.tools li').removeClass('current');
		}
		return false;
	});

	$("#shareNewsletter").click(function(){
		
		$("div.toolsContainer").hide();
		$("div.toolsContainer.mail").show();
		
	});

	/**
	* Tabs
	**/
	$('.tabs.dynamic .tabsHeader a').click(function(){
		if( !$(this).hasClass('current') ) {
			var clickedTab = $(this).attr('class');

			$(this).parent().find('a').removeClass('current');
			$(this).addClass('current');	
			$(this).parent().parent().find('.tabsBody .tab').hide();
			$(this).parent().parent().find('.tabsBody .tab').removeClass('current');
			$(this).parent().parent().find('.tabsBody .tab.' + clickedTab).addClass('current');
			$(this).parent().parent().find('.tabsBody .tab.current').show();
		}
		else {
			//$(this).parent().find('a').removeClass('current');
		}
		return false;
	});


	/**
	* Custom dropdowns
	**/
	$('.dropdown select').change(function() {
		$(this).parent().find('.dropdownLabel').eq(0).html($(this).val());
	});

	$('#muutosMoottori .dropdown select#issueSelector').change(function() {
		window.location = $(this).val();
	});

	/**
	* Toggle sidebar login box visibility
	**/
	$('#toggle-login').click(function(){
		$('#user-login').toggle();
		
		if( $('.loginHeader a span').hasClass('close') ) {
			$('.loginHeader a span').removeClass('close');
			$('.loginHeader a span').addClass('open');
		}
		else if ( $('.loginHeader a span').hasClass('open') ) {
			$('.loginHeader a span').removeClass('open');
			$('.loginHeader a span').addClass('close');
		}
		
		return false;
	});

	/**
	* Sidebar search submit
	**/

	$('#mainsearchbutton').click(function(){

		$("#searchForm").submit();

	});
	
	$('#twitterShare').click(function(){
		redirectBitlyToTwitter($(this).attr('href'), $(this).attr('rel'));
		return false;
	});
	

	/**
	* COMMENTS
	**/

	if(uriObject.queryKey.sent == 'true'){

		var message = '';
	
		if(ez_content_language == 'fi'){
	

			message += '<div class="systemMessage">';
			message += '<h3>Viestisi on lähetetty</h3>';
//			message += '<p>Kiitos kommentistasi.</p>';
			message += '</div>';

			$("#comments div.commentListBody").prepend(message);
			
		}
		
	}

	setTimeout(function(){
		$("div.systemMessage").slideDown("slow");
	},750);
	

	if(($('#commentInsert').find('#commentError')).length > 0) {
		window.location.hash="commentInsert";
	}
	
	var count = $("#myList").children().length;

/*
	$('ul#firstLevelnavi li:nth-child(5)').after('<li class="divider lastli"></li>');
	$('ul#firstLevelnavi li:nth-child(5)').addClass('lastli');
	$('ul#firstLevelnavi li:eq(5)').nextAll().addClass('green');
*/

	$("#sharePrint").click(function(){
		window.print();
		return false;		
	});
	
	
	$("#mainContent .autoHeightWrapper").each(function(thisWrapper){
	
		var maxHeight = 0;
		
		$(this).find("div.contentHeight").each(function() {
			maxHeight = Math.max(maxHeight, $(this).height()); 
		});
		
		$(this).find('.border-mc').height(maxHeight+50);
		
		
	});
    
});

function redirectBitlyToTwitter(originalUrl, title){

	var shortUrl = '';

	// set up default options
	var defaults = {
	version:    '2.0.1',
	login:      'kauppafi',
	apiKey:     'R_b2e1fa24133863a929a6ecfed7225774',
	history:    '0',
	longUrl:    originalUrl
	};
	
	// Build the URL to query
	var daurl = "http://api.bit.ly/shorten?"
	+"version="+defaults.version
	+"&longUrl="+defaults.longUrl
	+"&login="+defaults.login
	+"&apiKey="+defaults.apiKey
	+"&history="+defaults.history
	+"&format=json&callback=?";
	
	
	// Get data
	$.getJSON(daurl, function(data){

		for ( var i in data.results ){
			redirectUrl = 'http://twitter.com/home?status=' + title + ' ' + data.results[i].shortUrl;
			break;
		}
		
		window.location = redirectUrl;
		
	});



}


/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

