/**
 * revealOneMore
 * 
 * Apply to a link to reveal "one more" item of a given selection of <code>items</code>
 * when the link gets clicked. Displays a <code>maxInfo</code> element (optional) when
 * the last item is shown.
 */
(function($) {
	$.fn.revealOneMore = function(options) {

		var $elem = $(this);

		return $elem.removeClass('hide').click(revealNextItem);

		function revealNextItem(e) {
			var $hiddenItems = $(options['items'] + ':hidden');
			$hiddenItems.filter(':first').removeClass('hide');

			if ($hiddenItems.length <= 1) {
				$elem.addClass('hide');
				if (options['maxInfo']) {
					$(options['maxInfo']).removeClass('hide');
				}
			}

			e.preventDefault();
		}

	}
})(jQuery);

/**
 * sampleText
 * 
 * Set value of input field to given text until input field gets the focus.
 */
(function($) {
	$.fn.sampleText = function(text) {
		return $(this).val(text).addClass('inactive')
			.focus(function(e) {
				$(this).val('').removeClass('inactive');
			});
	}
})(jQuery);

/**
 * clearSampleText
 * 
 * Removes sampleText from given inputField.
 */
(function($) {
	$.fn.clearSampleText = function(inputField) {
		
		return $(this).click(function() {
			if ($(inputField).hasClass('inactive')) {
				$(inputField).val('');
			}
		});
		
	}
})(jQuery);

/**
 * switchToggle
 * 
 * Enables an object to show a given "on" element and hide an "off" element
 * when clicked. Initially executes the behavior when the object is ":checked". 
 */
(function($) {
	$.fn.switchToggle = function(options) {

		if ($(this).is(':checked')) {
			doToggle();
		}

		return $(this).click(doToggle);

		function doToggle(e) {
			$(options['on']).show();
			$(options['off']).hide();
		}

	}
})(jQuery);

/**
 * simpleAccordion
 * 
 * Called on a definition list (dd) to activate an accordion-like behavior.
 */
(function($) {
	$.fn.simpleAccordion = function() {

		var $elem = $(this);

		return $elem.find('> dt > a').click(doAccordion).end()
			.find('> dt > a[class!="on"]').parent().next('dd').hide();

		function doAccordion(e) {
			var $this = $(this);

			if ($this.hasClass('on')) { closeNode($this); }
				else { openNode($this); }

			closeChildNodes($this);
			e.preventDefault();
		}

		/**
		 * Removes active status from current link and hides its dd.
		 */
		function closeNode($this) {
			$this.removeClass('on').parent().next('dd').hide();
		}

		/**
		 * Hides all visible dd's and shows only the current one.
		 * Also removes active status from all links except the current one.
		 */
		function openNode($this) { 
			$elem.find('> dd:visible').hide().end()
				.find('> dt > a').removeClass('on');
			$this.addClass('on')
				.parent().next().show();
		}

		/**
		 * Close all child dd's and remove active status from links.
		 */
		function closeChildNodes($this) {
			$this.parent().next('dd').find('> dl > dt > a').removeClass('on').end()
				.find('> dl > dd').hide();
		}

	}
})(jQuery);

/**
 * Callview date selection stuff
 */
function callViewSelectDates() {

	if ($("#errorFieldset").length > 0) {
		/* do not hide selectDates when input contains invalid data */
		$("#period .buttonSmall").removeClass("active");
		$("#selectDatesButton").addClass("active");
		$("#selectDates").show();
	} else if ($("#period .buttonSmall.active").length > 0) {
		$("#selectDatesButton").removeClass("active");
		$("#selectDates").hide();
	} else {
		$("#selectDatesButton").addClass("active");
	}

	$("#selectDatesLink").click(function(event){
		$("#selectDates").show();
		$("#period .buttonSmall").removeClass("active");
		$("#selectDatesButton").addClass("active");

		event.preventDefault();
	});
}

