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

	// fill the siteSearch with the title
//	var siteSearch = $j('#siteSearch-container');
	var siteSearch = $j('#siteSearch');
	if (siteSearch.length) {
		// input logic
		var fillInputWithTitle = function() {
			if ($j(this).val() == '' && $j(this).attr('title')) {
				$j(this).val($j(this).attr('title'));
				$j(this).addClass('inputDefaultValue');
			}
		};
	
		var removeTitleFromInput = function() {
			if ($j(this).hasClass('inputDefaultValue') && $j(this).val() == $j(this).attr('title')) {
				$j(this).val('');
				$j(this).removeClass('inputDefaultValue');
			}
		}

		$j('#inputSword', siteSearch).each(fillInputWithTitle);
		$j('#inputSword', siteSearch).blur(fillInputWithTitle);
		$j('#inputSword', siteSearch).bind('click focus', removeTitleFromInput);
		$j('form', siteSearch).bind('submit', function() {
			$j('#inputSword').each(removeTitleFromInput);
		});
	}

	$j('.tabContainerHeader a').click(function(evt) {
		var container = $j(this).parents('.tabContainer');
		$j(this).parent().parent().find('a.active').removeClass('active');
		$j(this).addClass('active');

		var contentId = $j(this).parent().attr('rel');
		container.find('.tabContainerContent').hide();
		$j('#' + contentId).show();
		
		// hide all items before the container, if you select another tab than the first one
//		if ($j(this).parent().prev().length == 0) {
//			$j(container).prevAll('.csc-default').show();
//		} else {
//			$j(container).prevAll('.csc-default').hide();
//		}
		
		evt.stopPropagation();
		evt.preventDefault();
		return false;
	});

	$langSel = $j('#langSel');
	$j('p a', $langSel).click(function(evt) {
		evt.preventDefault();
	});
	
	$langSel.click(function(evt) {
		$j(this).addClass('opened')
			.find('ul').show();

		evt.stopPropagation();

		$j('body').one('click', function(evt) {
			$j('#langSel').removeClass('opened')
				.find('ul').hide();
			evt.stopPropagation();
			//evt.preventDefault();
			return false;
		});
	});
	
	$langSelTop = $j('#langSelTop');
	$j('p a', $langSelTop).click(function(evt) {
		evt.preventDefault();
	});
	
	$langSelTop.click(function(evt) {
		$j(this).addClass('opened')
			.find('ul').show();

		evt.stopPropagation();

		$j('body').one('click', function(evt) {
			$j('#langSelTop').removeClass('opened')
				.find('ul').hide();
			evt.stopPropagation();
			//evt.preventDefault();
			return false;
		});
	});
	
	$calDropdown = $j('#calendarQuarterDropdown');
	if ($calDropdown.length) {
		var href = window.location.href;
		href = decodeURI(href);
		if (href.indexOf('getdate') !== -1) {
			currentDate = href.substr(href.indexOf('getdate')+9, 8);
			$calDropdown.val(currentDate);
		}
		$calDropdown.change(function() {
			$j(this).parent('form').submit();
		});
	}
	
	$j('.tabContainerHeader ul').children('li').each(function(){
		if ($j(this).children('a').length == 0) {
			var currentSite = window.location.pathname;
			var currentLang = currentSite.match(/\/([a-zA-Z_-]{2,3})\//);
			if (currentLang) {
				$j(this).wrapInner('<a href="/'+currentLang[1]+'/service/login/?redirect_url='+currentSite+'" title="Login" />');
			} else {
				$j(this).wrapInner('<a href="/service/login/?redirect_url='+currentSite+'" title="Login" />');
			}
		}
	});

});
