var $ = jQuery
var facebookUrl = "http://www.facebook.com/pages/Sanctuary-of-Asclepios/355004946751";
var contactUsUrl = "inquires@sanctuaryofasclepios.org";

$(function(){
  $("#slides").slides({
    preload: true,
    preloadImage: 'imgs/loading.gif',
    play: 10000,
    pause: 2500,
    hoverPause: true,
    fadeSpeed: 700,
    effect: 'fade'
  });
});

function addNavigation(containerElement) {
  $(containerElement).append('<ul>');
  var current = document.location + "";
  $('ul', containerElement).append('<li><a href="index.html" ' + (current.indexOf('index') > 0 ? 'class="selected"' : "") + '>Home</a></li>');  
  $('ul', containerElement).append('<li><a href="about-us.html" ' + (current.indexOf('about-us') > 0 ? 'class="selected"' : "") + '>About Us</a></li>');
  $('ul', containerElement).append('<li><a href="meditation.html" ' + (current.indexOf('meditation') > 0 ? 'class="selected"' : "") + '>Meditation</a></li>');
  $('ul', containerElement).append('<li><a href="workshops.html" ' + (current.indexOf('workshops') > 0 ? 'class="selected"' : "") + '>Workshops</a></li>');
  $('ul', containerElement).append('<li><a href="therapy.html" ' + (current.indexOf('therapy') > 0 ? 'class="selected"' : "") + '>Therapy</a></li>');
  $('ul', containerElement).append('<li><a href="energy-work.html" ' + (current.indexOf('energy-work') > 0 ? 'class="selected"' : "") + '>Energy Work</a></li>');
  $('ul', containerElement).append('<li><a href="events.html" ' + (current.indexOf('events') > 0 ? 'class="selected"' : "") + '>Events</a></li>');
  $('ul', containerElement).append('<li><a href="http://rzwrp.deunc.servertrust.com" target="shop">Shop</a></li>');
  $('ul', containerElement).append('<li><a href="contact-us.html" ' + (current.indexOf('contact-us') > 0 ? 'class="selected"' : "") + '>Contact Us</a></li>');
}

function addFacebook(containerElement) {
  $(containerElement).append('<a href="' + facebookUrl + '" target="facebook">Like Us on Facebook</a>')
}

function addContactUs(containerElement) {
  $(containerElement).append('<a href="contact-us.html">Contact Us</a>')
}

function addQuotation(containerElement) {
  var quotations = new Array();
  quotations[0] = '<div>"Success is not a future state."</div> - Eckhart Tolle';
  quotations[1] = '<div>"If it is peace you want, seek to change yourself, not other people. It is easier to protect your feet with slippers than to carpet the whole of the earth."</div> - Anthony de Mello';
  quotations[2] = '<div>"What\'s the world\'s greatest lie?" the boy asked, completely surprised. "It\'s this: that at a certain point in our lives, we lose control of what\'s happening to us, and our lives become controlled by fate. That\'s the world\'s greatest lie." the man replied. </div>- The Alchemist, Paulo Coelho';
  quotations[3] = '<div>" There is no such place as heaven. Heaven is not a place and it is not a time. Heaven is being perfect"</div> - Jonathan Livingston Seagull<br> - Richard Bach';
  quotations[4] = '<div>"When one keeps dwelling on the faults of others, his own compulsions grow worse, making it harder to overcome them."</div> - Dhammapada';
  quotations[5] = '<div>"To be in the presence of another\'s shadow and not reply is nothing short of genius. No one has the right to dump their shadow on you, and you have the right to self protection."</div> - Robert Johnson<br>- Owning Your Own Shadow';
  quotations[6] = '<div>"Tell your heart that the fear of suffering is worse than the suffering itself. And no heart has ever suffered when it goes in search of its dreams, because every second of the search is a second\'s encounter with God and the eternity."</div> - The Alchemist, Paulo Coelho.';
  quotations[7] = '<div>"Part of waking up is that you live your life as you see fit. And understand: That is NOT selfish. The selfish thing is to demand that someone else live their life as YOU see fit. THAT\'S selfish!"</div> - Anthony de Mello';
  quotations[8] = '<div>"Miracles occur naturally as expressions of love. The real miracle is the love that inspires them. In this sense everything that comes from love is a miracle."</div> - Course in Miracles';
  quotations[9] = '<div>"Miracles are healing because they supply a lack; They are performed by those who temporarily have more for those who temporarily have less."</div> - Course in Miracles';
  quotations[10] = '<div>"Everyone is made up of three people: The person we think that we are, the person who others think that we are, the person we actually are"</div> - Sai Baba';
  quotations[11] = '<div>"Driven by fear, people run for security to mountain and forests, to sacred spots and shrines. But none of these can be a safe refuge, because they cannot free the mind from fear"</div> - The Awakened One, Dhammapada';
  quotations[12] = '<div>"Find out what a person fears the most, that is where they will develop next."</div> - Carl Jung';
  quotations[13] = '<div>"You don\'t chase darkness out o a room with a broom, you turn on the light."</div> - Anthony de Mello';
  quotations[14] = '<div>"The repair of our fractured world must start with individuals who have the insight and courage to own their own shadow"</div> - Robert Johnson';
  quotations[15] = '<div>"My house, my body, my car, my school, my, my, my. But who is this "my"? Who am "I"? He never asks that question."</div> - Sai Baba';
  quotations[16] = '<div>"Awareness is alteration in your head. Action is alteration in your heart. Change is how you manifest that in the world."</div> - Paul Miller';
  quotations[17] = '<div>"Belief is building your house on quicksand. Knowing is building it on rock."</div> - Paul Miller';
  quotations[18] = '<div>"The resilience of your inner peace is directly related to the depth of your spirituality."</div> - Paul Miller';
  quotations[19] = '<div>"Never try to force your mind. It will respond easily to tenderness and patient training."</div> - Sai Baba';
  
  var randomnumber=Math.floor(Math.random()*19)
  $(containerElement).append(quotations[randomnumber]);
}