(function($) {
	$.fn.toggleEvnRadios = function(){
		
		if ($(".itemisedBill.radiogroup").length > 0) {
		
			if ($('#privateCustomerRadio').length) {
			  var customerType = $('#privateCustomerRadio').is(":checked") ? "Private" : "Corporate";
			} else {
			  var customerType = "Private";
			}
			
		   var evnSetting = $(".itemisedBill.radiogroup input[type=radio]:checked").attr("class").split(" ")[1];
		
			 $("#evnInputHint span").hide();
			 $("#evnInputHint ." + evnSetting + customerType).show();
		}
		
		return $(this);
	}
})(jQuery);

function displayUserInfo(data) { 
	if (data != null) {
		$('#accountLogin').hide().after($(data));
	}
}

function fetchUserInfo(url){
	$.ajax({
		url: url, 
		dataType: 'script'
	});
}

/** chb trackingpixel test */
function trackingPixel(page, partnerId) {
	if (page == 'order/customerEdit') {
		if (partnerId == '1010') {
			var google_conversion_id = 1068709819;
			var google_conversion_language = "en";
			var google_conversion_format = "3";
			var google_conversion_color = "666666";
			var google_conversion_label = "ULxrCJWHgQIQu-_M_QM";
			var google_conversion_value = 0;

			$.getScript('http://www.googleadservices.com/pagead/conversion.js');
			
			return('true');
		}
	}	
}


/**
 * Setup plugins used by all projects.
 */
(function($) {
	$(document).ready(function() {

		/* sampleText for search box */
		$('#searchInput').sampleText('z.B. Mailbox');
		$('#submitSearch').clearSampleText('#searchInput');

		/* switchToggle for private/corporate customer radio buttons */
		$('#privateCustomerRadio').switchToggle({ on: '#personInputDiv', off: '#corporateInputDiv' });
		$('#corporateCustomerRadio').switchToggle({ on: '#corporateInputDiv', off: '#personInputDiv' });

		/* revealOneMore for recommendees on recommendation pages */
		$('#addRecommendee').revealOneMore({ items: '.recommendee', maxInfo: '#maxRecommendeesInfo' });

		/* simpleAccordion for FAQ questions and answers */
		$('.accordionContent').simpleAccordion();
		$('.accordionInnerContent').simpleAccordion();

		/* show tip box for slow pdf download */
		$('#pdfLinkTip').hide();
		$('#createPdfLink').click(function(event){
			$('#pdfLinkTip').show();
			$('#createPdfLink').hide();
		});
		
		/* toggle evn radio buttons */
		$('.itemisedBill.radiogroup input[type=radio], #privateCustomerRadio, #corporateCustomerRadio').toggleEvnRadios().change(function() {
			$(this).toggleEvnRadios();
		});
		
		/* selectDate for callView page */
		callViewSelectDates();

		/* open FAQ question and answer, if anchor is given */
		var anchorName = location.href.split('#')[1];	
		if (anchorName != null && anchorName != "") {
			themeName = anchorName.substr(0, anchorName.indexOf("_", anchorName.indexOf("_") + 1));	
			$('a[name="' + themeName + '"], a[name="' + anchorName + '"]').addClass('on').parent().next('dd').show();
			$(this).scrollTo('a[name="' + themeName + '"]');
		}
		
		/* add css class when it is a mac */
        if (navigator.appVersion.indexOf("Mac")!=-1) {
            $('body').addClass('macOs');
        }
        if (navigator.appVersion.indexOf("Linux")!=-1) {
            $('body').addClass('linux');
        }
		
        /* to avoid multiple topups */
        $('#paymentDataForm .buttonNext input').click(function(event){
			$('#paymentDataForm .buttonNext').css({"visibility":"hidden"});
		});
        
        /* hide nettokom allnet200 */
        $('#tariff_smartphone_option_allnet_200').parent().hide();
        if ($('#imprintLink').attr("href") == 'http://www.nettokom.de/impressum.html') {
        	$('#tariff_smartphone_options').parent().hide();
        	smartphone_link = $('[href="/tarif-optionen/smartphone-option.html"]').parent();
        	smartphone_link.add(smartphone_link.prev()).hide();
        }
        
	});
})(jQuery);

