/* Author: Stas Kulesh
   Sliday.com */

jQuery.noConflict();
jQuery(document).ready(function($){

// E-mail defuscator

  $('a[href^="mailto:"]').addClass('email');
  if ($('a.email').length > 0) { 
    $('a.email').defuscate(); 
  }

// External links auto-styler

  $('a').filter(function() {
    return this.hostname && this.hostname !== location.hostname;
  }).addClass('external-link');

  $('a.external-link').click(function(){
    window.open(this.href);
    return false;
  });

// Addthis button

	$('.addthis').hover(
		function(){ return addthis_open(this, '', '[URL]', '[TITLE]'); },
		function(){ addthis_close();  }
	);	
  $('.addthis').click(function(){ return addthis_sendto(); });

// Swap value text form

  if($('#wizard').length){
    $('.services-container').remove();
    $('body').css({'overflow':'hidden','position':'relative'});
    $('#wizard').appendTo('body');
   
    $('#wizard').show().animate({'left':'0'}, 2000);
    
    $('#wizard').hover(function(){
      $(this).html("Click me and you're deadman!");
    }, function(){
      $(this).html("Move along. There is nothing to see here.");
    });
    jQuery.fn.jmp3=function(passedOptions){var playerpath="/addons/themes/sliday/js/";var options={"filepath":"/addons/themes/sliday/evil-laugh.mp3","backcolor":"","forecolor":"ffffff","width":"25","repeat":"no","volume":"50","autoplay":"true","showdownload":"true","showfilename":"false"};if(passedOptions){jQuery.extend(options,passedOptions);}return this.each(function(){var filename=options.filepath+jQuery(this).html();var validfilename=filename.indexOf(".mp3");if(validfilename==-1){return false;}var mp3html='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';mp3html+='width="'+options.width+'" height="20" ';mp3html+='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">';mp3html+='<param name="movie" value="'+playerpath+'singlemp3player.swf?';mp3html+='showDownload='+options.showdownload+'&file='+filename+'&autoStart='+options.autoplay;mp3html+='&backColor='+options.backcolor+'&frontColor='+options.forecolor;mp3html+='&repeatPlay='+options.repeat+'&songVolume='+options.volume+'" />';mp3html+='<param name="wmode" value="transparent" />';mp3html+='<embed wmode="transparent" width="'+options.width+'" height="20" ';mp3html+='src="'+playerpath+'singlemp3player.swf?';mp3html+='showDownload='+options.showdownload+'&file='+filename+'&autoStart='+options.autoplay;mp3html+='&backColor='+options.backcolor+'&frontColor='+options.forecolor;mp3html+='&repeatPlay='+options.repeat+'&songVolume='+options.volume+'" ';mp3html+='type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';mp3html+='</object>';if(options.showfilename=="false"){jQuery(this).html("");}jQuery(this).prepend(mp3html+"&nbsp;");if(jQuery.browser.msie){this.outerHTML=this.outerHTML;}});};

    $('#wizard').click(function(){
      /**** jQuery mp3 *****/ 
      if( $('.mp3 object').length) {
        $('.mp3').remove();
        $('<div class="mp3"></div>').appendTo('article');
      }
      $('.mp3').jmp3();
      $(this).html("Mwahahaha! You are one puny human, aren't you?");
      return false;
    });
  }

  if($('#hpslideshow').length) { 
    $('#hpslideshow').cycle();
    $('#hpslideshow-overlay').hover(function(){
      //$('#portfolio-button').css('display','block');
      if ($.browser.msie) {
        $('#portfolio-button').show();
      } else {
        $('#portfolio-button').stop().fadeTo('fast', 1);
      }
    }, function(){
      if ($.browser.msie) {
        $('#portfolio-button').hide();
      } else {
        $('#portfolio-button').stop().fadeTo('slow', 0);
      }
    });
    $('#portfolio-button, #hpslideshow-overlay').click(function(){
      window.location=$('#portfolio-button').attr('href');
      return false;
    });
    $('.scrollable1, .browse, #navi1').removeClass('short');

    $('.items div').hover(function(){
      $(this).css('cursor', 'pointer');
    });

    $('.items div').click(function(){
      window.location = $(this).find('a').attr('href');
      return false;
    });
  }
  
  $('.opacity50').css('opacity', '0.5');
  $('.opacity50').hover(function(){
    $(this).stop().fadeTo('fast',1);
  },function(){
    $(this).stop().fadeTo('fast',0.5);
  });
  
  if($('.scrollable1').length) { $('.scrollable1').scrollable({ circular: true }).autoscroll().navigator("#navi1"); }

  if($('.cycle').length) { $('.cycle').cycle({pauseOnPagerHover: 1, next: '#next-cycle', prev: '#prev-cycle', delay: 7000, pause: 1, random: 0}); 
    //$('.scrollable1').scrollable({ circular: true }).autoscroll().navigator("#navi1"); 
  }

  Cufon.now();

  if($('#case-study').length){
    $('.box1').appendTo($('#box1')).fadeIn();
    $('.box2').appendTo($('#box2')).fadeIn();
    $('.box3').appendTo($('#box3')).fadeIn();
    $('h1').appendTo($('#case-slideshow-container div.text-container')).fadeIn();
    $('.box0').appendTo($('#case-slideshow-container div.text-container')).fadeIn();

    $('#case-slideshow').html($('.case-study-gallery').html()).fadeIn();
    $('.case-study-gallery').remove();

    $('#case-slideshow').cycle();

  }

  if($('#contact').length){
    
    $('#live-chat-toggler').click(function(){
      $(this).css('background-position', 'top left !important');
      $(this).slideUp(function(){
        $('article textarea').animate({width: '267px'});
        $('#live-chat').slideDown();
      });
      return false;
    });
    
    $('input[name^="contact_email"]').attr({'required':'required','value': 'Your Email'}).addClass('swap-value');
    $('input[name^="contact_name"]').attr({'required':'required','value': 'Your Name'}).addClass('swap-value');
    $('textarea[name^="message"]').attr({'required':'required','value': 'Your Message'}).addClass('swap-value');
    //$('#footer-form textarea').attr('required','required');
  
    $('#contact article form').validator().submit(function(e){
      if(!e.isDefaultPrevented()) {
        // tell user that everything is OK
        $("#errors").fadeIn().html("<p>All good! Sending now...</p>");
        // prevent the form data being submitted to the server
        $('#contact article form').ajaxSubmit(function(){
          $('#contact article form').fadeOut(function(){
            $('#errors').html('<p>Thank you for your inquery.</p><p>Someone from Sliday team will get to you shortly.</p>');
          });
        });
      }
      return false;
    });
  }
    
  $('#footer-form').validator().submit(function(e){
    if(!e.isDefaultPrevented()) {
      $('footer button').html('Sending...');
      $('#footer-form').ajaxSubmit(function() { 
        $('#footer-form fieldset').fadeOut(function(){
          $('#footer-form fieldset').before('<p class="success">Thank you for your inquery. Someone from Sliday team will get to you shortly.</p>');
          $('footer button').html('Sent').fadeOut();
        });
      });    
    }
    return false;
  });

  if($('#portfolio').length){
  
      $('.portfolio-gallery.case-items a:nth-child(2n)').css('margin-right','0');
      $('.portfolio-gallery a:nth-child(4n)').css('margin-right','0');
      
      var alphabet=["A","B","C","D"];

      var i = 0;
      var j = 0;

      $('.portfolio-gallery').each(function(){
        i = 0;
        j = 1;

        $(this).find('a:lt(4)').each(function(){
          $(this).prepend('<div class="num-top">'+ alphabet[i++] + '</div>');
        });
        $(this).find('a:nth-child(4n+1)').each(function(){
          $(this).prepend('<div class="num-left">'+ j +'</div>');
          j++;
        });
      });

      var i = 0;
      $('.case-items div.num-top, .case-items div.num-left').remove();
      $('.case-items a:lt(2)').each( function(){
          $(this).prepend('<div class="num-top">'+ alphabet[i++] + '</div>');
      });
      j = 1;
      $('.case-items').find('a:nth-child(2n+1)').each(function(){
        $(this).prepend('<div class="num-left">'+ j +'</div>');
        j++;
      });
      
			$("a[rel=logotypes]").fancybox({
        'titlePosition'		: 'outside',
				'overlayColor'		: '#003',
				'overlayOpacity'	: 0.75,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			}); 
			
      $("a[rel=websites]").fancybox({
        'titlePosition'		: 'outside',
				'overlayColor'		: '#300',
				'overlayOpacity'	: 0.75,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});    


			$("a[rel='mini-websites']").fancybox({
        'titlePosition'		: 'outside',
				'overlayColor'		: '#030',
				'overlayOpacity'	: 0.75,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});    

      $('.portfolio-gallery a').hover(function(){
        $(this).find('div').hide();
        //$(this).find('div').stop().animate({left: '-3%', bottom: '-3%',opacity: '0'},'easein');
        $(this).stop().animate({boxShadow: '0 0 50px #000'},'easein');
        $(this).find('img').stop().animate({ width: '106%', height: '106%',left: '-3%', top: '-3%'}, 'easein');
      },function(){
        $(this).find('img').stop().animate({ width: '100%', height: '100%',left: '0%', top: '0%'}, 'easeout');
        //$(this).find('div').stop().animate({left: '1px', bottom: '-1px',opacity: '1'},'easeout');
        $(this).stop().animate({boxShadow: '0 0 0 #000'},'easout', function(){
          $(this).find('div').stop().fadeTo('fast',1);
        });
      });
      
      $(".portfolio-gallery a").each(function(){
        $(this).append('<div>'+$(this).attr('title')+'</div>');
      });      
      
  }

  swapValues = [];
  $(".swap-value").each(function(i){
      swapValues[i] = $(this).val();
      $(this).focus(function(){
          if ($(this).val() == swapValues[i]) {
              $(this).val("");
          }
      }).blur(function(){
          if ($.trim($(this).val()) == "") {
              $(this).val(swapValues[i]);
          }
      });
  });  
  
});

