jQuery.noConflict();

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

// Custom sorting plugin
(function($) {
  $.fn.sorted = function(customOptions) {
    var options = {
      reversed: false,
      by: function(a) { return a.text(); }
    };
    $.extend(options, customOptions);
    $data = $(this);
    arr = $data.get();
    arr.sort(function(a, b) {
      var valA = options.by($(a));
      var valB = options.by($(b));
      if (options.reversed) {
        return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
      } else {		
        return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
      }
    });
    return $(arr);
  };
})(jQuery);

// DOMContentLoaded
$(function() {

  // get the first collection
  var $portfolioGrid = $('#portfolioGrid');

  // clone portfolioGrid to get a second collection
  var $data = $portfolioGrid.clone(false,false);
	
	$('#portfolioCategories li a').each(function(index) {
		$(this).click(function(event) {
			event.preventDefault();
			if($(this).attr('data-val') == 'all'){
				var $filteredData = $data.find('li');
			} 
			else{
				var $filteredData = $data.find('li[data-type="' + $(this).attr('data-val') +'"]');
			}
			// call quicksand
			$portfolioGrid.quicksand($filteredData, {
				duration: 800,
				//easing: 'swing'
				easing: 'easeInOutQuad'
			});
			window.setTimeout(function() {
				$('.iconHolder').each(function(index) {
					var prt = $(this).parents('.imageHolder');
					prt.hover(
					  function () {
						$(this).find('.iconHolder').fadeIn('slow');
					  }, 
					  function () {
						$(this).find('.iconHolder').fadeOut('slow');
					  }
					);
				});
				$('.cboxElement').each(function(index) {
					var relObj;
					var groupName;
					if ($(this).attr('data-rel')){
						groupName = $(this).attr('data-rel');
						relObj = {rel:groupName}
					}	
					$(this).removeClass("cboxElement");
					
					if($(this).attr('class') == 'lbVideo'){
						vWidth = $(this).attr('data-iframewidth');
						vHeight = $(this).attr('data-iframeheight');
						$(this).colorbox({iframe:true, innerWidth:vWidth, innerHeight:vHeight});
					}
					else{
						$(this).colorbox(relObj); 
					}
				});
			}, 900);
			
			
			
		});
	});
	
	// attempt to call Quicksand on every form change
	// $filterType.add($filterSort).change(function(e) {
		// if ($($filterType+':checked').val() == 'all') {
			// var $filteredData = $data.find('li');
		// } 
		// else{
			// var $filteredData = $data.find('li[data-type=' + $($filterType+":checked").val() + ']');
		// }
		// if ($('#filter input[name="sort"]:checked').val() == "size") {
			// var $sortedData = $filteredData.sorted({
				// by: function(v) {
					// return parseFloat($(v).find('span[data-type=size]').text());
				// }
			// });
		// }
		// else {
			////if sorted by name
			// var $sortedData = $filteredData.sorted({
				// by: function(v) {
				// return $(v).find('strong').text().toLowerCase();
				// }
			// });
		// }
		
		////call quicksand
		// $portfolioGrid.quicksand($sortedData, {
		  // duration: 800,
		  ////easing: 'swing'
		  // easing: 'easeInOutQuad'
		// });
	
		// window.setTimeout(function() {
			// $('.iconHolder').each(function(index) {
				// var prt = $(this).parents('.imageHolder');
				// prt.hover(
				  // function () {
					// $(this).find('.iconHolder').fadeIn('slow');
				  // }, 
				  // function () {
					// $(this).find('.iconHolder').fadeOut('slow');
				  // }
				// );
			// });
			// $('.cboxElement').each(function(index) {
				// var relObj;
				// var groupName;
				// if ($(this).attr('data-rel')){
					// groupName = $(this).attr('data-rel');
					// relObj = {rel:groupName}
				// }	
				// $(this).removeClass("cboxElement");
				
				// if($(this).attr('class') == 'lbVideo'){
					// vWidth = $(this).attr('data-iframewidth');
					// vHeight = $(this).attr('data-iframeheight');
					// $(this).colorbox({iframe:true, innerWidth:vWidth, innerHeight:vHeight});
				// }
				// else{
					// $(this).colorbox(relObj); 
				// }
			// });
			
			
		// }, 900);
	// });

});


	//Remove bottom margin from last widget
	if(jQuery('#sideBar').length){
		jQuery('#sideBar').children('.widget:last').css({'margin-bottom':'0px','padding-bottom':'0px','background-image':'none'});
	}
	
	//hide preloaders on hidden images
	if ($('.imagePreloader:hidden').length ) {
		$('.imagePreloader:hidden').css('display','none');
	}

	//PRELOAD IMAGES
	$('.imageHolder img').imgpreload
	({
		each: function()
		{
			if($(this).siblings().length > 0){
				$(this).siblings('.imagePreloader').delay(700).fadeOut('slow');
				//$(this).siblings('.imagePreloader').fadeOut("slow");
			}
			else{
				$(this).parent().siblings('.imagePreloader').delay(700).fadeOut('slow');
				//$(this).parent().siblings('.imagePreloader').fadeOut("slow");
			}

		}
	});
	
	//ADD SEARCH INPUT ANIMATION
	$('#search_field').focus(function() {
		$(this).fadeTo('fast', 1);
	});
	$('#search_field').blur(function() {
		$(this).fadeTo('fast', 0);
	});
	
	//ADD IMAGE ICON HOVER LISTENERS/HANDLERS
	$('.iconHolder').each(function(index) {
		var prt = $(this).parents('.imageHolder');
		prt.hover(
		  function () {
			$(this).find('.iconHolder').fadeIn('slow');
		  }, 
		  function () {
			$(this).find('.iconHolder').fadeOut('slow');
		  }
		);
	});
	
	$('#floatingSocial ul li').each(function(index) {
		$(this).hover(
		  function () {
			if ( !$.browser.msie ) {
				$(this).animate({opacity: 1});
			}
		  }, 
		  function () {
			if ( !$.browser.msie ) {
				$(this).animate({opacity: .65});
			}
		  }
		);
	});	
	$('.socialIcons ul li').each(function(index) {
		$(this).hover(
		  function () {
			if ( !$.browser.msie ) {
				$(this).animate({opacity: 1});
			}
		  }, 
		  function () {
			if ( !$.browser.msie ) {
				$(this).animate({opacity: .35});
			}
		  }
		);
	});	
	
	//ADD BUTTON STYLES TO WP GENERATED HTML
	jQuery('.comment-reply-link').addClass('buttonPro small '+ defaultBtnColor);
	jQuery('#submit').addClass('buttonPro '+ defaultBtnColor);
	jQuery('#cancel-comment-reply-link').addClass('buttonPro '+ defaultBtnColor);
	jQuery('#searchsubmit').addClass('buttonPro medium '+ defaultBtnColor);
	
	//ADD COLORBOX FUNCTIONALITY FOR IMAGES
	$('.lightbox').each(function(index) {
		//remove button classes & then add them back in at end
		var btnClasses = '';
		if ($(this).attr('data-btn')){
			btnClasses = $(this).attr('data-btn');
			$(this).removeClass(btnClasses);
		}
		
		$(this).removeClass('lightbox');
		var vWidth;
		var vHeight;
		var groupName = '';
		var relObj;
		if($(this).attr('class') == 'lbIFrame'){
			vWidth = $(this).attr('data-iframewidth');
			vHeight = $(this).attr('data-iframeheight');
			$(this).colorbox({width:vWidth, height:vHeight, iframe:true});
		}
		else if($(this).attr('class') == 'lbVideo'){
			vWidth = $(this).attr('data-iframewidth');
			vHeight = $(this).attr('data-iframeheight');
			$(this).colorbox({iframe:true, innerWidth:vWidth, innerHeight:vHeight});
		}
		else{
			if ($(this).attr('data-rel')){
				groupName = $(this).attr('data-rel');
				relObj = {rel:groupName}
			}
			$(this).colorbox(relObj); 
		}
		
		if ($(this).attr('data-btn')){
			$(this).addClass(btnClasses);
		}
		
	});
	
	//ADD SCROLL TO TOP ANIMATION
    $(".backToTop").click(function(event){
        event.preventDefault();
        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({scrollTop:0}, 500);
    });

	//ADD TOGGLE CONTENT FUNCTIONALITY
	jQuery('.toggle_content').click(function() {
		jQuery(this).parent().siblings('.toggle_show').slideToggle('fast');
		var toggleBtn = jQuery(this).parent().siblings('.toggle').children();
		if(toggleBtn.hasClass('up')){
			toggleBtn.removeClass('up');
			toggleBtn.addClass('down');
		}
		else if(toggleBtn.hasClass('down')){
			toggleBtn.removeClass('down');
			toggleBtn.addClass('up');
		}
	});
	jQuery('.toggle').click(function() {
		jQuery(this).siblings('.toggle_show').slideToggle('fast');
		if(jQuery(this).children().hasClass('up')){
			jQuery(this).children().removeClass('up');
			jQuery(this).children().addClass('down');
		}
		else if(jQuery(this).children().hasClass('down')){
			jQuery(this).children().removeClass('down');
			jQuery(this).children().addClass('up');
		}
	});
	
	//Google Like Button Script
	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();
	
	//Facebook Like Button Script
	(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) {return;}
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
	
	fixPngs();
	addToolTips();
	
});

