/** ******** */
/* INIT AJAX */
/** ******** */
$.ajaxSetup( {
	url : '/inc/ajx/ajx_server.php',
	type : "POST",
	async : true,
	cache : false,
	dataType : 'json',
	timeout : 40000,
	success : function(data, textStatus) {
		if (data.javascript)
			if (data.javascript.length)
				$.each(data.javascript, function(key, val) {
					try {
						eval(val);
					} catch (e) {
						alert('eval error: ' + e)
					}
				});
		if (data.divs)
			$.each(data.divs, function(key, val) {
				$('.template_' + key).html(val);
			});
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('general error: ' + thrownError);
	}
});
/** *************** */
/* LAZY LOAD IMAGES */
/** *************** */
// $( function() {
// $(document).ready( function() {
// jQuery( function() {
// jQuery("img").lazyload( {
// placeholder :"images/spacer.gif",
// effect :"fadeIn"
// });
// });
// });
// });
/** ****************************** */
/* WIDGET DRAGGABLE CONTENT EDITOR */
/** ****************************** */
$(function() {
	if ($.cookie('is_login') == 1)
		init_draggable_content_editor();
});
function init_draggable_content_editor() {
	$("span.djcms_editor_btn_add").remove();
	$(".djcms_editor_column").append("<span class='djcms_editor_btn_add ui-icon ui-icon-plus' title='Nieuw item toevoegen' style='float:right'></span>");
	$("<link/>", { // load frames css todo: werkt niet op een sub path
				rel : "stylesheet",
				type : "text/css",
				href : "./templates/inc/jquery.draggable.css"
			}).appendTo("head");
	$(".djcms_editor_column").css('width', '100%').css('float', 'left').css('padding-bottom', '10px').css('border', '1px dotted gray');
	$(".djcms_editor_element").css('border', '1px dotted gray').css('min-height', '20px');
	$("span.djcms_editor_btn_add").click(function() {
		if ($(this).attr('click') != 1) {
			$(this).attr('click', 1);
			var cid = (($.getUrlVar('cid')) ? $.getUrlVar('cid') : $.cookie('cid_home')).replace(/#.*$/, "");
			var props = new Object;
			props.dialog_src = './index.php?module=cms_add_widget&column=' + $(this).parent().attr('id') + "&cid=" + cid;
			props.dialog_title = "Toevoegen van een content veld";
			props.dialog_width = 550;
			props.dialog_height = 400;
			props.dialog_buttons = 'toevoegen|sluiten|vernieuwen';
			cms_dialog_frame(props, cid);
			$(this).attr('click', 0).delay(2000);
		}
	});
	$("span.djcms_editor_btn_del").click(function() {
		if ($(this).attr('click') != 1) {
			$(this).attr('click', 1);
			if (confirm(urldecode($.cookie('del_confirm')))) {
				var cid = (($.getUrlVar('cid')) ? $.getUrlVar('cid') : $.cookie('cid_home')).replace(/#.*$/, "");
				var section = $(this).attr('id').replace('djcms_editor_', '');
				$("div." + $(this).attr('id')).remove();
				$.ajax( {
					data : "class=wysiwyg_data&type=del_element&cid=" + cid + "&section=" + section
				});
			}
			$(this).attr('click', 0).delay(2000);
		}
	});
	$("span.djcms_editor_btn_edit").click(function() {
		if ($(this).attr('click') != 1) {
			$(this).attr('click', 1);
			var cid = (($.getUrlVar('cid')) ? $.getUrlVar('cid') : $.cookie('cid_home')).replace(/#.*$/, "");
			var section = $(this).attr('id').replace('djcms_editor_', '');
			$("table.djcms_editor_" + section).css('visibility', 'hidden').css('position', 'absolute');
			var html = $(this).attr('html');
			var type = $(this).attr('editor');
			$.ajax( {
				data : "class=wysiwyg_data&type=get_editor" + "&cid=" + cid + "&section=" + section + "&html=" + html + "&widget=" + type
			});
			$(this).attr('click', 0).delay(2000);
		}
	});
	$(".djcms_editor_column").ready(function() {
		$(".djcms_editor_column").sortable( {
			handle : 'span.djcms_editor_btn_drag',
			forcePlaceholderSize : true,
			connectWith : '.djcms_editor_column',
			cursor : 'move',
			cursorAt : {
				top : 0,
				left : 0
			},
			placeholder : 'ui-sortable-placeholder',
			tolerance : 'pointer',
			stop : function(event, ui) {
				var cid = (($.getUrlVar('cid')) ? $.getUrlVar('cid') : $.cookie('cid_home')).replace(/#.*$/, "");
				var outline = {};
				$("div.djcms_editor_element").each(function() {
					var section = $(this).attr('section');
					outline[section] = {};
					outline[section]['column'] = trim($(this).parent().attr('id'));
					outline[section]['section'] = section;
					outline[section]['editor'] = $(this).attr('editor');
					outline[section]['html'] = $(this).attr('html');
					$("div.djcms_editor").each(function() {
						if ($(this).attr('id') == "djcms_editor_" + section)
							outline[section]['value'] = $(this).html();
					});
					$.ajax( {
						data : "class=wysiwyg_data&type=update_structure&cid=" + cid + "&content_outline=" + urlencode($.toJSON(outline))
					});
				});
			}
		});
	});
}
/** ****** */
/* DIALOGS */
/** ****** */
$(function() {
	$("#cms_dialog_window").dialog( {
		// modal: true,
		autoOpen : false,
		width : 500,
		height : 500,
		buttons : {
			'Sluiten' : function() {
				$(this).dialog('close');
			}
		},
		resizeStop : function(event, ui) {
			var iFrameObj = document.getElementById('cms_dialog_frame');
			iFrameObj.width = $("#cms_dialog_window").dialog('option', 'width') - 20;
			iFrameObj.height = $("#cms_dialog_window").dialog('option', 'height') - 80;
		},
		close : function(event, ui) {
			try {
				top.document.getElementById('cms_dialog_frame').src = './?module=static_loading';
			} catch (e) {
			}
		}
	});
});
function cms_dialog_window_opslaan(obj) {
	alert('functie verwijderen 1'); // todo: verwijderen
	var formObj = getFrameContents('cms_dialog_frame').forms["djcms_FCKeditor"];
	if (formObj.onsubmit && !formObj.onsubmit())
		return;
	formObj.submit();
}
function cms_dialog_window_uitloggen() {
	 
	$.ajax( {
		data : "class=login&type=do_logout"
	});
}
function start_editor(wid, cid, propHtml, selectedClass) {
	$("table.djcms_editor_" + wid).css('visibility', 'hidden').css('position', 'absolute');
	$.ajax( {
		data : "class=wysiwyg_data&type=get_editor" + "&cid=" + cid + "&section=" + wid + "&html=" + propHtml + "&widget=" + selectedClass
	}); parent.$('#cms_dialog_window').dialog("close");
}
function btn_sluiten() {
	parent.$('#cms_dialog_window').dialog("close");
}
function btn_vernieuwen() {
	location.reload();
}
function cms_dialog_frame(obj, id) {
	// close old dialog
	top.document.getElementById('cms_dialog_frame').style.visibility = 'hidden';
	$("#cms_dialog_window").dialog("close");
	parent.$("#cms_dialog_window").dialog("close");
	// dialog_title
	var dialog_title = (obj.dialog_title) ? obj.dialog_title : obj.getAttribute('dialog_title');
	$("#cms_dialog_window").dialog('option', 'title', dialog_title);
	parent.$("#cms_dialog_window").dialog('option', 'title', dialog_title);
	// dialog_width
	var dialog_width = (obj.dialog_width) ? obj.dialog_width : obj.getAttribute('dialog_width');
	if (dialog_width) {
		$("#cms_dialog_window").dialog('option', 'width', dialog_width);
		parent.$("#cms_dialog_window").dialog('option', 'width', dialog_width);
		top.document.getElementById('cms_dialog_frame').width = (dialog_width - 20);
	}
	// dialog_height
	var dialog_height = (obj.dialog_height) ? obj.dialog_height : obj.getAttribute('dialog_height');
	if (dialog_height) {
		$("#cms_dialog_window").dialog('option', 'height', dialog_height);
		parent.$("#cms_dialog_window").dialog('option', 'height', dialog_height);
		top.document.getElementById('cms_dialog_frame').height = (dialog_height - 90);
	}
	// dialog_src
	var dialog_src = (obj.dialog_src) ? obj.dialog_src : obj.getAttribute('dialog_src');
	if (dialog_src) {
		if (id)
			top.document.getElementById('cms_dialog_frame').src = dialog_src.replace(/cid=([0-9]+)/, "cid=" + id);
		else
			top.document.getElementById('cms_dialog_frame').src = dialog_src
	}
	// dialog_buttons
	var dialog_buttons = (obj.dialog_buttons) ? obj.dialog_buttons : obj.getAttribute('dialog_buttons');
	if (dialog_buttons) {
		var funcs = dialog_buttons.split("|");
		var buttons = "$('#cms_dialog_window').dialog('option', 'buttons', { \n";
		for ( var i in funcs)
			buttons += "'" + ucwords(trim(strtolower(funcs[i]).replace(/_/g, " "))) + "' : function() { $('#cms_dialog_frame')[0].contentWindow.btn_" + trim(strtolower(funcs[i])) + "(); }, \n";
		buttons = trim(buttons).substring(0, trim(buttons).length - 1) + "});";
		eval("try {" + buttons + "} catch (e) {}; \n try {" + buttons.replace(/\$/g, "parent.$") + "} catch (e) {};");
	}
	// dialog other props
	$("#cms_dialog_window").dialog('option', 'modal', true);
	parent.$("#cms_dialog_window").dialog('option', 'modal', true);
	// open dialog
	$("#cms_dialog_window").dialog("open");
	parent.$("#cms_dialog_window").dialog("open");
	return false;
}
/** ****** */
/* EXTENDS */
/** ****** */
$(function() {
	$.extend( {
		getUrlVars : function() {
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for ( var i = 0; i < hashes.length; i++) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return vars;
		},
		getUrlVar : function(name) {
			return $.getUrlVars()[name];
		}
	});
	$.extend($.ui.dialog.prototype, {
		'addbutton' : function(buttonName, func) {
			var buttons = this.element.dialog('option', 'buttons');
			buttons[buttonName] = func;
			this.element.dialog('option', 'buttons', buttons);
		}
	});
	$.extend($.ui.dialog.prototype, {
		'removebutton' : function(buttonName) {
			var buttons = this.element.dialog('option', 'buttons');
			delete buttons[buttonName];
			this.element.dialog('option', 'buttons', buttons);
		}
	});
});
function show_flashgallery(gallery, targetDiv) {
	var so = new SWFObject("/resources/flashgallery/flashgallery.swf", "gallery", "100%", "500", "8");
	so.addParam("quality", "high");
	so.addParam("allowFullScreen", "true");
	so.addParam("wmode", "transparent");
	so.addVariable("content_path", gallery);
	so.addVariable("color_path", "/resources/flashgallery/flashgallery.xml");
	so.addVariable("script_path", "/resources/flashgallery/flashgallery.php");
	so.write(targetDiv);
}

