/*
	© 2009 Alexander Maier by namics ag
 */

function overwriteLabelClickEvent(){
	// overwrite $currentForm labels click event
	$('label', $currentForm).click(function() {
		var $formElement = $('#' + $(this).attr('for'), $currentForm);

		if($formElement.attr('type') === 'checkbox'){
			$formElement.attr('checked',!$formElement.attr('checked'));
		}else if($formElement.attr('type') === 'text' || $formElement.get(0).tagName === 'TEXTAREA'){
			$formElement.focus();		
		}

		return false;		
	});
}

function activateSubmitButtons(){
	$('span.actionbutton', $currentForm).removeClass('inactive');
	$('span.actionbutton input', $currentForm).attr('disabled','');
}

(function($) {
	NX.example = NX.example || {};	

	$.extend(NX.example, {     		
		init: function(c) {			
		$('#contenttabs .contenttabscontent > div').hide(); // Hide all divs
		
		$('#contenttabs ul li a.tabs').click(function() { //When any link is clicked
			
			var currentTab = $(this).attr('href');

			// set current form			
			docid = $(this).parent().attr("id");
			if(docid != ''){ //tab template or bestellung&beratung
				docid = docid.split('');
				docid.shift();
				docid = docid.join('');		

				if(currentTab != "#tabs-5"){ // amaier: for tabs-5 no get needed				
					// no ajaxget for fProduct form and if content already loaded
					if( $('#contenttabs ul li:first').attr("id") != '' && $(currentTab).html()==""){
						$(currentTab).html('<br /><span class="tabloaderpos loading"></span>');
						$.get(ajaxurl + docid, function(data){
							$(currentTab).html(data);
							if($currentForm!="undefined") $currentForm = $('#bpForm_'+docid);
							overwriteLabelClickEvent();
							activateSubmitButtons();
						});
					}else{
						if($currentForm!="undefined") $currentForm = $('#bpForm_'+docid);						
					}					
				}	
			}

			$('#contenttabs ul li').removeClass('tabs-selected'); // Remove active class from all links
			$(this).parent().addClass('tabs-selected'); //Set clicked link class to active
			$('#contenttabs .contenttabscontent > div').hide(); // Hide all divs
			$(currentTab).show(); // Show div with id equal to variable currentTab
			return false;

		});
		
		// on product template open first tab
		if($('#contenttabs ul li:first').attr("id") == ''){			
			$('#contenttabs ul li a.tabs:eq(0)').trigger('click');
		}
	}
	});
})(jQuery);



