function flashEmbed(obj,div){
	// generate html code
	// for ie obejct
	var html = '<object ';
	if (!obj.id && !obj.name){
		var r = Math.round(Math.random()*100);
		html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
	} else {
		if (obj.id) html += 'id="'+obj.id+'" ';
		else html += 'id="'+obj.name+'" ';
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.classid) html += 'classid="'+obj.classid+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	if (obj.codebase) html += 'codebase="'+obj.codebase+'" ';
	html += '>';
	// append params
	for (var i in obj.param){
		html += '<param name="'+obj.param[i][0]+'" value="'+obj.param[i][1]+'"/>';
	}

	// for ns embed
	html += '<embed ';
	if (!obj.id && !obj.name){
		var r = Math.round(Math.random()*100);
		html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
	} else {
		if (obj.name) html += 'name="'+obj.name+'" ';
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	// append params
	//for (var i in obj.param){
	for( var i=0; i<obj.param.length; i++){

		if (obj.param[i]){
			if (obj.param[i][0]=='movie' || obj.param[i][0]=='src'){
				var _src = obj.param[i][1];
			}
                        html += obj.param[i][0]+'="'+obj.param[i][1]+'" ';
		}
	}
	html += '/>';
	html += '</object>';

	var isIE = (document.all)?true:false;
	if (isIE){
		document.getElementById(div).innerHTML = html;
	} else if (obj.type=='application/x-shockwave-flash' || obj.classid=='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'){
		// ie?? ?????? activex? flash? ??? ??
		document.getElementById(div).innerHTML = html;
	}
}

/* Misc */
function findPosX(obj)
{
        var curleft = 0;
        if (obj.offsetParent)
        {
                while (obj.offsetParent)
                {
                        curleft += obj.offsetLeft
                        obj = obj.offsetParent;
                }
        }
        else if (obj.x)
                curleft += obj.x;
        return curleft;
}

function findPosY(obj)
{
        var curtop = 0;
        if (obj.offsetParent)
        {
                while (obj.offsetParent)
                {
                        curtop += obj.offsetTop
                        obj = obj.offsetParent;
                }
        }
        else if (obj.y)
                curtop += obj.y;
        return curtop;
}

/* Flaon Detail Tooltips */
function show_details(obj, id, display) {
    var box = document.getElementById("details"+id);
    var ifrm = document.getElementById("ieframe");
    ifrm.style.display = box.style.display = (display)?"block":"none";
    if (display) {
        box.style.left = findPosX(obj);
        box.style.top = findPosY(obj)+15;
        ifrm.style.left = box.style.left;
        ifrm.style.top = box.style.top;
        ifrm.style.width = box.style.width;
        ifrm.style.height = box.style.height;
        ifrm.style.zIndex = box.style.zIndex - 1;
    }
}

/* Top Menubar */
function login_form(display) {
    var frm = document.getElementById("loginform");
    var ifrm = document.getElementById("ieframe");
    if (!frm || !ifrm) return;
    if (display) {
        show_menu("explore", false);
        ifrm.style.display = frm.style.display = "block";
        var tab = document.getElementById("tablogin");
        frm.style.left = findPosX(tab) + tab.offsetWidth - frm.offsetWidth;
        frm.style.top = findPosY(tab)+30;
        ifrm.style.left = frm.style.left;
        ifrm.style.top = frm.style.top;
        ifrm.style.width = frm.style.width;
        ifrm.style.height = frm.style.height;
        ifrm.style.zIndex = frm.style.zIndex - 1;
    } else {
        ifrm.style.display = frm.style.display = "none";
    }
}

function show_menu(name, display) {
    var btn = document.getElementById("tab"+name);
    var menu = document.getElementById("menu"+name);
    var ifrm = document.getElementById("ieframe");
    if (!btn || !menu || !ifrm) return;
    ifrm.style.display = menu.style.display = (display)?"block":"none";
    if (display) {
        menu.style.left = findPosX(btn) + btn.offsetWidth - menu.offsetWidth;
        menu.style.top = findPosY(btn)+30;
        ifrm.style.left = menu.style.left;
        ifrm.style.top = menu.style.top;
        ifrm.style.width = menu.style.width;
        ifrm.style.height = menu.style.height;
        ifrm.style.zIndex = menu.style.zIndex - 1;
    }
}

function explore_menu() {
    var menu = document.getElementById("menuexplore");
    if (!menu) return;
    var display = (menu.style.display != "block")?true:false;
    show_menu("explore", display);
    show_menu("myaccount", false);
    login_form(false);
}

function myaccount_menu() {
    var menu = document.getElementById("menumyaccount");
    if (!menu) return;
    var display = (menu.style.display != "block")?true:false;
    show_menu("myaccount", display);
    show_menu("explore", false);
}

function search_on_submit(frm)
{
    if (frm.q.disabled == false && frm.q.value == frm.q.defaultValue) {
        alert("Please enter keywords you want to search.");
        frm.q.focus();
        return false;
    }
    if (frm.u.disabled == false && frm.u.value == frm.u.defaultValue) {
        alert("Please enter username you want to search.");
        frm.u.focus();
        return false;
    }
    return true;
}

function search_on_focus(obj, mesg)
{
    if (obj.value == mesg) {
        obj.value = "";
    }
    obj.style.color = "#000000";
}

function search_on_blur(obj, mesg)
{
    if (obj.value != "") {
        return;
    }
    obj.value = mesg;
    obj.style.color = "#c9c9c9";
}

function search_option_change(obj)
{
    var video_box = document.getElementById('search_video');
    var user_box = document.getElementById('search_user');
    var video_input = document.search_form.q;
    var user_input = document.search_form.u;
    var search_by = new Array();
    var input = null;
    var unused_input = null;
    var box = null;
    var unused_box = null;
    var selected_option = obj.options[obj.selectedIndex];

    document.search_form.by_tag.value = "N";
    document.search_form.by_title.value = "N";
    if (selected_option.value == 'search_video_tag') {
        input = video_input;
        unused_input = user_input;
        box = video_box;
        unused_box = user_box;
        search_by.push(document.search_form.by_tag);
    } else if (selected_option.value == 'search_video_title') {
        input = video_input;
        unused_input = user_input;
        box = video_box;
        unused_box = user_box;
        search_by.push(document.search_form.by_title);
    } else if (selected_option.value == 'search_video') {
        input = video_input;
        unused_input = user_input;
        box = video_box;
        unused_box = user_box;
        search_by.push(document.search_form.by_tag);
        search_by.push(document.search_form.by_title);
    } else if (selected_option.value == 'search_user') {
        input = user_input;
        unused_input = video_input;
        box = user_box;
        unused_box = video_box;
    }

    for (var j=0; j<search_by.length; j++) {
        search_by[j].value = "Y";
    }

    box.style.display = 'inline';
    input.disabled = false;
    input.focus();
    if (unused_input.value != '' && unused_input.value != unused_input.defaultValue) {
        input.value = unused_input.value;
    }
    
    unused_input.value = '';
    unused_input.disabled = true;
    unused_box.style.display = 'none';
}

function is_ie()
{
    return (navigator.appName == "Microsoft Internet Explorer") ? true : false ;
}

function trim_text(txt, size) {
    if (size == null) size = 20
    if (txt.length > size) {
        txt = txt.substr(0, size) + "...";
    }
    document.write(txt);
}

function trim(txt) {
    var a = txt.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
}

function preload_images(links)
{
    for (var i=0; i<links.length; i++) {
       (new Image()).src = "/flaon/us/images/"+links[i];
    }
}

function preload_rating_icons()
{
    var icons = ["star_on.gif", "star_off.gif", "star_half.gif", "star_sm_on.gif", "star_sm_off.gif", "star_sm_half.gif"];
    preload_images(icons);
}

function get_json(url, callback)
{
    dataLoaded = callback;
    var script = document.createElement('script');
    script.type = 'text/javascript';
    if (!url.match('lang=')) {
        var lang = 'us';
        if (url.match(/\?/)) {
            url += '&lang=' + lang;
        } else {
            url += '?lang=' + lang;
        }
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

function render_fp_data(json_data)
{
    var list = ["most_viewed", "most_discussed", "most_recent", "editors_pick"];
    for (var i=0; list[i]; i++) {
        var output = document.getElementById(list[i]+'_output');
        var container = document.getElementById(list[i]+'_container');
        if (!container) { continue; }
        var item = document.getElementById(list[i]+'_item');
        if (!item) { continue; }
        var items = json_data[list[i]];
        if (!items) { 
            container.parentNode.removeChild(container);
            continue;
        }
        for (var j=0; items[j]; j++) {
            var new_item = item.cloneNode(true);
            append_node(new_item, list[i]+'_title', create_node('a', items[j].title, items[j].linkurl));
            append_node(new_item, list[i]+'_username', create_node('a', items[j].username, items[j].user_url));
            append_node(new_item, list[i]+'_duration', create_node('text', items[j].duration));
            append_node(new_item, list[i]+'_rating_icons', create_node('html', items[j].rating_icons));
            append_node(new_item, list[i]+'_previewer', create_previewer_node(json_data.server_name, items[j].idcode, items[j].xmlpath, items[j].linkurl, items[j].thumbnail));
            output.appendChild(new_item);
        }
        output.style.display = "block";
        container.removeChild(item);
        container.style.display = "block";
    }
}

function render_video_preview(user_id, json_data)
{
    var output = document.getElementById("preview_output_"+user_id);
    var container = document.getElementById("preview_container_"+user_id);
    var tmpl = document.getElementById("preview_tmpl_"+user_id);
    var videos = json_data['videos'];
    if (!output || !container || !tmpl || !videos) { return; }
    for (var i=0; i<videos.length; i++) {
        var new_item = tmpl.cloneNode(true);
        append_node(new_item, "preview_title_"+user_id, create_node('a', videos[i].title, videos[i].linkurl));
        append_node(new_item, "preview_duration_"+user_id, create_node('text', videos[i].duration));
        append_node(new_item, "preview_rating_"+user_id, create_node('html', videos[i].rating_icons));
        append_node(new_item, "preview_player_"+user_id, create_previewer_node(json_data.server_name, videos[i].idcode, videos[i].xmlpath, videos[i].linkurl, videos[i].thumbnail));
        new_item.style.display = "inline";
        output.appendChild(new_item);
    }
    if (videos.length == 0) {
        var empty_mesg = document.getElementById("preview_empty_"+user_id);
        empty_mesg.style.display = "block";
    }
    output.style.display = "block";
    container.removeChild(tmpl);
    container.style.display = "block";
}

function create_node(tagname, value, extra)
{
    var node;
    if (tagname == 'text') {
        node = document.createTextNode(value);
    } else if (tagname == 'a') {
        node = document.createElement('a');
        node.setAttribute("href", extra);
        node.setAttribute("target", "_parent");
        node.appendChild(document.createTextNode(value));
    } else if (tagname == 'html') {
        node = load_html(value);
    }
    return node;
}

function load_html(text)
{
    var div = document.createElement('div');
    div.innerHTML = text;
    return div;
}

function load_html_old(text)
{
    if (window.ActiveXObject) {     // code for IE
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(text);
    } else {    // code for Mozilla, Firefox, Opera, etc.
        var parser = new DOMParser();
        var doc = parser.parseFromString(text, "text/xml");
    }
    // documentElement always represents the root node
    return doc.documentElement;
}

// traverse all children under the node, find "id", append with "new_node"
function append_node(node, id, new_node)
{
    var found = 0;
    for (var i=0; i<node.childNodes.length; i++) {
        if (node.childNodes[i].getAttribute &&
            node.childNodes[i].getAttribute("id") == id) {
            node.childNodes[i].innerHTML = '';
            node.childNodes[i].appendChild(new_node);
            node.childNodes[i].removeAttribute("id");
            found = 1;
        } else if (node.childNodes[i].childNodes.length > 0) {
            found = append_node(node.childNodes[i], id, new_node);
        }
        if (found) break;
    }
    return found;
}

function create_previewer_node(server_name, idcode, xmlpath, linkurl, thumbnail)
{
    if (thumbnail) {
        var html = '';
        html += '<a href="' + linkurl + '" target="_parent" style="display:block;">';
        html += '<img src="' + thumbnail + '" border="0" width="133" height="100"/>';
        html += '</a>';
        return load_html(html);
    } else {
        var obj = new Object();
        obj.type = 'application/x-shockwave-flash';
        obj.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
        obj.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0';
        obj.width = '133px';
        obj.height = '100px';
        obj.id = 'xtvPreview_'+idcode;
        obj.swLiveConnect = 'true';
        obj.param = [ ['allowScriptAccess','always'],
                      ['movie',server_name+'/flaon/us/swfFiles/flaon_previewer.swf'],
                      ['src',server_name+'/flaon/us/swfFiles/flaon_previewer.swf'],
                      ['pluginspage','http://www.macromedia.com/go/getflashplayer'],
                      ['quality','high'],
                      ['name','xtvPreview_'+idcode],
                      ['id','xtvPreview_'+idcode],
                      ['loop','false'],
                      ['menu','false'],
                      ['swLiveConnect','true'],
                      ['wmode', 'transparent'],
                      ['flashVars', 'xmlPath='+escape(xmlpath)+'&dataPath=&guiPath='+server_name+'/flaon/us/swfFiles/&linkURL='+escape(linkurl)] ];
        return create_flash_node(obj);
    }
}

function create_flash_node(obj)
{
    var html = '<object ';
    if (!obj.id && !obj.name){
        var r = Math.round(Math.random()*100);
        html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
    } else {
        if (obj.id) html += 'id="'+obj.id+'" ';
        else html += 'id="'+obj.name+'" ';
    }
    if (obj.type) html += 'type="'+obj.type+'" ';
    if (obj.classid) html += 'classid="'+obj.classid+'" ';
    if (obj.width) html += 'width="'+obj.width+'" ';
    if (obj.height) html += 'height="'+obj.height+'" ';
    if (obj.codebase) html += 'codebase="'+obj.codebase+'" ';
    html += ">";

    // append params
    for (var i in obj.param){
        html += '<param name="'+obj.param[i][0]+'" value="'+obj.param[i][1]+'"/>';
    }

    // for ns embed
    html += '<embed ';
    if (!obj.id && !obj.name){
        var r = Math.round(Math.random()*100);
        html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
    } else {
        if (obj.name) html += 'name="'+obj.name+'" ';
    }
    if (obj.type) html += 'type="'+obj.type+'" ';
    if (obj.width) html += 'width="'+obj.width+'" ';
    if (obj.height) html += 'height="'+obj.height+'" ';
    for( var i=0; i<obj.param.length; i++) {
        if (obj.param[i]){
            if (obj.param[i][0]=='movie' || obj.param[i][0]=='src'){
                var _src = obj.param[i][1];
            }
            html += obj.param[i][0]+'="'+obj.param[i][1]+'" ';
        }
    }
    html += "/>";
    html += '</object>';
    return load_html(html);
}

function save_favorite(id, obj)
{
    if (!confirm("Are you sure you want to save this video to your favorite collection?")) return;
    var params = "idcode="+escape(id);
    ajax.open("POST", favorite_backend, true);
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.onreadystatechange = finish_save_favorite;
    ajax.send(params);
}

function finish_save_favorite()
{
    if (ajax.readyState != 4) return;
    var result = eval('(' + ajax.responseText + ')');
    if (result.status == 200) {
        alert("Saved this video to your favorite collection.");
        hide_object("save-favorite-link");
    } else {
        alert("Error saving to your favorite collection. Please try again later.");
    }
}
