function deactivateCheckboxes(id, checkboxName){
	var checkVal = $('#' + id + ' input').each(function(){
		if ($(this).attr('checked')) {
		
		}
		else {
			$(this).attr('disabled', 'disabled');
		}
	});
}

function textcheck(){
	maxtxt = 250;
	var txt = document.getElementById('teaserText').value;
	window.document.getElementById('counter').innerHTML = txt.length;
	if ((maxtxt - txt.length) < 0) {
		window.document.getElementById('counter').style.color = 'red';
	}
	else {
		window.document.getElementById('counter').style.color = 'green';
	}
}

var W3CDOM = (document.createElement && document.getElementsByTagName);

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

$(document).ready(function() {		
	$('#dynamicWrap').css('display', 'block');
	
	/*$("#contentWrap-1, #contentWrap-2, #contentWrap-3, #contentWrap-4, #contentWrap-5").sortable({
		accept: 'box',
		helperclass: 'sortHelper',
		connectWith: '.contentWrap',
		handle: '.boxHeader',
		tolerance: 'pointer',
		axis: 'y',
		onChange : function(ser){
		},
		onStart : function(){
			$.iAutoscroller.start(this, document.getElementsByTagName('body'));
		},
		onStop : function(){
			$.iAutoscroller.stop();
		}
	});
	$(".linkWraper").sortable({
		accept: 'link',
		helperclass: 'sortHelper',
		connectWith: '.linkWraper',
		handle: '.linkHeader',
		tolerance: 'pointer',
		axis: 'y',
		onChange : function(ser){
		},
		onStart : function(){
			$.iAutoscroller.start(this, document.getElementsByTagName('body'));
		},
		onStop : function(){
			$.iAutoscroller.stop();
		}
	});
	
	$("#contentWrap-1, #contentWrap-2, #contentWrap-3, #contentWrap-4, #contentWrap-5").disableSelection();
	$(".linkWraper").disableSelection();
		
	*/
	var globalBoxId;
	var globalBoxXmlFile;
	var globalBoxType;
	
	var globalLinkId;
	var globalLinkXmlFile;
	var globalLinkType;
	
	$('#teaserText').keyup(function(){
		var maxtxt = 250;
		var len = $('#teaserText').val().length;
		if ((maxtxt - len) < 0) {
			$('#counter').css('color', 'red');
			$('#teaserText').css('color', 'red');
		}
		else {
			$('#counter').css('color', 'green');
			$('#teaserText').css('color', 'black');
		}
		$('#counter').html(len);
	});
	
});

