/**
 *
 * @param {Object} '#submenu'
 */
/**
 * Overlabel
 */
( function( jQuery ) {
	 
    // plugin definition
    jQuery.fn.overlabel = function( options ) {
 
        // build main options before element iteration
        var opts = jQuery.extend( {}, jQuery.fn.overlabel.defaults, options );
 
        var selection = this.filter( 'label[for]' ).map( function() {
 
            var label = jQuery( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );
			
			var tag_name = field.tagName.toLowerCase();
 
            if ( !field || tag_name == 'select') return;
 
            // build element specific options
            var o = jQuery.meta ? jQuery.extend( {}, opts, label.data() ) : opts;
 
            label.addClass( o.label_class );
 
            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };
 
            jQuery( field )
                 .parent().addClass( o.wrapper_class ).end()
                 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
 
            return this;
 
        } );
 
        return opts.filter ? selection : selection.end();
    };
 
    // publicly accessible defaults
    jQuery.fn.overlabel.defaults = {
 
        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-90000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false
 
    };
 
} )( jQuery );


jQuery(document).ready(function(){
	//Change search submit href
	jQuery('#macina_submit').attr('href',jQuery('#searchform').attr('action'));
	
    var dtime;
    var ctime;
    var menu_level = -1; //count the first level menu-items to set the margin-top of the second menu-items
    var active_menu_level = jQuery('.menu_level1').find('.sMenu').index(jQuery('.menu_level1').find(".sMenu.mACT"));
    try {
        jQuery('#submenu').find("div")[0].style.marginTop = (20 * active_menu_level) + 'px';
    } 
    catch (e) {
    }
    var current_submenu = jQuery('#submenu').clone(); //store the current second menu
    try {
        if (jQuery(".tx-powermail-pi1").find('.tx-powermail-pi1_formwrap').length > 0) {
            jQuery('.csc-default').find('.tx-powermail-pi1').prev().addClass('close').click(function(){
            	toggleMailform();
        	});;
            showMailform();
            addCaptchaReload();
        }
		if(jQuery('.tx-powermail-pi1_confirmation').length > 0){
			//jQuery('#publishRechts .csc-header-n1').addClass('powermail-confirm');
			jQuery('.tx-powermail-pi1').parent().find('.csc-header').addClass('powermail-confirm');
		}
		if(jQuery('.tx-powermail-pi1_thx').length > 0){
			//jQuery('#publishRechts .csc-header').addClass('powermail-reply');
			jQuery('.tx-powermail-pi1').parent().find('.csc-header').addClass('powermail-reply');
		}
		if(jQuery('.tx-powermail-pi1_mandatory').length > 0){
			//jQuery('#publishRechts .csc-header').addClass('powermail-header');
			jQuery('.tx-powermail-pi1').parent().find('.csc-header').addClass('powermail-header');
		}
		if(jQuery("#publishRechts").find('.courseQuicksearch').length > 0){
			jQuery('#publishRechts .csc-header').addClass('powermail-header');
		}
		
		//add toggle 
        jQuery('.ac_co_categoryHeadline').each(function(){
            jQuery(this).click(function(){
                var cHeadline = jQuery(this);
                var cCourses = cHeadline.next('.courses');
                toggleCategoryContent(cCourses, cHeadline);
            })
        });
    } 
    catch (e) {
    }
    
    jQuery(".sMenu").each(function(){
        menu_level++;
        jQuery(this).hover(function(e){
            try {
                clearTimeout(ctime);
                clearTimeout(dtime);
            } 
            catch (e) {
            }
            var i = jQuery(".sMenu").index(jQuery(this));
            var top = 20 * i;
            var topPx = top + 'px';
            jQuery('#submenu').html('');
            jQuery(this).find('.hiddenMenu').clone().each(function(){
                jQuery('#submenu').append(jQuery(this));
                jQuery(this).css({
                    "display": "block"
                });
                jQuery('#submenu').hover(function(){
                    try {
                        clearTimeout(ctime);
                    } 
                    catch (e) {
                    }
                }, function(){
                    try {
                        clearTimeout(dtime);
                    } 
                    catch (e) {
                    }
                    dtime = setTimeout(function(){
                        jQuery('#submenu').find('.hiddenMenu').each(function(){
                            jQuery(this).css({
                                "display": "none"
                            });
                        });
                        jQuery('#submenu').html(current_submenu.html());
                    }, 2000);
                });
            });
            jQuery('#submenu').find("div")[0].style.marginTop = topPx;
        }, function(){
            ctime = setTimeout(function(){
                jQuery('#submenu').find('.hiddenMenu').each(function(){
                    jQuery(this).css({
                        "display": "none"
                    });
                });
                jQuery('#submenu').html(current_submenu.html());
            }, 2000);
        });
    });
	
	// Macina-Searchfield Overlabel
	var labels = jQuery('#searchField label');
	labels.overlabel();
	labels.show();
	labels.css({
		'color' : '#999999'
	});
	
	//Verinice.pro form Overlabels
	var labels_verinicepro = jQuery('fieldset.tx-powermail-pi1_fieldset_fachlicherkontakt label');
	labels_verinicepro.overlabel();
	labels_verinicepro.show();
	labels_verinicepro.css({
		'color' : '#999999'
	});
	
	var labels_verinicepro = jQuery('fieldset.tx-powermail-pi1_fieldset_rechnungskontakt label');
	labels_verinicepro.overlabel();
	labels_verinicepro.show();
	labels_verinicepro.css({
		'color' : '#999999'
	});
	
	//News-Searchfield Overlabel
	var labels_news = jQuery('#news-search-form label');
	labels_news.overlabel();
	labels_news.show();
	labels_news.css({
		'color' : '#999999'
	});
	
	//Overlabels for the forms in publishRechts
	var publish_right_labels = jQuery('#publishRechts .tx_powermail_pi1_fieldwrap_html_text label');
	publish_right_labels.overlabel();
	publish_right_labels.show();
	publish_right_labels.css({
		'color' : '#999999'
	});
	
	var publish_right_labels = jQuery('#publishRechts .tx_powermail_pi1_fieldwrap_html_textarea label');
	publish_right_labels.overlabel();
	publish_right_labels.show();
	publish_right_labels.css({
		'color' : '#999999'
	});
	
	var publish_right_labels = jQuery('#publishRechts .tx_powermail_pi1_fieldwrap_html_captcha label');
	publish_right_labels.overlabel();
	publish_right_labels.show();
	publish_right_labels.css({
		'color' : '#999999'
	});
	
	//add Tooltip to elements with class-name tooltip and shows title content
	jQuery('.tooltip').tooltip({
		showBody: " - ",
		showURL: false
	});
	jQuery('.tx-srlanguagemenu-pi1-flag').tooltip({
		showBody: " - ",
		showURL: false
	});
});

