var IE = /*@cc_on!@*/false;

var ilikes = [
  'Pumpkins', 
  'Hairy legs', 
  'Spending hours at the computer', 
  'Stinking out the house', 
  'Romantic walks on the beach', 
  'Hotted up cars', 
  'Cats', 
  'Stamp collecting', 
  '2 Siamese fighting fish, one bowl', 
  'The beach', 
  'Postmodern punk anthems', 
  'Algebra', 
  'Donuts', 
  'Rocks', 
  'Freeloading', 
  'Sleeping', 
  'Raincoats', 
  'Circus clowns', 
  'Mid 17th Century poetry', 
  'Medieval role playing', 
  'Star wars (original, not digitally remastered)', 
  'Head boppers', 
  'Fluffy toys', 
  'Facial fuzz', 
  'Acne', 
  'Horses', 
  'Short men', 
  'Strong coffee', 
  'chocolate', 
  'Pink', 
  'Spanners', 
  'Hand ball', 
  'Things that change colour', 
  'Mini-ponies', 
  'Punk rock', 
  'Armpits', 
  'Mono-brows', 
  'Sugar', 
  'Bloomers', 
  'Neptune', 
  'Changes to subsection 2b of the rugby league rules', 
  'Burn-outs', 
  'Deidre (she’s hot)', 
  'Off-cuts', 
  'Pinking shears', 
  'Sponge Bob Square Pants', 
  'Crochet ', 
  'Loud music', 
  'Origami', 
  'Black eye make-up', 
  'Counting cars', 
  'Air', 
  'Posing', 
  'Looking grumpy', 
  'One word answers', 
  'Derrick (he’s hot)', 
  'Jumping things', 
  'Board games', 
  'Surfing', 
  'Sport', 
  'Travel', 
  'Wednesday', 
  'Water', 
  'Bagpipes', 
  'Staying home', 
  'Going out', 
  'Sleeping in', 
  'Waking up early', 
  'Rockets', 
  'Conceptual art', 
  'Interpretive dance', 
  'Elevator music', 
  'Mime', 
  'Mr. Miagi', 
  'Rainbows', 
  'Love songs (from the 80s)', 
  'Love songs in general', 
  'Pirates', 
  'Individual cheese slices', 
  'Ham', 
  'Pies', 
  'Sandals', 
  'Cooking', 
  'Parties', 
  'Reading', 
  'Dancing', 
  'Laughing', 
  'Shopping trolleys', 
  'Hanging out', 
  'Looking disinterested'
];

var imusics = [
  'Rock',
  'Pop',
  'Rock/pop',
  'Emo',
  'Screamo',
  'Tropicana',
  'Death metal',
  'Punk',
  'Funk',
  'Soul',
  'Hip hop',
  'Metal',
  'Black metal',
  'Metal Metal Metal',
  'Disco',
  'Dance',
  'Jazz',
  'Experimental',
  'Polka',
  'Mass rock',
  'Alternative country',
  'Americana',
  'Folk',
  'Blues',
  'Christian',
  'Roots',
  'New age',
  'Acoustic',
  'Acid house',
  'Garage',
  'Fruity',
  'Indy',
  'Crap',
  'Musak',
  'Soundscape',
  'Reggae',
  'Hardcore',
  'Slowcore',
  'No-core',
  'Hawaiian',
  'Fashion Rock (all about the clothes, not the music)',
  'Bolivian Underground',
  'Gypsy punk',
  'Lip synch',
  'Covers'
];

var options = { 
  target:        'div#post',   // target element(s) to be updated with server response 
  beforeSubmit:  showRequest,  // pre-submit callback 
  success:       showResponse  // post-submit callback 

  // other available options: 
  //url:       url         // override for form's 'action' attribute 
  //type:      type        // 'get' or 'post', override for form's 'method' attribute 
  //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
  //clearForm: true        // clear all form fields after successful submit 
  //resetForm: true        // reset the form after successful submit 

  // $.ajax options can be used here too, for example: 
  //timeout:   3000 
}; 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
} 




