/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
 //navImage = "";
 
$.fn.slideFadeToggle = function(speed, easing, callback) 
{
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
}; 
 
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
 
  this.each(function() {
/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
 //navImage = "";
 
$.fn.slideFadeToggle = function(speed, easing, callback) 
{
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
}; 

var url
 
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
 
  this.each(function() {

    var root = this, zIndex = 100000;
    var chosenRootimg = "";
    
    
    if(navImage != ""){    
            newVal = $('[name=' + navImage + ']').attr("src");
            newVal = newVal.split(".jpg").join("-on.jpg");
            //alert(navImage);
            $('[name=' + navImage + ']').attr("src");
           // alert($('[name=' + navImage + ']').attr("src",newVal));
            // $('[name=' + navImage + ']').attr("src",newVal);
            //alert($('[name=' + navImage + ']').parent().parent().html());
            var imgobj = $('[name=' + navImage + ']').parent().parent();
            if (navImage == "home")
            {
                $(imgobj).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start_selected.png");
            }
            else
            {
                $(imgobj).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
            }
            $(imgobj).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
            
             
             //alert(newVal);
    }
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).hide(options.speed);
        }
      }, 200);
    }
  
function show() {
    
        if($(this).children("a").children('img').length > 0){
            var newVal = $(this).children("a").children("img").attr("src");
            if(newVal.indexOf("-on.") == -1)
                {
                    newVal = newVal.split(".jpg").join("-on.jpg");
                 }
           $(this).children("a").children("img").attr("src",newVal);
           if ($(this).children("a").children("img").attr("src").indexOf("spacer.gif") == -1)
           {
            if($(this).prev().html().indexOf("StartNav") != -1)
            {
              if($(this).prev().children("img").attr("src").indexOf("start_selected.png") == -1)
                {
                    $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start_selected.png");
                }
            }
            else
            {
                if($(this).prev().prev().children().html().indexOf("-on.jpg") > -1)
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left-right_selected.png");
                    }
                 else
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                    }

            }          
           }

           if($(this).next().next().length > 0)
            {

               if($(this).next().next().html().indexOf("EndNav") != -1)
                {
                  if($(this).next().next().children().html().indexOf("-on.jpg") == -1)
                    {
                        $(this).next().next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                    }
                }
             else
                {
                if($(this).next().next().children().html().indexOf("-on.jpg") > -1)
                    {
                         $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left-right_selected.png");
                    }
                 else
                    {
                        $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"/images/left_selected.png");
                    }
            }
            }
            else
            {
                if($(this).next().children().length != 0)
                    {
                        if($(this).next().html().indexOf("EndNav") != -1)
                        {
                          if($(this).next().children().html().indexOf("-on.jpg") == -1)
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                            }
                         }
                    }
            }
        }     
    
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).show(350);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
    
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
   
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
        
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); 
      
       if($(this).children("a").children('img').length > 0){
            var newVal = $(this).children("a").children("img").attr("src");
             if(newVal.indexOf("-on.") != -1)
                {  
                    newVal = newVal.split("-on.jpg").join(".jpg");
                 }
            
            if($(this).children("a").children("img").attr("name") != navImage){   
                if(newVal != ""){    
                   $(this).children("a").children("img").attr("src",newVal);
            if ($(this).children("a").children("img").attr("src").indexOf("spacer.gif") == -1)
            {
            
            
               if($(this).prev().html().indexOf("StartNav") != -1)
                {
                   if($(this).prev().children("img").attr("src").indexOf("start_selected.png") != -1)
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start.png");
                    }
                }
                else
                {
                     if($(this).prev().prev().children().html().indexOf("-on.jpg") > -1)
                        {
                            $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                        }
                     else
                        {
                            $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
                        }
                }
           }
           if($(this).next().length >0)
            {   
            if($(this).next().html().indexOf("EndNav") != -1)
            {
                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
            }
            else
            {

                  
                  if($(this).next().children().html().indexOf("-on.jpg") > -1)
                    {
                        $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                    }
                 else
                    {
                        
                    if($(this).next().next().length >0)
                        {
                            if($(this).next().next().children().html().indexOf("-on.jpg") > -1)
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                            }
                            else
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
                            }
                        }
     
                    }
                    }
            }

                }
            }
        }
      
      }
    );
    
  });

};

function findDroppyUrl(theLinkRef){
    document.location.href = $("#nav").find("[href*=" + theLinkRef + "]").attr("href");
}