function replaceForGet(str) {
//	var replace = new Array(/&uuml;/g, /&Uuml;/g, /&ouml;/g, /&Ouml;/g, /&auml;/g, /&Auml;/g, /&szlig;/g, /&shy;/g);
//	var by = new Array("ü", "Ü", "ö", "Ö", "ä", "Ä", "ß", "­");
	var replace = new Array(/&nbsp;/g, /&iexcl;/g, /&cent;/g, /&pound;/g, /&curren;/g, /&yen;/g, /&brvbar;/g, /&sect;/g, /&uml;/g, /&copy;/g, /&ordf;/g, /&laquo;/g, /&not;/g, /&shy;/g, /&reg;/g, /&macr;/g, /&deg;/g, /&plusmn;/g, /&sup2;/g, /&sup3;/g, /&acute;/g, /&micro;/g, /&para;/g, /&middot;/g, /&cedil;/g, /&sup1;/g, /&ordm;/g, /&raquo;/g, /&frac14;/g, /&frac12;/g, /&frac34;/g, /&iquest;/g, /&Agrave;/g, /&Aacute;/g, /&Acirc;/g, /&Atilde;/g, /&Auml;/g, /&Aring;/g, /&AElig;/g, /&Ccedil;/g, /&Egrave;/g, /&Eacute;/g, /&Ecirc;/g, /&Euml;/g, /&Igrave;/g, /&Iacute;/g, /&Icirc;/g, /&Iuml;/g, /&ETH;/g, /&Ntilde;/g, /&Ograve;/g, /&Oacute;/g, /&Ocirc;/g, /&Otilde;/g, /&Ouml;/g, /&times;/g, /&Oslash;/g, /&Ugrave;/g, /&Uacute;/g, /&Ucirc;/g, /&Uuml;/g, /&Yacute;/g, /&THORN;/g, /&szlig;/g, /&agrave;/g, /&aacute;/g, /&acirc;/g, /&atilde;/g, /&auml;/g, /&aring;/g, /&aelig;/g, /&ccedil;/g, /&egrave;/g, /&eacute;/g, /&ecirc;/g, /&euml;/g, /&igrave;/g, /&iacute;/g, /&icirc;/g, /&iuml;/g, /&eth;/g, /&ntilde;/g, /&ograve;/g, /&oacute;/g, /&ocirc;/g, /&otilde;/g, /&ouml;/g, /&divide;/g, /&oslash;/g, /&ugrave;/g, /&uacute;/g, /&ucirc;/g, /&uuml;/g, /&yacute;/g, /&thorn;/g, /&yuml;/g, /&fnof;/g, /&Alpha;/g, /&Beta;/g, /&Gamma;/g, /&Delta;/g, /&Epsilon;/g, /&Zeta;/g, /&Eta;/g, /&Theta;/g, /&Iota;/g, /&Kappa;/g, /&Lambda;/g, /&Mu;/g, /&Nu;/g, /&Xi;/g, /&Omicron;/g, /&Pi;/g, /&Rho;/g, /&Sigma;/g, /&Tau;/g, /&Upsilon;/g, /&Phi;/g, /&Chi;/g, /&Psi;/g, /&Omega;/g, /&alpha;/g, /&beta;/g, /&gamma;/g, /&delta;/g, /&epsilon;/g, /&zeta;/g, /&eta;/g, /&theta;/g, /&iota;/g, /&kappa;/g, /&lambda;/g, /&mu;/g, /&nu;/g, /&xi;/g, /&omicron;/g, /&pi;/g, /&rho;/g, /&sigmaf;/g, /&sigma;/g, /&tau;/g, /&upsilon;/g, /&phi;/g, /&chi;/g, /&psi;/g, /&omega;/g, /&thetasym;/g, /&upsih;/g, /&piv;/g, /&bull;/g, /&hellip;/g, /&prime;/g, /&Prime;/g, /&oline;/g, /&frasl;/g, /&weierpv/g, /&image;/g, /&real;/g, /&trade;/g, /&alefsym;/g, /&larr;/g, /&uarr;/g, /&rarr;/g, /&darr;/g, /&harr;/g, /&crarr;/g, /&lArr;/g, /&uArr;/g, /&rArr;/g, /&dArr;/g, /&hArr;/g, /&forall;/g, /&part;/g, /&exist;/g, /&empty;/g, /&nabla;/g, /&isin;/g, /&notin;/g, /&ni;/g, /&prod;/g, /&sum;/g, /&minus;/g, /&lowast;/g, /&radic;/g, /&prop;/g, /&infin;/g, /&ang;/g, /&and;/g, /&or;/g, /&cap;/g, /&cup;/g, /&int;/g, /&there4;/g, /&sim;/g, /&cong;/g, /&asymp;;/g, /&ne;/g, /&equiv;/g, /&le;/g, /&ge;/g, /&sub;/g, /&sup;/g, /&nsub;/g, /&sube;/g, /&supe;/g, /&oplus;/g, /&otimes;/g, /&perp;/g, /&sdot;/g, /&amp;/g, /&lt;/g, /&gt;/g, /&OElig;/g, /&oelig;/g, /&Scaron;/g, /&scaron;/g, /&Yuml;/g, /&circ;/g, /&tilde;/g, /&ensp;/g, /&emsp;/g, /&thinsp;/g, /&zwnj;/g, /&zwj;/g, /&lrm;/g, /&rlm;/g, /&ndash;/g, /&mdash;/g, /&lsquo;/g, /&rsquo;/g, /&sbquo;/g, /&ldquo;/g, /&rdquo;/g, /&bdquo;/g, /&dagger;/g, /&Dagger;/g, /&permil;/g, /&lsaquo;/g, /&rsaquo;/g, /&euro;/g, /&/g);
	var by = new Array(" ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "ƒ", "Α", "Β", "Γ", "Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω", "α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "ς", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "ϑ", "ϒ", "ϖ", "•", "…", "′", "″", "‾", "⁄", "℘", "ℑ", "ℜ", "™", "ℵ", "←", "↑", "→", "↓", "↔", "↵", "⇐", "⇑", "⇒", "⇓", "⇔", "∀", "∂", "∃", "∅", "∇", "∈", "∉", "∋", "∏", "∑", "−", "∗", "√", "∝", "∞", "∠", "∧", "∨", "∩", "∪", "∫", "∴", "∼", "≅", "≈", "≠", "≡", "≤", "≥", "⊂", "⊃", "⊄", "⊆", "⊇", "⊕", "⊗", "⊥", "⋅", "&", "<", ">", "Œ", "œ", "Š", "š", "Ÿ", "ˆ", "˜", '" "', '" "', '" "', '"‌"', '"‍"', "‎", "‏", "–", "—", "‘", "’", "‚", "“", "”", "„", "†", "‡", "‰", "‹", "›", "€", "und");
//	alert("VOR a Replace: " + str);
	for (var i=0; i<replace.length; i++) {
		
		str = str.replace(replace[i], by[i]);
		
	}
//	str = str.replace(/&uuml;/g, "ü");
//	str = str.replace(/&Uuml;/g, "Ü");
//	str = str.replace(/&ouml;/g, "ö");
//	str = str.replace(/&Ouml;/g, "Ö");
//	str = str.replace(/&auml;/g, "ä");
//	str = str.replace(/&Auml;/g, "Ä");
//	str = str.replace(/&szlig;/g, "ß");
//	alert("NACH REPLACE: " + str);
	return str;
}

