/* Cufon */
Cufon.replace('h1, caption, #legal_tips .h1, #pos_form h2, #teaser_container h2, #clear_green_links h2, #clear_green_facts_container h2, #homepage_overview h2, #terras_overview h2, th, .link_back strong, #perfect_serve_fris h2, .component dt, #top-100 h2, #perfect_serve_tips h2');


/* jQuery functionality */
jQuery.fn.replaceLegend = function(tag){
	jQuery(this).children("fieldset").each(function(){
		var legendUsurper = tag || 'h4'
		var legendReplacer = jQuery("legend", this).remove().text();
		jQuery("<" + legendUsurper + "></" + legendUsurper + ">").text(legendReplacer).prependTo(this).wrapInner("<span></span>");
	});
};

jQuery.fn.usCenter = function() {
	var usWidth = $(this).width();
	$(this).css({
		'position': 'absolute',
		'left': '50%',
		'top': '0',
		'margin-left': '-' + ( usWidth / 2) + 'px'
	});
};

jQuery.fn.usToggle = function(tag, block) {
	var toggleLink  = tag || 'h2'
	var toggleBlock = block || 'faq_answer'
	var toggleThat  = $(this);
	$(this).find(toggleLink).click(function(){
		var toggleChain = $(this).next('div.' + toggleBlock)
		if ( toggleChain.is(':hidden') ) {
			$(toggleThat.selector + ' ' + toggleLink).not($(this)).removeClass('active');
			jQuery('#faq_container div.' + toggleBlock).slideUp(175);
			toggleChain.slideDown(175);
		}
		$(this).addClass('active');
	});
	$(this).find('div.' + toggleBlock).hide();
};

jQuery.fn.usPopup = function(width,height) {			
	$(this).click(function(e) {
		 var xPos = (screen.availWidth - width) / 2
		 var yPos = (screen.availHeight - height ) / 2
		 
		 //We want to center our popup on the correct screen
		 if(e.screenX && e.screenX > screen.availWidth) {
		 	 xPos = screen.availWidth + xPos;
		 }
		 
		 var service = window.open(
	         $(this).attr('href'),
	         $(this).text(),
	         "menubar=no,toolbar=no,resizeable=no,scrollbars=yes," +
	         "width="+width+",height="+height+",screenX="+xPos+",screenY="+yPos+",left="+xPos+",top="+yPos+"," +
	         "screenX="+xPos+",screenY="+yPos+",left="+xPos+",top="+yPos
	     );
		 service.focus();
		 return false;
	});
};

jQuery.fn.leaf = function() {
	$('<div id="doodad"></div>').appendTo('#content_wrapper');
}

function checkboxSlideToggle(checkbox) {
	var d = $(checkbox).parent('div').next('div.disabled')
	if ( $(checkbox).is(':checked') ) {
		d.slideDown().find('input').removeAttr('disabled');
	}
	else {
		d.slideUp().find('input').attr('disabled','disabled');			
	}
}

jQuery(function($) {

	$("#home #flash_container").flashembed({
	    src: 'assets/swf/Lipton.swf',
        wmode: "transparent"
    });
	
	$('a[href^=http://], a[href$=.pdf]').attr('target', '_blank');

	$('#faq_container').usToggle();
	$('#sub_navigation ul').usCenter();

	$('form').replaceLegend();
	$('form').find('.disabled').hide();
	
	//initilize checkboxes if they are already checked.
	checkboxSlideToggle($('form div.enabler input:checkbox:checked'));
	$('form div.enabler input:checkbox').click(function(){
		checkboxSlideToggle(this);
	});
	
	
	/**
	 * bestellen: fill company types after changing main category
	 */
	$("#registration_container #company_type").change(function() {
		$.post(
			"bestellen.php", 
			{
				'salesChannelId': $(this).val(),
				'getSales': 'true'
			},
			function(data, textStatus) {
				$("#company_type_entrepreneur option:not(:first-child)").remove();
				$("#company_type_entrepreneur").removeAttr('disabled');
				
					var groups = data.getElementsByTagName('group')
					for(var i = 0; i < groups.length; i++) {
						var group = groups[i];
						$("#company_type_entrepreneur").append(
							"<option value=\"" + group.getAttribute('id')  + "\">" +
							group.firstChild.nodeValue +
							"</option>"
						);
					}
			},
			"xml"
		);
	});
	/********************************************************************************************/

	
	if (typeof document.body.style.maxHeight === "undefined") {
		
		$('#faq_container h2:hover').hover(
			function () {
				$(this).addClass('hover');
			}, 
			function () {
				$(this).removeClass('hover');
			}
		);

		$('#faq_container li > div').addClass('helper');

		$('#logo, #side:not(.contact), #tips_intro, #employee_tips, #flash_container, #content_header, #homepage_overview, #huxley').pngFix();

		$('#employee_tips img').wrap("<div></div>");		
	}

	/*@cc_on
	   /*@if (@_jscript_version <= 5.7)   // IE 6 & 7  (8 = 5.8) 
		$('#top-100 ol:has([start])').each(function() {
			var start = $(this).attr('start');
			for(var i = start-1; i >= 1; i--) {
				$(this).prepend("<li class=\"invisible-sandwich\"></li>");
			}
		});
		
		$('#registration_container button').css('cursor', 'pointer').click(function() {
			$(this).parents('form').submit();
		});		

	 /*@end
	@*/

    if ( jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == '7' ) {
		$('#about_lipton_container .content_column').css('padding-bottom','1em');
		$('#variant_title').css('padding-top','1em');
		$('#employee_tips').css('padding-bottom','1em');
		$("<hr class=\"clear\" />").insertBefore('#rss_container .clear');
    }




	$('a.popup').usPopup(500, 500);

	$('#links_navigation li:first').addClass('first');

	$('#content_wrapper').leaf();
	
});