function showMailform(){
    try {
        var mailF = jQuery(".tx-powermail-pi1").hide();
        var header = jQuery('.tx-powermail-pi1').parent().find('.csc-header');
        header.removeClass("open");
        header.addClass('close');
        setTimeout(function(){
            mailF.slideDown("normal");
            header.removeClass("close");
            header.addClass('open');
        }, 3000);
    } 
    catch (e) {
    }
}

function toggleMailform(){
    try {
        var header = jQuery('.tx-powermail-pi1').parent().find('.csc-header');
        jQuery(".tx-powermail-pi1").slideToggle("normal");
        header.hasClass("open") ? header.removeClass("open").addClass('close') : header.removeClass("close").addClass('open');
    } 
    catch (e) {
    }
}

function toggleCategoryContent(cCourses, cHeadline){
    try {
        cCourses.slideToggle("normal");
        cHeadline.hasClass("ac_co_open") ? cHeadline.removeClass("ac_co_open").addClass("ac_co_close") : cHeadline.removeClass("ac_co_close").addClass("ac_co_open");
    } 
    catch (e) {
    }
}

function addCaptchaReload(){
    jQuery('#captcha_reload').click(function(){
    
        jQuery.ajax({
            type: "GET",
            url: 'typo3conf/ext/captcha/captcha/captcha.php',
            success: function(data){
                var img_src = jQuery('#captcha_img').attr('src');
                var timestamp = new Date().getTime();
                jQuery('#captcha_img').attr('src', img_src + '?' + timestamp);
            }
        });
    });
}

function debug(msg){
    try {
        console.log(msg);
    } 
    catch (e) {
    }
}
