var page_title;

jQuery().ready(function() {
  init_tiny();

  jQuery('.apero_form select').live('change', update_dish_amount);
  jQuery('.apero_form select').each(update_dish_amount);

  // Page navigation
  jQuery('#menu a, #submenu a').live('click', function(){
    jQuery(this).parents('#menu, #submenu').children('li.selected').removeClass('selected');
    jQuery('#content').focus();

    jQuery(this).css({position:'relative'}).animate({
      left: jQuery(this).parent().width() - jQuery(this).width()
    }, 500, function(){
      jQuery(this).css({left:0}).parents('li').addClass('selected');
    });

    var path = jQuery(this).attr('href');

    jQuery.ajax({
      url: path + '.js',
      success: function(data){
        change_content(data, path);
      }
    });
    return false;
  });

  // add asset to tiny
  jQuery('.add_to_tinymce li').click(function(){
    var link = jQuery(this).attr('data-link');
    var type = jQuery(this).attr('data-type');
    var name = jQuery(this).attr('data-name');

    if(type == 'image'){
      add_image_to_tinymce(link);
    }else if(type == 'flash'){
      add_flash_to_tinymce(link);
    }else{
      add_document_to_tinymce(name, link)
    }
  });

  finish_page_show();
});

update_dish_amount = function(){
  var table = jQuery(this).parents('table');
  var amount = jQuery(this).attr('value');
  var price = parseFloat(jQuery(this).parents('tr').children('.price').html().replace('à CHF ', ''));
  var total = parseInt(amount) * parseFloat(price);

  jQuery(this).parents('tr').children('.total').html(total.toFixed(2));
  update_apero_amount();
}

update_apero_amount = function(){
  var amount = 0;
  jQuery('.apero_form .total').each(function(){
    amount += parseFloat(jQuery(this).html());
  });

  jQuery('.overall').html(amount.toFixed(2));
}

// tinyMCE
add_flash_to_tinymce = function(link) {
  new_flash = jQuery('<object width="100" height="100" data="'+link+'" type="application/x-shockwave-flash"><param name="src" value="'+link+'" /></object>');
  insert_html_to_tiny(new_flash);
}

add_image_to_tinymce = function(link) {
  new_img = jQuery('<img src=\'' + link + '\'/>');
  insert_html_to_tiny(new_img);
}

add_document_to_tinymce = function(name, link) {
  document_link = jQuery('<a href="'+ link +'" target="_blank"  >' + name + '</a>');
  insert_html_to_tiny(document_link);
}

function insert_html_to_tiny(element) {
  jQuery('#comp_content').tinymce().execCommand('mceInsertContent',false, jQuery('<div>').append(element).html());
}

// initialize TinyMCE
init_tiny = function(){
  jQuery('#comp_content, #comp_translations_attributes_0_content, #comp_translations_attributes_1_content').tinymce({
     theme : "advanced",
     plugins : "safari,style,preview,inlinepopups,media,paste,nonbreaking,xhtmlxtras,advimage",
     language: 'de',
     relative_urls: false,
     external_link_list_url: '/ls-R.js',
     auto_resize: true,

     theme_advanced_toolbar_location : "top",
     theme_advanced_toolbar_align : "left",

     theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifyfull,justifyright,formatselect,pastetext,link,unlink,hr,undo,redo,image,media",
     theme_advanced_buttons2 : "",
     theme_advanced_buttons3 : ""
  });
}

// Change the content on the frontend
change_content = function(data, path) {
  var container = jQuery('<div>' + data + '</div>');
  // fade menu and content
  if(container.children('#submenu, .big-box, .left-box').length > 0){
    jQuery('#submenu, .left-box, .right-box, .big-box').fadeOut(1000, function(){
      jQuery(this).remove();
      if(jQuery('#submenu, .left-box, .right-box, .big-box').length == 0){
        jQuery('#menu').after(data);
        jQuery('#submenu, .left-box, .right-box, .big-box').hide().fadeIn();
        document.title = page_title;
        finish_page_show();
        track_path(path);
      }
    });
  }else{
    // only fade content
    jQuery('.right-box').fadeOut(1000, function(){
      jQuery(this).remove();
      if(jQuery('.right-box').length == 0){
        jQuery('#submenu').after(data);
        jQuery('.right-box').hide().fadeIn();
        document.title = page_title;
        finish_page_show();
        track_path(path);
      }
    });
  }
}

// Finish up the fade in of submenu and content
finish_page_show = function(){
  // randomize background image on frontpage
  if(jQuery('.right-box').hasClass('startseite')){
    jQuery('.right-box').css({
      'background':'transparent url(/images/backgrounds/0_BG_rechts_bild0' + parseInt(Math.random() * 9 + 1) + '.jpg) no-repeat top left'
    });
  }
}

// track site for google analytics
track_path = function(path){
  if (typeof urchinTracker == 'function'){
    urchinTracker(path);
  }
}