function addFooter(containerElement) {
//  $(containerElement).append('<ul>');
  addNavigation(containerElement);
}

function loadBlog(containerElement) {
  parseRSS('http://paulatsoa.wordpress.com/feed/', function(data) {
    var entries = data.entries;
    if (entries != undefined) {
      $(containerElement).append('<li><a href="' + entries[0].link + '" target="blog">' + entries[0].title + '</a></li>');
      $(containerElement).append('<li><a href="' + entries[1].link + '" target="blog">' + entries[1].title + '</a></li>');
      $(containerElement).append('<li><a href="' + entries[2].link + '" target="blog">' + entries[2].title + '</a></li>');
      $(containerElement).append('<li><a href="' + entries[3].link + '" target="blog">' + entries[3].title + '</a></li>');
      $(containerElement).append('<li><a href="' + entries[4].link + '" target="blog">' + entries[4].title + '</a></li>');
      $(containerElement).append('<li><a href="' + entries[5].link + '" target="blog">' + entries[5].title + '</a></li>');
    }
  });
}

function showSubSection(sectionId, selectedLink) {
  jQuery('.subsection.selected').hide();
  jQuery('.subsection').removeClass('selected');
  jQuery('#'+ sectionId).addClass('selected');
  jQuery('#'+ sectionId).show();
  
  jQuery('.showSubSection').removeClass('selected-nav');
  jQuery('#'+ selectedLink).addClass('selected-nav');
}

function parseRSS(url, callback) {
  $.ajax({
    url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
    dataType: 'json',
    success: function(data) {
      callback(data.responseData.feed);
    }
  });
}

function addRightSubNav() {

  jQuery('#right').append('<a href="testimonials.html"><div id="testimonials">Testimonials</div></a>');
  jQuery('#right').append('<a href="http://paulatsoa.wordpress.com/" target="blog"><div id="blog">Read our Blog</div></a>')
  jQuery('#right').append('<a href="http://rzwrp.deunc.servertrust.com/category_s/1818.htm" target="shop"><div id="prints">Inspirational Prints</div></a>');
 // jQuery('#right').append('<a href="forms.html"><div id="forms">Download Forms</div></a>');
  
}

$(document).ready(function() {
  addNavigation($('#navigation'));
  addFacebook($('#facebook'));
  addContactUs($('#contact-us'));  
  addRightSubNav();
  addFooter($('#footer'));
  
  loadBlog($('#articles'));
  
  /* Are we preloading a subsection? */
  var preload = getUrlParam("section")
  if (preload.length > 0) {
    showSubSection(preload, preload + '-link');
  }
  
  $('.showSubSection').click(function(e) {
    var selected_id = this.id;
    var id = this.id;
    id = id.replace(/-link[0-9a-z]*/,'');
    showSubSection(id,selected_id);
  });
  
});

function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