function findDroppyUrlReplace(theLinkRef,appendage,replaceWith){
    document.location.href = $("#nav").find("[href*=" + theLinkRef + "]").attr("href").split(theLinkRef).join(replaceWith) + appendage;
}
    var root = this, zIndex = 100000;
    var chosenRootimg = "";
    
    
    if(navImage != ""){    
            newVal = $('[name=' + navImage + ']').attr("src");
            newVal = newVal.split(".jpg").join("-on.jpg");
            //alert(navImage);
            $('[name=' + navImage + ']').attr("src");
           // alert($('[name=' + navImage + ']').attr("src",newVal));
            // $('[name=' + navImage + ']').attr("src",newVal);
            //alert($('[name=' + navImage + ']').parent().parent().html());
            var imgobj = $('[name=' + navImage + ']').parent().parent();
            if (navImage == "home")
            {
                $(imgobj).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start_selected.png");
            }
            else
            {
                $(imgobj).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
            }
            $(imgobj).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
            
             
             //alert(newVal);
    }
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).hide(options.speed);
        }
      }, 200);
    }
  
function show() {
    
        if($(this).children("a").children('img').length > 0){
            var newVal = $(this).children("a").children("img").attr("src");
            if(newVal.indexOf("-on.") == -1)
                {
                    newVal = newVal.split(".jpg").join("-on.jpg");
                 }
           $(this).children("a").children("img").attr("src",newVal);
           if ($(this).children("a").children("img").attr("src").indexOf("spacer.gif") == -1)
           {
            if($(this).prev().html().indexOf("StartNav") != -1)
            {
              if($(this).prev().children("img").attr("src").indexOf("start_selected.png") == -1)
                {
                    $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start_selected.png");
                }
            }
            else
            {
                if($(this).prev().prev().children().html().indexOf("-on.jpg") > -1)
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left-right_selected.png");
                    }
                 else
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                    }

            }          
           }

           if($(this).next().next().length > 0)
            {

               if($(this).next().next().html().indexOf("EndNav") != -1)
                {
                  if($(this).next().next().children().html().indexOf("-on.jpg") == -1)
                    {
                        $(this).next().next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                    }
                }
             else
                {
                if($(this).next().next().children().html().indexOf("-on.jpg") > -1)
                    {
                         $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left-right_selected.png");
                    }
                 else
                    {
                        $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"/images/left_selected.png");
                    }
            }
            }
            else
            {
                if($(this).next().children().length != 0)
                    {
                        if($(this).next().html().indexOf("EndNav") != -1)
                        {
                          if($(this).next().children().html().indexOf("-on.jpg") == -1)
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                            }
                         }
                    }
            }
        }     
    
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).show(350);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
    
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
   
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
        
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); 
      
       if($(this).children("a").children('img').length > 0){
            var newVal = $(this).children("a").children("img").attr("src");
             if(newVal.indexOf("-on.") != -1)
                {  
                    newVal = newVal.split("-on.jpg").join(".jpg");
                 }
            
            if($(this).children("a").children("img").attr("name") != navImage){   
                if(newVal != ""){    
                   $(this).children("a").children("img").attr("src",newVal);
            if ($(this).children("a").children("img").attr("src").indexOf("spacer.gif") == -1)
            {
            
            
               if($(this).prev().html().indexOf("StartNav") != -1)
                {
                   if($(this).prev().children("img").attr("src").indexOf("start_selected.png") != -1)
                    {
                        $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/start.png");
                    }
                }
                else
                {
                     if($(this).prev().prev().children().html().indexOf("-on.jpg") > -1)
                        {
                            $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/left_selected.png");
                        }
                     else
                        {
                            $(this).prev().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
                        }
                }
           }
           if($(this).next().length >0)
            {   
            if($(this).next().html().indexOf("EndNav") != -1)
            {
                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
            }
            else
            {

                  
                  if($(this).next().children().html().indexOf("-on.jpg") > -1)
                    {
                        $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                    }
                 else
                    {
                        
                    if($(this).next().next().length >0)
                        {
                            if($(this).next().next().children().html().indexOf("-on.jpg") > -1)
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/right_selected.png");
                            }
                            else
                            {
                                $(this).next().children("img").attr("src",mapToTheParentDirectory + getPaths()+"images/nothing_selected.jpg");
                            }
                        }
     
                    }
                    }
            }

                }
            }
        }
      
      }
    );
    
  });

};

function findDroppyUrl(theLinkRef){
    document.location.href = $("#nav").find("[href*=" + theLinkRef + "]").attr("href");
}

function findDroppyUrlReplace(theLinkRef,appendage,replaceWith){
    document.location.href = $("#nav").find("[href*=" + theLinkRef + "]").attr("href").split(theLinkRef).join(replaceWith) + appendage;
}
