//jquery extends fn
jQuery.fn.extend({
   check: function() {
     return this.each(function() { this.checked = true; });
   },
   uncheck: function() {
     return this.each(function() { this.checked = false; });
   },
   emptyCheck: function(value) {
        var val = jQuery.trim($(this).val());
        if(val.length < 1) {
            alert(value);
            $(this).focus();
            return false;
        }
        return true;
    },
    numberCheck: function(value, cnt) {
        var num = jQuery.trim($(this).val()).replace(/\,/gi, "");
        
        if(!cnt) {
            if(isNaN(num) || !num ) {
                alert(value);
                $(this).focus();
                return false;            
            }
        } else {
            if(isNaN(num) || !num || num.length != cnt) {
                alert(value);
                $(this).focus();
                return false;            
            }
        }
        return true;
    }, trim: function() {
        return jQuery.trim($(this).val());
    },
    checkPeriod: function(value, start, end) {
        var num = jQuery.trim($(this).val()).replace(/\,/gi, "");
        if(isNaN(num) || !num || num < start || num > end ) {
            alert(value);
            $(this).focus();
            return false;            
        }
        return true;
    },
    checkLength: function(value, start, end) {
        var num = $(this).val();
        if(!num || num.length < start || num.length > end ) {
            alert(value);
            $(this).focus();
            return false;            
        }
        return true;
    },
    observe: function(func, time) {
        $(this).focus(function(event){
            tid=setInterval(func, time);
        });
        
        $(this).blur(function(event){
            clearInterval(tid);
        });
    },
    currency: function() {
        $(this).keyup(function(e) {
            var num = jQuery.trim($(this).val()).replace(/[^0-9]/gi, "");
            if(!num) {
                return false;
            }
            
            num = parseInt(num) + "";
            var length = num.length;
            var per = parseInt(length/3);
            var other = parseInt(length%3);
            
            var temp2 = "";
            (other > 0 && per > 0) ? temp2 = num.substr(0,other)+"," : temp2 = num.substr(0,other);
            for(i=0;i<per;i++) {
                temp2 = temp2+num.substr(other,3);
                if(i != per-1) temp2 = temp2 + ",";
                other += 3;
            }
            
            $(this).val(temp2);
        });
        $(this).focus(function(e) {
            if($(this).val() == 0) $(this).val('');
        });
        $(this).blur(function(e) {
            if($(this).val() == "") $(this).val(0);
        });
    }
});

//콤마찍기
function currencyComma() {
    jQuery.each($(":input[class*='currency']"), function() {
        $(this).keyup(function(e) {
            var num = jQuery.trim($(this).val()).replace(/[^0-9]/gi, "");
            if(!num) {
                $(this).val("");
                return false;
            }
            
            num = parseInt(num) + "";
            var length = num.length;
            var per = parseInt(length/3);
            var other = parseInt(length%3);
            
            var temp2 = "";
            (other > 0 && per > 0) ? temp2 = num.substr(0,other)+"," : temp2 = num.substr(0,other);
            for(i=0;i<per;i++) {
                temp2 = temp2+num.substr(other,3);
                if(i != per-1) temp2 = temp2 + ",";
                other += 3;
            }
            
            $(this).val(temp2);
        });
        $(this).focus(function(e) {
            if($(this).val() == 0) $(this).val('');
        });
        $(this).blur(function(e) {
            if($(this).val() == "") $(this).val(0);
        });
    })
}

//숫자에 콤마 찍어주기
function comma(num) {
    if(!num) {
        return 0;
    }
    
    num = parseInt(num) + "";
    var length = num.length;
    var per = parseInt(length/3);
    var other = parseInt(length%3);
    
    var temp2 = "";
    (other > 0 && per > 0) ? temp2 = num.substr(0,other)+"," : temp2 = num.substr(0,other);
    for(i=0;i<per;i++) {
        temp2 = temp2+num.substr(other,3);
        if(i != per-1) temp2 = temp2 + ",";
        other += 3;
    }
    
    return temp2;
}

//마우스 오버 함수
function levelOver() {
    $(".levelImg").mousemove(function(event) {
       if($("#levelPopup").css("display") == "none") {
            var src = $(this).attr("src");
            var img = src.replace(/level/gi, "level/big");
            
            $("#levelPopupImg").attr("src", img);
            $("#levelPopup").show();
       }
       
       var x = event.pageX + 20;
       var y = event.pageY - 10;
       
       $("#levelPopup").css("top", y);
       $("#levelPopup").css("left", x);
    });
    
    $(".levelImg").mouseout(function() {
        $("#levelPopup").hide();
    });
}

//마우스 오버부분
var temp = "";
function globalTemp1() {
    if(temp != "Y") {
        levelOver();
    } else {
        temp = "Y";
    }
}

//flash 이미지
function flash_show(width, height, src) {
    var temp = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" name="topNaviFlash">\
                <param name="movie" value="'+src+'">\
                <param name="wmode" value="transparent">\
                <param name="allowScriptAccess" value="always"/>\
                <param name="quality" value="high">\
                <param name="menu" value="false">\
                <embed src="'+src+'" quality="high" menu="false" allowScriptAccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"  wmode="transparent" width="'+width+'" height="'+height+'"></embed></object>';
   document.write(temp);
}

//기본적으로 실행되야 할것들 넣어주기.
$(document).ready(function(){
    //콤마 찍어주기 부분.
    currencyComma();
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch(ignored) {}
    
    //레벨 마우스 오버
    levelOver();
});