function addToolTips(){

	/**
	Vertigo Tip by www.vertigo-project.com
	*/

	this.vtip = function() {    
		this.xOffset = -25; // x distance from mouse
		this.yOffset = -50; // y distance from mouse       
		
		jQuery(".vtip").unbind().hover(    
			function(e) {
				this.t = this.title;
				this.title = ''; 
				this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
				
				jQuery('body').append( '<p id="vtip" style="z-index:30000;">' + this.t + '</p>' );
						
				jQuery('p#vtip').css("top", this.top+"px").css("left", this.left+"px").fadeIn("fast");
				
			},
			function() {
				this.title = this.t;
				jQuery("p#vtip").fadeOut("fast").remove();
			}
		).mousemove(
			function(e) {
				this.top = (e.pageY + yOffset);
				this.left = (e.pageX + xOffset);
							 
				jQuery("p#vtip").css("top", this.top+"px").css("left", this.left+"px");
			}
		);            
		
	};

	jQuery(document).ready(function(jQuery){vtip();}) 

}

function fixPngs(){

	var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);

	if(isIE6){
		jQuery(document).ready(function($) {
			//when document is ready fix pngs for ie6
			DD_belatedPNG.fix('.jqueryslidemenu');
			DD_belatedPNG.fix('.jqueryslidemenu ul li');
			DD_belatedPNG.fix('#imageLogo');
			DD_belatedPNG.fix('#submit_search');
			DD_belatedPNG.fix('#featuredWrap');
			//Hardcode Menu Width because IE6 sucks
			jQuery('.jqueryslidemenu').css('width', '519px');
			jQuery('#searchBox').css('word-spacing', '-1em');
			//Fixes z-index bug in IE6
			jQuery('#content').css('z-index', '603');
	
	
		});
	}
}

function uniqeid(){
	var newDate = new Date;
	return newDate.getTime();
}
