

var isIE = navigator.appVersion.indexOf('MSIE') != -1;
var isIE6 = navigator.appVersion.indexOf('MSIE 6') != -1;

var validiatePair = {key:"", no:""};

function displayMultimedia(src, width, height, options) {
    var html = _displayMultimedia(src, width, height, options);
    if(html) document.writeln(html);
}

function _displayMultimedia(src, width, height, options) {
    if(src.indexOf('files') == 0) src = request_uri + src;
	if(src.indexOf('/') == 0) src = request_uri + src.substr(1);

    var defaults = {
        wmode : 'transparent',
        allowScriptAccess : 'sameDomain',
        quality : 'high',
        flashvars : '',
        autostart : false,
		id : ''
    };

    var params = jQuery.extend(defaults, options || {});
	var autostart = (params.autostart && params.autostart != 'false') ? 'true' : 'false';
	delete(params.autostart);

    var clsid = "";
    var codebase = "";
    var html = "";

    if(/\.(gif|jpg|jpeg|bmp|png)$/i.test(src)){
        html = '<img src="'+src+'" width="'+width+'" height="'+height+'" />';
    } else if(/\.flv$/i.test(src) || /\.mov$/i.test(src) || /\.moov$/i.test(src) || /\.m4v$/i.test(src)) {
        html = '<embed src="'+request_uri+'common/tpl/images/flvplayer.swf" allowfullscreen="true" autostart="'+autostart+'" width="'+width+'" height="'+height+'" flashvars="&file='+src+'&width='+width+'&height='+height+'&autostart='+autostart+'" wmode="'+params.wmode+'" />';
    } else if(/\.swf/i.test(src)) {
        clsid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';

        if(typeof(enforce_ssl)!='undefined' && enforce_ssl){ codebase = "https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0"; }
        else { codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0"; }
        html = '<object classid="'+clsid+'" codebase="'+codebase+'" width="'+width+'" height="'+height+'" flashvars="'+params.flashvars+'" id="' + params.id + '">';
        html += '<param name="movie" value="'+src+'" />';
        for(var name in params) {
            if(params[name] != 'undefined' && params[name] != '') {
                html += '<param name="'+name+'" value="'+params[name]+'" />';
            }
        }
        html += ''
            + '<embed src="'+src+'" autostart="'+autostart+'"  width="'+width+'" height="'+height+'" flashvars="'+params.flashvars+'" wmode="'+params.wmode+'" id="' + params.id + '" />'
            + '</object>';
    }  else {
		if (jQuery.browser.mozilla || jQuery.browser.opera) {
			// firefox and opera uses 0 or 1 for autostart parameter.
			autostart = (params.autostart && params.autostart != 'false') ? '1' : '0';
		}

        html = '<embed src="'+src+'" autostart="'+autostart+'" width="'+width+'" height="'+height+'"';
        if(params.wmode == 'transparent') {
            html += ' windowlessvideo="1"';
        }
        html += ' />';
    }
    return html;
}

/*-------------------------------------------------------------------------
   쿠키 가져오기
  -------------------------------------------------------------------------*/
function getCookie( name ){
	var nameOfCookie = name + "=";
	var x = 0;
	while ( x <= document.cookie.length )
	{
		var y = (x+nameOfCookie.length);
		if ( document.cookie.substring( x, y ) == nameOfCookie ) {
			if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
			endOfCookie = document.cookie.length;
			return unescape( document.cookie.substring( y, endOfCookie ) );
		}
			
		x = document.cookie.indexOf( " ", x ) + 1;
			
		if ( x == 0 ) break;
	}
			return "";
}


/*-------------------------------------------------------------------------
   쿠키 생성
  -------------------------------------------------------------------------*/
function setCookie( name, value, expiredays ) { 

		if(expiredays != null){
			var todayDate = new Date(); 
			todayDate.setDate(todayDate.getDate() + expiredays);
			document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";";
		}else{
			document.cookie = name + "=" + escape( value ) + "; path=/;";
		}
} 

/*-------------------------------------------------------------------------
   쿠키 삭제
  -------------------------------------------------------------------------*/
function delCookie (name) {  
		var exp = new Date();  
		exp.setTime (exp.getTime() - 1);  
		var cval = getCookie (name);  
		document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}



var CommonCodes = {

	DDD:null, MBD:null, EML:null,

	init:function(group){
		if(eval('this.'+ group) == null)eval('this.set'+ group +'()');
	},

	setDDD:function(){
		this.DDD = [
			{text:'02', value:'02'}, 
			{text:'031', value:'031'},
			{text:'032', value:'032'},
			{text:'033', value:'033'},
			{text:'041', value:'041'},
			{text:'042', value:'042'},
			{text:'043', value:'043'},
			{text:'051', value:'051'},
			{text:'052', value:'052'},
			{text:'053', value:'053'},
			{text:'054', value:'054'},
			{text:'055', value:'055'},
			{text:'061', value:'061'},
			{text:'062', value:'062'},
			{text:'063', value:'063'},
			{text:'064', value:'064'}
		];
	},

	setMBD:function(){
		this.MBD = [
			{text:'010', value:'010'}, 
			{text:'011', value:'011'},
			{text:'016', value:'016'},
			{text:'017', value:'017'},
			{text:'018', value:'018'},
			{text:'019', value:'019'}
		];
	},

	setEML:function(){
		this.EML = [
			{text:'chollian.net', value:'chollian.net'},
			{text:'dreamwiz.com', value:'dreamwiz.com'},
			{text:'empal.com', value:'empal.com'},
			{text:'freechal.com', value:'freechal.com'},
			{text:'hanmail.net', value:'hanmail.net'},
			{text:'hotmail.com', value:'hotmail.com'},
			{text:'lycos.co.kr', value:'lycos.co.kr'},
			{text:'korea.com', value:'korea.com'},
			{text:'nate.com', value:'nate.com'},
			{text:'naver.com', value:'naver.com'},
			{text:'paran.com', value:'paran.com'},
			{text:'unitel.co.kr', value:'unitel.co.kr'},
			{text:'yahoo.co.kr', value:'yahoo.co.kr'}
		];
	},

	setFOT:function(){
		this.FOT = [
			{text:'해당없음', value:'N/A'},
			{text:'불량', value:'Faulty'},
			{text:'일부불량', value:'Some Faulty'},
			{text:'정상', value:'Normal'},
			{text:'재실차완료', value:'ReTest Completed'},
			{text:'미개발', value:'UnDeveloped'}
		];
	},

	writeOptions:function(group, value){

		//option => {name:#, id:#, value:#}
		this.init(group);
		var temp = eval('this.'+ group);

		//document.write("<select name='"+ (option.name ? option.name : group) +"' id='"+ (option.id ? option.id : group) +"'>");
		for(var i = 0, j = temp.length;i < j;i++)
			document.write("<option value='"+ temp[i].value +"'"+ (temp[i].value != value ? "" : " selected='selected'" ) +">"+ temp[i].text +"</option>")
		//document.write("</select>");

	},
	

	writeOptionsReturn:function(group, value){

		//option => {name:#, id:#, value:#}
		this.init(group);
		var temp = eval('this.'+ group);
		var a = ""
		//document.write("<select name='"+ (option.name ? option.name : group) +"' id='"+ (option.id ? option.id : group) +"'>");
		for(var i = 0, j = temp.length;i < j;i++)
			a += ("<option value='"+ temp[i].value +"'"+ (temp[i].value != value ? "" : " selected='selected'" ) +">"+ temp[i].text +"</option>")
		//document.write("</select>");

		return a;

	}
};

function openWin(url, w, h, name, bScrollbar, t, l, bResizable) {

    if(bScrollbar == null)
        bScrollbar = 0;

    if(bResizable == null)
        bResizable = 0;

    if(name == null)
        name = "popWin";

	if(w >= screen.width){ //스크린 상테에 따라 스크롤바 자동표시
			w = screen.width - 40;
			bScrollbar = 1;
	}

	if(h >= screen.height){ //스크린 상테에 따라 스크롤바 자동표시
			h = screen.height - 40;
			w = w + 20;
			bScrollbar = 1;
	}

    if(t == null)
        t = (screen.height-h)/2;

    if(l == null)
        l = (screen.width-w)/2;

    var PopWin = window.open(url, name, "toolbar=0, channelmode=0, location=0, directories=0, resizable="+ bResizable +", menubar=0, scrollbars="+ bScrollbar +", width=" + w + ", height=" + h +", top="+t+", left="+l);

    if(PopWin == null){
        alert(appMessage.disabledPopup);
        return;
    }

    PopWin.focus();

	return PopWin;
}

function popupContent(container, url){

	var contentPage;
	if (!url) {
		contentPage = openWin("about:blank", 100, 100, "ContentViewer", "yes");
		contentPage.document.open();
		contentPage.document.writeln("<html><head><link href='/Contents/application.css' media='all' rel='stylesheet' type='text/css' /></head><body>"); //스타일등을 지정
		contentPage.document.writeln(container.innerHTML);
		contentPage.document.writeln("</body></html>");
		contentPage.document.close();
	}
	else
	    contentPage = openWin(url, 100, 100, "ContentViewer", "yes");


	$(contentPage.document).ready(function (){
		contentPage.resizeBy($(this).find("body").width(), $(this).find("body").height());
		contentPage.moveTo(100, 100);
		setTimeout('contentPage.resizeBy($(this).find("body").width(), $(this).find("body").height());', 500);
	});

}

function printContent(container) {
        contentPrinter.document.open();
        contentPrinter.document.writeln("<html><head><link href='/stylesheets/prime.css' media='all' rel='stylesheet' type='text/css' /><link href='/stylesheets/print.css' media='print' rel='stylesheet' type='text/css' /></head><body>"); //스타일등을 지정
        contentPrinter.document.writeln(container.innerHTML);
        contentPrinter.document.writeln("</body></html>");
        contentPrinter.document.close();

        contentPrinter.document.execCommand('Print');

}

var loadingBar = {
	intervalKey:0,
	show: function (){
		$("#waiting_window").fadeTo('fast', 0.6);
		this.listner();
	},
	hide: function (){
		$(window).unbind("scroll", this.onWindowScroll);
		$(window).unbind("resize", this.onWindowScroll);
		$("#waiting_window").fadeOut('fast');
	},
	listner: function () {
		this.onWindowScroll();
		$(window).bind("scroll", this.onWindowScroll);
		$(window).bind("resize", this.onWindowScroll);
	},
	onWindowScroll: function (){
		$("#waiting_window").height($(document).height());
		$("#waiting_progress").css("top", $(document).scrollTop() + $.clientCoords().h / 2 - 50);
	}
};

$.clientCoords = function(){
	if(window.innerHeight || window.innerWidth){
		return {w:window.innerWidth, h:window.innerHeight}
	}
	return {
		w:document.documentElement.clientWidth,
		h:document.documentElement.clientHeight
	}
}

var intervalKey = 0;
var scrollTopPx = 0;
$(document).ready(function (){
	
	$("#menuabsolute").mouseout(function (){
		if(isIE) 
			$("object#navigator").get(0).divOutAction();
		else 
			$("embed#navigator").get(0).divOutAction();
	});

	if ($("#public_quick").size() > 0)
	{
		$("#public_quick").css("marginTop", "0");
		intervalKey = setInterval(moveQuick, 10);
	}

	if ($("select.email_host_helper").size() > 0)
	{
		var txtMailHost = $("select.email_host_helper").prev();
		if(txtMailHost.val() == "")
		{
			txtMailHost.hide();
		}
		else if($("select.email_host_helper > option[value='" + txtMailHost.val() + "']").size() > 0)
		{
			txtMailHost.hide();
			$("select.email_host_helper > option[value='" + txtMailHost.val() + "']").attr("selected", "selected");
		}
		$("select.email_host_helper").change(function (){
			if($(this).val() == "*")
			{
				txtMailHost.val("");
				txtMailHost.show();
				return;
			}
			txtMailHost.val($("select.email_host_helper").val());
			txtMailHost.hide();
		});
	}

	if($('#update_items').size() > 0)
	{
		$('#update_items').bxSlider({ ticker:true, tickerSpeed: 5000, tickerHover:true });
		$('.bx-window').css("width", ($("#update_container").width() - 10) + "px");
	}

	if ($("#waiting_window").css("display") != "none") loadingBar.listner();

	$("textarea[length-view]").each(function (idx, item){
		$(item).keydown(function (){
			var lblName = $(this).attr("length-view");
			var len = parseInt($(this).attr("maxlength")) - parseInt($(this).val().length);
			$(lblName).html(len);
			if (len < 0) $(lblName).css("color", "#FF0000");
			else $(lblName).css("color", "#555555");
		});
	});

	$("a.confirm[title]").each(function (idx, item) {

		$(item).click(function (){
			return confirm($(item).attr("title"));
		});

	});
	
	$("a.ajax[title]").each(function (idx, item) {
		
		$(item).click(function (){
			loadingBar.show();
			$.ajax({
				url: $(item).attr("href"),
				success: function (){
					alert($(item).attr("title"));
					loadingBar.hide();
				},
				error: function () {
					alert(appMessages.DATA_SEND_FAULT);
					loadingBar.hide();
				}
			});

			return false;
		});

	});


	$("form[ajax-layer]").each(function (idx, item) {
		$(item).submit(function () {
			loadingBar.show();
			$($(item).attr("ajax-layer")).html("");
			$.ajax({
				url: $(item).attr("action"),
				type: $(item).attr("method"),
				data: $(item).serialize(),
				success: function (data) {
					$($(item).attr("ajax-layer")).html(data);
					loadingBar.hide();
				},
				error: function () {
					alert(appMessages.CONNECTION_FAULT);
					loadingBar.hide();
				}
			});

			return false;
		});
	});

	$("input[type='text'].datepicker").each(function (idx, item) {
		$(item).datepicker({
			showOn: "button",
			buttonImage: request_uri + "images/common/img_calender.gif",
			buttonImageOnly: true,
			dateFormat: "yy-mm-dd"
		});

	});


	if ($(".tab-page-ctrl").size() > 0)
	{
		$(".tab-page-ctrl").find("div.js-nav-item").hide();
		if ($(".tab-page-ctrl").find(".tabHolder > ul > li.selected a").size() > 0)
		{
			var def_id = $(".tab-page-ctrl").find(".tabHolder > ul > li.selected a").attr("href");
			$(".tab-page-ctrl").find(def_id).show();

			$(".tab-page-ctrl").find(".tabHolder > ul > li a").click(function () {
				$(".tab-page-ctrl").find(".tabHolder > ul > li").removeClass("selected");
				$($(this).parent()).addClass("selected");
				
				$(".tab-page-ctrl").find("div.js-nav-item").hide();
				$(".tab-page-ctrl").find($(this).attr("href")).show();

				return false;
			});
		}
		//tabHolder
	}


	if( $("th > input[type='checkbox'].check_helper").size() > 0)
	{
		$("th > input[type='checkbox'].check_helper").click(function (){
			$("td > input[type='checkbox'][rel='" + $(this).attr("rel") + "']:enabled").attr("checked", $(this).attr("checked"));
		});
	}

});

function moveQuick(){

	scrollTopPx = $(document).scrollTop() - 200;
	if (scrollTopPx < 0) scrollTopPx = 0;
	var kk = parseInt($("#public_quick").css("marginTop").replace(/px/g, ""));
	$("#public_quick").css("marginTop", kk + (scrollTopPx - kk) / 6);

}



function setGnbHeight(_height)
{
	$("#menuabsolute").css("height", _height + "px");
}
