jQuery(document).ready(function($) {
	
	var Preload = [
		{
			src: 'fileadmin/templates/js/lightbox-min.js',
			cond: function() {
				return true;
			},
		},
		{
			src: 'fileadmin/templates/js/lexikon.js',
			cond: function() {
				return $('.tx-sgglossary-pi1').hasClass('tx-sgglossary-pi1');
			},
		},
		{
			src: 'fileadmin/templates/js/rgaccordeon.js',
			cond: function() {
				return $('.rgaccord1-nest').hasClass('rgaccord1-nest');
			},
		},
		{
			src: 'fileadmin/templates/js/wm.js',
			cond: function() {
				return $('.wm_product_wrap').hasClass('wm_product_wrap');
			},
		},
	];


function mainInit() {
	if(typeof String.prototype.isImage == 'function') {
		$.each($('img'), function() {
			if($(this).parent('a').attr('href') && $(this).parent('a').attr('href').isImage()) {
				$(this).lightbox();
			}
		});
	}

	
	function round(zahl) {
		zahl = String(Math.round(zahl * Math.pow(10,2)) / Math.pow(10,2)).split('.');
		if(!zahl[1]) {
			zahl[1] = '00';
		}
		while(zahl[1].length < 2) {
			zahl[1] += '0';
		}
		return zahl[0]+','+zahl[1];	
	}

	$.fn.switchValue = function(defVal){
		$(this).focus(function() {
			if($(this).attr('value') == defVal) {
				$(this).attr('value', '');
			}
		});
		$(this).blur(function() {
			if($(this).attr('value') == '') {
				$(this).attr('value', defVal);
			}
		});
	}


	// SEARCH VALUE
    if($('#tx_indexedsearch .search_input').attr('value') == "")
    	$('#tx_indexedsearch .search_input').attr('value', 'Suchbegriff...');
    $('#tx_indexedsearch .search_input').switchValue('Suchbegriff...');
    
    // EXTRANET
	if($('.wrapper').hasClass('extranet')) {
		// MENU OPENING
		$('#sidebar .menu li.menu-level1-with-subpage').hover(function() {
			$(this).addClass('hover');
			$(this).animate({'opacity' : 1}, 500, function() {
				if($(this).hasClass('hover')) {
					$(this).animate({'height' : $(this).children('.menu-level2').height()+22}, 50*$(this).find('li').length);
				}
			});
			
		}, function() {
			$(this).removeClass('hover');
		});
		$('#sidebar .menu').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
			$(this).animate({'opacity' : 1}, 1000, function() {
				if(!$(this).hasClass('hover')) {
					$.each($('#sidebar .menu li.menu-level1-with-subpage'), function(key, value) {
						$(this).stop().animate({'height' : 22}, 500);
					});
				}
			});
			
		});
		
		// LOGIN FOCUS
		if($('input#user').attr('id') == 'user') {
			$('input#user').focus();
		}
		
		// UNSERE ANGEBOTE // EXLUSIV BEI UNS
		if($('#exklusivform').attr('id') == 'exklusivform') {
			$('#exklusivform .move').attr('title', 'Hier klicken und ziehen zum verschieben.');
			$('#exklusivform .close').addClass('hidden').next('br').remove();
			$.each($('#exklusivform .formelement'), function(k, v) {
				$(v).find('input').first().addClass('headline').switchValue('Neues Element');
				var e = $(v).find('.abstand150').find('input').first();
				e.addClass('pricetext').switchValue('ihr Preis');
				$.each(e.nextAll(), function(k, v) {
					if($(v).attr('type') == 'text') {
						$(v).addClass('price').switchValue('19,95');
						$(v).blur(function() {
							$(this).attr('value', round(parseFloat($(v).attr('value').replace(',', '.'))));
						});
						return false;
					}
				});
				//e.nextAll().find('input').first()
			});
			
			
			$('#exklusivform .open').add('#exklusivform .close').click(function(event) {
				$(event.target).addClass('hidden');
				if($(event.target).hasClass('open')) {
					$(event.target).next().removeClass('hidden');
				} else if($(event.target).hasClass('close')) {
					$(event.target).prev().removeClass('hidden');
				}
			});
			
		}
		
	}
	
	// MAINPAGE
	if($('.wrapper').hasClass('mainpage')) {
		// MAIN_MENU SUBMENU WIDTH
		/*$.each($('.main_menu .menu-level2').add('.main_menu .menu-level3'), function(key, value) {
			var width = 0;
			$.each($(value).find('li'), function(key, value) {
				if($(value).width() > width) {
					width = $(value).width();
				}
			});
			$(value).children('li').css({'width' : width});
		});*/

		// MAINPAGE MAIN_MENU SUBMENU FOCUS
		$('*').click(function(event) {
			$('.main_menu .temp_focus').removeClass('temp_focus');
		});
		$('.main_menu a').focus(function(event) {
			$('.main_menu .temp_focus').removeClass('temp_focus');
			if($(this).parent('li').hasClass('menu-level1-current-active') 
				|| $(this).parent('li').hasClass('menu-level1-with-subpage')
			) {
				$(this).parent('li').addClass('temp_focus');
			} else {
				$(this).closest('.menu-level2').parent('li').addClass('temp_focus');
			}
		});
		$('.main_menu a').blur(function(event) {
			$(this).parent('li').removeClass('temp_focus');
		});
	}
	
}//Start
function mainPreload(i) {
	if(typeof Preload[i] !== "undefined" && Preload[i]) {
		if(Preload[i].cond()) {
			$('head').append('<script src="'+Preload[i].src+'" type="text/javascript"></script>')
		}
		i++;
		mainPreload(i);
	} else {
		mainInit();
	}
}
mainPreload(0);
});// jQ

