/*
//version1
File Name   : sub_GNB.js
Author      : Bang Daejong
*/

/*
window.onload = function() {
    controlGNB();
    controlSearch();
}
*/

function onloadGNB() {
    controlGNB();
    controlSearch();
}

if (window.addEventListener) {
    window.addEventListener("load", onloadGNB, false);
}
else if (window.attachEvent) {
    window.attachEvent("onload", onloadGNB);
}

function controlGNB(){
    var showroomGNB = document.getElementById('gnb-showroom');
    var showroomSNB = document.getElementById('snb-showroom');
    var companyOverviewGNB = document.getElementById('gnb-companyOverview');
    var companyOverviewSNB = document.getElementById('gnb-companyOverview').getElementsByTagName('div')[0];
    var hyundaiExperienceGNB = document.getElementById('gnb-hyundaiExperience');
    var hyundaiExperienceSNB = document.getElementById('gnb-hyundaiExperience').getElementsByTagName('div')[0];

    var gnbs = [showroomGNB, companyOverviewGNB, hyundaiExperienceGNB];
    var snbs = [showroomSNB, companyOverviewSNB, hyundaiExperienceSNB];
    var index = -1;
    var timer = null;

    function overGNB(idx) {
        clearTimeout(timer);

        if (index != idx) {
            index = idx;

            for (var i = 0; i < gnbs.length; i++) {
                jQuery(snbs[i]).stop();

                if (i == index) {
                    gnbs[i].getElementsByTagName('a')[0].className = 'gnb on';
                    snbs[i].style.height = null;
                    snbs[i].style.minHeight = 0;

                    jQuery(snbs[i]).slideDown();
                }
                else {
                    gnbs[i].getElementsByTagName('a')[0].className = 'gnb';
                    snbs[i].style.display = 'none';
                }
            }
        }
    };

    var outGNB = function() {
        clearTimeout(timer);

        timer = setTimeout(function() {
            index = -1;

            for (var i = 0; i < gnbs.length; i++) {
                gnbs[i].getElementsByTagName('a')[0].className = 'gnb';
                jQuery(snbs[i]).slideUp();
            }
        }, 500);
    };

    for (var i = 0; i < gnbs.length; i++) {
        gnbs[i].onmouseover = snbs[i].onmouseover = function() {
            var idx = i;
            return function(){ overGNB(idx); };
        }();

        gnbs[i].onmouseout = outGNB;
    }

    /*
    function overGNB(GNB,SNB){
        GNB.className = 'gnb on';
        //SNB.style.display = 'block';
        SNB.style.height = null;
        jQuery(SNB).slideDown();

        SNB.onmouseover = function(){
            if (window.gnb_timer) {
                clearTimeout(window.gnb_timer);
                window.gnb_timer = null;
            }
            GNB.className = 'gnb on';
            //SNB.style.display = 'block';
            SNB.style.height = null;
            jQuery(SNB).slideDown();
        }

        GNB.onmouseout = function(){
            window.gnb_timer = setTimeout(function() {
                GNB.className = 'gnb';
                //SNB.style.display = 'none';
                jQuery(SNB).slideUp();
            }, 100);
        }
        SNB.onmouseout = function(){
            window.gnb_timer = setTimeout(function() {
                GNB.className = 'gnb';
                //SNB.style.display = 'none';
                jQuery(SNB).slideUp();
            }, 100);
        }
    }

    showroomGNB.onmouseover = function(){
        overGNB(showroomGNB,showroomSNB);
    }

    companyOverviewGNB.onmouseover = function(){
        overGNB(companyOverviewGNB,companyOverviewSNB);
    }

    hyundaiExperienceGNB.onmouseover = function(){
        overGNB(hyundaiExperienceGNB,hyundaiExperienceSNB);
    }
    */

    var articlePassengerLi = document.getElementById('article-passenger').getElementsByTagName('li');
    var articleRecreationalLi = document.getElementById('article-recreational').getElementsByTagName('li');

    for(var i=0;i<articlePassengerLi.length;i++){
        articlePassengerLi[i].onmouseover = function(){
            this.getElementsByTagName('p')[0].style.color = '#0f5699';

            this.onmouseout = function(){
                this.getElementsByTagName('p')[0].style.color = '#666';
            }
        }
        articlePassengerLi[i].onclick = function(){
            location.href = this.getElementsByTagName('a')[0].href;
        }
    }

    for(var i=0;i<articleRecreationalLi.length;i++){
        articleRecreationalLi[i].onmouseover = function(){
            this.getElementsByTagName('p')[0].style.color = '#0f5699';

            this.onmouseout = function(){
                this.getElementsByTagName('p')[0].style.color = '#666';
            }
        }
        articleRecreationalLi[i].onclick = function(){
            location.href = this.getElementsByTagName('a')[0].href;
        }
    }
}

function controlSearch(){
    var searchInput = document.getElementById('article-search').getElementsByTagName('input')[0];

    searchInput.onclick = function(){
        searchInput.style.backgroundImage = 'none';
    }
}

