var jqueryNew = $;
var isLoading = false;
document.domain = 'creativbaukasten.de';

$(document).ready(function(){
	/**
	 * LAZY Plugin load
	 */
	$.lazy({					
		src: './javascript/jquery.corner.js',
		name: 'corner',		
		cache: true
	});
	
	$.lazy({					
		src: './javascript/superfish.js',
		name: 'superfish',		
		cache: true
	});
	
	$.lazy({					
		src: './javascript/prettyPhoto/js/jquery.prettyPhoto.js',
		name: 'prettyPhoto',
		dependencies: {		
			css: ['./javascript/prettyPhoto/css/prettyPhoto.css']
		},
		cache: true
	});
	
	$.lazy({					
		src: './javascript/tipTip/jquery.tipTip.minified.js',
		name: 'tipTip',
		dependencies: {		
			css: ['./javascript/tipTip/tipTip.css']
		},
		cache: true
	});
	
	$.lazy({					
		src: './javascript/jquery.tweet.js',
		name: 'tweet',	
		cache: true
	});
	
	$.lazy({					
		src: './javascript/passwordStrengthMeter.js',
		name: 'passwordStrength',		
		cache: true
	});
	
//	$.lazy({					
//		src: './javascript/TextboxList/TextboxList.js',
//		name: 'textboxlist',
//		dependencies: {	
//			js: ['./javascript/TextboxList/TextboxList.Autocomplete.js'],
//			js: ['./javascript/TextboxList/GrowingInput.js'],
//			css: ['./javascript/TextboxList/TextboxList.css']
//		}
//	});
	
	$.lazy({					
		src: './javascript/jquery.autogrow-textareas.js',
		name: 'autogrow',
		cache: true
	});
	
	$.lazy({
		src: './javascript/jPicker/jPicker-latest.min.js',
		name: 'jPicker',
		cache: true,
		dependencies: {
			css: ['./javascript/jPicker/jPicker-latest.min.css']
		}
	});
	
	/**
	 * RoundedCorners if Modernizr no-borderradius
	 */	
	if($('html.no-borderradius').length){
		$('.corners').corner();	
 		$('.corner_top, div.advantage-left, div.advantage, div.advantage-single').corner('top');		
		$('div.cdciguide h4.step span.bullet').corner('7px');
		$('.corner_bottom').corner('bottom');
		$('.corner_right, #main_column h4.brown, #main_column h4.orange, #main_column h4.cyan, #main_column h4.green').corner('right 5px');
		$('.corner_left').corner('left 5px');
		$('.corner_bottom_left').corner('bl');
		$('.corner_bottom_right').corner('br');
	} 	

		/**
	 * Submenu Folding
	 
	 $('ul.subnavigation.level_1').accordion({
			event: "mouseover",
			autoHeight: false,
			animated: 'bounceslide'
	});*/
	
	/**
	 * Boxfolding 
	 */
	$('div.foldingbox > h2').wrapInner("<a href=\"javascript:;\" class=\"start-foldingarrow\"><\/a>");

	$('a.start-foldingarrow').click(function(){
		boxcontent = $(this).parent().next();
		if (boxcontent.css('display') == 'block') {
			boxcontent.slideUp("normal");
			$(this).css('background-image', 'url(../right-arrow-folded.gif)');
		}
		else {
			boxcontent.slideDown("normal");
			$(this).css('background-image', 'url(../right-arrow-opened.gif)');
		}
	});

	/**
	 * Search 
	 */
	var defaultSearchterm = $('form.headersearch > div.formbody > input#ctrl_1').attr('value');
	
	$('form.headersearch > div.formbody > input#ctrl_1').blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', defaultSearchterm);
		}
	});

	$('form.headersearch > div.formbody > input#ctrl_1').focus(function() {
		if ($(this).attr('value') == defaultSearchterm) {
			$(this).attr('value', '');
		}
	});

	/**
	 * Font scaler
	 */
	$('a#fontsize-small').click(function() {
		$('body').css('font-size', '60%');
	});
	
	$('a#fontsize-medium').click(function() {
		$('body').css('font-size', '75%');
	});
	
	$('a#fontsize-large').click(function() {
		$('body').css('font-size', '90%');
	});

	/**
	 * Advantage-boxes
	 */
	var advantageMaxHeight = $('div.advantage-left > ul').height();
	if ($('div.advantage > ul').height() > advantageMaxHeight) {
		advantageMaxHeight = $('div.advantage > ul').height(); 
	}
	
	$('div.advantage-left > ul').height(advantageMaxHeight);
	$('div.advantage > ul').height(advantageMaxHeight);	
	
	/**
	 * ImageDb
	 */
	
	/**
	 * Lässt den Benutzer ein Bild aus der Bilddatenbank auswählen
	 * @param string fnCompleted Der Funktionsname der Funktion, die bei Abschluss hier wieder aufgerufen werden soll
	 */
	function imageDbChooseImage(fnCompleted) {
	    ImageDbPopupWindow = window.open('Bildauswahl.html', "Bildauswahl", "width=640,height=480,left=100,top=200,dependent=yes,scrollbars=yes,location=no,menubar=no,resizable=yes,status=yes,toolbar=no");
	    ImageDbPopupWindow.focus();
	}
	
	/**
	 * Lightbox auf a Tags mit rel=lightbox
	 */
	if($('a[rel^="lightbox"]').length){
		$('a[rel^="lightbox"]').prettyPhoto({
			'social_tools': '',
			'gallery_markup': ''
		});
	}
	
	if($('.searchwrapper').length){
		initializeSearchinput();
	}
});

