function OpenWindow(url,width,height, name, scrlls) {
	x = (640 - width)/2, y = (480 - height)/2;
	
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	var window_name = (name) ? name : 'mb_' + get_random();
	var scrlls = scrlls || "yes";

	window.open (url, window_name, 'width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+ ',location=no,status=yes,resizable=yes,scrollbars=' + scrlls + ',titlebar=yes');
}

function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length-charSearchFor.length; i++)
            {
                  if (charSearchFor == strSearch.substring(i,charSearchFor.length))
                  {
                        return i;
                  }
            }
            return -1;
}

function ShowBanner(_ele_ids, _uri, _location) {
	var objs = new Array();
	for (var i in _ele_ids) {
		var obj = document.getElementById(_ele_ids[i]);
		if (obj) {
			objs.push(obj);
		}
	}

	$.ajax({
		type: "POST",
		url: '/ajax/banner.php',
		data: 'uri=' + _uri + '&location=' + _location,
		success: function(xml) {
			
			if (xml) {
				var i = 0;
				$(xml).find('banner').each(function(){
					objs[i].innerHTML = $(this).find('content').text();
					i++;
				});
			}
		}
	});		 
}

function open_video_player(_video_uri, _preview_uri, _width, _height) {
	if (_video_uri) {
		var w = (_width) ? _width : 320;
		var h = (_height) ? _height : 260;
		var window_name = 'video_player';
		var scrlls = 'no';
		var url = '/video_player.php?uri=' + _video_uri + '&width=' + w + '&height=' + h;
		if (_preview_uri) url += '&preview=' + _preview_uri;
	
		window.open(url, window_name, 'width=' + w + ',height=' + h + ',location=no,status=yes,resizable=yes,scrollbars=' + scrlls + ',titlebar=yes');
	}
}

function poll_load(_id) {
	$('#poll').empty();
	$('#poll').css('background', 'url(/f/loading.gif) no-repeat center');
	$('#poll').css('padding', '40px 0 90px 0');
	$('#poll').load('/ajax/poll.php', {id: _id}, function() {
		$('#poll').css('background', 'none');
		$('#poll').css('padding', '0');
	});
	return true;
}

function poll_form_init(_id) {
	if ($('#poll_answer_custom') && $('input[name = "poll_answer"]')) { // making answer for type 2 be single
		$('#poll_answer_custom').focus(function() {
			$('input[name = "poll_answer"]').each(function() {
				this.checked = false;
			});
		});
		$('input[name = "poll_answer"]').click(function() {
			$('#poll_answer_custom').each(function() {
				this.value = '';
			});
		});
	}
	
	$('#poll_form').submit(function() {
		
		var poll_answers = new Array();
		$('input[name = "poll_answer"], input[name = "poll_answer[]"]').each(function() {
			if (this.checked) {
				poll_answers.push(this.value);
			}
		});
		
		var poll_answer_custom = $('#poll_answer_custom').val();
				
		if (poll_answers.length > 0 || poll_answer_custom.length > 0) {
			$('#poll').empty();
			$('#poll').css('background', 'url(/f/loading.gif) no-repeat center');
			$('#poll').css('padding', '40px 0 90px 0');
			$('#poll').load('/ajax/poll.php', {
					id: _id,
					poll_answers: poll_answers.join(','),
					poll_answer_custom: poll_answer_custom
				}, function() {
				$('#poll').css('background', 'none');
				$('#poll').css('padding', '0');
			});
		}
		
		return false;
	});
}


function wysiwygInit(_mode, _editor_selector, _lang) {	
	tinyMCE.init({
		mode: _mode,
		theme: 'simple',
		editor_selector: _editor_selector,
		cleanup_on_startup: true,
		remove_trailing_nbsp: true,
		remove_linebreaks: false,
		fix_list_elements: true,
		fix_table_elements: true,
		verify_html: true,
		language: _lang,
		relative_urls: false,
		document_base_url: '/',
		strict_loading_mode: false,
		apply_source_formatting: true,
		convert_fonts_to_spans: true
	});
}

function applyWysiwyg(_lang) {
	var elems = document.getElementsByTagName('TEXTAREA');
	for (var i = 0; i < elems.length; i++) {
		if (elems[i].className.indexOf('wysiwyg') > -1) {
			var id = elems[i].getAttribute('id');
			wysiwygInit('textareas', 'wysiwyg_' + id, _lang)
		}
	}
}

