// --------------------------
// LOAD THE COMMON FUNCTIONS
// --------------------------
jQuery(document).ready(function(){   
   sm_filter();
   sm_gotop();
   sm_preload();
   sm_position();
   sm_width();
   sm_overlay();
   jQuery('.preload img, .thumb img, #new-slider img').css({opacity: 0});   
});

// --------------------------
// FUNCTIONS
// --------------------------
// CUSTOM PRELOAD FOR .preload, .thumb class
// -----------------------------------------
function sm_preload() {
   jQuery('.preload img, .thumb img, #new-slider img').one('load', function() {
      jQuery(this).delay(250).animate({opacity:1}, 350);     
         }).each(function() {
            if(this.complete)
               jQuery(this).trigger('load');
   });   
}  

// -------------
// CUSTOM SLIDER
// -------------
function sm_customslider(){
   jQuery('#new-slider').after('<div id="nav">').cycle({
      fx: 'turnUp', 
       timeout: 6000,
       speedIn:  900, 
       easeIn: 'easeOutExpo',
       before: onBefore,
       after: onAfter,
       pager: '#nav',
       next: '#slide-nextNav',
       prev: '#slide-prevNav'
   });      
   
   sm_slide_imgload();
   sm_slide_controls();
}

function onBefore() { 
   jQuery('.popup').animate({ opacity : 0},100);
}

function onAfter() { 
   jQuery('.popup').delay(150).animate({ opacity : 1},350); 
}

// -----------------------
// CUSTOM SLIDER IMG LOAD
// -----------------------
function sm_slide_imgload(){
   jQuery('#new-slider img').one('load', function() {
      jQuery(this).delay(250).animate({opacity:1}, 350);
         }).each(function() {
            if(this.complete)
               jQuery(this).trigger('load');
   });   
}

// -----------------------
// CUSTOM SLIDER CONTROLS
// -----------------------
function sm_slide_controls(){
   jQuery('#slide-controls a').css({ opacity: 0 });
   jQuery('#slideshow').hover(function(){
      jQuery('#slide-controls a').animate({ opacity : 1 },200); 
   }, function(){
      jQuery('#slide-controls a').animate({ opacity : 0 },200); 
   });
}

// ---------------------------
// NIVO SLIDER SETTINGS
// ---------------------------
function sm_nivo(){
   /*jQuery('#slideshow').nivoSlider({
           effect:'fold', //Specify sets like: 'fold,fade,sliceDown'
           slices:10,
           animSpeed:500, //Slide transition speed
           pauseTime:3000,
           startSlide:0, //Set starting Slide (0 index)
           directionNav:true, //Next & Prev
           directionNavHide:true, //Only show on hover
           controlNav:true, //1,2,3...
           controlNavThumbs:false, //Use thumbnails for Control Nav
           controlNavThumbsFromRel:false, //Use image rel for thumbs
           controlNavThumbsSearch: '.jpg', //Replace this with...
           controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
           keyboardNav:true, //Use left & right arrows
           pauseOnHover:true, //Stop animation while hovering
           manualAdvance:false, //Force manual transitions
           captionOpacity:0.8, //Universal caption opacity
       }); */ 
}

// ---------------------------
// NAVIGATION AUTO POSITIONING
// ---------------------------
function sm_position(){
     var msie6 = jQuery.browser == 'msie' && jQuery.browser.version < 7;

     if (!msie6) {
       var top = jQuery('#main-nav').offset().top - parseFloat(jQuery('#main-nav').css('margin-top').replace(/auto/, 0));
       jQuery(window).scroll(function (event) {
         var y = jQuery(this).scrollTop();

         if (y >= top) {
            jQuery('#main-nav').addClass('fixed');
         } else {
            jQuery('#main-nav').removeClass('fixed');
         }
       });
     }  
}

function sm_width(){
   
   function resize(){
      var browserwidth = jQuery(window).width();
      if ((browserwidth) < '1070'){
         jQuery('div.post-date').addClass('inner');
      } else {
         jQuery('div.post-date').removeClass('inner');
      }
   }
   
   resize();
   
   jQuery(window).resize(function() {
      resize();
   });
}

// -------------------
// HORIZONTAL CAROUSEL
// -------------------
function sm_horz_carousel(){
   // Lastest works slideshow
   jQuery('.horz-carousel').cycle({
      fx: 'scrollHorz',
      easing: 'easeInOutQuad',
       speed:  'slow', 
       timeout: 0, 
       next:   '#arr-next', 
       prev:   '#arr-prev'
   });   
}

// ------------------
// FANCY BOX SETTINGS
// ------------------
function sm_fancy(){
   // fancy box
   jQuery('.thumb').fancybox();      
}

// -------
// OVERLAY
// -------
function sm_overlay(){
   // IE FIX
   if(jQuery.browser.msie){
      jQuery('a.thumb').hover(function(){           
         jQuery(this).stop().animate({opacity : .6},150);
      }, function(){
         jQuery(this).stop().animate({opacity : 1},280);
      });      
   }  
   // img overlay
   jQuery('a.thumb').hover(function(){           
      jQuery(this).stop().animate({opacity : 0.8},150);
   }, function(){
      jQuery(this).stop().animate({opacity : 1},280);
   });
}