function showPreview(){
	var url = document.URL + "get=ajax&mode=preview";
	
	jQuery.each($('textarea'), function(){
		tinyMCE.execCommand('mceRemoveControl', false, this.id);
	});
	
	var s = $('#tx_fe_be_cm_addNew').serializeArray();
	var t = {};
	
	jQuery.each(s,function(){
		//tinyMCE.execCommand('mceRemoveControl',false,'{$id}');
		t[this.name] = replaceForGet(this.value);
	});
	
	json = JSON.stringify(t);
//	alert("AUS JS:" + json);
//	$('#contentWrap-4').html(json);
//	alert(JSON.stringify(t));
	$.ajax({ 
	    method: "get",
	    url: document.URL,
	    data: "get=ajax&mode=preview&json="+json,
	    beforeSend: function(){
	    	$.blockUI({ message: '<img src="/typo3conf/ext/leonhardt_fe_be_cm/img_shared/busy.gif" /> Vorschau wird generiert ...' });
	    },
	  	complete: function(){ 
	  	},
		success: function(html){
//			alert("AUS PHP: " + html);
			$('#contentWrap-4').html(html);
			
			jQuery.each($('textarea'), function(){		
				tinyMCE.execCommand('mceAddControl', false, this.id);
			});
			tinyMCE.execCommand('mceRemoveControl', false, 'teaserText');
	    }
   	});
//	$.post(url, {module : "saveCampaign",vars : JSON.stringify(t)},function(data){
//		$('#contentWrap-4').html(data);
//	});
}

// unblock when ajax activity stops 
$(document).ajaxStop($.unblockUI);

function serialize(s)
{
	serial = $.SortSerialize(s);
	alert(serial.hash);
};


function showBoxHTML(id){
	alert($('#' + id).html());
}

function renderLayerBox(content){

}

function openLoading() {
	$.openPopupLayer({
		name: "openLoading",
		url: "/typo3conf/ext/leonhardt_fe_be_cm/lib/html/loading.html"
	});
}

function deleteBox(id){
	$('#' + id).remove();
}

function selectBoxTypeToAdd(){
	$.openPopupLayer({
		name: "selectBoxTypeToAdd",
		url: "/typo3conf/ext/leonhardt_fe_be_cm/lib/html/addContentBox.html"
	});
	
}

function selectLinkTypeToAdd(){
	$.openPopupLayer({
		name: "selectLinkTypeToAdd",
		url: "/typo3conf/ext/leonhardt_fe_be_cm/lib/html/addLinkType.html"
	});
}

function addBox(xml, id){
	//alert(xml + ' ' + id);
	/*error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.responseText);
        alert(thrownError);
    }*/
	$.ajax({ 
	    method: "get",
	    url: document.URL,
	    data: "get=ajax&mode=addbox&xmlFile="+xml+"&frombox="+id,
	    beforeSend: function(){
	    	//openLoading()
	    	//$("#loading").dialog('open');
	    	$.blockUI({ message: '<img src="/typo3conf/ext/leonhardt_fe_be_cm/img_shared/busy.gif" /> Content-Element wird angelegt ...' });
	    },
	  	complete: function(){ 
	  		//$.closePopupLayer('openLoading');
	  		//$("#loading").dialog('close'); 
	  		//$.unblockUI;
	  	},
		success: function(html){
			//alert(html);
			$('#' + id).append(html);
	    }
   	});	
	
}

