/* 
 * This is the sambol js file
 */

//ad wait icon to page
function addWait() {

   if ($('#ajax_box').is('*')) {

    $('#ajax_box').append('<div id="wait_back"><div id="indicator">&nbsp;</div></div>');

   }else{

       $('body').append('<div id="wait_back"><div id="indicator">&nbsp;</div></div>');

       $('#wait_back').css({width: $(window).width()+' px', height: $(window).height()+' px'});
       

   }

}

function removeWait() {

   $('#wait_back').remove();

}


function checkActive() {

    if ($('input#active:checked').is('*')) {

        //item is active just make sure stuff set

        $('#theEditor').css('border', '1px solid black');
        $('#theEditor .alert').text('');
        $('#active_label').css({color: 'black', fontWeight: 'normal'});

    }else{
        //item not active allert status
        $('#theEditor').css('border', '1px solid red');
        $('#theEditor .alert').text('!!Page inactive, will not display.');
        $('#active_label').css({color: 'red', fontWeight: 'bold'});

    }

}

function statusUpdate(msg) {

    $('#status').text(msg).fadeIn('slow');

    setTimeout(function(){

            $('#status').fadeOut('slow', function () {
                
                $(this).text('');
                                
                
            });


}, 2500);

}


$(function() {

    $('#content #left').css({width:'600px', height: '244px'}).cycle({speed: 2000, timeout: 6000, delay: 500});

    var seg = window.location.pathname;

    //create active tab for current menu item
    $("a[href$="+seg+"]:not(.nogo)").css('background', '#c87219');

    if ($('#controlbar').is('*')) {

        $('.edit').each(function() {

               $(this).append('<div class="edit_icon">&nbsp</div>');

               //get keyword
               var component;

               var spID;

               if ($(this).hasClass('front')) {

                   component = 'front';
               }

               if ($(this).hasClass('dockitem') ) {

                   component = 'menu';

                   spID = this.id;
               }

               if ($(this).hasClass('menupage')) {

                    component = 'menu';

                    spID = this.id;

               }
               
               if ($(this).hasClass('page')) {

                   component = 'page';

                   spID = this.id;

               }

               var wWidth = $(document).width();
               var wHeight = $(document).height();

               $(this).find('.edit_icon')

               .hover(function() {

                    $(this).css('background', 'url(/rsc/img/edit_icon_roll.png)');

               },function () {

                   $(this).css('background', 'url(/rsc/img/edit_icon.png)');

               })
               .live('click', function() {

                  if (component == 'page') {

                       /*
                        Handle page editor here

                       */

                       addWait();
                       
                       $.post('/ajax.php', {'component':component, 'data':spID}, function(data) {

                            removeWait();

                            var RTeditor = 1;

                            $('.page').fadeOut('slow', function() {

                                $('#content').append(data);

                                $('#wysiwyg').wysiwyg();

                                checkActive();

                                $('input#save').bind('click', function() {

                                     var updatedContent = $('#wysiwyg').val();

                                     var titleVal = $('input#title').val();

                                     var activeVal = $('input#active:checked').val();

                                     $('#theEditor').remove();

                                     addWait();

                                     $.post('/ajax.php', {'component':component, 'data':spID, action: 'save', 'value': updatedContent, 'title': titleVal, 'active': activeVal, 'RTeditor': RTeditor}, function(data) {

                                          removeWait();

                                          $('.page .innerContent').html(data);

                                          $('.page').fadeIn('slow');

                                          checkActive();


                                     });


                                })

                                $('input#cancel').bind('click', function() {

                                    $('#theEditor').remove();

                                    $('.page').fadeIn('slow');


                                });


                                $('input#togle').bind('click', function() {

                                        if ($('.wysiwyg').is('*')) {
                                         
                                            /*
                                             *Editor is there so remove
                                             **/

                                            $('.wysiwyg').remove();
                                            $('#wysiwyg').show().autogrow();
                                            $('#togle').val('Editor');

                                            RTeditor = 0;

                                        }else{

                                            /*
                                             *Editor not there so ad it
                                             */
                                            $('#wysiwyg').wysiwyg();
                                            $('#togle').val('Edit html');

                                            RTeditor = 1;
                                        }

                                });

                            });

                       });


                  } else {


                          $.post('/ajax.php', {'component':component, 'data':spID}, function(data) {



                                $('body').append('<div id="block_out"></div>').append(data);

                                $('#block_out').css('height', wHeight);

                                var boxWidth =  $('#ajax_box').width();

                                $('#ajax_box').css('left', Math.round((wWidth - boxWidth)/2));


                                $('#close').hover (
                                    function() {$(this).css('border', '2px solid red')},
                                    function() {$(this).css('border', '2px solid black')})
                                    .bind('click',function() { $('#ajax_box, #block_out').remove();});

                                /*
                                 * Front Promo Editor Bindings
                                 *
                                 */
                                $('.promo_edit_item').each(function() {

                                    var theId = this.id.replace('id_', '');
                                    var theName = $(this).find('h3').text();
                                    var theString = $(this).find('textarea').val();
                                    var thisItem = $(this).find('.promo_item');

                                    $(this).find('input[type=submit]').bind('click', function() {

                                       var set = confirm('Set Front Page Promo to: '+theName);

                                       if(set) {

                                            addWait();

                                            $.post('/ajax.php', {'component':'setfront', 'id':theId}, function(d3) {

                                                 removeWait();

                                                 if (d3.status == 'ok') {

                                                    window.location = '/';


                                                 }

                                            }, 'json');



                                       }

                                       return false;


                                    });


                                    $(this).find('textarea').bind('focusout', function () {

                                            addWait();

                                            $.post('/ajax.php', {'component':'setfronttext', 'id':theId, 'text':$(this).val()}, function(d4) {

                                                 removeWait();

                                                 if (d4.status != 'fail') {

                                                    $(this).val(d4.status);

                                                    $(thisItem).html(d4.obj);

                                                     statusUpdate('Item id '+theId+' updated.');

                                                 }else{

                                                      $(this).empty();

                                                 }

                                            }, 'json');

                                        return false;

                                    });

                                     $(this).find('[name=link]').bind('focusout', function () {

                                            addWait();

                                            $.post('/ajax.php', {'component':'setfrontlink', 'id':theId, 'text':$(this).val()}, function(d5) {

                                                 removeWait();

                                                 if (d5.status != 'fail') {

                                                    $(this).val(d5.status);

                                                    statusUpdate('Item id '+theId+' updated.');

                                                 }else{

                                                      $(this).empty();

                                                 }

                                            }, 'json');

                                        return false;

                                    });



                                });

                                 /*
                                  * Menu items section
                                  *
                                  */

                                 $('#add').bind('click', function() {

                                     addWait();

                                     $.post('/ajax.php', {'component':'menu', 'data':spID, 'what': 'addrow', 'menu':'M'}, function(data) {

                                            removeWait();

                                            statusUpdate('Item added.');

                                            $('#no_items').remove();

                                            $('table').append(data);

                                     }, 'html');

                                 })

                                 //delete button
                                 $('.del').live('click', function() {

                                     var theId = $(this).parent().parent().attr('id');

                                     var conf = confirm('Delete this Item? (id: '+theId+')');

                                     if (conf) {

                                         addWait();

                                         $.post('/ajax.php', {'component':'menu', 'data':spID, 'what': 'delrow', 'menu':'M', 'id': theId}, function(data) {

                                            removeWait();

                                           if (data.result == 1) {

                                                statusUpdate('Item '+theId+' deleted.');

                                                $('#'+theId).remove();

                                            }


                                         }, 'json');


                                     }


                                 });


                                 //front promo add button
                                 $('#promoAdd').bind('click', function() {

                                     alert('not implemented');

                                 })

                                 //handle the field
                                 $('input').live('focusout', function () {

                                        var thisClass = $(this).attr('class');
                                        var theId = $(this).parent().parent().attr('id');
                                        var theValue = $(this).val();

                                        if ($(this).attr('type') == 'checkbox' ) {

                                           theValue = ($(this).attr('checked')) ? '1': 'deactivate';

                                        }

                                        if (theValue) {

                                            addWait();

                                             $.post('/ajax.php', {'component':'menu', 'data':spID, 'what': 'editrow', 'field':thisClass, 'menu':'M', 'value':theValue, 'id': theId}, function(data) {

                                                removeWait();

                                                if (data.result == 1) {

                                                    statusUpdate('Item '+theId+' updated. '+thisClass);

                                                }


                                             }, 'json');

                                        }

                                 });



                            }, 'html');

                    }

               });


        });

    }

    $('#menuDock .dockitem .back, #general_menu .dockitem .back').hover (

      function() {
        $(this).css({background: 'gray'});

      },

      function() {
        $(this).css({background: '#ED9030'});
      }

    );

    $('#general_menu .dockitem span').each(function() {

        var h = $(this).outerHeight();

        $(this).css({marginTop: '-'+h+'px', top: h+3});

    });

    var wrapperWidth = $('#wrapper').width();

    var ieOffset = (navigator.appName == 'Microsoft Internet Explorer') ? 60 : 0;

    var genWidth = Math.round(($('#general_menu .dockitem').outerWidth(true) * 4)+ieOffset);


    $('#general_menu').css({width: genWidth, marginLeft: Math.round(((wrapperWidth - genWidth) /2)-10)});


    var menuWidth = Math.round(($('#menu .menu_item').outerWidth(true) * 2) + 10);

    $('#menu').css('width', menuWidth);

    /*
     * Ad the grad to lunch page
     *
     */
    $('#content:has(#lunch)').css({backgroundImage: 'url(/rsc/img/light-grad.jpg)', backgroundRepeat: 'repeat-x'});

    $('#mapSidebar a').attr('target', '_blank');

    //fix lunch menu table in IE
    $('table#tblMain').attr('align', 'center');

    $('#menuDock .arrowRight').hover(function(){

                             $(this).css({background: 'url(/rsc/img/arB_roll.png)', backgroundRepeat: 'no-repeat', backgroundPosition: 'center'});

                          },
                          function(){

                              $(this).css({background: 'url(/rsc/img/arB.png)', backgroundRepeat: 'no-repeat', backgroundPosition: 'center'});

                          });


    $('#menuDock .arrowLeft').hover(function(){

                         $(this).css({background: 'url(/rsc/img/alB_roll.png)', backgroundRepeat: 'no-repeat', backgroundPosition: 'center'});

                      },
                      function(){

                          $(this).css({background: 'url(/rsc/img/alB.png)', backgroundRepeat: 'no-repeat', backgroundPosition: 'center'});

                      });

    $('ul.list').find('span').each(function () {

           if ($(this).text().length > 18) {

               $(this).css({letterSpacing: '-1px'});

           }

    });


    $('.list, #general_menu').find('.back').each(function () {
    
        $(this).after('<div class="drop_shad">&nbsp;</div>');

    });



});