/**
 * Let Property Script
 * Dependencies: Mootools v1.1+
 * @author Kevin Dew <kev@dewsolutions.co.uk>
 * @copyright Copyright Kevin Dew, 2007
 */


var scroll = new Fx.Scroll(window, {wait: false, duration: 250, transition: Fx.Transitions.Quad.easeInOut});

// feb 5th 2010 - commented out all ajax

//window.addEvent('domready', setupSubmit);
//function setupSubmit(e)
//{
//	if($('let_property_gen'))
//	{
//
//		if(typeof tinyMCE != 'undefined' && $('mce_editor'))
//		{
//			tinyMCE.execCommand('mceAddControl', false, 'mce_editor');
//			tinyInitCallback();
//		}
//
//		var elements = $$('#let_property_gen input[name^=submit]');
//		for(var i = 0; i < elements.length ; i++)
//		{
//			$(elements[i]).addEvent('click', function(e)
//			{
//				var qaAppend = '';
//				if($(this).getProperty('name') != '')
//				{
//					var value = ($(this).getProperty('value') != '') ? encodeURIComponent($(this).getProperty('value')) : 'empty';
//					qaAppend = '&' + $(this).getProperty('name') + '=' + value
//				}
//
//				if(typeof tinyMCE != 'undefined' && $('mce_editor'))
//				{
//					//tinyMCE.execCommand('mceRemoveControl', false, 'mce_editor');
//					tinyMCE.triggerSave();
//				}
//
//				var queryString = $('let_property_gen').toQueryString();
//				if(queryString.length)
//					queryString = queryString + '&';
//				queryString = queryString + 'ajax=true' + qaAppend;
//
//				//try fix the problem caused by pound signs
//				queryString = queryString.replace(/%C2%A3/g, "%A3");
//
//				var submitAjax = new Ajax(
//							  'let-property.php',
//							  {
//							  method: 'post',
//							  //update: $('center_column2'),
//							  data: queryString,
//							  autoCancel: true,
//							  onComplete: ajaxProcessed
//							  }
//							  ).request();
//
//				if($('next_step'))
//					new Element('div',{'class': 'loading'}).injectTop('next_step');
//
//				$$('#let_property_gen input[name^=submit]').setProperty('disabled', 'disabled');
//				new Event(e).stop();
//			});
//		}
//	}
//
//	if($('let_property_images'))
//	{
//		var elements = $$('#let_property_images input[name^=submit]');
//		for(var i = 0; i < elements.length; i++)
//		{
//			$(elements[i]).addEvent('click', function(e)
//			{
//				//handle image uploads
//				//create an iframe
//				if(!$('upload'))
//					new Element('iframe', {'name': 'upload', 'id': 'upload', 'class': 'hidden_iframe'}).injectInside($$('body')[0]);
//				if(!$('ajax'))
//					new Element('input', {'type': 'hidden', 'name': 'ajax', 'id': 'ajax', 'value': 'true'}).injectInside($('let_property_images'));
//				$('let_property_images').setProperty('target', 'upload');
//
//				if($(this).getProperty('name') != '')
//				{
//					if($('hidden_' + $(this).getProperty('name')))
//						$('hidden_' + $(this).getProperty('name')).setProperty('value', $(this).getProperty('value'));
//					else
//						new Element('input', {'type': 'hidden', 'name': $(this).getProperty('name'), 'id': 'hidden_' + $(this).getProperty('name'), 'value': $(this).getProperty('value')}).injectInside($('let_property_images'));
//				}
//
//				//disable the buttons
//				$$('#let_property_images input[type=submit]').setProperty('disabled', 'disabled');
//				$$('#let_property_images input[type=image]').setProperty('disabled', 'disabled');
//
//				if($('next_step'))
//					new Element('div',{'class': 'loading'}).injectTop('next_step');
//
//				//ie seems to need this
//				$('let_property_images').submit();
//			});
//
//		}
//
//	}
//
//
//
//	if($('hear_about_us'))
//	{
//		function hearAboutUsChange()
//		{
//			if(!$('hearAboutUsOtherCont') || !$('hear_about_us_other'))
//				return;
//
//			if($('hear_about_us').getValue().toLowerCase() == 'other')
//			{
//				if($('hearAboutUsOtherCont').hasClass('hide'))
//					$('hearAboutUsOtherCont').removeClass('hide');
//			}
//			else
//			{
//				if(!$('hearAboutUsOtherCont').hasClass('hide'))
//				{
//					$('hearAboutUsOtherCont').addClass('hide');
//					$('hear_about_us_other').setProperty('value', '');
//				}
//			}
//		}
//
//		$('hear_about_us').addEvent('change', hearAboutUsChange);
//
//		hearAboutUsChange();
//	}
//
//}
//
//function ajaxProcessed(text)
//{
//	if(typeof tinyMCE != 'undefined' && $('mce_editor') && $$('#let_property_gen input'))
//	{
//		//stop tinymce hogging focus
//		$$('#let_property_gen input')[0].focus();
//		tinyMCE.execCommand('mceRemoveControl', false, 'mce_editor');
//	}
//	if(text != '')
//		$('center_column2').empty().setHTML(text);
//	setupSubmit();
//	scroll.toTop();
//
//}
//function loadedIframe()
//{
//	//code changed from using dom elements as ie goes mental with them instead the dirty html approach is used
//	var tabs = top.frames['upload'].document.getElementById('letpropertytabs');
//	var div = top.frames['upload'].document.getElementById('letpropertydivcontainer');
//	$('letpropertytabs').setHTML(tabs.innerHTML);
//	$('letpropertydivcontainer').setHTML(div.innerHTML);
//	ajaxProcessed('');
//	var body = top.frames['upload'].document.getElementsByTagName('body')[0];
//	if(body)
//		body.innerHTML = '';
//}

