/* ------------------------------------------------------------------------
	prettyCheckboxes
	
	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: All the non user friendly custom checkboxes solutions ;)
	Version: 1.1
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */
	
	jQuery.fn.prettyCheckboxes = function(settings) {
		settings = jQuery.extend({
			checkboxWidth: 17,
			checkboxHeight: 17,
			className : 'prettyCheckbox',
			display: 'list'
		}, settings);
		
		var arrBoxesOrg = new Array();
		var arrBoxesCur = new Array();
		
		$(this).each(function(){
			// Find the label
			$label = $('label[for="'+$(this).attr('id')+'"]');

 			startobjBox = $(this).parents('li.clickopen').find('div.dropdownEntry a'); 
			startstrBox = $(this).attr('id').substring(0,13);
			startdisplayBox = $(this).parents('div.containerDropdown').find('div.myacademicsProfileList');

			// Add the checkbox holder to the label
			$label.prepend("<span class='holderWrap'><span class='holder'></span></span>");

			// If the checkbox is checked, display it as checked
			if($(this).is(':checked')) {
					if(!arrBoxesOrg[startstrBox]){
						arrBoxesOrg[startstrBox] = $(this).parents('li.clickopen').find('div.dropdownEntry a').attr('innerHTML');
						$(this).parents('li.clickopen').find('div.dropdownEntry a').attr('innerHTML','');
					}
					if(arrBoxesCur[startstrBox]){
						arrBoxesCur[startstrBox] = arrBoxesCur[startstrBox] + '#' + $label.attr('innerHTML').replace(/<\/?[^>]+>/gi, '') + '#';
					}else{
						arrBoxesCur[startstrBox] = '#' + $label.attr('innerHTML').replace(/<\/?[^>]+>/gi, '') + '#';
					}
					
          startobjBox.attr('innerHTML',arrBoxesCur[startstrBox].split('##').join(', ').split('#').join(''));
					if(startdisplayBox) { startdisplayBox.attr('innerHTML',arrBoxesCur[startstrBox].split('##').join('<br />').split('#').join('')); }
					
        $label.addClass('checked');
      };

			// Assign the class on the label
			$label.addClass(settings.className).addClass($(this).attr('type')).addClass(settings.display);

			// Assign the dimensions to the checkbox display
			$label.find('span.holderWrap').width(settings.checkboxWidth).height(settings.checkboxHeight);
			$label.find('span.holder').width(settings.checkboxWidth);

			// Hide the checkbox
			$(this).addClass('hiddenCheckbox');

			// Associate the click event
			$label.bind('click',function(){
				$('input#' + $(this).attr('for')).triggerHandler('click');
								
				/* Werte in Box schreiben */					
				objBox = $(this).parents('li.clickopen').find('div.dropdownEntry a'); 
				strBox = $(this).attr('for').substring(0,13);
        displayBox = $(this).parents('div.containerDropdown').find('div.myacademicsProfileList');
        
				if($('input#' + $(this).attr('for')).is(':checked')){
					arrBoxesCur[strBox] = arrBoxesCur[strBox].split('#' + $(this).attr('innerHTML').replace(/<\/?[^>]+>/gi, '') + '#').join('');
					
					if(!arrBoxesCur[strBox]){					
						objBox.attr('innerHTML',arrBoxesOrg[strBox]);
  					if(displayBox != undefined) { displayBox.attr('innerHTML',''); }
					}else{
  					var textToWrite =  arrBoxesCur[strBox].split('##').join(', ').split('#').join('');
  					if(textToWrite.length > 50) textToWrite = textToWrite.substring(0, 50) + ' ...';
						objBox.attr('innerHTML',textToWrite);
						//objBox.attr('innerHTML',arrBoxesCur[strBox].split('##').join(', ').split('#').join(''));
  					if(displayBox != undefined) { displayBox.attr('innerHTML',arrBoxesCur[strBox].split('##').join('<br />').split('#').join('')); }
					}
				}else{						
					
					if(!arrBoxesOrg[strBox]){
						arrBoxesOrg[strBox] = $(this).parents('li.clickopen').find('div.dropdownEntry a').attr('innerHTML');
						$(this).parents('li.clickopen').find('div.dropdownEntry a').attr('innerHTML','');
					}
					if(arrBoxesCur[strBox]){
						arrBoxesCur[strBox] = arrBoxesCur[strBox] + '#' + $(this).attr('innerHTML').replace(/<\/?[^>]+>/gi, '') + '#';
					}else{
						arrBoxesCur[strBox] = '#' + $(this).attr('innerHTML').replace(/<\/?[^>]+>/gi, '') + '#';
					}
					var textToWrite =  arrBoxesCur[strBox].split('##').join(', ').split('#').join('');
					if(textToWrite.length > 50) textToWrite = textToWrite.substring(0, 50) + ' ...';
          objBox.attr('innerHTML',textToWrite);
					
					//objBox.attr('innerHTML',arrBoxesCur[strBox].split('##').join(', ').split('#').join(''));
					if(displayBox != undefined) { displayBox.attr('innerHTML',arrBoxesCur[strBox].split('##').join('<br />').split('#').join('')); }
				}
								
				if($('input#' + $(this).attr('for')).is(':checkbox')){
					$(this).toggleClass('checked');
					$('input#' + $(this).attr('for')).checked = true;
					
					$(this).find('span.holder').css('top',0);
				}else{
					$toCheck = $('input#' + $(this).attr('for'));

					// Uncheck all radio
					$('input[name="'+$toCheck.attr('name')+'"]').each(function(){
						$('label[for="' + $(this).attr('id')+'"]').removeClass('checked');	
					});

					$(this).addClass('checked');
					$toCheck.checked = true;
				};
			});
			
			$('input#' + $label.attr('for')).bind('keypress',function(e){
				if(e.keyCode == 32){
					if($.browser.msie){
						$('label[for="'+$(this).attr('id')+'"]').toggleClass("checked");
					}else{
						$(this).trigger('click');
					}
					return false;
				};
			});
		});
	};
	
	checkAllPrettyCheckboxes = function(caller, container){
		if($(caller).is(':checked')){
			// Find the label corresponding to each checkbox and click it
			$(container).find('input[type=checkbox]:not(:checked)').each(function(){
				$('label[for="'+$(this).attr('id')+'"]').trigger('click');
				if($.browser.msie){
					$(this).attr('checked','checked');
				}else{
					$(this).trigger('click');
				};
			});
		}else{
			$(container).find('input[type=checkbox]:checked').each(function(){
				$('label[for="'+$(this).attr('id')+'"]').trigger('click');
				if($.browser.msie){
					$(this).attr('checked','');
				}else{
					$(this).trigger('click');
				};
			});
		};
	};
	
	function uncheckExplicitAllPrettyCheckboxes(container){
		$(container).find('input[type=checkbox]:checked').each(function(){
			$('label[for="'+$(this).attr('id')+'"]').trigger('click');
			if($.browser.msie){
				$(this).attr('checked','');
			}else{
				$(this).trigger('click');
			};
		});		
	};	
	
	function checkExplicitAllPrettyCheckboxes(container){
		$(container).find('input[type=checkbox]:not(:checked)').each(function(){
			$('label[for="'+$(this).attr('id')+'"]').trigger('click');
			if($.browser.msie){
				$(this).attr('checked','checked');
			}else{
				$(this).trigger('click');
			};
		});
		var subContainer = $(container).find('.headerSearchAdvancedSubdropdown');
    if($('ul:first', subContainer).is(':hidden')) { $(container).find('.dropdownSubEntry:first').trigger('click'); }	
	};	
	
	function uncheckExplicitAllSubPrettyCheckboxes(caller){
		$(caller).parent().parent().find('input[type=checkbox]:checked').each(function(){
			$('label[for="'+$(this).attr('id')+'"]').trigger('click');
			if($.browser.msie){
				$(this).attr('checked','');
			}else{
				$(this).trigger('click');
			};
		});		
	};	
	
	function checkExplicitAllSubPrettyCheckboxes(caller){
		$(caller).parent().parent().find('input[type=checkbox]:not(:checked)').each(function(){
			$('label[for="'+$(this).attr('id')+'"]').trigger('click');
			if($.browser.msie){
				$(this).attr('checked','checked');
			}else{
				$(this).trigger('click');
			};
		});	
	};	
	
	
