/* 
 * Telia.dk Navigation, Model: Fall 2009
 * @requires JQuery
 * Programmed by Fini Alring, Deducta.dk
 * Designed by Villads Spangsberg, KathArt.dk
 * Copyright 2009 Telia, TeliaSonera Group
 */

var navi = {
    openPopup: false,
    popupTimer: null,
    mouseOnMenu: false,
    currentMenuName: false,
    resetMenuImage: function(menuName) {
        if(menuName != false) {
            var menuImage = jQuery('.navi-menu[rel=' + menuName + ']').find('img');
            menuImage.attr("src", menuImage.attr("src").replace("_on","_off"));
        }
    },
    hide: function(elId) {
       // console.log('hider:' + navi.openPopup + ' - ' + elId + ' - ' + navi.mouseOnMenu);
        if((navi.openPopup == false && (elId) != navi.openPopup) || this.mouseOnMenu == false) {
            var tmpImageName = elId.substring(elId.lastIndexOf('-')+1);
            jQuery(elId).slideUp("fast", function() {navi.resetMenuImage(tmpImageName)});

        }
    },
    closeInactive: function(activeId) {

        jQuery('#header .navi-menu-popup').each(function() {
            var elId =  '#' + jQuery(this).attr('id');
            //console.log('closeInactive: ' + navi.openPopup + ' - ' + elId);
            if(elId != activeId) {
                //console.log('closeInactive: Closing ' + elId);
                jQuery(this).stop(true, true).hide();
            }
        });
    },
    init: function() {

        /* Load navigation data via AJAX, disabled - but can be enabled when needed. */
        /*
        jQuery.ajax({
            type: "GET",
            url: "navi_data.xml",
            cache: true,
            dataType: 'html',
            success: function(data){

                var xmlData = jQuery(data);
                jQuery("#navi-menu-popup-mobil").html(xmlData.find('#navi-menu-popup-mobil > *'));
                jQuery("#navi-menu-popup-bredbaand").html(xmlData.find('#navi-menu-popup-bredbaand > *'));
                jQuery("#navi-menu-popup-fasttlf").html(xmlData.find('#navi-menu-popup-fasttlf > *'));
                jQuery("#navi-menu-popup-tv").html(xmlData.find('#navi-menu-popup-tv > *'));
                jQuery("#navi-menu-popup-fordele").html(xmlData.find('#navi-menu-popup-fordele > *'));
                //jQuery("#navi-menu-popup-logon").html(xmlData.find('#navi-menu-popup-logon > *'));
            }
        });
        */
        
        


        // Match positions of popups according to main menu buttons.
        jQuery('#navi .navi-menu').each(function() {
            var el = jQuery(this);
            var elRel = el.attr('rel');
            var elOffsetX = 0;
            if(elRel == 'logon') {
                elOffsetX = -81; //  move popup to match handle in right side.
            }
            var targetPopup = 'navi-menu-popup-' + elRel;
            //console.log(el.position().left);
            jQuery('#' + targetPopup).css('left', el.position().left + elOffsetX + 'px'); //.css('top', el.position().top+14)
        });


        /* Remove title tags in all navigation links */
        jQuery('#navi a,.navi-menu-popup a').each(function() {

            jQuery(this).removeAttr('title');
        });

    },
    mouseover: function(target) {

        navi.mouseOnMenu = true;

        if(navi.currentMenuName != false) {
            var menuImage = jQuery('.navi-menu[rel=' + navi.currentMenuName + ']').find('img');
            menuImage.attr("src", menuImage.attr("src").replace("_on","_off"));
        }

        navi.currentMenuName = jQuery(target).attr('rel');
        var menuImage = jQuery('.navi-menu[rel=' + navi.currentMenuName + ']').find('img');
        menuImage.attr("src", menuImage.attr("src").replace("_off","_on"));

        var targetPopup = '#navi-menu-popup-' + navi.currentMenuName;

        if(this.openPopup && (targetPopup != this.openPopup)) {
           navi.closeInactive(targetPopup);
        }
        
        this.openPopup = targetPopup;

        jQuery(targetPopup).show();

        // Find tallest element in menu.
        var tallestEl = 0;

        jQuery(targetPopup + ' .navi-menu-popup-body > div').each(function() {

          var el = jQuery(this);
          var elHeight = el.height();

          if(elHeight > tallestEl) {
              tallestEl = elHeight;
          }

        });

        // Set seperator height to tallest element height.
        jQuery(targetPopup + ' .navi-popup-seperator').each(function() {

          jQuery(this).height(tallestEl-21);
        });

/*

        jQuery(targetPopup + ' .navi-popup-seperator').each(function() {

          var el = jQuery(this);
          console.log(el.prev("div"));
          var parentHeight = el.prev("div").height()
          console.log(parentHeight);
          el.height(parentHeight);

        });
 **/



        // .fadeIn("fast"); //slideDown('fast');
        //console.log('schedule show: ' + targetPopup);
    },
    mouseout: function(target) {

        navi.mouseOnMenu = false;
        var rel = jQuery(target).attr('rel');
        var targetPopup = '#navi-menu-popup-' + rel;

        if(rel == "music" || rel == "biz_nyhedsbrev") {
            navi.resetMenuImage(rel);
        } else {

            navi.popupTimer = setTimeout('navi.hide("' + targetPopup + '")', 1000);
        }
        //console.log('schedule hide: ' + targetPopup);
    },
    mouseoverPopup: function(target) {
        navi.mouseOnMenu = true;
        this.openPopup = '#' + jQuery(target).attr('id');
    },
    mouseoutPopup: function(target) {
        navi.mouseOnMenu = false;
        var targetPopup = '#' + jQuery(target).attr('id');
        navi.popupTimer = setTimeout('navi.hide("' + targetPopup + '")', 1000);
    }
}


