//
// Ultimate PSPTube Functions
//

//Gets the list of sites and displays them in the order specified
function CallGate_GetSiteList()
{
	var site = new Array();

	//Site order length is longer than site length
	if(site_order.length > SiteList.length){
		for(var i = 0; i < SiteList.length; i++) {
			if(SiteList[i].Name.substring(0, 1) == "P"){SiteList[i].Name = String.fromCharCode(127) + SiteList[i].Name;}
			site[i] = SiteList[i];
		}
		return site;
	}

	//Site order array contains a site that doesn't exist
	for(var i = 0; i < site_order.length; i++) {
		var found = 0;
		for(var i2 = 0; i2 < SiteList.length; i2++) {
			if(SiteList[i2].Name == site_order[i]){found = 1;}
		}
		if(found == 0){
			for(var i3 = 0; i3 < SiteList.length; i3++) {
				if(SiteList[i3].Name.substring(0, 1) == "P"){SiteList[i3].Name = String.fromCharCode(127) + SiteList[i3].Name;}
				site[i3] = SiteList[i3];
			}
			return site;
		}
	}

	//Order sites
	for(var i = 0; i < SiteList.length; i++) {
		for(var i2 = 0; i2 < SiteList.length; i2++) {
			if(SiteList[i2].Name == site_order[i]){
				if(SiteList[i2].Name.substring(0, 1) == "P"){SiteList[i2].Name = String.fromCharCode(127) + SiteList[i2].Name;}
				site[site.length] = SiteList[i2];
			}
		}
	}

	return site;
}

//Gets the video url
function CallGate_VideoURLResolver( url, option )
{
	for(var i = 0; i < SiteList.length; i++) {
		if(SiteList[i].CheckURL( url, option ) == 1) {
			return SiteList[i].GetURL( url, option );
		}
	}
	return null;
}

//Performs the search
function CallGate_SearchSite( sitename, keyword, start_index, length, option )
{
	for(var i = 0; i < SiteList.length; i++) {
		if(SiteList[i].Name == sitename) {
			return SiteList[i].Search( keyword, start_index, length, option );
		}
	}
	return null;
}


//Replace all diacratics in a string with their similar letter
function replaceDiacritics(s)
{
	var s;

	var diacritics =[
	/[\300-\306]/g, /[\340-\346]/g, // A, a
	/[\310-\313]/g, /[\350-\353]/g, // E, e
	/[\314-\317]/g, /[\354-\357]/g, // I, i
	/[\322-\330]/g, /[\362-\370]/g, // O, o
	/[\331-\334]/g, /[\371-\374]/g,  // U, u
	/[\321]/g, /[\361]/g, // N, n
	/[\307]/g, /[\347]/g, // C, c
	];

	var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];

	for (var i = 0; i < diacritics.length; i++){
		s = s.replace(diacritics[i],chars[i]);
	}

	return s;
}

//Removes all diacratics in a string
function removeDiacritics(s)
{
	var s;

	var diacritics =[
	/[\300-\306]/g, /[\340-\346]/g, // A, a
	/[\310-\313]/g, /[\350-\353]/g, // E, e
	/[\314-\317]/g, /[\354-\357]/g, // I, i
	/[\322-\330]/g, /[\362-\370]/g, // O, o
	/[\331-\334]/g, /[\371-\374]/g,  // U, u
	/[\321]/g, /[\361]/g, // N, n
	/[\307]/g, /[\347]/g, // C, c
	];

	for (var i = 0; i < diacritics.length; i++){
		s = s.replace(diacritics[i],"");
	}

	return s;
}

//Removes all unidentified characters in a string
function removeUnidentifiedChars(text)
{
	var contents = text.match(/&#(.*?);/g);
	if(contents == null){return text;}
	for(var i = 0; i < contents.length; i++){
		text = text.replace(contents[i], "");
	}
	return text;
}

function splitTo2Lines(text, line_length)
{
	var pos = text.indexOf("\n");
	if((pos == -1 || pos > line_length) && text.length > line_length){
		pos = text.lastIndexOf(" ", line_length);
		if(pos != -1){ text = text.substring(0, pos) + "\n" + text.substring(pos + 1, text.length); }
	}
	return text;
}

function TimestringToSeconds(timestring)
{
	var pos = timestring.indexOf(":");
	var mins = timestring.substring(0, pos);
	var secs = timestring.substring(pos + 1, timestring.length);
	if(mins.substring(0, 1) == "0" && mins.length > 1){
		mins = mins.substring(1, mins.length);
	}
	if(secs.substring(0, 1) == "0" && secs.length > 1){
		secs = secs.substring(1, secs.length);
	}
	var mins = parseInt(mins);
	var secs = parseInt(secs);
	if(mins == null || mins == -1){ var mins = 0; }
	if(secs == null || secs == -1){ var secs = 0; }
	mins = (mins * 60) + secs;
	return mins;
}

function ToFilename(text)
{
	return text.replace(/\\/g, "").replace(/\//g, "").replace(/\:/g, "").replace(/\*/g, "").replace(/\?/g, "").replace(/\"/g, "").replace(/\</g, "").replace(/\>/g, "").replace(/\|/g, "") + ".flv";
}

function ImproveText(text)
{
	var contents = text.match(/\<a href\=\"(.*?)\"\>(.*?)\<\/a\>/g);
	while(contents != null){
		text = text.replace("<a href=\"" + RegExp.$1 + "\">" + RegExp.$2 + "</a>", RegExp.$2);
		contents = text.match(/\<a href\=\"(.*?)\"\>(.*?)\<\/a\>/g);
	}
	contents = text.match(/\<img src\=\"(.*?)\"\>/g);
	while(contents != null){
		text = text.replace("<img src=\"" + RegExp.$1 + "\">", "");
		contents = text.match(/\<img src\=\"(.*?)\"\>/g);
	}
	return text.replace(/\<b\>/g, "").replace(/\<\/b\>/g, "").replace(/\<u\>/g, "").replace(/\<\/u\>/g, "").replace(/\<i\>/g, "").replace(/\<\/i\>/g, "").replace(/\\&apos;/g, "'").replace(/&amp;/g, "&").replace(/&quot;/g, "\"").replace(/&#39;/g, "'").replace(/&#039;/g, "'").replace(/\\\'/g, "'").replace(/&ndash;/g, "-").replace(/&apos;/g, "'").replace(/&lt;p&gt;/g, "").replace(/&lt;\/p&gt;/g, "");
}
