var max_back_wnd = 10;

function check_url_valid(url) {
	var temp_url = $.trim(url);
	if (temp_url.indexOf(" ") != -1) {
		return -1;
	}
	return temp_url;
}

function get_playlist() {
	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/get_playlistex",
		dataType: "xml",
		cache: false,
		error: function () {
		},
		success: function (data) {
			for (var i = 0; i < max_back_wnd; i++) {
				var uri = $(data).find("uri" + i).text();
				$('#uri' + i).attr("value", uri);
			}
		}
	});
}

function videoswitch_setting() {
	var ptmp = "";
	for (var i = 0; i < max_back_wnd; i++) {
		var url = $('#uri' + i).val();
		url = check_url_valid(url);
		if (url == -1) {
			alert($.i18n.prop("playlist_format_error_start_info") + i + $.i18n.prop("playlist_format_error_end_info"));
			return;
		}
		ptmp += ("uri" + i + "=" + encodeURIComponent(url) + "&");
	}
	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/set_playlistex" + "?" + ptmp,
		cache: false,
		success: function () {
			alert($.i18n.prop('playlist_setting_success'));
		},
		error: function () {
			alert($.i18n.prop('playlist_setting_failed'));
		}
	});
}

function click_setplay_btn(id) {
	var url = $('#uri' + id).val();
	url = check_url_valid(url);
	if (url == -1) {
		alert($.i18n.prop("playlist_format_error_start_info") + id + $.i18n.prop("playlist_format_error_end_info"));
		return;
	}

	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/set_playlist" + "?wnd=1&uri0_audio=1&uri0=" + encodeURIComponent(url),
		cache: false,
		success: function () {
			alert($.i18n.prop('playlist_setting_success'));
		},
		error: function () {
			alert($.i18n.prop('playlist_setting_failed'));
		}
	});
}

function parentpage_adjust_height() {
	window.parent.setup_iframe_height();
}

function language_setup() {
	var current_language = window.parent.get_language_setup();
	jQuery.i18n.properties({
		name: 'common',
		path: '../lang/',
		mode: 'map',
		language: current_language,
		callback: function () {
			for (var i = 0; i < max_back_wnd; i++) {
				$('#videoswitch_url' + i).html($.i18n.prop('videoswitch_url') + (i + 1) + $.i18n.prop('videoswitch_colon'));
				$('#setplay_btn_' + i).val($.i18n.prop('videoswitch_setplay'));
			}
			$('#videoswitch_backup_header').html($.i18n.prop('videoswitch_backup_header'));
			$('#videoswitch_setting').val($.i18n.prop('playlist_setting'));
			$('#header_text').html($.i18n.prop('index_videoswitch'));
			parentpage_adjust_height();
		}
	});
}

$(function () {
	language_setup();
	get_playlist();
});