function pageLoad(hash) {
  // hash doesn't contain the first # character.
  if(hash) {
    // restore ajax loaded state
    if($.browser.msie) {
      // jquery's $.load() function does't work when hash include special characters like åäö.
      hash = encodeURIComponent(hash);
    }
    
    $('div#loading').css({ display: 'block' });
    $.sound.play('assets/sounds/BAUcompu-SuperBAU-7699_hifi.mp3');
    $('div#main div.inside').fadeOut('normal');
    $('div#right div.inside').fadeOut('normal');
    $('div#nav').load(hash + ' div#nav div.mod_customnav');
    $('div#main').load(hash + ' div#main div.inside');
    $('div#right').load(hash + ' div#right div.inside', function() {
      $('div#loading').css({ display: 'none' }); 
      gherkin.prepInterface();
    });
    
  } else {
    // start page
  }
}

var gherkin = {
  
  prepInterface : function() {
    gherkin.prepNavigation();
    gherkin.prepFormUI();
    gherkin.prepScrollables();
    gherkin.prepFrontendAdmin();
    gherkin.prepTinyMCE();
    gherkin.prepLoginBox();
    gherkin.deletePost();
    gherkin.deleteEvent();
  }
  
  ,prepNavigation : function() {
    if (IE) {
      
    } else {
     $('a').live('click', function(evt) {
        // only left click
       // if (evt.button != ($.browser.msie ? 1 : 0) || evt.ctrlKey || evt.shiftKey || evt.altKey) return;
        if (!$(this).hasClass('highslide') && !$(this).hasClass('external') && !$(this).hasClass('mceButton') && !$(this).hasClass('link') && !$(this).hasClass('stbutton')) {
          var hash = $(this).attr('href');
          hash = hash.replace(/^.*#/, '');
          hash = hash.replace(the_base, '');
          $.historyLoad(hash);
          return false;
        }
      }); 
    }
  }
  ,prepLoginBox : function() {
    $('div#header input.submit').attr({ value: ' ' });
  }
  ,prepFormUI : function() {
    // input boxes
    $('input').click(function() { $(this).select(); });
    
    // signup page
    // company = I LIKE
    if ($("label[for='ctrl_company']").length > 0) { $("label[for='ctrl_company']").html("I like"); }
    $('input#ctrl_company').parent().html("<select id='ctrl_company' name='company'></select><p>Check your public profile to see your changes!</p>");
    $.each(ilikes,
      function(intIndex, objValue) {
        $('select#ctrl_company').append("<option value='" + objValue + "'>" + objValue + "</option>");
      }
    );
    
    // street + website = BAND STUFF
    if ($("label[for='ctrl_street']").length > 0) { $("label[for='ctrl_street']").html("Are you a musician or in a band? If so what are you called?"); }
    $('input#ctrl_street').keyup(function() {
      var genre = $($('div.row_12').get(0));
      if (jQuery.trim($(this).val()) != '') {
        genre.fadeIn('slow');
      } else {
        genre.fadeOut('slow');
      }
    });
    if ($("label[for='ctrl_website']").length > 0) { $("label[for='ctrl_website']").html("What genre best describes you?"); }
    $('input#ctrl_website').parent().html("<select id='ctrl_website' name='website'></select>");
    $.each(imusics,
      function(intIndex, objValue) {
        $('select#ctrl_website').append("<option value='" + objValue + "'>" + objValue + "</option>");
      }
    );
    //$('input#ctrl_street').parent().html("");
    
    // street = copy?
    //$('tr.row_9').css({ display: 'none' });
    // security question = maths
    $("label[for='ctrl_registration']").html("To make sure you're not a robot");
    
    // form validation: signup (sign-up.html)
    if ($('form#signup_request_form').length > 0) {
      $('form#signup_request_form').submit(function() {
        // check toc's
        if ($('input#tou_tick:checked').length == 0) {
          $('p#accept_it').css({
            display: 'block'
          });
          return false;
        }
        // fix birthday
        var dob = $('input#ctrl_dateOfBirth').val();
        //alert(dob);
        dob = dob.substring(dob.length - 4);
        //alert(dob);
        $('input#ctrl_dateOfBirth').val('01/01/' + dob);
      });
    }
        
    
    // public fields checkboxes
    if ($('input#opt_publicFields_0').length > 0) {
      document.getElementById('opt_publicFields_0').checked = true; // First Name
      document.getElementById('opt_publicFields_4').checked = true; // Company
    }
  }
  
  ,prepFrontendAdmin : function() {
    if ($("input.approve").length > 0 || $("input.member_approve").length > 0) {
      $("input.approve").click(function() {
        var parent_post = $(this).parent().parent().parent().parent().parent().parent().parent().parent();
        $.get("/custom/admin.php", { postId: this.id.replace('approve', ''), approve: 1 },
          function(data){
            $(parent_post).fadeOut('slow');
            alert('This post has been approved');
          }
        );
      });
      $("input.deny_confirm").click(function() {
        var parent_post = $(this).parent().parent().parent().parent().parent().parent();
        $.get("/custom/admin.php", { postId: this.id.replace('deny', ''), deny: 1,  email_address: $(this).attr('alt') },
          function(data){
            $(parent_post).fadeOut('slow');
            alert('This post has been Denied');
          }
        );
      });
      $("input.member_approve").click(function() {
        var parent_post = $(this).parent().parent().parent().parent().parent().parent();
        $.get("/custom/admin.php", { memberId: this.id.replace('member_approve', ''), member_approve: 1, email_address: $(this).attr('alt') },
          function(data){
            $(parent_post).fadeOut('slow');
            alert('Member has been approved');
          }
        );
      });
      $("input.member_deny_confirm").click(function() {
        var parent_post = $(this).parent().parent().parent().parent().parent().parent();
        $.get("/custom/admin.php", { memberId: this.id.replace('deny', ''), member_deny: 1 },
          function(data){
            $(parent_post).fadeOut('slow');
            alert('Member has been Denied');
          }
        );
      });
    };
  }
  
  ,deletePost : function() {
    $("input.delete").click(function() {
      var parent_post = $(this).parent().parent()
      $.get("/custom/admin.php", { postId: this.id.replace('deleted', ''), post_deleted: 1 },
        function(data){
          $(parent_post).fadeOut('slow');
          alert('This post has been deleted');
        }
      );
    });
  }
  
  ,deleteEvent : function() {
    $("input.delete_events").click(function() {
      var parent_post = $(this).parent().parent()
      $.get("/custom/admin.php", { eventId: this.id.replace('deleted', ''), event_deleted: 1 },
        function(data){
          $(parent_post).fadeOut('slow');
          alert('This event has been deleted');
        }
      );
    });
  }
  
  ,prepScrollables : function() {
     // scrollable: posts
    $("div#post").scrollable({
      size: 1, 
      items: '.inner', 
      navi: '#tabs'
    });
    
    // scrollable: events sidebar
    $('div.right_button').click(function() {
      $('div.right_button').removeClass('disable');
      $('div.left_button').removeClass('disable');
      if (parseInt($('div.upcoming_events').css('left')) > -(380 * ($('div.upcoming_events div.black_bg').length-1))) {
        $('div.upcoming_events').animate({ 
          left: (parseInt($('div.upcoming_events').css('left')) - 380) + "px"
        }, 300, function() { if (parseInt($('div.upcoming_events').css('left')) > -(380 * ($('div.upcoming_events div.black_bg').length-1))) {} else { $('div.right_button').addClass('disable'); } });
      } else {
        $('div.right_button').addClass('disable');
      }
    });
    $('div.left_button').click(function() {
      $('div.right_button').removeClass('disable');
      $('div.left_button').removeClass('disable');
      if (parseInt($('div.upcoming_events').css('left')) != 0) {
        $('div.upcoming_events').animate({ 
          left: (parseInt($('div.upcoming_events').css('left')) + 380) + "px"
        }, 300, function() { if (parseInt($('div.upcoming_events').css('left')) != 0) {} else { $('div.left_button').addClass('disable'); } });
      } else {
        $('div.left_button').addClass('disable');
      }
    });
  }
    
  ,prepTinyMCE : function() {
    /*tinyMCE.init({
      mode : "textareas",
      theme : "simple"
    });*/
    /*tinyMCE.init({
      mode : "textareas",
      theme : "advanced",
      theme_advanced_buttons1 : "bold,italic,underline,link,unlink,bullist,blockquote,undo", 
      theme_advanced_buttons2 : "", 
      theme_advanced_buttons3 : "", 
      skin : "o2k7"
    });*/
  }
  
}

$(function() {
  $.historyInit(pageLoad, "");
  gherkin.prepInterface();
});
