/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/
(function($) {
  
  $('div.pgfoot div.widget_twitter h3.boxhead')
      .prepend('Latest <span>tweet</span>')
      .find('a').remove();
      
  
  $('body.blog #maincontent .post:odd').addClass('alt');

  $('ul.tabs').tabSwitcher();
  
  $('#s').labelizor();
  
  $('#maincontent, div.pgextra1').equalizeHeights();
  
  $('#noflicker').remove();
  $('body').addClass('js-active');

  $('body.home #maincontent div.entry')
    .listscroller({
        item: 'li',
        classPrefix: 'carousel',
        aspect: 'horizontal',
        paging: true,
        windowSize: 1,
        stepSize: 1,
        animation: 'carousel',
        autoScrollDelay: 3000
      });

  
  //portfolio expander
  var maincontentUls = $('body.portfolio #maincontent ul').wrap('<div class="ulwrap"></div>');
  $(window).bind('load', function() {
      
      maincontentUls
          .each(function(i) {
              
              var open = false,
                  parentElm = $(this).closest('.ulwrap'),
                  height1 = parentElm.height(),
                  height2 = $(this).height(),
                  count = $('> li', this).length,
                  shown = count < 3 ? count : 3, 
                  showall = $('<span class="counter">Showing <span class="shown">' + shown + '</span> of ' + count + '</span> ');
              
              showall.insertBefore(parentElm);
              
              if (count > 3) 
              {
                  showall
                    .append( 
                        $('<a class="showall" href="#">Show all +</a>')
                            .bind('click', function() {
                                
                                $(this).html( !open ? 'Show less -' : 'Show all +' ).blur();
                                
                                parentElm.animate({ height : !open ? height2 : height1 }, 'normal', 'swing');
                                
                                showall.find('span.shown').html( !open ? count : 3 );
                                
                                open = !open;
                                
                                return false; 
                              })
                          );
              }
          });
    }); //end onload
      
      
      
  //popups
  $('body:not(.home):not(.portfolio) div.pgmain div.entry img:not(.wp-smiley)')
      .each(function() {
      
          var _href = $(this).attr('src').replace(/(-291x\d*|-275x\d*|-453x\d*)/, '');
          
          $(this)
              .wrap('<a class="fancypop"></a>')
              .parent()
              .attr('href', _href)
              .attr('rel', 'group')
              .append('<span class="zoomicon"></span>')
              .fancybox({
                'zoomSpeedIn': 300, 
                'zoomSpeedOut': 200,
                'padding'     : 0,
                'overlayOpacity' : 0.5,
                'imageScale': false,
                'centerOnScroll': false
              })
        });
            
 
 
  //popup windows
  $.fn.popup = function ( cfg ) {
      var popupElm = $(this),
          curtainElm = $.curtain({
                  className: 'popup-curtain',
                  bg:        '#000000',
                  opacity:   .60
                });
        popupElm
            .before(curtainElm)
            .css({
                  top:        100 + $(window).scrollTop()
                })
            .fickle({
                fadein: 350,
                onOpen:   function (e) { curtainElm.fadeIn(100)  },
                onClose:  function (e) { popupElm.fadeOut(200, function() { curtainElm.fadeOut(100); }) },
                onClosed: function (e) { curtainElm.curtain('destroy'); }
              })
            .fickle('open');
        curtainElm.click(function() { popupElm.fickle('close') });

    return popupElm;
  };
 

 var form = $('#wpcf7-f1-p544-o1');
 form.find('div.wpcf7-response-output').prependTo(form);

  $('.contact .boxbody').each(function() {
      var link = $('a', this),
          isOpen = false,
          wait = $('<div class="contactform loading"><img src="/wp-content/themes/daggardropi/img/ajax-loader.gif" alt="" /></div>');
      link.bind('click', function() {
          var contactForm = link.data('contactForm');
          if (!isOpen) {
            wait.appendTo('body').popup();
            //ajax contactform
            $.get(
                link.attr('href'),
                function(data){
                    var contactForm = $(data).find('form.wpcf7-form').prepend('<input type="hidden" name="_wpcf7_is_ajax_call" value="1" />').parent().prepend('<h2 class="boxhead">Say hello <span>to me</span></h2>');
                    $(wait).removeClass('loading').find('img').remove();
                    contactForm.hide().appendTo( wait ).slideDown(300)
                        .find('form').autoValidate().submit(function() {
                            var theForm = $(this);
                            if (theForm.isValid()) {
                                $.post(
                                    theForm.attr('action'), 
                                    theForm.serialize(),
                                    function(response) {
                                        var msgText = response.message;
                                        msgText = $.trim(msgText).split(/\s*,\s*/);
                                        $('div.form', theForm).find('> p').log().remove().end().html( '<p class="response">' + msgText[0] + '<span>' + msgText[1] + '</span></p>' );
                                        setTimeout(function() {
                                          wait.fickle('close');
                                        }, 3000);
                                    },
                                    'json'
                                  );
                            }
                            return false;
                        });
                    link.data('contactForm', contactForm);
                    isOpen = true;
                  }
              );
          }
          else
          {
            contactForm.parent().popup();
          }
          return false;
        });

    });


    /* Christmas splash screen
    var splash = $('<div class="ipopup-curtain"></div>' +
                  '<div class="contactform splash">' +
                  '<div class="boxbody">' +
                    '<img src="/wp-content/uploads/2009/12/jol2009.jpg" />' +
                  '</div>' +
                '</div>'),
        closeSplash = function() { splash.fadeOut(500, function(){ splash.remove(); }); };

    if(!$.cookie('splash'))
    {
      splash
          .hide()
          .appendTo('body')
          .filter('.splash')
              .css({
                  top:  100 + $(window).scrollTop()
                })
          .end()
          .filter('.ipopup-curtain')
              .css({
                  opacity: .7,
                  'background-color': '#000000',
                  'position': 'fixed',
                  'width': '100%',
                  'height': '100%',
                  'top': 0,
                  'left': 0
                })
          .end()
          .click(function(){
              closeSplash();
          });

      splash.find('img:first').bind('load readystatechange', function() {
          splash.fadeIn(400, function(){ setTimeout(function(){ closeSplash(); }, 8000); });
        });
      splash.find('img')[0].src += ''; //readystate hack

    }

    $.cookie('splash', '1', { path:'/', expires:3 });
    
*/


  $('form').autoValidate();


  //hacks
  if($.browser.msie && $.browser.version < 7) {
    $('img[src$=".png"]').ifixpng();

    $('body').append('<div style="position:absolute; top:0; right:0; display:block; padding:8px 0; width:100%; text-align:center; background:#43007F;">This website does not work properly in Internet Explorer 6, please upgrade to more <a href="http://www.microsoft.com/windows/Internet-explorer/">recent version</a> or switch to <a href="http://www.firefox.com">Firefox</a>, <a href="http://www.google.com/chrome/">Chrome</a> or <a href="http://www.opera.com">Opera</a>.</div>');
  };

})(jQuery)