/*
 * Function Equivalent to java.net.URLEncoder.encode(String, "UTF-8")
*/
function encodeURL(str){

    var s0, i, s, u;
    s0 = "";                // encoded str
    for (i = 0; i < str.length; i++){   // scan the source
        s = str.charAt(i);
        u = str.charCodeAt(i);          // get unicode of the char
        if (s == " "){s0 += "+";}       // SP should be converted to "+"
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                s0 = s0 + s;            // don't escape
            }
            else {                  // escape
                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     // quaternary byte format (extended)
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){        // triple byte format
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                      // double byte format
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}

function decodeURL(str){
    var s0, i, j, s, ss, u, n, f;
    s0 = "";                // decoded str
    for (i = 0; i < str.length; i++){   // scan the source str
        s = str.charAt(i);
        if (s == "+"){s0 += " ";}       // "+" should be changed to SP
        else {
            if (s != "%"){s0 += s;}     // add an unescaped char
            else{               // escape sequence decoding
                u = 0;          // unicode of the character
                f = 1;          // escape flag, zero means end of this sequence
                while (true) {
                    ss = "";        // local str to parse as int
                        for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
                            sss = str.charAt(++i);
                            if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
                                ss += sss;      // if hex, add the hex character
                            } else {--i; break;}    // not a hex char., exit the loop
                        }
                    n = parseInt(ss, 16);           // parse the hex str as byte
                    if (n <= 0x7f){u = n; f = 1;}   // single byte format
                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
                    if (f <= 1){break;}         // end of the utf byte sequence
                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
                    else {break;}                   // abnormal, format error
                }
            s0 += String.fromCharCode(u);           // add the escaped character
            }
        }
    }
    return s0;
}

function getBanner(id, w, h) {
	$.ajax({
		url: '/api/banner/'+id,
		type: 'GET',
		error: function(){
			//alert("오류가 발생하였 습니다. 관리자에게 문의하세요.");
		},
		success: function(xml){
			if ($(xml).find("bannerId").text() != '') {
				width = $(xml).find("imageWidth").text()!=''?$(xml).find("imageWidth").text():w;
				height = $(xml).find("imageWidth").text()!=''?$(xml).find("imageHeight").text():h;
				filename = $(xml).find("fileName").text();
				bid = $(xml).find("bannerId").text();
				target = $(xml).find("bannerTargetType").text()=='1'?'_top':'_new';
				html ='<div style="text-align:center;position:absolute; width:'+width+'px; height:'+height+'px;"><a href=\'/banner/banner.do?bannerId='+bid+'\'" target="'+target+'"><img src="/images/front/image/img_blank.gif" width="'+width+'" height="'+height+'" /></a></div>';
				if ($(xml).find("fileType").text()=="2") {
					html += "<img src='/media/"+filename+".image' width='"+width+"px' height='"+height+"px' />";
					$("#banner"+id).html(html);
				} else {
				    html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="banner'+id+'" align="middle">';
				    html += '<param name="allowScriptAccess" value="always" />';
					html += '<param name="movie" value="/media/'+filename+'.image" />';
					html += '<param name="quality" value="high" />';
					html += '<param name="bgcolor" value="#ffffff" />';
					html += '<param name="wmode" value="transparent" />';
					html += '<param name="menu" value="0" />';
					html += '<embed src="/media/'+filename+'.image" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="banner'+id+'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
					html += '</object>';

					$("#banner"+id).html(html);
				}
				$("#googleBnner"+id).css("display","none");
			} else {
				$("#googleBnner"+id).css("display","block");
			}
		}
	});
}

function getPortlet(id) {
	$.ajax({
		url: '/api/portlet/'+id,
		type: 'GET',
		error: function(){
			//alert("오류가 발생하였 습니다. 관리자에게 문의하세요.");
		},
		success: function(xml){
			var html = "";

			html += "<div id=\"portletTitle" + id + "\" class=\"rightOnBox\">" + $(xml).find('portletTitle').text() +"</div>";
			html += "<div id=\"portlet" + id + "\" class=\"rightType displayOn\">";
		    html += "<ul>";

			$(xml).find('portletContentsList').each(function(){
				targetLinkUrl = $(this).find("targetLinkUrl").text();
				title = $(this).find("title").text();
				thumbnailUrl = $(this).find("thumbnailUrl").text();

				html += "<li><div onClick=\"javascript:document.location.href='" + targetLinkUrl + "'\">" + title + "</div></li>";
			});

			html += "</ul>";
			html += "</div>";

			$(".portlet"+id).html(html);
		}
	});
}

function getBannerJs(id, w, h) {
	$.ajax({
		url: 'http://tv.devmento.co.kr/banner/3.json',
		dataType: 'jsonp',
		jsonp: 'callback',
		error: function(){
			//alert("오류가 발생하였 습니다. 관리자에게 문의하세요.");
		},
		success: function(data){
			if (data.banner.bannerId != '') {
				width = data.banner.imageWidth!=''?data.banner.imageWidth:w;
				height = data.banner.imageWidth!=''?data.banner.imageHeight:h;
				filename = data.banner.fileName;
				bid = data.banner.bannerId;
				target = data.banner.bannerTargetType=='1'?'_new':'_new';

				html ='<div style="text-align:center;position:absolute; width:'+width+'px; height:'+height+'px;"><a href=\'/banner/banner.do?bannerId='+bid+'\'" target="'+target+'"><img src="/images/front/image/img_blank.gif" width="'+width+'" height="'+height+'" /></a></div>';
				if ($(xml).find("fileType").text()=="2") {
					html += "<img src='/media/"+filename+".image' width='"+width+"px' height='"+height+"px' />";
					$("#banner"+id).html(html);
				} else {
				    html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="banner'+id+'" align="middle">';
				    html += '<param name="allowScriptAccess" value="always" />';
					html += '<param name="movie" value="/media/'+filename+'.image" />';
					html += '<param name="quality" value="high" />';
					html += '<param name="bgcolor" value="#ffffff" />';
					html += '<param name="wmode" value="transparent" />';
					html += '<param name="menu" value="0" />';
					html += '<embed src="/media/'+filename+'.image" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="banner'+id+'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
					html += '</object>';

					$("#banner"+id).html(html);
				}
				$("#googleBnner"+id).css("display","none");
			} else {
				$("#googleBnner"+id).css("display","block");
			}
		}
	});
}

function getPortletJs(id) {

	$.ajax({
		url: 'http://tv.devmento.co.kr/portlet/3.json',
		dataType: 'jsonp',
		jsonp: 'callback',
		success: function(data) {
			var html = "";

			html += "<div id=\"portletTitle" + id + "\" class=\"rightOnBox\">" + data.portletList.portletManage.portletTitle +"</div>";
			html += "<div id=\"portlet" + id + "\" class=\"rightType2 displayOn\">";
		    html += "<ul>";

		    $.each(data.portletList.portletContentsList, function(index, entry) {
		    	targetLinkUrl = entry.targetLinkUrl;
		    	title = entry.title;
		    	thumbnailUrl = entry.thumbnailUrl;

		    	html += "<li><div onClick=\"javascript:document.location.href='" + targetLinkUrl + "'\">" + title + "</div></li>";
		    });

			html += "</ul>";
			html += "</div>";

			$("#portlet"+id).html(html);
		},
		error: function(err) {
		}

	});

}