jQuery(document).ready(function() {

    navi.init();

    jQuery(window).resize(function() {
        navi.init();
    });

    jQuery(window).load(function () {
      navi.init();
    });

    jQuery('#navi .navi-menu').hover(
        function () {
            navi.mouseover( jQuery(this) );
        },
        function () {
            navi.mouseout( jQuery(this) );
        }
    );

    // Main Menu Popup over
    jQuery('#header .navi-menu-popup').mouseover(function() {
        navi.mouseoverPopup( jQuery(this) );
    });

    // Main Menu Popup out
    jQuery('#header .navi-menu-popup').mouseout(function() {
        navi.mouseoutPopup( jQuery(this) );
    });


    // Clear form field when focused, and reset when blurred if empty.
    jQuery("#all_query,.navi-menu-popup input.text, #question").focus(function () {
         
         if(this.value == this.defaultValue) {
             this.value = '';
         }

    }).blur(function () {

         if(this.value == '') {
             this.value = this.defaultValue;
         }
    });

    /* Remove line breaks in content zone, this is replaced by css rules on h1, h2, links etc.. */
    //jQuery('#content br').remove();

/*
    jQuery.post('http://telia.delegate.dk/AddPermission.asmx/AddContact', { email: 'string', name: 'string', phone: 'string', analyze: false },
        function(data, textStatus) {

            alert("Data Loaded: " + "(" + textStatus + ") "+ data);
        }, 'jsonp'
    );
*/
/*
$.getJSON("http://telia.delegate.dk/AddPermission.asmx/AddContact?jsonp=?",{email: 'string', name: 'string', phone: 'string', analyze: false},
    function(data){
    alert(data);
    });
*/

/*
  $.ajax({
    type: "GET",
    url: "http://telia.delegate.dk/AddPermission.asmx/AddContact",
    data: {email: 'string', name: 'string', phone: 'string', analyze: false},
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: function(data) {

        alert(data);
    }
  });
*/

/*
$.getJSON("http://therealurl.appspot.com?format=json&;amp;url=bit.ly/q&amp;callback=?",
    function(data){ alert(data.url) }
); */


});



/* Udlandspriser - Taletid */


function chooseCountry(countryId) {

  jQuery("#countryList > div:visible").slideUp();

  jQuery("#countryList > div." + countryId).slideDown();

  isFlashSelecting = 1;
}

/* Udlandspriser - Taletid - END */
