var ajax_url = "/handler_ajax.php";
var ytpcnt = 1;
var isie = is_browser_ie();

/************************************
new functions
************************************/

function ModifyTitle(title,t) {
   title = title.replace('\/',' ');
   if (t != "un") { title = title.replace(' ','_'); }
   title = title.replace('&','and');
   title = title.replace('\?','');
   //title = urlencode(title);
   return title;
}

function convert_bbcode(otext) {
	if (otext != "") {
		otext = otext.replace(/\n/g,"<br />");
		
		otext = otext.replace(/\[a url='([^']*)'\]/g,"<a target='_new' href='$1'>");
		otext = otext.replace(/\[\/a\]/g,"</a>");
		
		otext = otext.replace(/\[img url='([^']*)'\]/g,"<img src='$1' border='0'>");
	
		otext = otext.replace(/\[youtube='([^']*)'\]/g,"<object width='"+(Math.ceil(425*ytpcnt))+"' height='"+(Math.ceil(355*ytpcnt))+"'><param name='movie' value='http://www.youtube.com/v/$1'></param><param name='wmode' value='transparent'></param><embed src='http://www.youtube.com/v/$1' type='application/x-shockwave-flash' wmode='transparent' width='"+(Math.ceil(425*ytpcnt))+"' height='"+(Math.ceil(355*ytpcnt))+"'></embed></object>");
		
		otext = otext.replace(/\[b\]/g,"<strong>");
		otext = otext.replace(/\[\/b\]/g,"</strong>");
		
		otext = otext.replace(/\[i\]/g,"<em>");
		otext = otext.replace(/\[\/i\]/g,"</em>");
		
		otext = otext.replace(/\[u\]/g,"<span class='underline'>");
		otext = otext.replace(/\[\/u\]/g,"</span>");
		
		return otext;
	} else {
		return "no data for convert_bbcode!";
	}
}

function get_config(t) {
	var ajrq = new Request({
		url:ajax_url,
		onSuccess: function(responseText, responseXML) {
			//ssr(gsr() + "return from get_config():\n"+responseText+"\n");
			switch (t) {
				case "pmconfig":
					pmf(JSON.decode(responseText));
					break;
				case "imconfig":
					imf(JSON.decode(responseText));
					break;
			}
		},
		onFailure: function(){ return false; }
	}).send({data:{ util:"configvars", stype:t }});
}

function autoSelect(el) {
	// one-click select a block of text
    if(el.tagName === "TEXTAREA" || (el.tagName === "INPUT" && el.type === "text")) {
        el.select();
        return;
    }
    
    if (window.getSelection) { // FF, Safari, Opera
        var sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.selection) { // IE
        document.selection.empty();
        var range = document.body.createTextRange();
        range.moveToElementText(el);
        range.select();
    }
};

function find_position( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

/************************************
old functions
************************************/

function is_browser_ie() {
	if (window.navigator.appName.indexOf("Explorer") > -1) {
		return true;
	} else {
		return false;
	}
}

function set_country(formname,state_select,country_select) {
	/* empty on purpose... check why later */
}

function set_state(formname,state_select,country_select) {
	populatestates(formname);
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			x[i].onkeyup = x[i].onchange = checkMaxLength;
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		var tempstring = this.value.substr( 0, maxLength );
		this.value = tempstring;
	}
}

function new_captcha() {
	var randomnumber=Math.floor(Math.random()*1001)
	var capdiv = document.getElementById("imgCaptcha");
	capdiv.src = "/captcha_image.php?a=" + randomnumber;
}

function popMiniChat(id) {
	var mypage = "/minichat/index.php?imid=" + id;
	var myname = "mt_minichat";
	var w = 380; // minichat width
	var h = 267; // minichat height
	var resize = 0;
	var scroll = 0;
	var winl = (screen.width - w) / 2;
	if (h < screen.height && h != 0) {
		var wint = (screen.height - h) / 2;
	} else {
		var wint = 0;
		h = (screen.height - 50);
		winl = winl + 16;
	}
	var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',toolbar=0,scrollbars='+scroll+',location=0,statusbar=0,menubar=0,resizable='+resize;
	var win = window.open(mypage, myname, winprops);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function setPopupTitle(title) {
	var parentlist = parent.document.getElementsByTagName("body")[0];
	var len = parentlist.childNodes.length;
	var list = "";
	for (i=0; i<parentlist.childNodes.length;i++) {
		if (parentlist.childNodes[i].id=="popupContainer") {
			var popupTitle = parentlist.childNodes[i].childNodes[0].childNodes[0].childNodes[0];
		}
	}
	popupTitle.innerHTML = title;
}

function load_portal(passfunc, var1, var2, var3, var4) {
	alert("somebody's still calling load_portal!\n\npassfunc: "+passfunc);
}

function check_all(oform,button) {
	if (!$("checkflag")) {
		var elem = document.createElement("input");
		elem.type = "hidden";
		elem.name = "checkflag";
		elem.value = "uncheck";
		elem.id = "checkflag";
		oform.appendChild(elem);
	}
	if (oform.checkflag.value == 'check') {
		var checkflag = "";
		oform.checkflag.value = 'uncheck';
		button.value = 'Check All';
	} else {
		var checkflag = "checked";
		oform.checkflag.value = 'check'
		button.value = 'Uncheck All';
	}
	for (i=0; i<oform.elements.length; i++) {
		if (oform.elements[i].type == "checkbox") {
			oform.elements[i].checked = checkflag;
		}
	}
}

function set_radio(oForm,oRadio,oVal) {
	for (var i=0; i < oForm.elements[oRadio].length; i++) {
		if (oForm.elements[oRadio][i].value == oVal) {
			oForm.elements[oRadio][i].checked = true;
		}
	}
}

function do_png_check() {
	var b =navigator.userAgent.toLowerCase();
	if (b.indexOf('msie')>0
		&& parseFloat(b.substring(b.indexOf('msie')+5)) < 7)
		{ // detect for IE (could be more specific to PC and version, but this works for the test)
		is = document.getElementsByTagName('IMG'); // get all images
		for(x=0; x<is.length; x++){ // cycle through those images
			if(is[x].src.indexOf('.png')){ // only do this to png files
				is[x].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+is[x].src+"', sizingMethod='scale')"; // apply the filter stuff that makes IE do it's magic
				is[x].src = "/images/null.gif"; // replace the image with a clear gif so that the filter can show through
			}
		}
	}
}

/************************************
general init
************************************/
var isie = is_browser_ie();

window.addEvent('domready', function() {

	do_png_check();
	$$(".autosel").addEvent('click', function () { autoSelect(this); });

	// assigns squeezebox behavior to <a href="#" class="boxed" rel="config">
	SqueezeBox.assign($$('a.boxed'), { parse: 'rel' });

});
