var popupLoaded;
var defaultContent;

jQuery(document).ready(function($) {
    // Placeholders for browsers not supporting the placeholder attribute
    if (!Modernizr.input.placeholder){
        $('[placeholder]').focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function() {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();    
    }
    
    // Rotate Images on Hover
    if ($("a#hiring").length > 0) {
        $("a#hiring").hover(
            function () { $(this).animate({rotate: '10deg'}, 1000); },
            function () { $(this).animate({rotate: '0deg'}, 1000); }
        );
    }
    
    // Types Taxonomy Hover (work section)
    if ($('#portfolio-container').length > 0) {
        $('#portfolio-container a.hidden').hide();
        $('#portfolio-container li').bind('mouseover', function() {
            $('a.hidden', $(this)).slideDown();
        });
        $('#portfolio-container li').bind('mouseleave', function() {
            $('a.hidden', $(this)).slideUp();
        });
    }
    
    // Types Taxonomy Image Click
    // Add ?ajax=1 to URLs so when opened in Zoombox they use correct single-portfolio-ajax.php template
    $('a.zoombox').addClass('hidden').hide();
    $('a.zoombox').each(function(i) {
        $(this).attr('href', $(this).attr('href')+'?ajax=1');     
    });
    $('body.single-portfolio').addClass('fancybox');
    $('body.term-websites a.zoombox').fancybox({
		'width'		         :	939,
		'height'		     :	523,
		'padding'            :  0,
		'overlayOpacity'	 :	'0.8',
		'overlayColor'		 :	'#000',
		'hideOnContentClick' :   false,
		'autoScale'     	 :   false,
		'autoSize'           :   false,
		'fitToView'          :   false,
   		'openEffect'		 :   'elastic',
		'closeEffect'	     :   'elastic',
		'type'			     :   'iframe',
		'scrolling'          :   'no',
		'showNavArrows'      :   true	    
    });
    
    $('a.moreinfobox').fancybox({
        'width'		         :	939,
		'height'		     :	523,
		'overlayOpacity'	 :	'0.8',
		'overlayColor'		 :	'#000',
		'hideOnContentClick' :   false,
		'autoScale'     	 :   false,
   		'openEffect'		 :   'elastic',
		'closeEffect'	     :   'elastic',
		'type'			     :   'iframe',
		'scrolling'          :   'no',
		'showNavArrows'      :   false	  
    });
    
    $('body:not(.term-websites) a.zoombox').fancybox({
		'overlayOpacity'	 :	'0.8',
		'overlayColor'		 :	'#000',
		'hideOnContentClick' :   false,
   		'openEffect'		 :   'elastic',
		'closeEffect'	     :   'elastic',
		'type'			     :   'image',
		'scrolling'          :   'no',
		'showNavArrows'      :   true,
		'autoSize'           :   true,
		'fixToView'          :   false,
		'autoPlay'           :   false,
		'playSpeed'          :   8000
    });

    $('#portfolio-container li a:not(.zoombox)').bind('click', function(e){
        e.preventDefault();
        $('a.zoombox', $(this).parent()).click();
    });

    $('.gallery-container img').bind('click', function(e){
      var thumbs = $('div.gallery-thumbnails ul li a');

      var nbThumbs = thumbs.length;

      var selectedThumbIndex = null;
      $(thumbs).each(function(index, elem){
        if($(elem).hasClass('selected'))
        {
          selectedThumbIndex = index;
        }
      });

      var nextIndex = (selectedThumbIndex + 1 >= nbThumbs)? 0 : selectedThumbIndex + 1;
      $(thumbs.get(nextIndex)).click();
    });

    // Zoombox Gallery Thumbnails
    $('div.gallery-thumbnails ul li a').bind('click', function(e) {
        e.preventDefault();

        var thumbs = $('div.gallery-thumbnails ul li a');
      
        var previousIndex = null;
        $(thumbs).each(function(index, elem){
          if($(elem).hasClass('selected'))
          {
            previousIndex = index;
          }
        });
      
        $('div.gallery-thumbnails ul li a').removeClass('selected');
        $('a', $(this).parent()).addClass('selected');

        var nbThumbs = thumbs.length;
        var selectedThumbIndex = null;
        $(thumbs).each(function(index, elem){
          if($(elem).hasClass('selected'))
          {
            selectedThumbIndex = index;
          }
        });

        $('.gallery-container img:nth('+previousIndex+')').fadeOut();
        $('.gallery-container img:nth('+selectedThumbIndex+')').fadeIn();

    });
    
    $('ul#supersized li a').bind('click', function(e) {
        e.preventDefault();
    });
    
    /*
    // Types Taxonomy Image Click (work section)
    // Displays the single-project.php template in a modal window
    $('#portfolio-container li').bind('click', function(e) {
        if(!window.XMLHttpRequest) return; // Don't load the popup in IE6
        e.preventDefault();

        // Fill overlay to document dimensions
        $('#overlay').css('height', $(document).height()+'px');
        $('#overlay').show();
        
        // Position popup in center of browser viewport
        defaultContent = $('#popupMain').html();  // Get default loading image for restoration when popup closed
        $('#popup').css('top', (($(window).height()/2)-($('#popup').height()/2))+'px');
        $('#popup').css('left', (($(window).width()/2)-($('#popup').width()/2))+'px');
        $('#popup').show();
        
        // Get content and display inside popup
        $.ajax({
            url: $(this).find('a[rel=portfolio]').attr('href'),
            success: function(data) {
                $('#popup #popupMain').html(data);
                $('#popup').show();
                $('#popup div#nav ul').css('margin-left', (($('#popup div#nav').width() - $('#popup div#nav ul').width()) / 2));
                
                if ($('div.gallery-only').length > 0) ResizePopup();
            } 
        });

        popupLoaded = true;
    });
    
    // Change position of popup on window resize
    $(window).resize(function() {
        if (!popupLoaded) return;
        $('#popup').css('top', (($(window).height()/2)-($('#popup').height()/2))+'px');
        $('#popup').css('left', (($(window).width()/2)-($('#popup').width()/2))+'px');    
    });
    
    // Close popup if close button clicked, overlay clicked or ESC key pressed
    $('a.close').live('click', function(e) {
        e.preventDefault();
        ClosePopup();
    });
    $('#overlay').live('click', function(e) { ClosePopup(); });

    
    $('#popup div#nav ul li a').live('click', function(e) {
        e.preventDefault();
        var action = $(this).parent().attr('class'); 
        ChangeImage(action);
    });
    
    // Keypresses for closing popup and popup gallery
    if ($('body.tax-types').length > 0) {
        $(document).keyup(function(e) { 
            if (e.keyCode == 27 && popupLoaded) ClosePopup(); 
            if (e.keyCode == 37) ChangeImage('prev');
            if (e.keyCode == 39) ChangeImage('next');
        });
    }
    */
});

function ClosePopup() {
    if (!popupLoaded) return false;

    $('#overlay').hide();
    $('#popup').hide();
    $('#popupMain').html(defaultContent); // Restore default loading image
    
    popupLoaded = false;    
}

function ChangeImage(action) {
    var currentImageEle = $('#popup div.gallery-thumbnails ul li a.selected').parent();

    // Establish new image element
    if (action == 'next') {
        GetNextImage(currentImageEle);
    } else {
        GetPreviousImage(currentImageEle);
    }    
}

function GetNextImage(currentImageEle) {
    newImageEle = $(currentImageEle).next(); // Next
    if ($(newImageEle).length == 0) newImageEle = $('#popup div.gallery-thumbnails ul li:nth-child(1)'); // Reached end of list, get first
    DisplayNewImage(newImageEle);            
}

function GetPreviousImage(currentImageEle) {
    newImageEle = $(currentImageEle).prev(); // Previous
    if ($(newImageEle).length == 0) newImageEle = $('#popup div.gallery-thumbnails ul li:nth-child('+$('#popup div.gallery-thumbnails ul li').length+')'); // Reached start of list, get last    
    DisplayNewImage(newImageEle);
}

function DisplayNewImage(newImageEle) {
    var dims = $('a img', $(newImageEle)).attr('class').split('-');
    var newImageWidth = dims[1];
    var newImageHeight = dims[2];

    $('#popup div.gallery-container img').attr('src', $('a', $(newImageEle)).attr('href'));
    $('#popup div.gallery-container img').attr('width', newImageWidth);
    $('#popup div.gallery-container img').attr('height', newImageHeight);
    
    $('#popup div.gallery-thumbnails ul li a').removeClass('selected'); 
    $('a', $(newImageEle)).addClass('selected');
    
    if ($('div.gallery-only').length > 0) ResizePopup();
    
    // Update navigation
    $('#popup div#nav ul li.caption').text($('a', $(newImageEle)).attr('title'));
    $('#popup div#nav ul').css('margin-left', (($('#popup div#nav').width() - $('#popup div#nav ul').width()) / 2));        
}

function ResizePopup() {
    var width = $('div.gallery-only div.gallery-container img').attr('width');
    var height = $('div.gallery-only div.gallery-container img').attr('height');
    
    $('div.gallery-container').width(width);
    $('div.gallery-container').height(height);

    $('#popupBody').width($('div.gallery-container').outerWidth());
    $('#popupBody').height($('div.gallery-container').outerHeight() + $('div.gallery-thumbnails').outerHeight());
    
    $('#popupMain').width($('#popupBody').outerWidth());
    $('#popupMain').height(($('#popupBody').outerHeight() + $('#nav').outerHeight()));
    
    $('#popup #nav').width($('#popupBody').width());
    
    $('#popup').width($('#popupMain').outerWidth() + 15);
    $('#popup').height($('#popupMain').outerHeight());
    
    $('#popup').css('top', (($(window).height()/2)-($('#popup').height()/2))+'px');
    $('#popup').css('left', (($(window).width()/2)-($('#popup').width()/2))+'px');

    $('#popup div#nav ul').css('margin-left', (($('#popup div#nav').width() - $('#popup div#nav ul').width()) / 2)); 
}
