/* --- Navigation --- */
function load_nav(){
   
   // Animation and click functions
   jQuery("#nav .main-menu .menu li").hover(
      function(){
         jQuery(this).find("> ul").fadeOut(0).stop(true, true).fadeIn(500);
      },
      function(){
         jQuery(this).find("> ul").hide();
      }).click(function(){
            
      // Clear the taxonomy cookie
      jQuery.cookie('taxonomy', '');
      
   });
     
   // Append the parent class to elements that have a submenu
   jQuery("#nav .main-menu .menu li").each(function(){
      if (jQuery(this).find(".sub-menu").length > 0) jQuery(this).addClass("parent");
   });
   
   // Append the Cufon class to top items only
   //jQuery("#nav .main-menu .menu > li").addClass("cufon");
   
   // -- Append the curl shape to the submenus
   jQuery('#nav .main-menu .menu li.parent > a').append("<span class='curl'></span>");
   
}

/* --- Subheader --- */
function load_subheader(){
  
   // Portfolio taxonomy filter
   jQuery("#terms .term").click(function(){

      // Get the taxonomy
      var tax = jQuery(this).attr("data-tax");
      
      // Save it in a cookie
      jQuery.cookie('taxonomy', tax);
      
      // Refresh the page
      window.location.reload()
      
      return false;
      
   });
   
}
  
/* --- Main --- */
function load_main(){

   // -- Widget Lists (Append the curl shape to widget list items in the sidebar)
   jQuery('#main .widget_archive ul li a, #main .widget_categories ul li a, #main .widget_links ul li a, #main .widget_nav_menu ul li a, #main .widget_meta ul li a, #main .widget_pages ul li a, #main .widget_recent_entries ul li, #main .widget_rss ul li a').prepend("<span class='curl'><span class='inner curl'></span></span>");
   
   // -- More-Link(Append the curl shape)
   jQuery('#main .post-entry .more-link .curl').remove();
   jQuery('#main .post-entry .more-link').prepend("<span class='curl'><span class='inner curl'></span></span>");
   
   // -- 'Button' class append to #commentform submit
   jQuery('#commentform .form-submit #submit').addClass("button");
   
   // -- Flicker Widget
   jQuery(' .widget_flickr > div a').each(function(){
      
      // Get img src
      var src = "" + jQuery(this).find("img").attr("src");   
      
      // Replace to point to the original size
      src = src.replace("_s.jpg", ".jpg");
      src = src.replace("_s.gif", ".gif");
      src = src.replace("_s.png", ".png");

      // Replace href with img src
      jQuery(this).addClass("lb").addClass("image").attr("href", src).attr("data-gallery" , "group-flickr").wrap("<div class='frame'></div>");
      
      // Add borders
      jQuery(this).parents(".frame").prepend("<span class='border top small'></span><span class='border left small'></span><span class='border bottom small'></span><span class='border right small'></span>");

   });
   
   // -- Adjust jPlayer progress bar width
  jPlayerW();
   
   // -- Load More
   var loadMoreAvailable = true;
   jQuery("#load-more").click(function(){
      
      var post_data = {"post-type": jQuery(this).attr("data-post-type"), "items-per-page": jQuery(this).attr("data-items-per-page"), "layout": jQuery(this).attr("data-layout"), "excerpt-limit": jQuery(this).attr("data-excerpt-limit"), "more-link-keyword": jQuery(this).attr("data-more-link"), "hide-more-link": jQuery(this).attr("data-hide-more-link"), "counter": jQuery(this).attr("data-counter"), "taxonomy": jQuery(this).attr("data-tax"), "seed": jQuery(this).attr("data-seed")}
      
      // Change text
      var text = jQuery("#load-more span").html();
      jQuery("#load-more span").html(jQuery("#load-more").attr("data-loading")).addClass("preload");  

      //Ajax magic starts here...
       if (loadMoreAvailable){
         loadMoreAvailable = false;
         jQuery.ajax({
            type: 'POST', url: templateDir + '/includes/loop-get-posts.php', data: post_data, 
            success: function(data) {

               // Append the new data
               if (!jQuery.browser.msie){
                  var new_data = "<div id='temp-data' class='hidden'>" + data + "</div>";
                  jQuery("#main .entries").append(new_data); 
  
               } else {
                  jQuery("#main .entries").append(data); 
               }
               
               // Reappend more text
               function f(){
                  jQuery('#temp-data').fadeIn().removeAttr('id').removeClass("hidden");
                  jQuery("#load-more span").html(text).removeClass("preload");
                  if (data.length < 300) jQuery("#load-more span").html(jQuery("#load-more").attr("data-end")).removeClass("preload");
               }
               
               setTimeout(f, 1000);

               // Update the clear div
               jQuery("#main .entries > .clear").remove(); 
               jQuery("#main .entries").append("<div class='clear'></div>");
            
               // -- More-Link(Append the curl shape)
               jQuery('#main .post-entry .more-link .curl').remove();
               jQuery('#main .post-entry .more-link').prepend("<span class='curl'><span class='inner curl'></span></span>");
               
               // -- Adjust jPlayer progress bar width
               jPlayerW();
            
               // Reinit jQuery Functions
               lightbox();
               
               // Can press the button again
               loadMoreAvailable = true;

            }

         }); //End Ajax magic
      }
   });
   
   
}