function initializeSearchinput(){
	$('.searchwrapper').each(function(index, element) {
		if($(this).find('.searchicon').length){
			$(this).find('.searchicon').click(function(e) {
				$(this).parent().find('.searchinput').focus();
			});
		}
		if($(this).find('.searchinput').val() != ''){
			$(this).parent().find('.reset').fadeIn('fast');
		}
		$(this).find('.searchinput').keyup(function(e) {
			if($(this).val() == ''){
				$(this).parent().find('.reset').fadeOut('fast');
			}else{
				$(this).parent().find('.reset').fadeIn('fast');
			}
		});
		$(this).find('.reset').click(function(e) {
			$(this).parent().find('.searchinput').val('');
			$(this).parent().find('.searchinput').trigger('change');
			$(this).fadeOut('fast');
		});
	});
}

function initializeTipTip(elements, settings){
	if(settings){
		$(elements).tipTip(settings);
	}else{
		$(elements).tipTip();
	}
}

//Resize iFrame
function iFrameResize(iHeight){
	$(".autoHeight").css("height", iHeight); 
}

//Loadingscreen start
function startLoading(){
	isLoading = true;
	if (!$('#loading').length) {
		$('body').prepend('<div id="loading" style="display: none;"><div class="imageShadow pngfix"></div><img class="loading" src="./tl_files/cbk-theme/pleasewait.gif" width="320" height="84" alt="Bitte warten..." /></div>');
		$('#loading').fadeIn();
	}
}

//Loadingscreen stop
function stopLoading(){
	if ($('#loading').length) {
		$('#loading').fadeOut('slow', function() {
			$('#loading').remove();
		});
		isLoading = false;
	}
}

function parseXml(xmlHttpRequest) {
	$(xmlHttpRequest.responseXML).find('action').each(function(key, value) {
		switch ($(value).attr('method')) {
			case "insertHtml":
				$($(value).attr('target')).html($(value).text());
				break;
			case "setAttribute":
				$($(value).attr('target')).attr($(value).attr('attribute'), $(value).text());
				break;
			case "alert":
				alert($(value).text());
				break;
			case "fadeIn":
				$($(value).attr('target')).fadeIn();
				break;
			case "fadeOut":
				$($(value).attr('target')).fadeOut();
				break;
			case "redirect":
				window.location.href = 'www.creativbaukasten.de';
				break;
			case "execute": 	
				eval($(value).attr('target'));
				break;
			default: 
				break;	
		}
	});
}

//Browserwarnung
var $buoop = {reminder:1} 
$buoop.ol = window.onload; 
window.onload=function(){ 
	if ($buoop.ol) $buoop.ol(); 
	var e = document.createElement("script"); 
	e.setAttribute("type", "text/javascript"); 
	e.setAttribute("src", "http://browser-update.org/update.js"); 
	document.body.appendChild(e); 
} 

