window.addEvent('domready', initLinks );
window.addEvent('domready', initTips );

function initLinks() {
		// catch the links
		$$('.dyn_link').addEvent('click', function(e) {
			
			// need to split the variables
			var str_to_split = $(this).getProperty('href');
			var args = str_to_split.split('?')[1];
			
			var req = new Request.HTML({
										method: 'get',
										url: $(this).getProperty('href'),
										data: args,
										onRequest: function() {
											$('dyn_content').getChildren().destroy();
											$('dyn_content').set('html', $('dyn_loader').get('html') );
											e = new Event(e).stop();
										},
										update: $('dyn_content'),
										onComplete: function(responseHTML) {
											$$('.dyn_link').removeEvents('click');
											initLinks();
										}
			}).send();
		});
}

function initLinksFrontpage() {
		// catch the links
		$$('.dyn_link_front').addEvent('click', function(e) {
			
			// need to split the variables
			var str_to_split = $(this).getProperty('href');
			var args = str_to_split.split('?')[1];
			
			var elem = $(this);
			
			var req = new Request.HTML({
										method: 'get',
										url: $(this).getProperty('href'),
										data: args + '&page_ajax=1',
										onRequest: function() {
											$('dyn_content').set('html', $('dyn_loader').get('html') );
											e = new Event(e).stop();
										},
										update: $('dyn_content'),
										onComplete: function(responseHTML) {
											$$('.dyn_link_front').removeEvents('click');
											initLinksFrontpage();
											if( (typeof(pageTracker) != "undefined") ) {
												pageTracker._trackPageview("/"+ elem.get('html') );
											}
										}
			}).send();
		});
}

function initSubmitButtons() {
	
	// get the forms first.
	$$('.dyn_form').each( function( item, index ) {
		
		// now we need to get all the submit buttons in each form and add their respective ajax call.
		item.getElements('.dyn_submit').addEvent('click', function(e) {
			
			// create AJAX calls for each button click
			var req = new Request.HTML({
								method: item.getProperty('method'),
								url: item.getProperty('action'),
								data: item.toQueryString(),
								onRequest: function() {
									$('div_submit').set('html', $('dyn_loader_submit').get('html') );
									e = new Event(e).stop();
								},
								update: $('div_submit'),
								urlEncoded: 'false',
								onComplete: function(responseHTML) {
									$$('.dyn_submit').removeEvents('click');
									initSubmitButtons();
								}
			}).send();			
		});
	});
}

function initMCE() {
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "advimage,safari,pagebreak,style,layer,table,save,advhr,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,pagebreak",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,pastetext,pasteword,|,undo,redo,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote",
		theme_advanced_buttons2 : "link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,ltr,rtl,|,charmap,media,|,hr,removeformat,visualaid,|,sub,sup",
		theme_advanced_buttons3 : "tablecontrols,|,formatselect,fontselect,fontsizeselect,|,pagebreak",
		// theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		extended_valid_elements : 'a[class|style|name|href|target|title|onclick],iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]',

		// Example content CSS (should be your site CSS)
		content_css : "style.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",
		entity_encoding : "raw",

		// Replace values for the template plugin
		template_replace_values : {
				username : "Some User",
				staffid : "991234"
		}
	});
}

function initMCE_lite() {
	tinyMCE.init({
		// General options
		mode : "textareas",
    		theme : "advanced",
		plugins: "table,advimage,advlink",
    		theme_advanced_buttons1 : "bold,italic,underline,link,unlink,bullist,blockquote,undo,justifyleft,justifycenter,justifyright", 
    		theme_advanced_buttons2 : "tablecontrols,image,code", 
    		theme_advanced_buttons3 : "", 

		extended_valid_elements : 'a[class|name|href|target|title|onclick]',
		entity_encoding : "raw",

		// Example content CSS (should be your site CSS)
		content_css : "style.css"
	});
}

function confirmDialog(el) {

	Sexy.confirm( el.getProperty('title'), {
		onComplete: function(returnvalue) {
			if( returnvalue ) {
				createAjaxLink(el, 'dyn_content');
			}
			else return false;
		}
	});
	
}

function confirmMassDialog(el, arrayName, location)  {
	
	var getString = location; // LOCATION MUST HAVE ? IN THE END
	var titleString = el.getProperty('title');
	
	var elementLength = $$(arrayName).length;
	
	if( elementLength <= 0 ) {
		Sexy.alert('No Items were selected.');
		return false;
	}
	
	$$(arrayName).each( function(item) {
		if( item.getProperty('checked') ) {
			getString += '&' + item.getProperty('name') + '=' + item.getProperty('value');
			titleString += '- <b>' + item.getProperty('title') + '</b><br/>';
		}
	});
	
	el.setProperty('href', getString );

	Sexy.confirm( titleString, {
		onComplete: function(returnvalue) {
			if( returnvalue ) {
				createAjaxLink(el, 'dyn_content');
			}
			else return false;
		}
	});
	
}

function controlCheckBoxes(caller, el) {
	// get the elements to process and process
	$$(el).each( function (item) {
		// perform setting / unsetting
		item.setProperty('checked', caller.getProperty('checked') );
	});
	
}

function createAjaxLink( element, divName) {
	
	// need to split the variables
	var str_to_split = element.getProperty('href');
	var args = str_to_split.split('?')[1];
	var location = str_to_split.split('?')[0];
	
	var req = new Request.HTML({
								method: 'get',
								url: location,
								data: args,
								onRequest: function() {
									if( $('dyn_loader') )
										$(divName).set('html', $('dyn_loader').get('html') );
									//e = new Event(e).stop();
								},
								update: $(divName),
								onComplete: function(responseHTML) {}
	}).send();
}

function toggleCheckboxParent(el){
	
	if( el.getProperty('checked') )
		el.getParent('tr').setStyle('background-color', '#f38d11');
	else
		el.getParent('tr').setStyle('background-color', '');
		
}

function changeImage( dest, selItem ) {
	var selValue = selItem.getProperty('value');
	
	//alert(selValue);
	
	var req = new Request.HTML({
								method: 'get',
								url: 'toolbox/exportSchemeScreenshot.php',
								data: 'scheme='+selValue,
								onRequest: function() {
									$(dest).set('html', $('dyn_loader').get('html') );
									//e = new Event(e).stop();
								},
								update: $(dest),
								onComplete: function(responseHTML) {}
	}).send();
}

function initTips() {
		// catch the links
		$$('.lnk_tooltip').each( function(item) {
			
			item.addEvent( 'click', function(e) {
				// stop the event
				e = new Event(e).stop();
			});
		});
			
			var myTips = new Tips( '.lnk_tooltip', {className: 'tooltip' });
			
			myTips.addEvent('show', function(tip){
				tip.setStyle('opacity', 0);				
				tip.getElement('.tip-text').set('html', '' );
				tip.fade('0.9');
			});
}

function sendFrontContactForm( parentDiv, formID ) {
	
	var req = new Request.HTML({
								method: $(formID).getProperty('method'),
								url: $(formID).getProperty('action'),
								data: $(formID).toQueryString(),
								onRequest: function() {
									$(parentDiv).set('html', $('dyn_loader').get('html') );
									//e = new Event(e).stop();
								},
								update: $(parentDiv),
								onComplete: function(responseHTML) {}
	}).send();
}