/* --- Footer --- */
function load_footer(){
   
   // -- Widget Lists (Append the curl shape to widget list items in the footer)
   jQuery('#footer .widget_archive ul li a, #footer .widget_categories ul li a, #footer .widget_links ul li a, #footer .widget_nav_menu ul li a, #footer .widget_meta ul li a, #footer .widget_pages ul li a, #footer .widget_recent_entries ul li, #footer .widget_rss ul li a').prepend("<span class='curl'><span class='inner curl'></span></span>");

   // -- Init the toggle animation
   toggle_footer();

}

// When the DOM is ready...
jQuery(window).ready(function() {
   
   // Preload the images
   if (!jQuery.browser.msie) preload("#main .inner_frame");

   // Load Nav
   load_nav();
   
   // Load Subheader
   load_subheader();
   
   // Load Main
   load_main();
   
   // Load Footer
   load_footer();

   // Cufon
   setCufon('h1, h2, h3, h4, h5, h6, .cufon, .title');
   
   
   /* Plugins init */

   // jQueryTools Accordion
   jQuery(".accordion").tabs(".accordion div.pane", {
      tabs: '.title', 
      effect: 'slide', 
      initialIndex: null
   });

   // jQueryTools Tabs
   jQuery("ul.tabs").tabs("div.panes > div");

   // Notifications
   jQuery(".notification").click(function() {
      jQuery(this).fadeOut();
   });

   // Toogle
   jQuery(".toggle a.toggle_button").click(function() {

      var button = jQuery(this);
      var block  = jQuery(this).parents('.toggle').find('.description');

      if (block.is(":visible")) button.find('.sign').text('+');
      else button.find('.sign').text('-');

      block.slideToggle();
      return false;
   });
  
   // Set the portfolio items width and height to be equal
   jQuery(".blog .layout-column-2A.post-entry, .blog .layout-column-3A.post-entry, .blog .layout-column-4A.post-entry").equalHeights(100,2000);
   jQuery(".portfolio .layout-column-2A.post-entry, .portfolio .layout-column-3A.post-entry, .portfolio .layout-column-4A.post-entry").equalHeights(100,2000); 

   // Init Lightbox Zoom
   lightbox();
  
   /* Search Form */
   var search_value = jQuery("#searchform #s").val();

   // Focus In (Clean the search value)
   jQuery("#searchform #s").focus(function() {
      if (jQuery(this).val() == search_value) jQuery(this).val("");
   });

   // Focus Out (Reset the original value if no input was entered)
   jQuery("#searchform #s").focusout(function() {
      if (jQuery(this).val().length == 0) jQuery(this).val(search_value);
   });

   /* Widget Contact Form */
   
   // Author
   var author = jQuery(".widget_contact #widget-author").val();
   jQuery(".widget_contact #widget-author").focus(function() {
      if (jQuery(this).val() == author) jQuery(this).val("");
   });
   jQuery(".widget_contact #widget-author").focusout(function() {
      if (jQuery(this).val().length == 0) jQuery(this).val(author);
   });
   
   // Email
   var email = jQuery(".widget_contact #widget-email").val();
   jQuery(".widget_contact #widget-email").focus(function() {
      if (jQuery(this).val() == email) jQuery(this).val("");
   });
   jQuery(".widget_contact #widget-email").focusout(function() {
      if (jQuery(this).val().length == 0) jQuery(this).val(email);
   });
   
   // Comment
   var comment = jQuery(".widget_contact #widget-comment").val();
   jQuery(".widget_contact #widget-comment").focus(function() {
      if (jQuery(this).val() == comment) jQuery(this).val("");
   });
   jQuery(".widget_contact #widget-comment").focusout(function() {
      if (jQuery(this).val().length == 0) jQuery(this).val(comment);
   });

   //Scroll to Top Animation
   jQuery('a[href=#top]').click(function(){
      //If Safari or Chrome...
      if (jQuery.browser.webkit)
         jQuery('body').animate({
            scrollTop:0
         }, 'slow', "easeOutQuad");
      // Any other browser...
      else
         jQuery('html').animate({
            scrollTop:0
         }, 'slow', "easeOutQuad");
      return false;
   });
   
 
});