tinyMCE.init({
	mode: "none",
	theme: "advanced",
	theme_advanced_buttons1 : "bold,italic,underline,separator,undo,redo",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_path : false,
	theme_advanced_resizing : true,
	theme_advanced_resize_horizontal : false,
	browsers : "msie,gecko,opera,safari",
	dialog_type : "modal",
	gecko_spellcheck : true,
	remove_linebreaks: false,
	plugins : "inlinepopups,tabfocus",
	valid_elements : "-strong,-b,-em,-i,-u,-p,br",
	onchange_callback : "tinyChangeCallback",
	handle_event_callback : "tinyEventCallback",
	tab_focus : ':prev,:next',
    width : '412',
    height: '322'
});


/**
 * Object for counting characters in tinymce
 */
var charCount =
{
	initiated: false,
	currentLength: 0,
	bufferLength: 0,
	//string for storing content to replace if someone crosses the length
	contentBuffer: '',
	//edit this to increase decrease maximum characters
	maxLength: 3000,
	//the id the span element that stores character count has
	spanId: 'character_count',
	//initiate object
	init: function()
	{
		this.initiated = true;
		this.editorId = tinyMCE.selectedInstance.editorId;

		//create status bar text
		if ($(this.editorId + '_path_row')) {
			new Element('div', {'class':'mceStatusbarPathText'})
                .setHTML('Characters: <span id="'+this.spanId+'">'+this.currentLength+'</span>/'+this.maxLength).injectBefore(this.editorId + '_path_row');
        }

		this.checkLength();
	},
	//check length
	checkLength: function()
	{        
		if(!this.initiated)
			return true;

		// we were getting errors accessing the frame by name when replaced in the page, changed to 0
		// this could cause other problems as we use iframes to upload image
        //
		var content = $(this.editorId + '_ifr').contentWindow.document.body.innerHTML;

		//var content = top.frames[0].document.body.innerHTML;
		//remove html
		content = content.replace(/(<[^>]*>|&nbsp;)/gi, '');

		//update status
		if(content.length != this.currentLength)
		{
			this.currentLength = content.length;
			this.updateLength(this.currentLength);
		}

		//overlength reset content and length
		if(content.length > this.maxLength)
		{
			tinyMCE.setContent(this.contentBuffer);
			this.updateLength(this.bufferLength);
		}
		//otherwise save content for an undo
		else
		{
			this.contentBuffer = $(this.editorId + '_ifr').contentWindow.document.body.innerHTML;
			//this.contentBuffer = top.frames[0].document.body.innerHTML;
			this.bufferLength = content.length;
		}

		return true;
	},
	//update the span with current character count
	updateLength: function(lengthInt)
	{
		if($(this.spanId))
			$(this.spanId).setText(lengthInt);

	}

};

