$(function(){
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    // List Item Hover
    //
    
    $('.listItem').live('mouseenter',function(){
        $(this).css({
            'backgroundColor':'#fdfdfd',
            'border':'1px solid #dcdcdc'
        })    
    });
    $('.listItem').live('mouseleave',function(){
        $(this).css({
            'backgroundColor':'',
            'border':'1px solid transparent'
        })    
    });
    
    
    $('.list_rules').find('td').live('mouseenter',function(){
        var tdOffset = $('.list_rules').find('td:visible').eq($(this).index()).offset();
        var $div = $('.pagination:visible').find('.vertical_block');
        
        $div.css({       
            backgroundColor : '#fdfdfd',
            position: 'absolute',
            left : tdOffset.left - $('#left-side').outerWidth() - 2,
            height : $('.pagination:visible').outerHeight(true),
            width : '17px'
        })
        
        $div.show();
        
    });
    
    
    $('.list_rules').find('td').live('mouseleave',function(){
        $('.vertical_block').hide();
    });
    
         
});


//$(whatever).outerHTML(); -> Gibt aktuellen Selektor als String aus
(function($){$.fn.extend({outerHTML:function(value){if(typeof value==="string"){var $this=$(this),$parent=$this.parent();var replaceElements=function(){var $img=$this.find("img");if($img.length>0){$img.remove();}
var element;$(value).map(function(){element=$(this);$this.replaceWith(element);})
return element;}
return replaceElements();}else{return $("<div />").append($(this).clone()).html();}}});})(jQuery);


(function($){
$.unserialize = function(serializedString){
var str = decodeURI(serializedString);
var pairs = str.split('&');
var obj = {}, p, idx, val;
for (var i=0, n=pairs.length; i < n; i++) {
p = pairs[i].split('=');
idx = p[0];

if (idx.indexOf("[]") == (idx.length - 2)) {
// Eh um vetor
var ind = idx.substring(0, idx.length-2)
if (obj[ind] === undefined) {
obj[ind] = [];
}
obj[ind].push(p[1]);
}
else {
obj[idx] = p[1];
}
}
return obj;
};
})(jQuery);

function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}


//Function zum überprüfen ob eine Selector existiert
jQuery.fn.exists = function(){
    if(this.length == 0){
        return false;
    }else{
        return true;
    }
}



   


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fileviewer skalieren
//

function toggleFileView($class){
    if($class == 'expert'){
        //Groß
        $('.expert').show();
        $('#filedata').css('height','295px');
        $('#filepreview img').removeAttr('height');
        $('#fileviewer,#filepreview,#filepreview img').css('height','');
    }else{
        //klein
        $('.expert').hide();
        $('#filedata').css('height','141px');
        $('#fileviewer').css('height','155px');
        $('#filepreview').css('height','140px');
        $('#filepreview img').css('height','110px');
    }
    alertSize();
}







/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Seitenblättern Funktion
//
// @sample: pagination()
//

function pagination(page){
    if(typeof page !== 'undefined'){
        if(typeof $('.page-' + page).attr('class') != 'undefined'){
            $('body').data('page', page);
            $('.site').removeClass('siteactive');
            $('.site[rel='+ page +']').addClass('siteactive');
            $('.pagination').hide();
            $('.page-' + page).show();
        }
    }
}

function setPage(page){
    if(typeof page != 'undefined'){
        $('body').data('page','1');
        setPage();
    }else{
        if(typeof $('body').data('page') != 'undefined'){
            pagination($('body').data('page'));    
        }    
    }
}









/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Formular füllen
// @param: Form = Formularname, data = Object mit inhalten und Spaltennamen
// @sample: pagination()
//

