/**
 * ztInputHint Plugin
 *
 * @version 1.2 (04/03/2010)
 * @requires jQuery v1.3+
 * @author Zeljko Trulec <trulec.de>
 * @copyright Copyright (c) 2010, Zeljko Trulec
 * @see https://code.google.com/p/ztinputhint/
 * 
 * Distributed under the terms of the GNU General Public License
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
(function($){$.fn.ztinputhint=function(g){var h={hint:null,sourceAttrib:'title',hintClass:false};if(g&&typeof g=='object'){$.extend(h,g)}else if(g){h.hint=g}return this.each(function(){var a=$(this);if(!a.is('input:text, input:password')){return false}var b='ztInputHint_'+a.attr('name');var c='<input type="text" value="" style="display: none;" />';var d=h.hint||a.attr(h.sourceAttrib);var e=h.hintClass||a.attr('class');$(c).insertBefore(a);var f=a.prev('input:first');if(d){f.attr('class',e);f.attr('size',a.attr('size'));f.attr('tabIndex',a.attr('tabIndex'));f.val(d);a.attr('autocomplete','off');f.focus(function(){$(this).hide();cont=$(this).next('input:first');cont.show();cont.focus()});a.blur(function(){if($(this).val()==''){$(this).prev('input:first').show();$(this).hide()}});if(h.iconClass&&!a.hasClass(h.iconClass)){a.addClass(h.iconClass)}a.blur()}})};$.fn.ztInputHint=$.fn.ztinputhint})(jQuery);;
// Remember in D7, Jquery was namespaced!
quinnTheme = {
  init: function(){
    quinnTheme.form.checkboxes.replace();
    quinnTheme.form.submits.replace();
    quinnTheme.blocks.video.init();
    quinnTheme.views.photoGallery.init();
    // Textbox Hints
    try {
      jQuery('input.hint:not(.hint-processed)').addClass('hint-processed').ztInputHint({
        sourceAttrib: 'title',
        hintClass: 'ui-hint'}
      );
    } catch(err) {
      // alert(err);
    }
  }
  ,views: {
    photoGallery: {
      height: 600
      ,width: 820
      ,init: function(){
        if(!jQuery('body').hasClass('photoGallery-init')){
          jQuery('body').addClass('photoGallery-init');

          jQuery('.view-id-photo_gallery #edit-field-procedure-value').live('change', function(event){
            jQuery('.view-id-photo_gallery .loading-graphic').show();
            jQuery('.view-id-photo_gallery #edit-submit-photo-gallery').click();
          });
          
          // Bind the pager images to load in the view
          jQuery('.view-id-photo_gallery a').live('click', function(event){
            jQuery('.view-id-photo_gallery .loading-graphic').show();
            var $this = jQuery(this);
            jQuery('.view-id-photo_gallery .before-and-after-target').load( $this.attr('href') + '?render=media-popup #media-browser-page-wrapper'
              ,function(){
              jQuery('.view-id-photo_gallery .loading-graphic').hide();
              }
            );
            return false;
          });
        }
        // Bind all links to photo gallery to load and open the modal
        var $this;
        var addition; 
        jQuery('a:not(.cboxElement)').each(function(index){
          $this = jQuery(this);
          if( $this.attr('href').indexOf('photo-gallery') > 0 ){
            if( $this.parents('.view-id-photo_gallery').length > 0 ){
              return true;
            }
            if( this.href.indexOf("?") > -1 ){
              addition = '&';
            } else {
              addition = '?';
            }
            $this.colorbox({
              href: this.href + addition + 'render=media-popup'
              ,iframe: true
              ,innerWidth: quinnTheme.views.photoGallery.width
              ,innerHeight: quinnTheme.views.photoGallery.height
              ,scrolling: false
            });
          }
        });
      }
    } // /photoGallery
  } // /views
  ,blocks: {
    video: {
      width: 500
      ,height: 385
      ,init: function(){
        jQuery('.view-home-page-video a').each(function(){
          jQuery(this).colorbox({
            href: this.href + '?render=media-popup'
            ,iframe: true
            ,innerWidth: quinnTheme.blocks.video.width
            ,innerHeight: quinnTheme.blocks.video.height
            ,scrolling: false
          });
        });
      } // /init
    } // /video
  } // /blocks
  ,form: {
      submits: {
        replace: function(){
          // Replace specific form submit buttons as requested
          var id;
          var style;
          var html;
          var $this;
          jQuery('#node-8 .form-submit').not('.wrapper-processed').each(function(){
            $this = jQuery(this);
            $this.addClass('wrapper-processed');
            id = $this.attr('id');
            style = $this.attr('style');
            if( id !== undefined && id.length > 0){
              // Create a faux checkbox
              html = '<div id="form-id-' + id + '" class="form-submit-wrapper">' + $this.attr('value') + ' <img src="/sites/all/themes/quinn/images/arrow-right-white.png" alt="" /></div>';
              $this.css({
                  'display' : 'none'
                  ,'visibility' : 'hidden'
              }).after(html);
              
              // Bind the behaviors to it
              jQuery('#form-id-' + id ).click(function(event){
                var $this = jQuery(this);
                var id = $this.attr('id').replace('form-id-', '');
                var $target = jQuery('#'+id);
                $target.click();
              }).attr('style', style );
            }
          });
        }
      }
      ,checkboxes: {
         replace: function(){
            var id;
            var style;
            var html;
            var $this;
            var $fauxBox;
            jQuery('form input[type=checkbox]').not('.wrapper-processed').each(function(){
              $this = jQuery(this);
              $this.addClass('wrapper-processed');
              id = $this.attr('id');
              style = $this.attr('style');
              if( id !== undefined && id.length > 0){
                // Create a faux checkbox
                html = '<div id="form-id-' + id + '" class="form-checkbox-wrapper"></div>';
                $this.css({
                    'display' : 'none'
                    ,'visibility' : 'hidden'
                }).after(html);
                
                $fauxBox = jQuery('#form-id-' + id )
                
                // Ensure the default matches
                if( $this.attr('checked') ){
                  $fauxBox.addClass('checked');
                }
                
                // Bind the behaviors to it
                $fauxBox.click(function(event){
                  var $this = jQuery(this);
                  var id = $this.attr('id').replace('form-id-', '');
                  var $target = jQuery('#'+id);
                  $target.click();
                  if( $target.attr('checked') ){
                    $this.addClass('checked');
                  } else {
                    $this.removeClass('checked');
                  }
                }).attr('style', style );
                
                // Ensure any label for it fires the visual updates as well
                jQuery('label[for='+id+']').click(function(event){
                  var id = jQuery(this).attr('for');
                  var $fauxBox = jQuery('#form-id-' + id);
                  var $checkbox = jQuery('#'+id);
                  if( $checkbox.attr('checked') ){
                    $fauxBox.addClass('checked');
                  } else {
                    $fauxBox.removeClass('checked');
                  }
                });
              }
            });
         } 
      }
  }
}

Drupal.behaviors.quinnTheme = {
  attach: function (context, settings) {
    quinnTheme.init(context, settings);
  }
};;

