
// remove nav selection for blog when on portfolio page
jQuery(document).ready(function() {
	
	jQuery('body.single-portfolio #nav li').removeClass('current_page_parent');
	jQuery('body.single-portfolio #nav li.portfoliopage').addClass('current_page_parent');
	
});

// Tabs
jQuery(document).ready(function() {

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

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

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

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

});

// Tabs
jQuery(document).ready(function() {

	//When page loads...
	jQuery(".main-post").hide(); //Hide all content
	jQuery("ul.tabs2 li:first").addClass("active").show(); //Activate first tab
	jQuery(".main-post:first").show(); //Show first tab content

	//On Click Event
	jQuery("ul.tabs2 li").click(function() {

		jQuery("ul.tabs2 li").removeClass("active"); //Remove any "active" class
		jQuery(this).addClass("active"); //Add "active" class to selected tab
		jQuery(".main-post").hide(); //Hide all tab content

		var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		jQuery(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	//back to top
	jQuery('.scrollTop').click(function(){
		jQuery('html, body').animate({scrollTop: '0px'}, 1000);
		return false;
	});

});

// Sidebar
jQuery('#slider-sb').cycle({ 
    fx:     'scrollHorz',
    speed:  '500', 
    timeout: 0, 
    next:   '#next2', 
    prev:   '#prev2' 
});

// testimonials
jQuery('#testimonials-sb').cycle({ 
    fx:     'scrollHorz',
    speed:  '500', 
    timeout: 0, 
    next:   '#next5', 
    prev:   '#prev5' 
});

// portfolio
jQuery('#portfolio-slider').cycle({ 
    fx:     'scrollHorz',
    speed:  '500', 
    timeout: 0, 
    next:   '#next6', 
    prev:   '#prev6' ,
	fit: '1',
	height: 'auto'
});

// Latest 
jQuery('.set1').cycle({ 
    fx:     'scrollHorz',
    speed:  'slow', 
    timeout: 0, 
    next:   '#next3', 
    prev:   '#prev3', 
	fit: 	'1',
	height: '350'
});

jQuery('.set2').cycle({ 
    fx:     'scrollHorz',
    speed:  'slow', 
    timeout: 0, 
    next:   '#next4', 
    prev:   '#prev4', 
	fit: '1',
	height: 'auto'
});

jQuery(document).ready(function(){

	// image hover effect
	jQuery('.portfolio-image-hover').mouseenter(function(e) {
		jQuery(this).children('a').children('span').fadeIn(200);
	}).mouseleave(function(e) {
		jQuery(this).children('a').children('span').fadeOut(200);
	});

});


// Drop down menus

var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

jQuery(document).ready(function()
{	jQuery('#nav > li').bind('mouseover', jsddm_open);
	jQuery('#nav > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

// Input focuses

jQuery(document).ready(function(){
		
	// clear input on focus
	jQuery('.clearme').focus(function(){
		if(jQuery(this).val()==this.defaultValue){
			jQuery(this).val('');
		}
	});
	
	// if field is empty afterward, add text again
	jQuery('.clearme').blur(function(){
		if(jQuery(this).val()==''){
			jQuery(this).val(this.defaultValue);
		}
	});
	
});

// Switch view

jQuery(document).ready(function(){

    if(jQuery('.switch_thumb').hasClass('swap'))
	{
		
		jQuery("a.switch_thumb").toggle(function(){
			jQuery(this).removeClass("swap");
			jQuery(".display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").removeClass("row-view");
			});
		}, function () {
			jQuery(this).addClass("swap");
			jQuery(".display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").addClass("row-view");
			});
		});
	
	}
	else
	{
		jQuery("a.switch_thumb").toggle(function(){
			jQuery(this).addClass("swap");
			jQuery(".display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").addClass("row-view");
			});
		}, function () {
			jQuery(this).removeClass("swap");
			jQuery(".display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").removeClass("row-view");
			});
		});

	}

});

// Quicksand

jQuery(document).ready(function() {

  // get the action filter option item on page load
  var $filterType = jQuery('#filterOptions li.current a').attr('class');
	
  // get and assign the portfolio-items element to the
	// $holder varible for use later
  var $holder = jQuery('ul.portfolio-items');

  // clone all items within the pre-assigned $holder element
  var $data = $holder.clone();

  // attempt to call Quicksand when a filter option
	// item is clicked
	jQuery('#filterOptions li a').click(function(e) {
		// reset the active class on all the buttons
		jQuery('#filterOptions li').removeClass('current');
		
		// assign the class of the clicked filter option
		// element to our $filterType variable
		var $filterType = jQuery(this).attr('class');
		jQuery(this).parent().addClass('current');
		
		if ($filterType == 'all') {
			// assign all li items to the $filteredData var when
			// the 'All' filter option is clicked
			var $filteredData = $data.find('li');
		} 
		else {
			// find all li elements that have our required $filterType
			// values for the data-type element
			//var $filteredData = $data.find('li[data-type=' + $filterType + ']');
			var $filteredData = $data.find('li[data-type*=' + $filterType + ']');
		}
		
		// call quicksand and assign transition parameters
		$holder.quicksand($filteredData, {
			duration: 800,
			easing: 'easeInOutQuad'
		});
		
		return false;
	});
});

// PrettyPhoto

jQuery(document).ready(function(){
    jQuery("a[rel^='prettyPhoto']").prettyPhoto();
  });

// wizylike 
function wizylike(post_id, user_id, type){
	
	if(post_id >= 1 && (user_id == 0 || user_id >= 0)){
		if (type === 'like') {
			
			// like button clicked
			jQuery('#wizylike-post-'+post_id+' .wizylike_like_unlike').addClass('wizylike_loading');
			
			jQuery.post(wizylike_url + '/',
						{post_id: post_id, user_id: user_id, like: 'like'}, 
						function(result){
							jQuery('#wizylike-post-'+post_id+' .wizylike_count').remove();	
							jQuery('#wizylike-post-'+post_id+' .wizylike_like_unlike').remove();
							jQuery('#wizylike-post-'+post_id+' .wizylike_linebreaker.last').remove();
							
							jQuery('#wizylike-post-'+post_id).append(result)
							
						});
			
		} else if (type === 'unlike') {
			
			// unlike button clicked
			jQuery('#wizylike-post-'+post_id+' .wizylike_like_unlike').addClass('wizylike_loading');
			
			jQuery.post(wizylike_url + '/',
						{post_id: post_id, user_id: user_id, like: 'unlike'}, 
						function(result){
							jQuery('#wizylike-post-'+post_id+' .wizylike_count').remove();	
							jQuery('#wizylike-post-'+post_id+' .wizylike_like_unlike').remove();
							jQuery('#wizylike-post-'+post_id+' .wizylike_linebreaker.last').remove();
							
							jQuery('#wizylike-post-'+post_id).append(result)
							
						});
					
		} // end like type check
	} // end post id check
} // end wizylike


function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  //document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
  
  jQuery('.twitter_update_list').html(statusHTML.join(''));
}

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';
  }
}
