// Cross site FRONT-END functions
// Author: Yanay Zohar

jungoapp = {
  'lang' : 'en',
  'phrases': {},                // will be filled dynamically
  tmm_menu_active : false,      //top menu active flag (sometimes needs to hide competing elements via z-index)  
  leads_server : 'http://www.netprox.info/', // was: 'http://173.255.211.155/'
  use_leads_server : false
};

$(document).ready( function(){
    
    // prevent no firebug support errors when calling console.log in code:
    if(typeof console === "undefined") {  
    console = {log: function() { }};
    }
    
    $('.features tr,table.light tr').find('td:last').css("border-right", "1px solid silver");
    
    // update language used, if set by url:
    if( $(document).getUrlParam("lang") )  
      jungoapp.lang =  $(document).getUrlParam("lang");    

   init_top_menu();
    
    $('#current-lang-display').click( function(){
          $('#language-select').toggle();
    });
    
    $('#language-select').bind("mouseleave",function(){
          $('#language-select').hide();
    });
    
    
    // if php injected language code, use it:
    if( $('#lang-for-js').html() )
        jungoapp.lang = $('#lang-for-js').html();
    
    if( $('#phrases-for-js').html() )
        jungoapp.phrases = JSON.parse( $('#phrases-for-js').html() );  // will make lang-depdendant phrases availalbe in JS code.
        
	  $('.admin-actions').parent().hover( 
              function(){$(this).children('.admin-actions').show();console.log( $(this).attr('id'));}, 
              function(){$(this).children('.admin-actions').hide();}    
    );
    
    if( typeof bind_editable_blocks_control==="function" ) // only avbailable for admins.
            bind_editable_blocks_control();
    
    // smooth scroll back-to-top links:
    $('a.scroll-top').click( function(){
        $("html, body").animate({ scrollTop: 0 }, "slow");
        return false;
    })
    

    //fancybox:
    if ($.fn.fancybox)
    $('a.fancy-box').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	false
    });
    
    JTracker();  // track current page    
    
    // some pages have dynamic place holder for next events:
          /*
          // implement only after new jungo.com server:
          // update in HTP-DEF: IN <PROMO_EVENTS> : <div id='optional-events-teaser' style='display:none;'></div>
          // and remove static texts
          $('#optional-events-teaser').load( get_scripts_path()+'st/events.php?teaser_text=1');
          if( $('#optional-events-teaser').html() )
          {
            $('#optional-events-teaser').show();
          } 
          */   
});

function init_top_menu(){

    var height_min = 268; // for products bubble (#tmm-products-bubble)
    
    $('#tmm-wrap').load( base_path_url+ 'st/_tmm_menu.php', function(){
    
                $('#tmm-titles').find('.tmm-single').hover( function(){

                    var id          = $(this).attr('id');
                    var shadow_id   = id+'-shadow';
                    var bubble_id   = id+'-bubble';

                    // hide all bubbles:
                    $('.tmm-bubble').hide();

                    // reset products' bubble height:
                    //$('#tmm-products-bubble').css("height", height_min+'px');

                    // clean up any selected items:
                    $('.tmmp').removeClass('tmmp-hover');
                    $('#tmm-titles').find('.tmm-single').removeClass('tmm-single-selected');

                    $(this).addClass('tmm-single-selected');
                    
                    // show only the related bubble menu (below)
                    $('#'+bubble_id).show();

                    apply_to_menu_state(true);
                    
                    // set menu layer on top of top-texts area:
                    $('#tmm-wrap').css('z-index','99');
                    $('#top-container').css('z-index','1');

                });

                $('#tmm-wrap,.tmm-bubble').bind('mouseleave', function(){
                    
                    $('#tmm-titles').find('.tmm-single').removeClass('tmm-single-selected');

                    // hide all bubbles:
                    $('.tmm-bubble').hide();
                    $('.tmmp-subs').hide();
                    
                    // set top-texts over (now) inactive top-menu area:
                    $('#tmm-wrap').css('z-index','1');
                    $('#top-container').css('z-index','99');

                    apply_to_menu_state(false);
                });

               $('.tmmp').hover( function(){

                   $('.tmmp').removeClass('tmmp-hover');
                   $(this).addClass('tmmp-hover');
                   $('#tmmp-sub').show(); // todo: add scroll to right sub content
               });

               $('.tmmp').click( function(){
                   location.href = $(this).find('a:first').attr('href');
               });
              
    });
}

function apply_to_menu_state( state){

    switch( state ){

        case true   :   jungoapp.tmm_menu_active = true;
                        $('#index-banner-wrap').css('z-index','2');  // prevent possible index banners to overcloud active menu
                        break;

        case false  :   jungoapp.tmm_menu_active = false;
                        $('#index-banner-wrap').css('z-index','999');  // prevent possible index banners to overcloud active menu
                        break;
    }
}

function fill_search_src(){
    
  $("#fr").val( location.href );
  
}



 

