$(function(){
        
        $('#testimonial-container').vTicker({
          speed: 900,
          pause: 10000,
          animation: 'fade',
          mousePause: false,
          showItems: 3,
          direction: 'down',
          height:530
        });
        
        $("#foo2").carouFredSel({

          auto : {
            pauseOnHover: true
          },
          items : {
            visible:1
          },
          scroll: {
            duration : 1000
          },
          prev : {
            button  : "#foo2_prev",
            key     : "left"
          },
          next : {
            button  : "#foo2_next",
            key     : "right"
          },
          pagination  : "#foo2_pag"
        });
        
        $("#foo3").carouFredSel({
 
          items : {
            visible:1
          },
          scroll: {
            duration : 1200
          },
          auto : {
            pauseOnHover: true
          },
          prev : {
            button  : "#foo3_prev",
            key     : "left"
          },
          next : {
            button  : "#foo3_next",
            key     : "right"
          },
          pagination  : "#foo3_pag"
        });
        
        $("#foo4").carouFredSel({

          auto : {
            pauseOnHover: true
          },
          items : {
            visible:1
          },
          scroll: {
            duration : 1400
          },
          prev : {
            button  : "#foo4_prev",
            key     : "left"
          },
          next : {
            button  : "#foo4_next",
            key     : "right"
          },
          pagination  : "#foo4_pag"
        });
        
        $("#foo5").carouFredSel({
 
          items : {
            visible:1
          },
          scroll: {
            duration : 1600
          },
          auto : {
            pauseOnHover: true
          },
          prev : {
            button  : "#foo5_prev",
            key     : "left"
          },
          next : {
            button  : "#foo5_next",
            key     : "right"
          },
          pagination  : "#foo5_pag"
        });
        
        // call accordion menu
        accordion();
        
        // search works
        searchWorks();
        
        // works detail portfolio
        $('.newsitem img').wrap(document.createElement('a')).parent().attr('rel','colorbox').each(function(){
          $(this).attr('href',$(this).children().attr('src'));
          $(this).children().attr('width',$(this).children().attr('width'));
        });
        
        // call colorbox
        $("a[rel='colorbox']").colorbox();
        
        // clear input default
        $('.placeholder').clearDefault();
        
        // tab jquery
        myTab();
        
});

function relative_time(time_value) {
      var values = time_value.split(" ");
      time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      delta = delta + (relative_to.getTimezoneOffset() * 60);
    
      if (delta < 60) {
        return 'less than a minute ago';
      } else if(delta < 120) {
        return 'about a minute ago';
      } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (120*60)) {
        return 'about an hour ago';
      } else if(delta < (24*60*60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
        return '1 day ago';
      } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
      }
}

function accordion(){
    //Set default open/close settings
    $('.acc_container').hide(); //Hide/close all containers
    //$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
    
    //On Click
    $('.acc_trigger').click(function(){
    	if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
    		$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
    		$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
    	}
        else{
            $(this).removeClass('active').next().slideUp();
        }
    	return false; //Prevent the browser jump to the link anchor
    });
}

function searchWorks(){
        // sidebar search work
        $('#worksForm').submit(function(){
            return false;
        });
        $('#searchinput').bindWithDelay('keypress',function(){
               
               if($(this).val() == ""){
                    return false;
               }
               
               var data = $('#worksForm').serialize();
               $('#searchResult').html('<li>Searching...</li>');
               $.ajax({
                   type: "POST",
                   url: $('#worksForm').attr('action'),
                   data: data,
                   dataType: "json",
                   success: function(result){
                     $('#searchResult').html('');
                     $('#resultCap').html('');
                     
                     if(result.founded == 1){
                         $.each(result.data, function(i){
                            $('#resultCap').html('<h4 style="color:#555;font-weight:bold">Search Results for "'+ $('#searchinput').val() +'"</h4>');
                            $('#searchResult').append('<li><a href="'+ result.data[i].url +'">'+ result.data[i].name +'</a></li>');
                         });
                     }
                     else{
                        $('#resultCap').html('<h4 style="color:#555;font-weight:bold">Your search return no result</h4>');
                     }
                   }
               });
            
        }, 500);
        
        // content search works
        $('#portfolioForm').submit(function(){
            return false;
        });
        $('#searchWorks').bindWithDelay('keypress',function(){
                
               if($(this).val() == ""){
                    return false;
               }
                
               var data = $('#portfolioForm').serialize();
               $('#portfolioResult').html('<b>Searching...</b>');
               $.ajax({
                   type: "POST",
                   url: $('#portfolioForm').attr('action'),
                   data: data,
                   dataType: "json",
                   success: function(result){
                     $('#portfolioResult').html('');
                     $('#result_title').html('');
                     
                     if(result.founded == 1){
                         $.each(result.data, function(i){
                            $('#result_title').html('<h3>Search Results for "'+ $('#searchWorks').val() + '"</h3>');
                            $('#portfolioResult').append('<li><a href="'+ result.data[i].url +'" class="eqcol">'+ result.data[i].img +'<br /><span class="title">'+ result.data[i].name +'</span><br /><span class="date">'+ result.data[i].date +'</span><br /><span class="desc">'+ result.data[i].desc +'</span></a></li>');
                         });
                     }
                     else{
                        $('#result_title').html('<h3>Your search return no result</h3>');
                     }
                   }
               });
            
        }, 500);
}

function myTab(){
    //When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
}

(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");                                
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);