function tinyEventCallback(e)
{
	var event = new Event(e);
	if(event.type == 'keyup' || event.type == 'keydown' || event.type == 'keypress')
		charCount.checkLength();
	return true;
}

function tinyChangeCallback()
{
	charCount.checkLength();
}

//moved to be called manually
function tinyInitCallback()
{
	setTimeout("charCount.init()", 1000);
}

function open_terms()
{
	window.open("popup_terms.php","terms","width=500,height=500,status=0,resizable=0,scrollbars=1");
}
function show_conversion()
{
	document.getElementById("conversion_inner").src = "secpay_inner.php?do_goal=1";
}

function loadTinyMce() {

	if (typeof tinyMCE != 'undefined' && $('mce_editor')) {
		tinyMCE.execCommand('mceAddControl', false, 'mce_editor');
		tinyInitCallback();
	}
}

function saveTinyMce() {
	if(typeof tinyMCE != 'undefined' && $('mce_editor'))
	{
		//tinyMCE.execCommand('mceRemoveControl', false, 'mce_editor');
		tinyMCE.triggerSave();
	}
}

function makeToggleLinks() {

	$each($$('a.let_property_toggle'), function(el) {

		//sliders init before click so all html is in place
		//makeSlider($E('.let_property_form_container', el.getParent().getParent()));
        el.removeEvents('click');
		el.addEvent('click', function(e) {

			var formContainer = $E('.let_property_form_container', this.getParent().getParent());

            var arrowToggle = $E('a.let_property_arrow_toggle', this.getParent().getParent());

            changeArrowStatus(
                $E('a.let_property_arrow_toggle', this.getParent().getParent()),
                'toggle'
            );
            
			//makeSlider(formContainer);

			$jQ(formContainer).slideToggle(lpSliderOptions.speed, lpSliderOptions.callback);

			new Event(e).stop();
		});

	});
}

function makeSlider(el) {
//	if (el) {
//
//		if (typeof el.slider == 'undefined') {
////			el.slider = new Fx.Slide(el, {duration: 500, onComplete: function(){
////                                if(this.wrapper.offsetHeight != 0)
////                                    this.wrapper.setStyle('height', 'auto');
////                        }});
//		}
//
//		if (el.hasClass('hide')) {
//			el.slider.hide();
//			el.removeClass('hide');
//		}
//	}
}

function bindHearAboutUs() {

    if($('hear_about_us'))
    {
        function hearAboutUsChange()
        {
            if(!$('hearAboutUsOtherCont') || !$('hear_about_us_other'))
                return;

            if($('hear_about_us').getValue().toLowerCase() == 'other')
            {
                if($('hearAboutUsOtherCont').hasClass('hide'))
                    $('hearAboutUsOtherCont').removeClass('hide');
            }
            else
            {
                if(!$('hearAboutUsOtherCont').hasClass('hide'))
                {
                    $('hearAboutUsOtherCont').addClass('hide');
                    $('hear_about_us_other').setProperty('value', '');
                }
            }
        }

        $('hear_about_us').addEvent('change', hearAboutUsChange);

        hearAboutUsChange();
    }
}

function changeArrowStatus(element, status)
{
    if ($(element)) {
        if (status == 'show'
        || (status == 'toggle'
         && $(element).hasClass('let_property_toggle_hidden'))
        ) {
            $(element)
                .removeClass('let_property_toggle_hidden')
                .setText('Hide');
        } else {
            $(element)
                .addClass('let_property_toggle_hidden')
                .setText('Show');
        }
    }
}

window.addEvent('domready', function() {
        makeToggleLinks();
        //tiny mce after links to fix iframe breaking in tinymce bug
        loadTinyMce();
        loadToolTips();

});

