﻿/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {
	
	// public method for url encoding
	encode : function (string) {
	    return escape(this._utf8_encode(string));
	},
	
	// public method for url decoding
	decode : function (string) {
	    return this._utf8_decode(unescape(string));
	},
	
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
	    string = string.replace(/\r\n/g,"\n");
	    var utftext = "";
	
	    for (var n = 0; n < string.length; n++) {
	
	        var c = string.charCodeAt(n);
	
	        if (c < 128) {
	            utftext += String.fromCharCode(c);
	        }
	        else if((c > 127) && (c < 2048)) {
	            utftext += String.fromCharCode((c >> 6) | 192);
	            utftext += String.fromCharCode((c & 63) | 128);
	        }
	        else {
	            utftext += String.fromCharCode((c >> 12) | 224);
	            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	            utftext += String.fromCharCode((c & 63) | 128);
	        }
	
	    }
	
	    return utftext;
	},
	
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
	    var string = "";
	    var i = 0;
	    var c = c1 = c2 = 0;
	
	    while ( i < utftext.length ) {
	
	        c = utftext.charCodeAt(i);
	
	        if (c < 128) {
	            string += String.fromCharCode(c);
	            i++;
	        }
	        else if((c > 191) && (c < 224)) {
	            c2 = utftext.charCodeAt(i+1);
	            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
	            i += 2;
	        }
	        else {
	            c2 = utftext.charCodeAt(i+1);
	            c3 = utftext.charCodeAt(i+2);
	            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
	            i += 3;
	        }
	
	    }
	
	    return string;
	}
}

function SearchGameAllMore(keyword) {
	location.href = 'search_all.daily?query=' + Url.encode(keyword);
}

function SearchNewsMore(keyword) {
	location.href = 'search_news.daily?query=' + Url.encode(keyword);
}

function SearchVideoMore(keyword) {
	location.href = 'search_video.daily?query=' + Url.encode(keyword);
}

function SearchGameInfoMore(keyword) {
	location.href = 'search_gamedb.daily?query=' + Url.encode(keyword);
}

function SearchGameTalkMore(keyword) {
	location.href = 'search_gametalk.daily?query=' + Url.encode(keyword);
}

function SearchESportsAllMore(keyword) {
	location.href = 'search_all.daily?query=' + Url.encode(keyword);
}

function SearchHotMore(keyword) {
	location.href = 'search_hot.daily?query=' + Url.encode(keyword);
}

function SearchProLeagueMore(keyword) {
	location.href = 'search_proleague.daily?query=' + Url.encode(keyword);
}

function SearchStarLeagueMore(keyword) {
	location.href = 'search_starleague.daily?query=' + Url.encode(keyword);
}

function SearchMSLMore(keyword) {
	location.href = 'search_msl.daily?query=' + Url.encode(keyword);
}

function SearchEtcLeagueMore(keyword) {
	location.href = 'search_etcleague.daily?query=' + Url.encode(keyword);
}

function SearchImageMore(keyword) {
	location.href = 'search_image.daily?query=' + Url.encode(keyword);
}

function SearchMovieMore(keyword) {
	location.href = 'search_movie.daily?query=' + Url.encode(keyword);
}

function SearchEPlayerMore(keyword) {
	location.href = 'search_playerdb.daily?query=' + Url.encode(keyword);
}

function SearchBoardMore(keyword) {
	location.href = 'search_board.daily?query=' + Url.encode(keyword);
}

