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

var jungoapp = {

  'lang' : 'en',
  'phrases': {}    // will be filled dynamically
};


$(document).ready( function(){

    
    // prevent no firebug support errors when calling console.log in code:
    if(typeof console === "undefined") {  
    console = {log: function() { }};
    }
    
    // update language used, if set by url:
    if( $(document).getUrlParam("lang") )  
      jungoapp.lang =  $(document).getUrlParam("lang");    

   
    $("ul.sf-menu").supersubs({ 
        minWidth:    16,   // minimum width of sub-menus in em units 
        maxWidth:    62,   // maximum width of sub-menus in em units 
        extraWidth:  1,    // extra width can ensure lines don't sometimes turn over 
                           // due to slight rounding differences and font-family 
        speed:       0,
        delay:       0,
        dropShadows: false

    }).superfish();  // call supersubs first, then superfish, so that subs are 
                     // not display:none when measuring. Call before initialising 
                     // containing tabs for same reason. 
   
    
    //activate "accordion" navs: slides the element with class "menu_body" when paragraph with class "menu_head" is clicked
    $("#acco-set p.acco-title").click(function()
    {
        $("#context-bar-content").css("border-bottom", "1px solid white"); // while expanding seperate from surroding text 
        $(this).next("div.acco-content").slideToggle(300).siblings("div.acco-content").slideUp("slow");
    });
    
    
    $("#context-bar").bind("mouseover",function(){
        
        //$("#context-bar").css("background-color","#F2F2F2");
        //$("#context-bar-content").css("background-color","#F2F2F2");
        $("#context-expanding").css("background-color","white");
        
        $(".acco-title").addClass("with-down-arrow-dark");
        
        $("#context-fix-corner").show();
    });
    
    $("#context-bar").bind("mouseleave",function(){
        
        $("#acco-set div.acco-content").hide("fast");
        
        $("#context-bar").css("background-color","transparent");
        $("#context-bar-content").css("background-color","transparent").css("border-bottom", "none");
        $("#context-expanding").css("background-color","transparent");
        
        $(".acco-title").removeClass("with-down-arrow-dark");
        $(".acco-title").removeClass("with-down-arrow-blue").css('font-weight','normal');
        
        $("#context-fix-corner").hide();
        
    });
    
    
    $(".acco-title").bind("mouseover",function(){
    
      $(".acco-title").removeClass("with-down-arrow-blue").css('font-weight','normal');
      $(this).addClass("with-down-arrow-blue").css('font-weight','bold');
    });
    
    $('#current-lang-display').bind("mouseover",function(){
          $('#language-select').show();
    });
    
    $('#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.

/*
    $('.minip').corner();    
    $('.minip2').corner();
    $('.docnote').corner();
    $('.mini-wide').corner();
*/
	$('.admin-actions').parent().hover( 
                                            function(){$(this).children('.admin-actions').show();console.log( $(this).attr('id'));}, 
                                            function(){$(this).children('.admin-actions').hide();}    
    );
    
    bind_editable_blocks_control();
    
    $('#clevercontext-mini.is-expandable').bind("mouseenter",function(){
          $('#clevercontext-max').show('fast');
    });
    
    $('#clevercontext-max').bind("mouseleave",function(){
          $('#clevercontext-max').hide();
    });
    
    
    
    
    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 bind_editable_blocks_control( div_scope ){
    
    var apply_on = div_scope ? '#'+div_scope+' a.editable-block-link' : 'a.editable-block-link';

    $( apply_on ).unbind('click').click( function(){

            var calling_block = $(this).parents('.editable-block').attr('id'); // so we scroll back to, when finished...
            
            gb_modal_show( $(this).attr("href"), calling_block );
            
            return false; // don't follow the default link href's
    });
    
    
    
    $('.editable-block').hover(
                                function(){$(this).find('.editable-controls').addClass('visible');},
                                function(){$(this).find('.editable-controls').removeClass('visible');}
                                );
    
}


function approve_editable_block( mblock_id ){

    $.ajax(
            {url : base_path_url+'st/do/admin/gridibase/_mblock_approve.php',
                type: "POST",
                data : {id : mblock_id},
                complete: function(){
                            apply_editable_block_state_change(mblock_id, 'approved');

                        }
            });

}


function editable_block_histroy( mblock_id ){

    var histroy = $('#gbm-revision-wrap');

    histroy.toggle();

    hisotry_visible = histroy.css('display')==='block';

    if( hisotry_visible ){

        // don't reload content???

        $('#gbm-revision-wrap').load(   base_path_url+'st/do/admin/gridibase/_block_history_browser.php',
                                    { id : mblock_id },
                                    function(){

                                    });

        $('#gbm-editing-container').hide(); //hide editor



    }
    else{

        $('#gbm-editing-container').show();
    }

}


function apply_editable_block_state_change( mblock_id, new_state ){

    var block = $('#mblock-'+mblock_id);
    
    var approve_link = block.find('.approve-link');

    

    //console.log('should be updated..');

    if( new_state=='approved' ){
            approve_link.hide();

            block.animate( {opacity : 0}, 400, function(){

                block.removeClass('editable-block-draft');
                block.animate( {opacity : 1}, 200 );
            });
            //block.removeClass('editable-block-draft');
    }
    else
    if( new_state=='modified' ){
            approve_link.show();
            block.addClass('editable-block-draft');

    }

}


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




 