// ----------------------
// PORTFOLIO WORKS FILTER
// ----------------------
function sm_filter(){   

   // work filter function
   jQuery('#filter ul a').click(function() {
      jQuery(this).css('outline','none');
      jQuery('#filter ul .current').removeClass('current');
      jQuery(this).parent().addClass('current');
      
      var filterVal = jQuery(this).text().toLowerCase().replace(/ /gi,'-');
      filterVal = sm_normalizeAccents(filterVal);
            
      if(filterVal == 'all-works') {
         jQuery('#worklist ul li.hidden').fadeIn('normal').removeClass('hidden');
      } else {
         
         jQuery('#worklist ul li').each(function() {
            if(!jQuery(this).hasClass(filterVal)) {
               jQuery(this).fadeOut('fast').addClass('hidden');
            } else {
               jQuery(this).fadeIn('normal').removeClass('hidden');
            }
         });
      }     
      return false;
   });
   
   jQuery('#filter ul a.now').click();
}  


function sm_normalizeAccents(str)
{
       var s=str;

       var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
       /[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
       /[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
       /[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
       /[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];

       var repChar=['A','a','E','e','I','i','O','o','U','u'];

       for(var i=0; i<rExps.length; i++)
       s=s.replace(rExps[i],repChar[i]);

       return s;
}

// ------------------------------------
// TWITTER SETTINGS FOR SIDEBAR TWEETS
// ------------------------------------
function sm_twitter(){
   jQuery("#twitter").getTwitter({
      userName: "simo_m", // your username
      numTweets: 2, // number of loaded tweets
      loaderText: "Loading tweets...",
      slideIn: true,
      slideDuration: 550,
      showHeading: false,
      showProfileLink: false,
      showTimestamp: true
   });   
}

// ---------------
// FORM VALIDATION   
// ---------------
function sm_validate(){
   // form validation                   
   jQuery('.contact-form').validate({
         rules: { 
                  name: {
                     required: true,
                     minlength: 2
                  },
                  message: {
                     required: true,
                     minlength: 10
                  }
               },
         submitHandler: function(form) {
                  jQuery(form).ajaxSubmit({
               target: "#result",
               resetForm: true
            });
         }
   });
}

// -------------------
// GOOGLE MAP SETTINGS
// -------------------
function sm_gmap(){
   // google map
   jQuery("#map").gMap({ 
      zoom: 14,
      markers: [{ 
      latitude: 51.5916824727648,   // change with your latitude
       longitude: -0.057849884033203125 // change with your longitude
      }]
   });
}

// ------
// GO TOP
// ------
function sm_gotop(){
   jQuery('a.gotop').click(function(){
      jQuery('html, body').animate({scrollTop:0}, 600, 'easeInOutQuad');
      return false;           
   });
}
// --------------------------------------------------------------
// Including plugins
// --------------------------------------------------------------
/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
*/
jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d)},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b},easeOutQuad:function(x,t,b,c,d){return -c*(t/=d)*(t-2)+b},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1){return c/2*t*t+b}return -c/2*((--t)*(t-2)-1)+b},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1){return c/2*t*t*t+b}return c/2*((t-=2)*t*t+2)+b},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b},easeOutQuart:function(x,t,b,c,d){return -c*((t=t/d-1)*t*t*t-1)+b},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1){return c/2*t*t*t*t+b}return -c/2*((t-=2)*t*t*t-2)+b},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1){return c/2*t*t*t*t*t+b}return c/2*((t-=2)*t*t*t*t+2)+b},easeInSine:function(x,t,b,c,d){return -c*Math.cos(t/d*(Math.PI/2))+c+b},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},easeInOutSine:function(x,t,b,c,d){return -c/2*(Math.cos(Math.PI*t/d)-1)+b},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},easeInOutExpo:function(x,t,b,c,d){if(t==0){return b}if(t==d){return b+c}if((t/=d/2)<1){return c/2*Math.pow(2,10*(t-1))+b}return c/2*(-Math.pow(2,-10*--t)+2)+b},easeInCirc:function(x,t,b,c,d){return -c*(Math.sqrt(1-(t/=d)*t)-1)+b},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1){return -c/2*(Math.sqrt(1-t*t)-1)+b}return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0){return b}if((t/=d)==1){return b+c}if(!p){p=d*0.3}if(a<Math.abs(c)){a=c;var s=p/4}else{var s=p/(2*Math.PI)*Math.asin(c/a)}return -(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0){return b}if((t/=d)==1){return b+c}if(!p){p=d*0.3}if(a<Math.abs(c)){a=c;var s=p/4}else{var s=p/(2*Math.PI)*Math.asin(c/a)}return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0){return b}if((t/=d/2)==2){return b+c}if(!p){p=d*(0.3*1.5)}if(a<Math.abs(c)){a=c;var s=p/4}else{var s=p/(2*Math.PI)*Math.asin(c/a)}if(t<1){return -0.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b}return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*0.5+c+b},easeInBack:function(x,t,b,c,d,s){if(s==undefined){s=1.70158}return c*(t/=d)*t*((s+1)*t-s)+b},easeOutBack:function(x,t,b,c,d,s){if(s==undefined){s=1.70158}return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined){s=1.70158}if((t/=d/2)<1){return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b}return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else{if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+0.75)+b}else{if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+0.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+0.984375)+b}}}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2){return jQuery.easing.easeInBounce(x,t*2,0,c,d)*0.5+b}return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*0.5+c*0.5+b}});
