var timeout    = 1000;
var closetimer = 0;
var ddmenuitem = 0;

var $jwPlayer;

function jsddm_open() {
  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('.children');
   if (ddmenuitem.css('display') == 'none') {
    //ddmenuitem.show('slow');
     ddmenuitem.css('display', 'block');
  }
}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('display', 'none');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

document.onclick = jsddm_close;

var canPlayHtml5 = canPlayHTML5Mp3();
$(document).ready(function(){
  
//  Replace the menu items with images
//  $('#primary li a').each(function(index) {
//    if ($(this).attr('title') == 'Home Page') {
//      $(this).html('');
//      $(this).append('<img src="/TakeAimPhotography/wp-content/themes/TakeAimDemo/images/HomeText98_29.png" />');
//    } else if ($(this).attr('title') == 'About') {
//      $(this).html('');
//      $(this).append('<img src="/TakeAimPhotography/wp-content/themes/TakeAimDemo/images/About125_36.png" />');
//    } else if ($(this).attr('title') == 'Galleries') {
//      $(this).html('');
//      $(this).attr('href', '#');
//      $(this).click(function() {
//        return false;
//      });
//      $(this).append('<img src="/TakeAimPhotography/wp-content/themes/TakeAimDemo/images/Galleries205_39.png" />');
//    } else if ($(this).attr('title') == 'Blog') {
//      $(this).html('');
//      $(this).append('<img src="/TakeAimPhotography/wp-content/themes/TakeAimDemo/images/Blog98_41.png" />');
//    }
//  });
  
  var safariBrowser = false;//$.browser.webkit || $.browser.safari;
  
  /*Set up the primary menu for navigation */
  $('#primary ul li ul li').each(function(index) {
    $ListTag = $(this).children('ul');
    if ($ListTag && $ListTag.children().length > 0)
      $ListTag.css('top', (($ListTag.children().length * -40) - 55) + "px" );
  });
  
  $('#primary ul li ul li.page_item').bind('mouseover', jsddm_open);
  
  /*Show and hide the navigation when the user hovers over the 'home' image. */
  $('#homeImage').hover(
           
  //Mouseover, fadeIn the hidden hover class    
    function() {
      $('#centerWell #pageTitle').css('display', 'none');
      $('#primary ul.xoxo').css('display', 'block');
    },

    //Mouseout, fadeOut the hover class  
    function() {  
      $('#centerWell #pageTitle').css('display', 'block');
      $('#primary ul.xoxo').css('display', 'none');
    }
  );
  
  $mediaFiles = $('a.autoplayMusic');
  
  var musicPlayerWidth = 220;
  if (safariBrowser) {
    musicPlayerWidth = 40;
  } 
  
  if ($mediaFiles.length > 0 && $('#mediaContainer').length > 0) {
  
    var mediaContent = $mediaFiles.attr('href');
    try {
      $('#mediaContainer').append('<embed src="' + mediaContent + '"  TYPE="audio/mp3" width="' + musicPlayerWidth + '" height="24" autoplay="true" hidden="true" loop="false" volume="100" />');
      //$('#mediaContainer').append('<audio src="' + mediaContent + '" autoplay="autoplay">' + '<embed src="' + mediaContent + '"  TYPE="audio/mp3" width="' + musicPlayerWidth + '" height="24" autoplay="true" hidden="true" loop="false" volume="100" />' + '</audio>');
    } catch (e) {
      
    }
    
    $('#mediaContent').append($mediaFiles);
  /*
    jwplayer("mediaContainer").setup({
        flashplayer: "http://www.yourownapplication.com/TakeAimPhotography/player.swf",
        file: mediaContent,
        autostart: true,
        volume: 80,
        width: musicPlayerWidth,
        height: 24,
        screencolor: "ffffff",
        idlehide: false,
        dock: true,
        controlbar: "bottom",
        playlist: "none",
        quality: "false",
    });
    
    if ($('#mediaContainerForSlideshow').length > 0) {
        jwplayer("mediaContainerForSlideshow").setup({
          flashplayer: "http://www.yourownapplication.com/TakeAimPhotography/player.swf",
          file: "http://www.yourownapplication.com/TakeAimPhotography/wp-content/uploads/2010/10/02TheOJaysLoveTrain.mp3",
          autostart: true,
          volume: 80,
          width: musicPlayerWidth,
          height: 24,
          screencolor: "ffffff",
          idlehide: false,
          dock: true,
          controlbar: "bottom",
          playlist: "none",
          quality: "false",
      });
    }
    */
    
  } else {
    $('#mediaContent, #main #mediaContent').css('display', 'none');
  }
  
  if (safariBrowser) {
    $('#mediaContainerWrapper').css('height', '24px').css('width', '40px').css('left', '0px').css('display', 'block');
    $('#wrapper').css('clear', 'both');
  } else {
  
    $("#mediaContent").hover(
             
    //Mouseover, fadeIn the hidden hover class    
      function() {
        $('#mediaContainerWrapper').css('height', '24px').css('width', '220px');
      },
  
      //Mouseout, fadeOut the hover class  
      function() {  
        $('#mediaContainerWrapper').css('height', '1px').css('width', '1px');
      }
    );
  }
  
  if ($.cookie("muteSound") == null || $.cookie("muteSound") == "" || $.cookie("muteSound") == "muteOff") {
//    $('#mediaContainerWrapper audio').get(0).play();
  } else {
    $("#mute").toggle();
    $("#soundOn").toggle();
    $('#mediaContainerWrapper').toggle();
//    $('#mediaContainerWrapper audio').get(0).pause();
    $('#mediaContainerWrapper embed').remove();
  }
  
  if (safariBrowser != true ) {
    $("#Sound").click(function() {
      $("#mute").toggle();
      $("#soundOn").toggle();
      $('#mediaContainerWrapper').toggle();
      
      if ($.cookie("muteSound") == null || 
          $.cookie("muteSound") == "" || 
          $.cookie("muteSound") == "muteOff") 
      {
        $.cookie("muteSound", "muted", { path: '/' });
//        $('#mediaContainerWrapper audio').get(0).pause();
        $('#mediaContainerWrapper embed').remove();
      } else {
        $.cookie("muteSound", "muteOff", { path: '/' });
//        $('#mediaContainerWrapper audio').get(0).play();
        $('#mediaContainer').append('<embed src="' + mediaContent + '"  TYPE="audio/mp3" width="' + musicPlayerWidth + '" height="24" autoplay="true" hidden="true" loop="false" volume="100" />');
      }
    });
  }
  /*Animate the Squiggle Image */
  $('#squiggleImage').hover(
         
    //Mouseover, fadeIn the hidden hover class    
    //Mouseover, fadeIn the hidden hover class    
    function() { 
      $(this).children('a').children('div').stop(true, true).animate({
        width: '100%',
        //left: '-35px',
      }, 200, function() {
        // Animation complete.
      });

    },

    //Mouseout, fadeOut the hover class  
    function() {
      $(this).children('a').children('div').stop(true, true).animate({
        width: '90%',
        //left: '-35px',
      }, 50, function() {
        // Animation complete.
      });
    }
  );
  
  /*Animate the Squiggle Image */
  $('#TakeAimTitleText').hover(
         
    //Mouseover, fadeIn the hidden hover class    
    //Mouseover, fadeIn the hidden hover class    
    function() {  
    },

    //Mouseout, fadeOut the hover class  
    function() {
      $('#galleryMenu').hide();
    }
  );
  
  $('#squiggleImage a').click(function() {
      var $rightNav = $('#rightNavigation');
      if ($rightNav.css('display') == 'none') {
        $rightNav.slideDown(1200);
      } else {
        $rightNav.slideUp( 1000 );
      }
  });
  
  $('#TakeAimTitleText a#TakeAimTitleLink').click(function() {
      $('#galleryMenu').show();
      return false;
  });
  
  $('#rightNavigation #galleries').hover(
    function()  {
       $('#galleryMenu').show();
    },
    function()  {
       $('#galleryMenu').hide();
    } 
  );
  
});

function verticalAlignDivInPage($divToAlign) {
  var divHeight = $divToAlign.height();
  var pageHeight = $(window).height();
  var margin = 0;
  if (divHeight > pageHeight) {
     margin = 0;
  } else {
    margin = ((pageHeight - divHeight) / 2)
  }
  $divToAlign.css('margin-top', '' + margin + 'px');
}

function canPlayHTML5Mp3() {
  try {
//    var canPlayMp3 = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/mp3');
//    return canPlayMp3;
  } catch (e) {
    alert(e);
  }
  return false;
}