function runInput(form,data){
    if(typeof data === 'undefined' || typeof form === 'undefined') return false;
  
    //Values(object) durchlaufen und inputs sowie select-felder befüllen
    for(var i in data){ 
       
       if($(form).find('input[name="'+ i +'"]').attr('type') == 'checkbox'){
            
            if( data[i] == 1 ||data[i] == 'true' ){
                $(form).find('input[name="'+ i +'"]').attr('checked', true);    
            }else{
                $(form).find('input[name="'+ i +'"]').attr('checked', false);                                                                                             
            }
            
            if(i == 'itransport_aktiv'){
                if(data[i] == 2){
                    $(form).find('input[name="'+ i +'"]').attr('checked', true);
                }
            }
       }
       
       if($(form).find('select[name="'+ i +'"] option[value="' + data[i] + '"]').val() == data[i] ){
            $(form).find('select[name="'+ i +'"] option[value="' + data[i] + '"]').attr('selected',true);
       }
       
       if($(form).find('input[name="'+ i +'"]').attr('type') == 'text' || $(form).find('input[name="'+ i +'"]').attr('type') == 'hidden' ){
            if(data[i] != ''){
                $(form).find('input[name="'+ i +'"]').val( data[i] );
            }
       }
    }
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// In iTransport verwendete Listen "klicken"
// Per Json übergebene Daten werden gesplitet und die entsprechenden Optionen angeklickt
//

function triggerList(form,list,data){
    if(typeof data === 'object'){
        for(var i in data){
            $('.listContainer[group='+ list +'] .list').find('.option[rel='+ data[i] +']').trigger('click');    
        }
    }
}













/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Checkboxen deaktivieren
//
// @param: Form-Tag Name
//

function uncheckboxes(formName){
    if(formName != ''){
        $('form[name='+ formName +'] input[type=checkbox]').attr('checked',false);    
    }
}










/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Formular zurücksetzten
//
 
function resetForm(formName){
    $(formName).find('input[type=text],input[type=hidden]').val('');
    $(formName).find('input[type=checkbox]').attr('checked', false);
    $(formName).find('select option:first').attr('selected', true);
    $(formName).find('select[name=country] option[value="de"]').attr('selected',true);
}









/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Formulare ein/ausblenden
//  @param: activeForm: Angabe des Aktiven Elements welches angezeigt werden soll
//  @param: pageForms: Globale Variable in der betreffenden Datei in Form eines Arrays
//  @sample: pageForms = new Array('#benutzerrechte','.formular','.gruppen','#liste'); 

function toggleForms(activeForm){
    for(var i = 0; i < pageForms.length; i++){
        if(pageForms[i] != '') $( pageForms[i] ).hide();
    }
    if(activeForm != '' && typeof activeForm !== 'undefined') $( activeForm ).show(); 
}








/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  In-Array-Function
//  

function in_array(item,arr) {
    for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
    return false;
}








/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Ajax Anzeige ein/ausblenden
//
//  @sample: loadBox({
//              append : '#userList',
//              css : {
//                  height : '50px'
//              }
//           });
//
//  @sample: loadBox({ remove : true }); 

function loadBox(obj){
    
    var defaults = {
        append : 'body',
        css : {
            position : 'relative',
            width : '100%',
            marginLeft: '0px',
            marginTop: '0px',
            height : '100%',
            backgroundColor : '#EFEFEF',
            zIndex : '1',
            opacity : 0.5
        },
        remove : false            
    };
    
    if(typeof obj.css !== undefined){
        optCss = $.extend( defaults.css, obj.css );    
    }else{
        optCss = defaults.css
    }
    
    if(typeof obj !== undefined){
        opt = $.extend( defaults, obj );    
    }else{
        opt = defaults
    }
    
    if(opt.remove != true){
        $div = '<div class="loadBox">';
        $div += '<div class="background"></div>';
        $div += '<div class="image"><img src="/gfx/loadanim.gif" width="20" border="0"></div>';
        $div += '</div>';
        
        $(opt.append).prepend($div);
        $('.loadBox').css({
            position : optCss.position,
            marginLeft : optCss.marginLeft,
            marginTop : optCss.marginTop,
            width : optCss.width,
            height : optCss.height,
            zIndex : optCss.zIndex 
        })
        $('.loadBox').find('.background').css({
            backgroundColor : optCss.backgroundColor,
            opacity : optCss.opacity
        });
    }else{
        $('.loadBox').remove();
    }   
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  IE 6 Alert
//

if($.browser.msie == true && parseInt($.browser.version) < 7){
    alert(_trans['js_ie6_alert']);        
}







/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Globale Funktionen
//


function regexx(Wert){
 	var Ausdruck = /^[0-9a-zA-ZöäüÖÄÜß @.]+$/ ;
 	if ( Wert.match(Ausdruck) ) {		
	 	return true;
 	}else{
		return false;
	}
}




function regexMail(Wert){	   
   var controll = true;
   var komma = /[,]+/;
   var Ausdruck = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/ ;

   if ( Wert.match(komma) ){
        //Wert Splitten
        Wert = Wert.replace(/\s/g,'');
        Wert = Wert.split(',');
        for(var i = 0 ; i < Wert.length ; i++){
           if ( Wert[i].match(Ausdruck) ){}else{
    	 	 controll = false;
	 	   }
        }   
   }else{
    if ( Wert.match(Ausdruck) ){		
	 	controll = true;
 	}else{
		controll = false;
	}
   }
    return controll;
}