function addLink(type, xml, id){
	//alert(type + ' ' + xml + ' ' + id);
	/*error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.responseText);
        alert(thrownError);
    }*/
	
	switch(type){
		case 0:
			$.ajax({ 
			    method: "get",
			    url: document.URL,
			    data: "get=ajax&mode=addbox&xmlFile="+xml+"&frombox="+id,
			    beforeSend: function(){
			    	//openLoading()
			    	//$("#loading").dialog('open');
			    	$.blockUI({ message: '<img src="/typo3conf/ext/leonhardt_fe_be_cm/img_shared/busy.gif" /> Link wird angelegt ...' });
			    },
			  	complete: function(){ 
			  		//$.closePopupLayer('openLoading');
			  		//$("#loading").dialog('close');
			  		//$.unblockUI;
			  	},
				success: function(html){
					//alert(html);
					$('#' + id + ' .linkWrap').before(html);
			    }
		   	});	
			break;
		case 1:
			$.ajax({ 
			    method: "get",
			    url: document.URL,
			    data: "get=ajax&mode=addbox&xmlFile="+xml+"&frombox="+id,
			    beforeSend: function(){
			    	//openLoading()
			    	//$("#loading").dialog('open');
			    	$.blockUI({ message: '<img src="/typo3conf/ext/leonhardt_fe_be_cm/img_shared/busy.gif" /> Link wird angelegt ...' });
			    },
			  	complete: function(){ 
			  		//$.closePopupLayer('openLoading');
			  		//$("#loading").dialog('close');
			  		//$.unblockUI;
			  	},
				success: function(html){
					//alert(html);
					$('#' + id + ' .linkWrap').after(html);
			    }
		   	});	
			break;
	}	
}

function deleteArticle(id){
	$.ajax({ 
	    method: "get",
	    url: document.URL,
	    data: "get=ajax&mode=delete&articleId="+id,
	    beforeSend: function(){
	    	$("#deleteBox_" + id).dialog('close');
	    	$.blockUI({ message: '<img src="/typo3conf/ext/leonhardt_fe_be_cm/img_shared/busy.gif" /> Beitrag wird gelöscht' });
	    },
	  	complete: function(){ 
	  		
	  	},
		success: function(html){
			location.reload();
	    }
   	});	
}

function selectBoxType(id){
	globalBoxId = id;
	//selectBoxTypeToAdd();
	$("#addContentBox").dialog('open');
}

function selectMarginalBoxType(id){
	globalBoxId = id;
	//selectBoxTypeToAdd();
	$("#addMarginalBox").dialog('open');
}

function selectBoxToAdd(newXmlType){
	//$.closePopupLayer('selectBoxTypeToAdd');
	$("#addContentBox").dialog('close');	
	
	switch(newXmlType){
		case 'imgTop':
			globalBoxXmlFile = xmlFileImgTop;
			break;
		case 'imgLeft':
			globalBoxXmlFile = xmlFileImgLeft;
			break;
	}
	
	addBox(globalBoxXmlFile, globalBoxId);
}

function selectMarginalBoxToAdd(newXmlType){
	//$.closePopupLayer('selectBoxTypeToAdd');
	$("#addMarginalBox").dialog('close');	
	
	switch(newXmlType){		
		case 'marginal':
			globalBoxXmlFile = xmlFileMarginal;
			break;
	}
	
	addBox(globalBoxXmlFile, globalBoxId);
}

function selectLinkType(type, id){
	globalLinkId = id;
	globalLinkType = type;
	//selectLinkTypeToAdd();
	$("#addLinkType").dialog('open');
}

function selectLinkToAdd(linkType){
	//$.closePopupLayer('selectLinkTypeToAdd');
	$("#addLinkType").dialog('close');	
	
	switch(linkType){
		case 'download':
			globalLinkXmlFile = xmlFileDownload;
			break;
		case 'link':
			globalLinkXmlFile = xmlFileLink;
			break;
		case 'mail':
			globalLinkXmlFile = xmlFileMail;
			break;
	}
	
	addLink(globalLinkType, globalLinkXmlFile, globalLinkId);
}