function MoveCategory(category, keyword) {
	if (category == 'gameall') {
		SearchGameAllMore(keyword);
	}
	else if (category == 'news') {
		SearchNewsMore(keyword);
	}
	else if (category == 'video') {
		SearchVideoMore(keyword);
	}
	else if (category == 'gamedb') {
		SearchGameInfoMore(keyword);
	}
	else if (category == 'gametalk') {
		SearchGameTalkMore(keyword);
	}
	else if (category == 'esportsall') {
		SearchESportsAllMore(keyword);
	}
	else if (category == 'hot') {
		SearchHotMore(keyword);
	}
	else if (category == 'prol') {
		SearchProLeagueMore(keyword);
	}
	else if (category == 'osl') {
		SearchStarLeagueMore(keyword);
	}
	else if (category == 'msl') {
		SearchMSLMore(keyword);
	}
	else if (category == 'etcl') {
		SearchEtcLeagueMore(keyword);
	}
	else if (category == 'image') {
		SearchImageMore(keyword);
	}
	else if (category == 'movie') {
		SearchMovieMore(keyword);
	}
	else if (category == 'playerdb') {
		SearchEPlayerMore(keyword);
	}
	else if (category == 'board') {
		SearchBoardMore(keyword);
	}
}

function RunIntegrateSearch() {
	var keyword;
	var turl;
	
	keyword = document.getElementById('sq_keywd');
	if (keyword == null) {
		return;
	}
	if (keyword.value == '') {
		alert('검색어를 입력하십시오.');
		return;
	}
	
	var s1 = document.getElementById('s1');
	switch (s1.value) {
		case 'gameall':
			turl = '/search/search_all.daily?query=' + Url.encode(keyword.value);
			break;
		case 'news':
			turl = '/search/search_news.daily?query=' + Url.encode(keyword.value);
			break;
		case 'video':
			turl = '/search/search_video.daily?query=' + Url.encode(keyword.value);
			break;	
		case 'gamedb':
			turl = '/search/search_gamedb.daily?query=' + Url.encode(keyword.value);
			break;	
		case 'gametalk':
			turl = '/search/search_gametalk.daily?query=' + Url.encode(keyword.value);
			break;	
		case 'hot':
			turl = '/search/search_hot.daily?query=' + Url.encode(keyword.value);
			break;
		case 'prol':
			turl = '/search/search_proleague.daily?query=' + Url.encode(keyword.value);
			break;
		case 'osl':
			turl = '/search/search_starleague.daily?query=' + Url.encode(keyword.value);
			break;
		case 'msl':
			turl = '/search/search_msl.daily?query=' + Url.encode(keyword.value);
			break;
		case 'etcl':
			turl = '/search/search_etcleague.daily?query=' + Url.encode(keyword.value);
			break;
		case 'image':
			turl = '/search/search_image.daily?query=' + Url.encode(keyword.value);
			break;
		case 'movie':
			turl = '/search/search_movie.daily?query=' + Url.encode(keyword.value);
			break;
		case 'playerdb':
			turl = '/search/search_playerdb.daily?query=' + Url.encode(keyword.value);
			break;
		case 'board':
			turl = '/search/search_board.daily?query=' + Url.encode(keyword.value);
			break;
	}
	
	if (turl != null)
		location.href = turl;
	return;
}

function resizeImgsWithMax(img, mwidth, mheigth)
{
	var height = img.height;
	var width = img.width;
	var targetHeight = height;
	var targetWidth = width;
	var ratio = 0.0;
	var maxWidth = mwidth;
	var maxHeight = mheigth;

	//축소
	if (width > maxWidth || height > maxHeight)
	{
		if (width > height)
		{
			if (width > maxWidth)
			{
				//리사이징
				ratio = height / width;
				
				targetWidth = maxWidth;
				targetHeight = ratio * targetWidth;
			}
		}
		else
		{
			if (height > maxHeight)
			{
				//리사이징
				ratio = width / height;
				
				targetHeight = maxHeight;
				targetWidth = ratio * targetHeight;
			}
		}
	}
	else//확대
	{
		if (width > height)
		{
			if (width < maxWidth)
			{
				//리사이징
				ratio = height / width;
				
				targetWidth = maxWidth;
				targetHeight = ratio * targetWidth;
			}
		}
		else
		{
			if (height < maxHeight)
			{
				//리사이징
				ratio = width / height;
				
				targetHeight = maxHeight;
				targetWidth = ratio * targetHeight;
			}
		}
	}
	
	img.height = targetHeight;
	img.width = targetWidth;
}
